Explorar o código

added several tests to the serializer (mainly for deserialization)

lsmith77 %!s(int64=13) %!d(string=hai) anos
pai
achega
b1ca0cdfe9
Modificáronse 1 ficheiros con 124 adicións e 2 borrados
  1. 124 2
      tests/Symfony/Tests/Component/Serializer/SerializerTest.php

+ 124 - 2
tests/Symfony/Tests/Component/Serializer/SerializerTest.php

@@ -4,6 +4,7 @@ namespace Symfony\Tests\Component\Serializer;
 
 use Symfony\Component\Serializer\Serializer;
 use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
 use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
 use Symfony\Tests\Component\Serializer\Fixtures\TraversableDummy;
 use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
@@ -20,7 +21,7 @@ use Symfony\Tests\Component\Serializer\Fixtures\NormalizableTraversableDummy;
 class SerializerTest extends \PHPUnit_Framework_TestCase
 {
     /**
-     * @expectedException \UnexpectedValueException
+     * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
      */
     public function testNormalizeNoMatch()
     {
@@ -43,7 +44,7 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException \UnexpectedValueException
+     * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
      */
     public function testDenormalizeNoMatch()
     {
@@ -51,6 +52,14 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
         $this->serializer->denormalize('foo', 'stdClass');
     }
 
+    public function testSerialize()
+    {
+        $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $result = $this->serializer->serialize(Model::fromArray($data), 'json');
+        $this->assertEquals(json_encode($data), $result);
+    }
+
     public function testSerializeScalar()
     {
         $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@@ -66,6 +75,74 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(json_encode($data), $result);
     }
 
+    /**
+     * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+     */
+    public function testSerializeNoEncoder()
+    {
+        $this->serializer = new Serializer(array(), array());
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->serialize($data, 'json');
+    }
+
+    /**
+     * @expectedException \Symfony\Component\Serializer\Exception\LogicException
+     */
+    public function testSerializeNoNormalizer()
+    {
+        $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->serialize(Model::fromArray($data), 'json');
+    }
+
+    public function testDeserialize()
+    {
+        $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+        $this->assertEquals($data, $result->toArray());
+    }
+
+    public function testDeserializeUseCache()
+    {
+        $this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+        $data = array('title' => 'bar', 'numbers' => array(2, 8));
+        $result = $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+        $this->assertEquals($data, $result->toArray());
+    }
+
+    /**
+     * @expectedException \Symfony\Component\Serializer\Exception\LogicException
+     */
+    public function testDeserializeNoNormalizer()
+    {
+        $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+    }
+
+    /**
+     * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+     */
+    public function testDeserializeWrongNormalizer()
+    {
+        $this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+    }
+
+    /**
+     * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+     */
+    public function testDeerializeNoEncoder()
+    {
+        $this->serializer = new Serializer(array(), array());
+        $data = array('title' => 'foo', 'numbers' => array(5, 3));
+        $this->serializer->deserialize(json_encode($data), '\Symfony\Tests\Component\Serializer\Model', 'json');
+    }
+
     public function testEncode()
     {
         $this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
@@ -82,3 +159,48 @@ class SerializerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($data, $result);
     }
 }
+
+class Model
+{
+    private $title;
+    private $numbers;
+
+    public static function fromArray($array)
+    {
+        $model = new self();
+        if (isset($array['title'])) {
+            $model->setTitle($array['title']);
+        }
+        if (isset($array['numbers'])) {
+            $model->setNumbers($array['numbers']);
+        }
+
+        return $model;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getNumbers()
+    {
+        return $this->numbers;
+    }
+
+    public function setNumbers($numbers)
+    {
+        $this->numbers = $numbers;
+    }
+
+    public function toArray()
+    {
+        return array('title' => $this->title, 'numbers' => $this->numbers);
+    }
+
+}