|
| 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
10000
span>; |
| 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 | +} |
0 commit comments