4
4
5
5
use Symfony \Component \Serializer \Serializer ;
6
6
use Symfony \Component \Serializer \Encoder \JsonEncoder ;
7
+ use Symfony \Component \Serializer \Normalizer \GetSetMethodNormalizer ;
7
8
use Symfony \Component \Serializer \Normalizer \CustomNormalizer ;
8
9
use Symfony \Tests \Component \Serializer \Fixtures \TraversableDummy ;
9
10
use Symfony \Tests \Component \Serializer \Fixtures \NormalizableTraversableDummy ;
20
21
class SerializerTest extends \PHPUnit_Framework_TestCase
21
22
{
22
23
/**
23
- * @expectedException \UnexpectedValueException
24
+ * @expectedException \Symfony\Component\Serializer\Exception\ UnexpectedValueException
24
25
*/
25
26
public function testNormalizeNoMatch ()
26
27
{
@@ -43,14 +44,22 @@ public function testNormalizeGivesPriorityToInterfaceOverTraversable()
43
44
}
44
45
45
46
/**
46
- * @expectedException \UnexpectedValueException
47
+ * @expectedException \Symfony\Component\Serializer\Exception\ UnexpectedValueException
47
48
*/
48
49
public function testDenormalizeNoMatch ()
49
50
{
50
51
$ this ->serializer = new Serializer (array ($ this ->getMock ('Symfony\Component\Serializer\Normalizer\CustomNormalizer ' )));
51
52
$ this ->serializer ->denormalize ('foo ' , 'stdClass ' );
52
53
}
53
54
55
+ public function testSerialize ()
56
+ {
57
+ $ this ->serializer = new Serializer (array (new GetSetMethodNormalizer ()), array ('json ' => new JsonEncoder ()));
58
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
59
+ $ result = $ this ->serializer ->serialize (Model::fromArray ($ data ), 'json ' );
60
+ $ this ->assertEquals (json_encode ($ data ), $ result );
61
+ }
62
+
54
63
public function testSerializeScalar ()
55
64
{
56
65
$ this ->serializer = new Serializer (array (), array ('json ' => new JsonEncoder ()));
@@ -66,6 +75,74 @@ public function testSerializeArrayOfScalars()
66
75
$ this ->assertEquals (json_encode ($ data ), $ result );
67
76
}
68
77
78
+ /**
79
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
80
+ */
81
+ public function testSerializeNoEncoder ()
82
+ {
83
+ $ this ->serializer = new Serializer (array (), array ());
84
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
85
+ $ this ->serializer ->serialize ($ data , 'json ' );
86
+ }
87
+
88
+ /**
89
+ * @expectedException \Symfony\Component\Serializer\Exception\LogicException
90
+ */
91
+ public function testSerializeNoNormalizer ()
92
+ {
93
+ $ this ->serializer = new Serializer (array (), array ('json ' => new JsonEncoder ()));
94
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
95
+ $ this ->serializer ->serialize (Model::fromArray ($ data ), 'json ' );
96
+ }
97
+
98
+ public function testDeserialize ()
99
+ {
100
+ $ this ->serializer = new Serializer (array (new GetSetMethodNormalizer ()), array ('json ' => new JsonEncoder ()));
101
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
102
+ $ result = $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
103
+ $ this ->assertEquals ($ data , $ result ->toArray ());
104
+ }
105
+
106
+ public function testDeserializeUseCache ()
107
+ {
108
+ $ this ->serializer = new Serializer (array (new GetSetMethodNormalizer ()), array ('json ' => new JsonEncoder ()));
109
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
110
+ $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
111
+ $ data = array ('title ' => 'bar ' , 'numbers ' => array (2 , 8 ));
112
+ $ result = $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
113
+ $ this ->assertEquals ($ data , $ result ->toArray ());
114
+ }
115
+
116
+ /**
117
+ * @expectedException \Symfony\Component\Serializer\Exception\LogicException
118
+ */
119
+ public function testDeserializeNoNormalizer ()
120
+ {
121
+ $ this ->serializer = new Serializer (array (), array ('json ' => new JsonEncoder ()));
122
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
123
+ $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
124
+ }
125
+
126
+ /**
127
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
128
+ */
129
+ public function testDeserializeWrongNormalizer ()
130
+ {
131
+ $ this ->serializer = new Serializer (array (new CustomNormalizer ()), array ('json ' => new JsonEncoder ()));
132
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
133
+ $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
134
+ }
135
+
136
+ /**
137
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
138
+ */
139
+ public function testDeerializeNoEncoder ()
140
+ {
141
+ $ this ->serializer = new Serializer (array (), array ());
142
+ $ data = array ('title ' => 'foo ' , 'numbers ' => array (5 , 3 ));
143
+ $ this ->serializer ->deserialize (json_encode ($ data ), '\Symfony\Tests\Component\Serializer\Model ' , 'json ' );
144
+ }
145
+
69
146
public function testEncode ()
70
147
{
71
148
$ this ->serializer = new Serializer (array (), array ('json ' => new JsonEncoder ()));
@@ -82,3 +159,48 @@ public function testDecode()
82
159
$ this ->assertEquals ($ data , $ result );
83
160
}
84
161
}
162
+
163
+ class Model
164
+ {
165
+ private $ title ;
166
+ private $ numbers ;
167
+
168
+ public static function fromArray ($ array )
169
+ {
170
+ $ model = new self ();
171
+ if (isset ($ array ['title ' ])) {
172
+ $ model ->setTitle ($ array ['title ' ]);
173
+ }
174
+ if (isset ($ array ['numbers ' ])) {
175
+ $ model ->setNumbers ($ array ['numbers ' ]);
176
+ }
177
+
178
+ return $ model ;
179
+ }
180
+
181
+ public function getTitle ()
182
+ {
183
+ return $ this ->title ;
184
+ }
185
+
186
+ public function setTitle ($ title )
187
+ {
188
+ $ this ->title = $ title ;
189
+ }
190
+
191
+ public function getNumbers ()
192
+ {
193
+ return $ this ->numbers ;
194
+ }
195
+
196
+ public function setNumbers ($ numbers )
197
+ {
198
+ $ this ->numbers = $ numbers ;
199
+ }
200
+
201
+ public function toArray ()
202
+ {
203
+ return array ('title ' => $ this ->title , 'numbers ' => $ this ->numbers );
204
+ }
205
+
206
+ }
0 commit comments