| 1 | <?php |
||
| 9 | class SerializerTestCase extends WebTestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var SerializerInterface |
||
| 13 | */ |
||
| 14 | private $serializer; |
||
| 15 | |||
| 16 | public function setUp() |
||
| 17 | { |
||
| 18 | static::bootKernel(); |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @return SerializerInterface |
||
| 23 | */ |
||
| 24 | protected function getSerializer() |
||
| 25 | { |
||
| 26 | if ($this->serializer) { |
||
| 27 | return $this->serializer; |
||
| 28 | } |
||
| 29 | |||
| 30 | $container = static::$kernel->getContainer(); |
||
| 31 | |||
| 32 | return $this->serializer = $container->get('serializer'); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param $obj |
||
| 37 | * |
||
| 38 | * @return array assoc |
||
| 39 | */ |
||
| 40 | protected function serialize($obj, array $context = array()) |
||
| 41 | { |
||
| 42 | $json = $this->getSerializer()->serialize($obj, 'json', $context); |
||
| 43 | |||
| 44 | return json_decode($json, true); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $data |
||
| 49 | * @param string $type |
||
| 50 | * @param array $context |
||
| 51 | * |
||
| 52 | * @return object |
||
| 53 | */ |
||
| 54 | protected function deserialize(array $data, $type, array $context = array()) |
||
| 55 | { |
||
| 56 | return $this->getSerializer()->deserialize(json_encode($data), $type, 'json', $context); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Assert $obj->name is $value. |
||
| 61 | * |
||
| 62 | * @param object $obj |
||
| 63 | * @param string $name |
||
| 64 | * @param mixed $value |
||
| 65 | */ |
||
| 66 | protected function assertPropertyValue($obj, $name, $value) |
||
| 67 | { |
||
| 68 | $this->assertEquals($value, ReflectionPropertyAccess::get($obj, $name)); |
||
| 69 | } |
||
| 70 | } |
||
| 71 |