|
12 | 12 | namespace Symfony\Component\Serializer\Tests\Normalizer;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
| 15 | +use Symfony\Component\Serializer\SerializerInterface; |
| 16 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
15 | 17 |
|
16 | 18 | class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
17 | 19 | {
|
18 | 20 | protected function setUp()
|
19 | 21 | {
|
| 22 | + $this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer'); |
20 | 23 | $this->normalizer = new GetSetMethodNormalizer;
|
21 |
| - $this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer')); |
| 24 | + $this->normalizer->setSerializer($this->serializer); |
22 | 25 | }
|
23 | 26 |
|
24 | 27 | public function testNormalize()
|
25 | 28 | {
|
26 |
| - $obj = new GetSetDummy; |
| 29 | + $obj = new GetSetDummy(); |
| 30 | + $object = new \stdClass(); |
27 | 31 | $obj->setFoo('foo');
|
28 | 32 | $obj->setBar('bar');
|
| 33 | + $obj->setObject($object); |
| 34 | + |
| 35 | + $this->serializer |
| 36 | + ->expects($this->once()) |
| 37 | + ->method('normalize') |
| 38 | + ->with($object, 'any') |
| 39 | + ->will($this->returnValue('string_object')); |
| 40 | + |
29 | 41 | $this->assertEquals(
|
30 |
| - array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar'), |
| 42 | + array( |
| 43 | + 'foo' => 'foo', |
| 44 | + 'bar' => 'bar', |
| 45 | + 'fooBar' => 'foobar', |
| 46 | + 'object' => 'string_object' |
| 47 | + ), |
31 | 48 | $this->normalizer->normalize($obj, 'any')
|
32 | 49 | );
|
33 | 50 | }
|
@@ -82,7 +99,7 @@ public function testUncallableCallbacks()
|
82 | 99 |
|
83 | 100 | public function testIgnoredAttributes()
|
84 | 101 | {
|
85 |
| - $this->normalizer->setIgnoredAttributes(array('foo', 'bar')); |
| 102 | + $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'object')); |
86 | 103 |
|
87 | 104 | $obj = new GetSetDummy;
|
88 | 105 | $obj->setFoo('foo');
|
@@ -154,12 +171,29 @@ public function provideCallbacks()
|
154 | 171 | ),
|
155 | 172 | );
|
156 | 173 | }
|
| 174 | + |
| 175 | + /** |
| 176 | + * @expectedException \LogicException |
| 177 | + * @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer |
| 178 | + */ |
| 179 | + public function testUnableToNormalizeObjectAttribute() |
| 180 | + { |
| 181 | + $serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface'); |
| 182 | + $this->normalizer->setSerializer($serializer); |
| 183 | + |
| 184 | + $obj = new GetSetDummy(); |
| 185 | + $object = new \stdClass(); |
| 186 | + $obj->setObject($object); |
| 187 | + |
| 188 | + $this->normalizer->normalize($obj, 'any'); |
| 189 | + } |
157 | 190 | }
|
158 | 191 |
|
159 | 192 | class GetSetDummy
|
160 | 193 | {
|
161 | 194 | protected $foo;
|
162 | 195 | private $bar;
|
| 196 | + protected $object; |
163 | 197 |
|
164 | 198 | public function getFoo()
|
165 | 199 | {
|
@@ -190,6 +224,16 @@ public function otherMethod()
|
190 | 224 | {
|
191 | 225 | throw new \RuntimeException("Dummy::otherMethod() should not be called");
|
192 | 226 | }
|
| 227 | + |
| 228 | + public function setObject($object) |
| 229 | + { |
| 230 | + $this->object = $object; |
| 231 | + } |
| 232 | + |
| 233 | + public function getObject() |
| 234 | + { |
| 235 | + return $this->object; |
| 236 | + } |
193 | 237 | }
|
194 | 238 |
|
195 | 239 | class GetConstructorDummy
|
@@ -218,3 +262,7 @@ public function otherMethod()
|
218 | 262 | throw new \RuntimeException("Dummy::otherMethod() should not be called");
|
219 | 263 | }
|
220 | 264 | }
|
| 265 | + |
| 266 | +abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface |
| 267 | +{ |
| 268 | +} |
0 commit comments