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

Skip to content

Commit 733d445

Browse files
committed
test denormalizer
1 parent 079b781 commit 733d445

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
$expected = new ObjectDummy();
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+
}

0 commit comments

Comments
 (0)
0