|
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 | 29 | $obj = new GetSetDummy(); |
| 30 | + $object = new \stdClass(); |
27 | 31 | $obj->setFoo('foo'); |
28 | 32 | $obj->setBar('bar'); |
29 | 33 | $obj->setCamelCase('camelcase'); |
| 34 | + $obj->setObject($object); |
| 35 | + |
| 36 | + $this->serializer |
| 37 | + ->expects($this->once()) |
| 38 | + ->method('normalize') |
| 39 | + ->with($object, 'any') |
| 40 | + ->will($this->returnValue('string_object')) |
| 41 | + ; |
| 42 | + |
30 | 43 | $this->assertEquals( |
31 | | - array('foo' => 'foo', 'bar' => 'bar', 'fooBar' => 'foobar', 'camelCase' => 'camelcase'), |
| 44 | + array( |
| 45 | + 'foo' => 'foo', |
| 46 | + 'bar' => 'bar', |
| 47 | + 'fooBar' => 'foobar', |
| 48 | + 'camelCase' => 'camelcase', |
| 49 | + 'object' => 'string_object', |
| 50 | + ), |
32 | 51 | $this->normalizer->normalize($obj, 'any') |
33 | 52 | ); |
34 | 53 | } |
@@ -116,7 +135,7 @@ public function testUncallableCallbacks() |
116 | 135 |
|
117 | 136 | public function testIgnoredAttributes() |
118 | 137 | { |
119 | | - $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase')); |
| 138 | + $this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase', 'object')); |
120 | 139 |
|
121 | 140 | $obj = new GetSetDummy(); |
122 | 141 | $obj->setFoo('foo'); |
@@ -188,13 +207,30 @@ public function provideCallbacks() |
188 | 207 | ), |
189 | 208 | ); |
190 | 209 | } |
| 210 | + |
| 211 | + /** |
| 212 | + * @expectedException \LogicException |
| 213 | + * @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer |
| 214 | + */ |
| 215 | + public function testUnableToNormalizeObjectAttribute() |
| 216 | + { |
| 217 | + $serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface'); |
| 218 | + $this->normalizer->setSerializer($serializer); |
| 219 | + |
| 220 | + $obj = new GetSetDummy(); |
| 221 | + $object = new \stdClass(); |
| 222 | + $obj->setObject($object); |
| 223 | + |
| 224 | + $this->normalizer->normalize($obj, 'any'); |
| 225 | + } |
191 | 226 | } |
192 | 227 |
|
193 | 228 | class GetSetDummy |
194 | 229 | { |
195 | 230 | protected $foo; |
196 | 231 | private $bar; |
197 | 232 | protected $camelCase; |
| 233 | + protected $object; |
198 | 234 |
|
199 | 235 | public function getFoo() |
200 | 236 | { |
@@ -235,6 +271,16 @@ public function otherMethod() |
235 | 271 | { |
236 | 272 | throw new \RuntimeException("Dummy::otherMethod() should not be called"); |
237 | 273 | } |
| 274 | + |
| 275 | + public function setObject($object) |
| 276 | + { |
| 277 | + $this->object = $object; |
| 278 | + } |
| 279 | + |
| 280 | + public function getObject() |
| 281 | + { |
| 282 | + return $this->object; |
| 283 | + } |
238 | 284 | } |
239 | 285 |
|
240 | 286 | class GetConstructorDummy |
@@ -263,3 +309,7 @@ public function otherMethod() |
263 | 309 | throw new \RuntimeException("Dummy::otherMethod() should not be called"); |
264 | 310 | } |
265 | 311 | } |
| 312 | + |
| 313 | +abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface |
| 314 | +{ |
| 315 | +} |
0 commit comments