JsonTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_View
  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. // Call Zend_View_Helper_JsonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_JsonTest::main");
  25. }
  26. require_once 'Zend/View/Helper/Json.php';
  27. require_once 'Zend/Controller/Front.php';
  28. require_once 'Zend/Controller/Response/Http.php';
  29. require_once 'Zend/Json.php';
  30. require_once 'Zend/Layout.php';
  31. /**
  32. * Test class for Zend_View_Helper_Json
  33. *
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. class Zend_View_Helper_JsonTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * Runs the test methods of this class.
  46. *
  47. * @return void
  48. */
  49. public static function main()
  50. {
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_JsonTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Sets up the fixture, for example, open a network connection.
  56. * This method is called before a test is executed.
  57. *
  58. * @return void
  59. */
  60. public function setUp()
  61. {
  62. Zend_View_Helper_JsonTest_Layout::resetMvcInstance();
  63. $this->response = new Zend_Controller_Response_Http();
  64. $this->response->headersSentThrowsException = false;
  65. $front = Zend_Controller_Front::getInstance();
  66. $front->resetInstance();
  67. $front->setResponse($this->response);
  68. $this->helper = new Zend_View_Helper_Json();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. }
  79. public function verifyJsonHeader()
  80. {
  81. $headers = $this->response->getHeaders();
  82. $found = false;
  83. foreach ($headers as $header) {
  84. if ('Content-Type' == $header['name']) {
  85. if ($found) {
  86. $this->fail('Content-Type header has been set twice.');
  87. return null;
  88. }
  89. $found = true;
  90. $value = $header['value'];
  91. }
  92. }
  93. $this->assertTrue($found);
  94. $this->assertEquals('application/json', $value);
  95. }
  96. public function testJsonHelperSetsResponseHeader()
  97. {
  98. $this->helper->json('foobar');
  99. $this->verifyJsonHeader();
  100. }
  101. /**
  102. * @group ZF-10675
  103. */
  104. public function testJsonHelperReplacesContentTypeReponseHeaderIfAlreadySet()
  105. {
  106. $this->response->setHeader('Content-Type', 'text/html');
  107. $this->helper->json('foobar');
  108. $this->verifyJsonHeader();
  109. }
  110. public function testJsonHelperReturnsJsonEncodedString()
  111. {
  112. $data = $this->helper->json(array('foobar'));
  113. $this->assertTrue(is_string($data));
  114. $this->assertEquals(array('foobar'), Zend_Json::decode($data));
  115. }
  116. public function testJsonHelperDisablesLayoutsByDefault()
  117. {
  118. $layout = Zend_Layout::startMvc();
  119. $this->assertTrue($layout->isEnabled());
  120. $this->testJsonHelperReturnsJsonEncodedString();
  121. $this->assertFalse($layout->isEnabled());
  122. }
  123. public function testJsonHelperDoesNotDisableLayoutsWhenKeepLayoutFlagTrue()
  124. {
  125. $layout = Zend_Layout::startMvc();
  126. $this->assertTrue($layout->isEnabled());
  127. $data = $this->helper->json(array('foobar'), true);
  128. $this->assertTrue($layout->isEnabled());
  129. }
  130. /**
  131. * @group ZF-12397
  132. */
  133. public function testJsonHelperWithKeepLayoutAsArray()
  134. {
  135. $layout = Zend_Layout::startMvc();
  136. $this->assertTrue($layout->isEnabled());
  137. $data = $this->helper->json(
  138. array(
  139. 'foobar',
  140. ),
  141. array(
  142. 'keepLayouts' => true,
  143. 'encodeData' => false,
  144. )
  145. );
  146. $this->assertTrue($layout->isEnabled());
  147. $this->assertSame(array('foobar'), $data);
  148. }
  149. /**
  150. * @group ZF-10977
  151. */
  152. public function testJsonHelperWillAcceptPreencodedJson()
  153. {
  154. $data = $this->helper->json(Zend_Json::encode(array('f')), false, false);
  155. $this->assertEquals('["f"]', $data);
  156. }
  157. /**
  158. * @group ZF-10977
  159. */
  160. public function testJsonHelperWillSendHeadersWhenProvidedWithPreencodedJson()
  161. {
  162. $data = $this->helper->json(Zend_Json::encode(array('f')), false, false);
  163. $this->verifyJsonHeader();
  164. }
  165. }
  166. /**
  167. * Zend_Layout subclass to allow resetting MVC instance
  168. */
  169. class Zend_View_Helper_JsonTest_Layout extends Zend_Layout
  170. {
  171. public static function resetMvcInstance()
  172. {
  173. self::$_mvcInstance = null;
  174. }
  175. }
  176. // Call Zend_View_Helper_JsonTest::main() if this source file is executed directly.
  177. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_JsonTest::main") {
  178. Zend_View_Helper_JsonTest::main();
  179. }