|
| 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 | +namespaceSymfony\Component\Serializer\Tests\Normalizer; |
| 13 | + |
| 14 | +usePHPUnit\Framework\TestCase; |
| 15 | +useSymfony\Component\Serializer\Normalizer\UnwrappingDenormalizer; |
| 16 | +useSymfony\Component\Serializer\Tests\Normalizer\Features\ObjectDummy; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Eduard Bulava <bulavaeduard@gmail.com> |
| 20 | + */ |
| 21 | +class UnwrappinDenormalizerTestextends TestCase |
| 22 | +{ |
| 23 | +private$denormalizer; |
| 24 | + |
| 25 | +private$serializer; |
| 26 | + |
| 27 | +protectedfunctionsetUp():void |
| 28 | + { |
| 29 | +$this->serializer =$this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock(); |
| 30 | +$this->denormalizer =newUnwrappingDenormalizer(); |
| 31 | +$this->denormalizer->setSerializer($this->serializer); |
| 32 | + } |
| 33 | + |
| 34 | +publicfunctiontestSupportsNormalization() |
| 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 | +publicfunctiontestDenormalize() |
| 42 | + { |
| 43 | +$expected =newObjectDummy(); |
| 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 | +publicfunctiontestDenormalizeInvalidPath() |
| 66 | + { |
| 67 | +$expected =newObjectDummy(); |
| 68 | + |
| 69 | +$this->serializer->expects($this->exactly(1)) |
| 70 | + ->method('denormalize') |
| 71 | + ->with(null) |
| 72 | + ->willReturn($expected); |
| 73 | + |
| 74 | +$obj =$this->denormalizer->denormalize( |
| 75 | + ['data' => ['foo' =>'foo','bar' =>'bar','baz' =>true]], |
| 76 | + ObjectDummy::class, |
| 77 | +'any', |
| 78 | + [UnwrappingDenormalizer::UNWRAP_PATH =>'[invalid]'] |
| 79 | + ); |
| 80 | + |
| 81 | +$this->assertNull($obj->getFoo()); |
| 82 | +$this->assertNull($obj->bar); |
| 83 | +$this->assertNull($obj->isBaz()); |
| 84 | + } |
| 85 | +} |