JsonTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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-2010 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. * PHPUnit test case
  28. */
  29. require_once 'PHPUnit/Framework/TestCase.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Serializer
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Serializer_Adapter_JsonTest extends PHPUnit_Framework_TestCase
  38. {
  39. private $_adapter;
  40. public function setUp()
  41. {
  42. $this->_adapter = new Zend_Serializer_Adapter_Json();
  43. }
  44. public function tearDown()
  45. {
  46. $this->_adapter = null;
  47. }
  48. public function testSerializeString()
  49. {
  50. $value = 'test';
  51. $expected = '"test"';
  52. $data = $this->_adapter->serialize($value);
  53. $this->assertEquals($expected, $data);
  54. }
  55. public function testSerializeFalse()
  56. {
  57. $value = false;
  58. $expected = 'false';
  59. $data = $this->_adapter->serialize($value);
  60. $this->assertEquals($expected, $data);
  61. }
  62. public function testSerializeNull()
  63. {
  64. $value = null;
  65. $expected = 'null';
  66. $data = $this->_adapter->serialize($value);
  67. $this->assertEquals($expected, $data);
  68. }
  69. public function testSerializeNumeric()
  70. {
  71. $value = 100;
  72. $expected = '100';
  73. $data = $this->_adapter->serialize($value);
  74. $this->assertEquals($expected, $data);
  75. }
  76. public function testSerializeObject()
  77. {
  78. $value = new stdClass();
  79. $value->test = "test";
  80. $expected = '{"test":"test"}';
  81. $data = $this->_adapter->serialize($value);
  82. $this->assertEquals($expected, $data);
  83. }
  84. public function testUnserializeString()
  85. {
  86. $value = '"test"';
  87. $expected = 'test';
  88. $data = $this->_adapter->unserialize($value);
  89. $this->assertEquals($expected, $data);
  90. }
  91. public function testUnserializeFalse()
  92. {
  93. $value = 'false';
  94. $expected = false;
  95. $data = $this->_adapter->unserialize($value);
  96. $this->assertEquals($expected, $data);
  97. }
  98. public function testUnserializeNull() {
  99. $value = 'null';
  100. $expected = null;
  101. $data = $this->_adapter->unserialize($value);
  102. $this->assertEquals($expected, $data);
  103. }
  104. public function testUnserializeNumeric()
  105. {
  106. $value = '100';
  107. $expected = 100;
  108. $data = $this->_adapter->unserialize($value);
  109. $this->assertEquals($expected, $data);
  110. }
  111. public function testUnserializeAsArray()
  112. {
  113. $value = '{"test":"test"}';
  114. $expected = array('test' => 'test');
  115. $data = $this->_adapter->unserialize($value);
  116. $this->assertEquals($expected, $data);
  117. }
  118. public function testUnserializeAsObject()
  119. {
  120. $value = '{"test":"test"}';
  121. $expected = new stdClass();
  122. $expected->test = 'test';
  123. $data = $this->_adapter->unserialize($value, array('objectDecodeType' => Zend_Json::TYPE_OBJECT));
  124. $this->assertEquals($expected, $data);
  125. }
  126. public function testUnserialzeInvalid()
  127. {
  128. $value = 'not a serialized string';
  129. $this->setExpectedException('Zend_Serializer_Exception');
  130. $this->_adapter->unserialize($value);
  131. }
  132. }