JsonTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Serializer
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Serializer_Adapter_Json
  24. */
  25. require_once 'Zend/Serializer/Adapter/Json.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Serializer
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Serializer_Adapter_JsonTest extends PHPUnit_Framework_TestCase
  34. {
  35. private $_adapter;
  36. public function setUp()
  37. {
  38. $this->_adapter = new Zend_Serializer_Adapter_Json();
  39. }
  40. public function tearDown()
  41. {
  42. $this->_adapter = null;
  43. }
  44. public function testSerializeString()
  45. {
  46. $value = 'test';
  47. $expected = '"test"';
  48. $data = $this->_adapter->serialize($value);
  49. $this->assertEquals($expected, $data);
  50. }
  51. public function testSerializeFalse()
  52. {
  53. $value = false;
  54. $expected = 'false';
  55. $data = $this->_adapter->serialize($value);
  56. $this->assertEquals($expected, $data);
  57. }
  58. public function testSerializeNull()
  59. {
  60. $value = null;
  61. $expected = 'null';
  62. $data = $this->_adapter->serialize($value);
  63. $this->assertEquals($expected, $data);
  64. }
  65. public function testSerializeNumeric()
  66. {
  67. $value = 100;
  68. $expected = '100';
  69. $data = $this->_adapter->serialize($value);
  70. $this->assertEquals($expected, $data);
  71. }
  72. public function testSerializeObject()
  73. {
  74. $value = new stdClass();
  75. $value->test = "test";
  76. $expected = '{"test":"test"}';
  77. $data = $this->_adapter->serialize($value);
  78. $this->assertEquals($expected, $data);
  79. }
  80. public function testUnserializeString()
  81. {
  82. $value = '"test"';
  83. $expected = 'test';
  84. $data = $this->_adapter->unserialize($value);
  85. $this->assertEquals($expected, $data);
  86. }
  87. public function testUnserializeFalse()
  88. {
  89. $value = 'false';
  90. $expected = false;
  91. $data = $this->_adapter->unserialize($value);
  92. $this->assertEquals($expected, $data);
  93. }
  94. public function testUnserializeNull() {
  95. $value = 'null';
  96. $expected = null;
  97. $data = $this->_adapter->unserialize($value);
  98. $this->assertEquals($expected, $data);
  99. }
  100. public function testUnserializeNumeric()
  101. {
  102. $value = '100';
  103. $expected = 100;
  104. $data = $this->_adapter->unserialize($value);
  105. $this->assertEquals($expected, $data);
  106. }
  107. public function testUnserializeAsArray()
  108. {
  109. $value = '{"test":"test"}';
  110. $expected = array('test' => 'test');
  111. $data = $this->_adapter->unserialize($value);
  112. $this->assertEquals($expected, $data);
  113. }
  114. public function testUnserializeAsObject()
  115. {
  116. $value = '{"test":"test"}';
  117. $expected = new stdClass();
  118. $expected->test = 'test';
  119. $data = $this->_adapter->unserialize($value, array('objectDecodeType' => Zend_Json::TYPE_OBJECT));
  120. $this->assertEquals($expected, $data);
  121. }
  122. public function testUnserialzeInvalid()
  123. {
  124. $value = 'not a serialized string';
  125. $this->setExpectedException('Zend_Serializer_Exception');
  126. $this->_adapter->unserialize($value);
  127. }
  128. }