Skip to content

Commit

Permalink
add discriminator in middle of hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
dan committed Sep 13, 2016
1 parent 3102b2e commit 54f5468
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* Copyright 2016 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle;


abstract class Base
{

}
23 changes: 23 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Chief.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright 2016 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle;

class Chief extends User
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright 2016 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle;

class Employee extends User
{
}
38 changes: 38 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* Copyright 2016 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle;

use JMS\Serializer\Annotation as Serializer;

/**
* @Serializer\Discriminator(field = "type", map = {
* "chief": "JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Chief",
* "employee": "JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Employee",
* })
*/
abstract class User extends Base
{
/** @Serializer\Type("integer") */
public $hours;

public function __construct($hours)
{
$this->hours = (integer) $hours;
}
}
24 changes: 24 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
use JMS\Serializer\Exclusion\DepthExclusionStrategy;
use JMS\Serializer\Tests\Fixtures\Node;
use JMS\Serializer\Tests\Fixtures\AuthorReadOnlyPerClass;
use JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Chief;
use JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Employee;

abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -951,6 +953,28 @@ public function testNestedPolymorphicObjects()
}
}

/**
* @group polymorphic
*/
public function testNestedMiddlePolymorphicObjects()
{
$garage = array(new Chief(3), new Employee(1));
$this->assertEquals(
$this->getContent('user_hierarchy'),
$this->serialize($garage)
);

if ($this->hasDeserializer()) {
$this->assertEquals(
$garage,
$this->deserialize(
$this->getContent('user_hierarchy'),
'array<JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\User>'
)
);
}
}

/**
* @group polymorphic
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ protected function getContent($key)
$outputs['car'] = '{"km":5,"type":"car"}';
$outputs['car_without_type'] = '{"km":5}';
$outputs['garage'] = '{"vehicles":[{"km":3,"type":"car"},{"km":1,"type":"moped"}]}';
$outputs['user_hierarchy'] = '[{"hours":3,"type":"chief"},{"hours":1,"type":"employee"}]';
$outputs['tree'] = '{"tree":{"children":[{"children":[{"children":[],"foo":"bar"}],"foo":"bar"}],"foo":"bar"}}';
$outputs['nullable_arrays'] = '{"empty_inline":[],"not_empty_inline":["not_empty_inline"],"empty_not_inline":[],"not_empty_not_inline":["not_empty_not_inline"],"empty_not_inline_skip":[],"not_empty_not_inline_skip":["not_empty_not_inline_skip"]}';
$outputs['object_with_object_property_no_array_to_author'] = '{"foo": "bar", "author": "baz"}';
Expand Down
11 changes: 11 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/xml/user_hierarchy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entry>
<hours>3</hours>
<type><![CDATA[chief]]></type>
</entry>
<entry>
<hours>1</hours>
<type><![CDATA[employee]]></type>
</entry>
</result>
6 changes: 6 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/yml/user_hierarchy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-
hours: 3
type: chief
-
hours: 1
type: employee

0 comments on commit 54f5468

Please sign in to comment.