Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Implementing #9 PHP 5.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
stanlemon committed Oct 10, 2014
1 parent 69d9ab7 commit c8ab5f1
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 114 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php

php: 5.4
php:
- 5.3
- 5.4

before_script: composer install --dev --prefer-dist

Expand Down
2 changes: 2 additions & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extensions:
- Knp\JsonSchemaBundle\PhpSpec\CompatibilityExtension
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ function it_uses_Schema_annotation_data_to_register_the_class_in_the_schema_regi
$container->has("json_schema.reflection_factory")->willReturn(true);
$container->get('json_schema.reflection_factory')->willReturn($factory);

$factory->createFromDirectory(Argument::any(), Argument::any())->willReturn([$refClass]);
$factory->createFromDirectory(Argument::any(), Argument::any())->willReturn(array($refClass));

$schema->name = 'foo';
$reader->getClassAnnotations($refClass)->willReturn([$schema]);
$reader->getClassAnnotations($refClass)->willReturn(array($schema));

$refClass->getName()->willReturn('App\\Entity\\Bar');

$registry->addMethodCall('register', ['foo', 'App\\Entity\\Bar'])->shouldBeCalled();
$registry->addMethodCall('register', array('foo', 'App\\Entity\\Bar'))->shouldBeCalled();

$this->process($container);
}
Expand All @@ -119,15 +119,15 @@ function it_uses_short_class_name_as_alias_if_annotation_name_is_not_set(
$container->get('annotation_reader')->willReturn($reader);
$container->has('json_schema.reflection_factory')->willReturn(true);
$container->get('json_schema.reflection_factory')->willReturn($factory);
$factory->createFromDirectory(Argument::any(), Argument::any())->willReturn([$refClass]);
$factory->createFromDirectory(Argument::any(), Argument::any())->willReturn(array($refClass));

$schema->name = null;
$reader->getClassAnnotations($refClass)->willReturn([$schema]);
$reader->getClassAnnotations($refClass)->willReturn(array($schema));

$refClass->getName()->willReturn('App\\Entity\\Foo');
$refClass->getShortName()->willReturn('Foo');

$registry->addMethodCall('register', ['foo', 'App\\Entity\\Foo'])->shouldBeCalled();
$registry->addMethodCall('register', array('foo', 'App\\Entity\\Foo'))->shouldBeCalled();

$this->process($container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ function it_registers_tagged_propery_handlers(
$container->hasDefinition('json_schema.generator')->willReturn(true);
$container->has('json_schema.generator')->willReturn(true);
$container->getDefinition('json_schema.generator')->willReturn($generatorDef);
$container->findTaggedServiceIds('json_schema.property.handler')->willReturn([
'json_schema.handler_1' => [ 0 => ['priority' => 10]],
'json_schema.handler_2' => [ 0 => ['priority' => 20]],
]);
$container->findTaggedServiceIds('json_schema.property.handler')->willReturn(array(
'json_schema.handler_1' => array( 0 => array('priority' => 10)),
'json_schema.handler_2' => array( 0 => array('priority' => 20)),
));
$referenceFactory->createReference('json_schema.handler_1')->willReturn($handlerRef1);
$referenceFactory->createReference('json_schema.handler_2')->willReturn($handlerRef2);

$generatorDef->addMethodCall('registerPropertyHandler', [$handlerRef1, 10])->shouldBeCalled();
$generatorDef->addMethodCall('registerPropertyHandler', [$handlerRef2, 20])->shouldBeCalled();
$generatorDef->addMethodCall('registerPropertyHandler', array($handlerRef1, 10))->shouldBeCalled();
$generatorDef->addMethodCall('registerPropertyHandler', array($handlerRef2, 20))->shouldBeCalled();

$this->process($container);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function it_creates_a_json_response_and_associate_a_json_schema_if_available(
)
{
$registry->getAlias(Argument::any())->willReturn('foo');
$router->generate('show_json_schema', ['alias' => 'foo'], true)->willReturn('http://localhost/schemas/foo.json');
$router->generate('show_json_schema', array('alias' => 'foo'), true)->willReturn('http://localhost/schemas/foo.json');

$response = $this->create($data);
$response->headers->get('Content-Type')->shouldBe('application/json');
Expand Down
55 changes: 25 additions & 30 deletions spec/Knp/JsonSchemaBundle/Model/PropertySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

class PropertySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('JsonSerializable');
}

function it_has_a_write_once_name_property()
{
$this->setName('a name');
Expand Down Expand Up @@ -40,14 +35,14 @@ function it_has_a_type_property()
{
$this->addType('the type');
$this->addType('another type');
$this->getType()->shouldBe(['the type', 'another type']);
$this->getType()->shouldBe(array('the type', 'another type'));
}

function its_addType_will_not_add_type_if_it_already_has_it()
{
$this->addType('the type');
$this->addType('the type');
$this->getType()->shouldBe(['the type']);
$this->getType()->shouldBe(array('the type'));
}

function it_has_a_write_once_pattern_property()
Expand All @@ -59,9 +54,9 @@ function it_has_a_write_once_pattern_property()

function it_has_a_write_once_enumeration_property()
{
$this->setEnumeration(['foo', 'bar', 'baz']);
$this->setEnumeration(['a', 'lo', 'ha']);
$this->getEnumeration()->shouldBe(['foo', 'bar', 'baz']);
$this->setEnumeration(array('foo', 'bar', 'baz'));
$this->setEnumeration(array('a', 'lo', 'ha'));
$this->getEnumeration()->shouldBe(array('foo', 'bar', 'baz'));
}

function it_has_a_write_once_minimum_property()
Expand Down Expand Up @@ -101,33 +96,33 @@ function it_has_a_write_once_format_property()

function it_has_a_write_once_disallowed_property()
{
$this->setDisallowed(['boolean', 'string']);
$this->setDisallowed(['number']);
$this->getDisallowed()->shouldBe(['boolean', 'string']);
$this->setDisallowed(array('boolean', 'string'));
$this->setDisallowed(array('number'));
$this->getDisallowed()->shouldBe(array('boolean', 'string'));
}

function it_serializes_type_as_a_string_if_it_has_single_value()
{
$this->addType('a type');
$this->jsonSerialize()->shouldBe(['type' => 'a type']);
$this->jsonSerialize()->shouldBe(array('type' => 'a type'));
}

function it_serializes_type_as_an_array_if_it_has_multiple_values()
{
$this->addType('a type');
$this->addType('another type');
$this->jsonSerialize()->shouldBe(['type' => ['a type', 'another type']]);
$this->jsonSerialize()->shouldBe(array('type' => array('a type', 'another type')));
}

function it_only_serializes_non_null_properties()
{
$this->jsonSerialize()->shouldBe([]);
$this->jsonSerialize()->shouldBe(array());
}

function it_serializse_enumeration_if_there_is_one()
{
$this->setEnumeration(['a','simple','list','of','choice']);
$this->jsonSerialize()->shouldBe(['enum' => ['a','simple','list','of','choice']]);
$this->setEnumeration(array('a','simple','list','of','choice'));
$this->jsonSerialize()->shouldBe(array('enum' => array('a','simple','list','of','choice')));
}

function it_serializes_minimum_maximum_and_exclusive_constraints_if_type_is_number()
Expand All @@ -137,13 +132,13 @@ function it_serializes_minimum_maximum_and_exclusive_constraints_if_type_is_numb
->setMinimum(10)
->setMaximum(15)
;
$this->jsonSerialize()->shouldBe([
$this->jsonSerialize()->shouldBe(array(
'type' => 'number',
'minimum' => 10,
'exclusiveMinimum' => false,
'maximum' => 15,
'exclusiveMaximum' => false,
]);
));
}

function it_serializes_minimum_maximum_exclusive_constraints_if_type_is_integer()
Expand All @@ -153,13 +148,13 @@ function it_serializes_minimum_maximum_exclusive_constraints_if_type_is_integer(
->setMinimum(10)
->setMaximum(15)
;
$this->jsonSerialize()->shouldBe([
$this->jsonSerialize()->shouldBe(array(
'type' => 'integer',
'minimum' => 10,
'exclusiveMinimum' => false,
'maximum' => 15,
'exclusiveMaximum' => false,
]);
));
}

function it_serializes_minLength_and_maxLength_if_type_is_string()
Expand All @@ -169,23 +164,23 @@ function it_serializes_minLength_and_maxLength_if_type_is_string()
->setMinimum(10)
->setMaximum(15)
;
$this->jsonSerialize()->shouldBe([
$this->jsonSerialize()->shouldBe(array(
'type' => 'string',
'minLength' => 10,
'maxLength' => 15,
]);
));
}

function it_serializes_disallow_as_array_if_it_has_been_defined()
{
$this->setDisallowed(["boolean", "number", ["type" => "string", "format" => "email"]]);
$this->setDisallowed(array("boolean", "number", array("type" => "string", "format" => "email")));

$this->jsonSerialize()->shouldBe([
'disallow' => [
$this->jsonSerialize()->shouldBe(array(
'disallow' => array(
'boolean',
'number',
['type' => 'string', 'format' => 'email'],
],
]);
array('type' => 'string', 'format' => 'email'),
),
));
}
}
14 changes: 7 additions & 7 deletions spec/Knp/JsonSchemaBundle/Model/SchemaSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function it_should_have_properties($property1, $property2)
$this->addProperty($property1);
$this->addProperty($property2);

$this->getProperties()->shouldBe(['prop1' => $property1, 'prop2' => $property2]);
$this->getProperties()->shouldBe(array('prop1' => $property1, 'prop2' => $property2));
}

