1 | <?php |
||
14 | class MajoraSerializerTest |
||
15 | extends PHPUnit_Framework_TestCase |
||
16 | { |
||
17 | /** |
||
18 | * tests serialize methods without any handler defined. |
||
19 | * |
||
20 | * @expectedException BadMethodCallException |
||
21 | * @expectedExceptionMessageRegExp #Unsupported format "test"*# |
||
22 | */ |
||
23 | public function testSerializeWithoutHandlers() |
||
24 | { |
||
25 | $serializer = new MajoraSerializer(array()); |
||
26 | $serializer->serialize(new \StdClass(), 'test'); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * tests serialize cases. |
||
31 | * |
||
32 | * @dataProvider serializeProvider |
||
33 | */ |
||
34 | public function testSerialize( |
||
35 | array $formatHandlers, |
||
36 | $data, $format, array $context, |
||
37 | $expectedReturn |
||
38 | ) { |
||
39 | $serializer = new MajoraSerializer($formatHandlers); |
||
40 | $this->assertEquals( |
||
41 | $expectedReturn, |
||
42 | $serializer->serialize($data, $format, $context) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | public function serializeProvider() |
||
47 | { |
||
48 | $cases = array(); |
||
49 | |||
50 | // Full case : 2 handlers and a scope |
||
51 | $handler1 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
52 | $handler1->serialize('123', 'scope') |
||
53 | ->willReturnArgument(0) |
||
54 | ->shouldBeCalled() |
||
55 | ; |
||
56 | |||
57 | $handler2 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
58 | $handler2->serialize(Argument::any(), Argument::any()) |
||
59 | ->shouldNotBeCalled() |
||
60 | ; |
||
61 | |||
62 | $cases[] = array( |
||
63 | array('json' => $handler1->reveal(), 'yaml' => $handler2->reveal()), |
||
64 | '123', 'json', array('scope' => 'scope'), |
||
65 | '123', |
||
66 | ); |
||
67 | |||
68 | // Default case, 2 handlers and no scope |
||
69 | $handler1 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
70 | $handler1->serialize(Argument::any(), Argument::any()) |
||
71 | ->shouldNotBeCalled() |
||
72 | ; |
||
73 | |||
74 | $handler2 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
75 | $handler2->serialize('123', 'default') |
||
76 | ->willReturnArgument(0) |
||
77 | ->shouldBeCalled() |
||
78 | ; |
||
79 | |||
80 | $cases[] = array( |
||
81 | array('json' => $handler1->reveal(), 'yaml' => $handler2->reveal()), |
||
82 | '123', 'yaml', array(), |
||
83 | '123', |
||
84 | ); |
||
85 | |||
86 | return $cases; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * tests deserialize methods without any handler defined. |
||
91 | * |
||
92 | * @expectedException BadMethodCallException |
||
93 | * @expectedExceptionMessageRegExp #Unsupported format "test"*# |
||
94 | */ |
||
95 | public function testDeserializeWithoutHandlers() |
||
96 | { |
||
97 | $serializer = new MajoraSerializer(array()); |
||
98 | $serializer->deserialize('hello', 'StdClass', 'test', array()); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * tests deserialize method. |
||
103 | */ |
||
104 | public function testDeserialize() |
||
105 | { |
||
106 | $handler1 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
107 | $handler1->deserialize('123', 'StdClass') |
||
108 | ->willReturnArgument(0) |
||
109 | ->shouldBeCalled() |
||
110 | ; |
||
111 | |||
112 | $handler2 = $this->prophesize('Majora\Framework\Serializer\Handler\FormatHandlerInterface'); |
||
113 | $handler2->deserialize(Argument::any(), Argument::any()) |
||
114 | ->shouldNotBeCalled() |
||
115 | ; |
||
116 | |||
117 | $serializer = new MajoraSerializer(array( |
||
118 | 'json' => $handler1->reveal(), |
||
119 | 'yaml' => $handler2->reveal(), |
||
120 | )); |
||
121 | |||
122 | $this->assertEquals( |
||
123 | '123', |
||
124 | $serializer->deserialize('123', 'StdClass', 'json') |
||
125 | ); |
||
126 | } |
||
127 | } |
||
128 |