/tests/ZendTest/Serializer/Adapter/JsonTest.php

https://github.com/zucchi/zf2 · PHP · 165 lines · 117 code · 31 blank · 17 comment · 0 complexity · 244a2cd6cb65e7b336f80efc7577322e MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Serializer
  9. */
  10. namespace ZendTest\Serializer\Adapter;
  11. use Zend\Serializer;
  12. /**
  13. * @category Zend
  14. * @package Zend_Serializer
  15. * @subpackage UnitTests
  16. * @group Zend_Serializer
  17. */
  18. class JsonTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * @var Serializer\Adapter\Json
  22. */
  23. private $adapter;
  24. public function setUp()
  25. {
  26. $this->adapter = new Serializer\Adapter\Json();
  27. }
  28. public function tearDown()
  29. {
  30. $this->adapter = null;
  31. }
  32. public function testAdapterAcceptsOptions()
  33. {
  34. $adapter = new Serializer\Adapter\Json();
  35. $options = new Serializer\Adapter\JsonOptions(array(
  36. 'cycle_check' => true,
  37. 'enable_json_expr_finder' => true,
  38. 'object_decode_type' => 1,
  39. ));
  40. $adapter->setOptions($options);
  41. $this->assertEquals(true, $adapter->getOptions()->getCycleCheck());
  42. $this->assertEquals(true, $adapter->getOptions()->getEnableJsonExprFinder());
  43. $this->assertEquals(1, $adapter->getOptions()->getObjectDecodeType());
  44. }
  45. public function testSerializeString()
  46. {
  47. $value = 'test';
  48. $expected = '"test"';
  49. $data = $this->adapter->serialize($value);
  50. $this->assertEquals($expected, $data);
  51. }
  52. public function testSerializeFalse()
  53. {
  54. $value = false;
  55. $expected = 'false';
  56. $data = $this->adapter->serialize($value);
  57. $this->assertEquals($expected, $data);
  58. }
  59. public function testSerializeNull()
  60. {
  61. $value = null;
  62. $expected = 'null';
  63. $data = $this->adapter->serialize($value);
  64. $this->assertEquals($expected, $data);
  65. }
  66. public function testSerializeNumeric()
  67. {
  68. $value = 100;
  69. $expected = '100';
  70. $data = $this->adapter->serialize($value);
  71. $this->assertEquals($expected, $data);
  72. }
  73. public function testSerializeObject()
  74. {
  75. $value = new \stdClass();
  76. $value->test = "test";
  77. $expected = '{"test":"test"}';
  78. $data = $this->adapter->serialize($value);
  79. $this->assertEquals($expected, $data);
  80. }
  81. public function testUnserializeString()
  82. {
  83. $value = '"test"';
  84. $expected = 'test';
  85. $data = $this->adapter->unserialize($value);
  86. $this->assertEquals($expected, $data);
  87. }
  88. public function testUnserializeFalse()
  89. {
  90. $value = 'false';
  91. $expected = false;
  92. $data = $this->adapter->unserialize($value);
  93. $this->assertEquals($expected, $data);
  94. }
  95. public function testUnserializeNull()
  96. {
  97. $value = 'null';
  98. $expected = null;
  99. $data = $this->adapter->unserialize($value);
  100. $this->assertEquals($expected, $data);
  101. }
  102. public function testUnserializeNumeric()
  103. {
  104. $value = '100';
  105. $expected = 100;
  106. $data = $this->adapter->unserialize($value);
  107. $this->assertEquals($expected, $data);
  108. }
  109. public function testUnserializeAsArray()
  110. {
  111. $value = '{"test":"test"}';
  112. $expected = array('test' => 'test');
  113. $data = $this->adapter->unserialize($value);
  114. $this->assertEquals($expected, $data);
  115. }
  116. public function testUnserializeAsObject()
  117. {
  118. $value = '{"test":"test"}';
  119. $expected = new \stdClass();
  120. $expected->test = 'test';
  121. $this->adapter->getOptions()->setObjectDecodeType(\Zend\Json\Json::TYPE_OBJECT);
  122. $data = $this->adapter->unserialize($value);
  123. $this->assertEquals($expected, $data);
  124. }
  125. public function testUnserialzeInvalid()
  126. {
  127. $value = 'not a serialized string';
  128. $this->setExpectedException(
  129. 'Zend\Serializer\Exception\RuntimeException',
  130. 'Unserialization failed: Decoding failed: Syntax error'
  131. );
  132. $this->adapter->unserialize($value);
  133. }
  134. }