Skip to content

Commit

Permalink
Fix some tests related to access order and make sorting explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 6, 2016
1 parent 216821c commit f677457
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/JMS/Serializer/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,13 @@ private function sortProperties()

case self::ACCESSOR_ORDER_CUSTOM:
$order = $this->customOrder;
uksort($this->propertyMetadata, function($a, $b) use ($order) {
$currentSorting = $this->propertyMetadata ? array_combine(array_keys($this->propertyMetadata), range(1, count($this->propertyMetadata))) : [];
uksort($this->propertyMetadata, function($a, $b) use ($order, $currentSorting) {
$existsA = isset($order[$a]);
$existsB = isset($order[$b]);

if ( ! $existsA && ! $existsB) {
return 0;
return $currentSorting[$a] - $currentSorting[$b];
}

if ( ! $existsA) {
Expand Down
38 changes: 24 additions & 14 deletions tests/JMS/Serializer/Tests/Metadata/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,40 @@

class ClassMetadataTest extends \PHPUnit_Framework_TestCase
{
public function testSetAccessorOrder()
public function getAccessOrderCases()
{
return [
[array('b', 'a'), array('b', 'a')],
[array('a', 'b'), array('a', 'b')],
[array('b'), array('b', 'a')],
[array('a'), array('a', 'b')],
[array('foo', 'bar'), array('b', 'a')],
];
}

/**
* @dataProvider getAccessOrderCases
*/
public function testSetAccessorOrderCustom(array $order, array $expected)
{
$metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder');
$metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b'));
$metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a'));
$this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_ALPHABETICAL);
$this->assertEquals(array('a', 'b'), array_keys($metadata->propertyMetadata));

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, array('b', 'a'));
$this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, array('a', 'b'));
$this->assertEquals(array('a', 'b'), array_keys($metadata->propertyMetadata));
$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, $order);
$this->assertEquals($expected, array_keys($metadata->propertyMetadata));
}

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, array('b'));
public function testSetAccessorOrderAlphabetical()
{
$metadata = new ClassMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder');
$metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'b'));
$metadata->addPropertyMetadata(new PropertyMetadata('JMS\Serializer\Tests\Metadata\PropertyMetadataOrder', 'a'));
$this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, array('a'));
$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_ALPHABETICAL);
$this->assertEquals(array('a', 'b'), array_keys($metadata->propertyMetadata));

$metadata->setAccessorOrder(ClassMetadata::ACCESSOR_ORDER_CUSTOM, array('foo', 'bar'));
$this->assertEquals(array('b', 'a'), array_keys($metadata->propertyMetadata));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function getContent($key)
$outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';
$outputs['groups_foobar'] = '{"foo":"foo","foobar":"foobar","bar":"bar"}';
$outputs['groups_default'] = '{"bar":"bar","none":"none"}';
$outputs['virtual_properties'] = '{"exist_field":"value","test":"other-name","virtual_value":"value","typed_virtual_property":1}';
$outputs['virtual_properties'] = '{"exist_field":"value","virtual_value":"value","test":"other-name","typed_virtual_property":1}';
$outputs['virtual_properties_low'] = '{"low":1}';
$outputs['virtual_properties_high'] = '{"high":8}';
$outputs['virtual_properties_all'] = '{"low":1,"high":8}';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<exist_field><![CDATA[value]]></exist_field>
<test><![CDATA[other-name]]></test>
<virtual_value><![CDATA[value]]></virtual_value>
<test><![CDATA[other-name]]></test>
<typed_virtual_property>1</typed_virtual_property>
</result>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exist_field: value
test: other-name
virtual_value: value
test: other-name
typed_virtual_property: 1

0 comments on commit f677457

Please sign in to comment.