8000 test denormalizer · symfony/symfony@00aeeff · GitHub
[go: up one dir, main page]

Skip to content

Commit 00aeeff

Browse files
committed
test denormalizer
1 parent 079b781 commit 00aeeff

File tree

4 files changed

+89
-3
lines changed
  • 4 files changed

    +89
    -3
    lines changed

    src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1,8 +1,8 @@
    11
    <?xml version="1.0" ?>
    22

    33
    <container xmlns="http://symfony.com/schema/dic/services"
    4-
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5-
    xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    4+
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5+
    xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    66

    77
    <parameters>
    88
    <parameter key="serializer.mapping.cache.file">%kernel.cache_dir%/serialization.php</parameter>

    src/Symfony/Component/Serializer/Normalizer/UnwrappingDenormalizer.php

    Lines changed: 3 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -16,6 +16,9 @@
    1616
    use Symfony\Component\Serializer\SerializerAwareInterface;
    1717
    use Symfony\Component\Serializer\SerializerAwareTrait;
    1818

    19+
    /**
    20+
    * @author Eduard Bulava <bulavaeduard@gmail.com>
    21+
    */
    1922
    final class UnwrappingDenormalizer implements DenormalizerInterface, SerializerAwareInterface, CacheableSupportsMethodInterface
    2023
    {
    2124
    use SerializerAwareTrait;
    Lines changed: 83 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,83 @@
    1+
    <?php
    2+
    3+
    /*
    4+
    * This file is part of the Symfony package.
    5+
    *
    6+
    * (c) Fabien Potencier <fabien@symfony.com>
    7+
    *
    8+
    * For the full copyright and license information, please view the LICENSE
    9+
    * file that was distributed with this source code.
    10+
    */
    11+
    12+
    namespace Symfony\Component\Serializer\Tests\Normalizer;
    13+
    14+
    use PHPUnit\Framework\TestCase;
    15+
    use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
    16+
    use Symfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy;
    17+
    18+
    /**
    19+
    * @author Eduard Bulava <bulavaeduard@gmail.com>
    20+
    */
    21+
    class UnwrappinDenormalizerTest extends TestCase
    22+
    {
    23+
    private $denormalizer;
    24+
    25+
    private $serializer;
    26+
    27+
    protected function setUp(): void
    28+
    {
    29+
    $this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock();
    30+
    $this->denormalizer = new UnwrappingDenormalizer();
    31+
    $this->denormalizer->setSerializer($this->serializer);
    32+
    }
    33+
    34+
    public function testSupportsNormalization()
    35+
    {
    36+
    $this->assertTrue($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]']));
    37+
    $this->assertFalse($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]', 'unwrapped' => true]));
    38+
    $this->assertFalse($this->denormalizer->supportsDenormalization([], new \stdClass(), 'any', []));
    39+
    }
    40+
    41+
    public function testDenormalize()
    42+
    {
    43+
    $expected = new ObjectDummy();
    44+
    $expected->setBaz(true);
    45+
    $expected->bar = 'bar';
    46+
    $expected->setFoo('foo');
    47+
    48+
    $this->serializer->expects($this->exactly(1))
    49+
    ->method('denormalize')
    50+
    ->with(['foo' => 'foo', 'bar' => 'bar', 'baz' => true])
    51+
    ->willReturn($expected);
    52+
    53+
    $result = $this->denormalizer->denormalize(
    54+
    ['data' => ['foo' => 'foo', 'bar' => 'bar', 'baz' => true]],
    55+
    ObjectDummy::class,
    56+
    'any',
    57+
    [UnwrappingDenormalizer::UNWRAP_PATH => '[data]']
    58+
    );
    59+
    60+
    $this->assertEquals('foo', $result->getFoo());
    61+
    $this->assertEquals('bar', $result->bar);
    62+
    $this->assertTrue($result->isBaz());
    63+
    }
    64+
    65+
    public function testDenormalizeInvalidPath()
    66+
    {
    67+
    $this->serializer->expects($this->exactly(1))
    68+
    ->method('denormalize')
    69+
    ->with(null)
    70+
    ->willReturn(new ObjectDummy());
    71+
    72+
    $obj = $this->denormalizer->denormalize(
    73+
    ['data' => ['foo' => 'foo', 'bar' => 'bar', 'baz' => true]],
    74+
    ObjectDummy::class,
    75+
    'any',
    76+
    [UnwrappingDenormalizer::UNWRAP_PATH => '[invalid]']
    77+
    );
    78+
    79+
    $this->assertNull($obj->getFoo());
    80+
    $this->assertNull($obj->bar);
    81+
    $this->assertNull($obj->isBaz());
    82+
    }
    83+
    }

    src/Symfony/Component/Serializer/Tests/SerializerTest.php

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -401,7 +401,7 @@ public function getMetad 63AA ataFor($value): ClassMetadataInterface
    401401

    402402
    public function hasMetadataFor($value): bool
    403403
    {
    404-
    return AbstractDummy::class === $value;
    404+
    return $value === AbstractDummy::class;
    405405
    }
    406406
    };
    407407

    0 commit comments

    Comments
     (0)
    0