Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit733d445

Browse files
committed
test denormalizer
1 parent079b781 commit733d445

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
<containerxmlns="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
<parameterkey="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
useSymfony\Component\Serializer\SerializerAwareInterface;
1717
useSymfony\Component\Serializer\SerializerAwareTrait;
1818

19+
/**
20+
* @author Eduard Bulava <bulavaeduard@gmail.com>
21+
*/
1922
finalclass UnwrappingDenormalizerimplements 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+
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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp