8000 added several tests to the serializer (mainly for deserialization) · symfony/symfony@b1ca0cd · GitHub
[go: up one dir, main page]

Skip to content

Commit b1ca0cd

Browse files
committed
added several tests to the serializer (mainly for deserialization)
1 parent ab0b4f3 commit b1ca0cd

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

tests/Symfony/Tests/Component/Serializer/SerializerTest.php

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\Serializer\Serializer;
66
use Symfony\Component\Serializer\Encoder\JsonEncoder;
7+
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
78
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
89
use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
910
use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
@@ -20,7 +21,7 @@
2021
class SerializerTest extends \PHPUnit_Framework_TestCase
2122
{
2223
/**
23-
* @expectedException \UnexpectedValueException
24+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
2425
*/
2526
public function testNormalizeNoMatch()
2627
{
@@ -43,14 +44,22 @@ public function testNormalizeGivesPriorityToInterfaceOverTraversable()
4344
}
4445

4546
/**
46-
* @expectedException \UnexpectedValueException
47+
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
4748
*/
4849
public function testDenormalizeNoMatch()
4950
{
5051
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
5152
$this->serializer->denormalize('foo', 'stdClass');
5253
}
5354

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+
5463
public function testSerializeScalar()
5564
{
5665
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@@ -66,6 +75,74 @@ public function testSerializeArrayOfScalars()
6675
$this->assertEquals(json_encode($data), $result);
6776
}
6877

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+
69146
public function testEncode()
70147
{
71148
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@@ -82,3 +159,48 @@ public function testDecode()
82159
$this->assertEquals($data, $result);
83160
}
84161
}
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

Comments
 (0)
0