e5876a04e1e56e192fd9c3122299f1829ba90ade
[yaffs-website] / ArrayDenormalizerTest.php
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\ArrayDenormalizer;
16 use Symfony\Component\Serializer\SerializerInterface;
17
18 class ArrayDenormalizerTest extends TestCase
19 {
20     /**
21      * @var ArrayDenormalizer
22      */
23     private $denormalizer;
24
25     /**
26      * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
27      */
28     private $serializer;
29
30     protected function setUp()
31     {
32         $this->serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer')->getMock();
33         $this->denormalizer = new ArrayDenormalizer();
34         $this->denormalizer->setSerializer($this->serializer);
35     }
36
37     public function testDenormalize()
38     {
39         $this->serializer->expects($this->at(0))
40             ->method('denormalize')
41             ->with(array('foo' => 'one', 'bar' => 'two'))
42             ->will($this->returnValue(new ArrayDummy('one', 'two')));
43
44         $this->serializer->expects($this->at(1))
45             ->method('denormalize')
46             ->with(array('foo' => 'three', 'bar' => 'four'))
47             ->will($this->returnValue(new ArrayDummy('three', 'four')));
48
49         $result = $this->denormalizer->denormalize(
50             array(
51                 array('foo' => 'one', 'bar' => 'two'),
52                 array('foo' => 'three', 'bar' => 'four'),
53             ),
54             __NAMESPACE__.'\ArrayDummy[]'
55         );
56
57         $this->assertEquals(
58             array(
59                 new ArrayDummy('one', 'two'),
60                 new ArrayDummy('three', 'four'),
61             ),
62             $result
63         );
64     }
65
66     public function testSupportsValidArray()
67     {
68         $this->serializer->expects($this->once())
69             ->method('supportsDenormalization')
70             ->with($this->anything(), __NAMESPACE__.'\ArrayDummy', $this->anything())
71             ->will($this->returnValue(true));
72
73         $this->assertTrue(
74             $this->denormalizer->supportsDenormalization(
75                 array(
76                     array('foo' => 'one', 'bar' => 'two'),
77                     array('foo' => 'three', 'bar' => 'four'),
78                 ),
79                 __NAMESPACE__.'\ArrayDummy[]'
80             )
81         );
82     }
83
84     public function testSupportsInvalidArray()
85     {
86         $this->serializer->expects($this->any())
87             ->method('supportsDenormalization')
88             ->will($this->returnValue(false));
89
90         $this->assertFalse(
91             $this->denormalizer->supportsDenormalization(
92                 array(
93                     array('foo' => 'one', 'bar' => 'two'),
94                     array('foo' => 'three', 'bar' => 'four'),
95                 ),
96                 __NAMESPACE__.'\InvalidClass[]'
97             )
98         );
99     }
100
101     public function testSupportsNoArray()
102     {
103         $this->assertFalse(
104             $this->denormalizer->supportsDenormalization(
105                 array('foo' => 'one', 'bar' => 'two'),
106                 __NAMESPACE__.'\ArrayDummy'
107             )
108         );
109     }
110 }
111
112 class ArrayDummy
113 {
114     public $foo;
115     public $bar;
116
117     public function __construct($foo, $bar)
118     {
119         $this->foo = $foo;
120         $this->bar = $bar;
121     }
122 }