/**
Expand All @@ -34,7 +34,7 @@ function it_should_be_serializable($property1)
{
$property1->getName()->willReturn('prop1');
$property1->isRequired()->willReturn(true);
$property1->jsonSerialize()->willReturn([]);
$property1->jsonSerialize()->willReturn(array());

$this->setTitle('some schema');
$this->setType('object');
Expand All @@ -43,16 +43,16 @@ function it_should_be_serializable($property1)
$this->addProperty($property1);

$this->jsonSerialize()->shouldBe(
[
array(
'title' => 'some schema',
'type' => 'object',
'$schema' => 'http://json-schema.org/draft-04/schema#',
'id' => 'http://example.com/schemas/user.json#',
'properties' => [
'properties' => array(
'prop1' => $property1
],
'required' => ['prop1'],
]
),
'required' => array('prop1'),
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace spec\Knp\JsonSchemaBundle\Property;

use Knp\JsonSchemaBundle\Model\Property;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -18,7 +17,7 @@ function let($classMetadataFactory, $classMetadata, $propertyMetadata, $property
{
$this->beConstructedWith($classMetadataFactory);
$propertyMetadata->name = 'some property';
$classMetadata->properties = [$propertyMetadata];
$classMetadata->properties = array($propertyMetadata);
$classMetadataFactory->getMetadataFor(Argument::any())->willReturn($classMetadata);
$property->getName()->willReturn('some property');
}
Expand All @@ -28,10 +27,10 @@ function let($classMetadataFactory, $classMetadata, $propertyMetadata, $property
*/
function it_should_set_enumeration_if_property_as_a_choice_constraint($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $choiceConstraint)
{
$propertyMetadata->constraints = [$choiceConstraint];
$choiceConstraint->choices = ['foo', 'bar'];
$propertyMetadata->constraints = array($choiceConstraint);
$choiceConstraint->choices = array('foo', 'bar');

$property->setEnumeration(['foo', 'bar'])->shouldBeCalled();
$property->setEnumeration(array('foo', 'bar'))->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -41,7 +40,7 @@ function it_should_set_enumeration_if_property_as_a_choice_constraint($classMeta
*/
function it_should_set_minimum_if_property_as_a_length_constraint_with_a_min_attribute($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $lengthConstraint)
{
$propertyMetadata->constraints = [$lengthConstraint];
$propertyMetadata->constraints = array($lengthConstraint);
$lengthConstraint->min = 15;

$property->setMinimum(15)->shouldBeCalled();
Expand All @@ -55,7 +54,7 @@ function it_should_set_minimum_if_property_as_a_length_constraint_with_a_min_att
*/
function it_should_set_maximum_if_property_as_a_length_constraint_with_a_max_attribute($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $lengthConstraint)
{
$propertyMetadata->constraints = [$lengthConstraint];
$propertyMetadata->constraints = array($lengthConstraint);
$lengthConstraint->max = 42;

$property->setMinimum(null)->shouldBeCalled();
Expand All @@ -70,10 +69,10 @@ function it_should_set_maximum_if_property_as_a_length_constraint_with_a_max_att
*/
function it_adds_type_if_property_as_a_type_constraint_that_is_not_already_added($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $typeNumberConstraint, $typeStringConstraint)
{
$propertyMetadata->constraints = [$typeNumberConstraint];
$propertyMetadata->constraints = array($typeNumberConstraint);
$typeNumberConstraint->type = 'number';

$property->getType()->willReturn(['string']);
$property->getType()->willReturn(array('string'));

$property->addType('number')->shouldBeCalled();

Expand All @@ -85,8 +84,8 @@ function it_adds_type_if_property_as_a_type_constraint_that_is_not_already_added
*/
function it_adds_date_format_if_property_as_a_date_constraint($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$property->setFormat(Property::FORMAT_DATE)->shouldBeCalled();
$propertyMetadata->constraints = array($constraint);
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_DATE)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -96,8 +95,8 @@ function it_adds_date_format_if_property_as_a_date_constraint($classMetadataFact
*/
function it_adds_date_time_format_if_property_as_a_datetime_constraint($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$property->setFormat(Property::FORMAT_DATETIME)->shouldBeCalled();
$propertyMetadata->constraints = array($constraint);
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_DATETIME)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -107,8 +106,8 @@ function it_adds_date_time_format_if_property_as_a_datetime_constraint($classMet
*/
function it_adds_time_format_if_property_as_a_time_constraint($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$property->setFormat(Property::FORMAT_TIME)->shouldBeCalled();
$propertyMetadata->constraints = array($constraint);
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_TIME)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -118,8 +117,8 @@ function it_adds_time_format_if_property_as_a_time_constraint($classMetadataFact
*/
function it_adds_email_format_if_property_as_an_email_constraint($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$property->setFormat(Property::FORMAT_EMAIL)->shouldBeCalled();
$propertyMetadata->constraints = array($constraint);
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_EMAIL)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -129,9 +128,9 @@ function it_adds_email_format_if_property_as_an_email_constraint($classMetadataF
*/
function it_adds_ipv6_format_if_property_as_an_ip_constraint_with_version_6($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$propertyMetadata->constraints = array($constraint);
$constraint->version = '6';
$property->setFormat(Property::FORMAT_IPV6)->shouldBeCalled();
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_IPV6)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand All @@ -141,9 +140,9 @@ function it_adds_ipv6_format_if_property_as_an_ip_constraint_with_version_6($cla
*/
function it_adds_ip_address_format_if_property_as_an_ip_constraint_with_version_4($classMetadataFactory, $classMetadata, $propertyMetadata, $property, $constraint)
{
$propertyMetadata->constraints = [$constraint];
$propertyMetadata->constraints = array($constraint);
$constraint->version = '4';
$property->setFormat(Property::FORMAT_IPADDRESS)->shouldBeCalled();
$property->setFormat(\Knp\JsonSchemaBundle\Model\Property::FORMAT_IPADDRESS)->shouldBeCalled();

$this->handle('some class', $property);
}
Expand Down
Loading

0 comments on commit c8ab5f1

Please sign in to comment.