JsonTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2014 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_Controller_Action_Helper_JsonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_JsonTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/Helper/Json.php';
  27. require_once 'Zend/Controller/Action/HelperBroker.php';
  28. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  29. require_once 'Zend/Controller/Front.php';
  30. require_once 'Zend/Controller/Response/Http.php';
  31. require_once 'Zend/Json.php';
  32. require_once 'Zend/Layout.php';
  33. /**
  34. * Test class for Zend_Controller_Action_Helper_Json
  35. *
  36. * @category Zend
  37. * @package Zend_Controller
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Controller
  42. * @group Zend_Controller_Action
  43. * @group Zend_Controller_Action_Helper
  44. */
  45. class Zend_Controller_Action_Helper_JsonTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_JsonTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. /**
  58. * Sets up the fixture, for example, open a network connection.
  59. * This method is called before a test is executed.
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. Zend_Controller_Action_Helper_JsonTest_Layout::resetMvcInstance();
  66. $this->response = new Zend_Controller_Response_Http();
  67. $this->response->headersSentThrowsException = false;
  68. $front = Zend_Controller_Front::getInstance();
  69. $front->resetInstance();
  70. $front->setResponse($this->response);
  71. $this->viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  72. Zend_Controller_Action_HelperBroker::addHelper($this->viewRenderer);
  73. $this->helper = new Zend_Controller_Action_Helper_Json();
  74. $this->helper->suppressExit = true;
  75. }
  76. /**
  77. * Tears down the fixture, for example, close a network connection.
  78. * This method is called after a test is executed.
  79. *
  80. * @return void
  81. */
  82. public function tearDown()
  83. {
  84. }
  85. public function verifyJsonHeader()
  86. {
  87. $headers = $this->response->getHeaders();
  88. $found = false;
  89. foreach ($headers as $header) {
  90. if ('Content-Type' == $header['name']) {
  91. $found = true;
  92. $value = $header['value'];
  93. break;
  94. }
  95. }
  96. $this->assertTrue($found);
  97. $this->assertEquals('application/json', $value);
  98. }
  99. public function testJsonHelperSetsResponseHeader()
  100. {
  101. $this->helper->encodeJson('foobar');
  102. $this->verifyJsonHeader();
  103. }
  104. public function testJsonHelperReturnsJsonEncodedString()
  105. {
  106. $data = $this->helper->encodeJson(array('foobar'));
  107. $this->assertTrue(is_string($data));
  108. $this->assertEquals(array('foobar'), Zend_Json::decode($data));
  109. }
  110. public function testJsonHelperDisablesLayoutsAndViewRendererByDefault()
  111. {
  112. $layout = Zend_Layout::startMvc();
  113. $this->assertTrue($layout->isEnabled());
  114. $this->assertFalse($this->viewRenderer->getNoRender());
  115. $this->testJsonHelperReturnsJsonEncodedString();
  116. $this->assertFalse($layout->isEnabled());
  117. $this->assertTrue($this->viewRenderer->getNoRender());
  118. }
  119. public function testJsonHelperDoesNotDisableLayoutsAndViewRendererWhenKeepLayoutFlagTrue()
  120. {
  121. $layout = Zend_Layout::startMvc();
  122. $this->assertTrue($layout->isEnabled());
  123. $this->assertFalse($this->viewRenderer->getNoRender());
  124. $data = $this->helper->encodeJson(array('foobar'), true);
  125. $this->assertTrue($layout->isEnabled());
  126. $this->assertFalse($this->viewRenderer->getNoRender());
  127. }
  128. public function testSendJsonSendsResponse()
  129. {
  130. $this->helper->sendJson(array('foobar'));
  131. $this->verifyJsonHeader();
  132. $response = $this->response->getBody();
  133. $this->assertSame(array('foobar'), Zend_Json::decode($response));
  134. }
  135. public function testDirectProxiesToSendJsonByDefault()
  136. {
  137. $this->helper->direct(array('foobar'));
  138. $this->verifyJsonHeader();
  139. $response = $this->response->getBody();
  140. $this->assertSame(array('foobar'), Zend_Json::decode($response));
  141. }
  142. public function testCanPreventDirectSendingResponse()
  143. {
  144. $data = $this->helper->direct(array('foobar'), false);
  145. $this->assertSame(array('foobar'), Zend_Json::decode($data));
  146. $this->verifyJsonHeader();
  147. $response = $this->response->getBody();
  148. $this->assertTrue(empty($response));
  149. }
  150. public function testCanKeepLayoutsWhenUsingDirect()
  151. {
  152. $layout = Zend_Layout::startMvc();
  153. $data = $this->helper->direct(array('foobar'), false, true);
  154. $this->assertTrue($layout->isEnabled());
  155. $this->assertFalse($this->viewRenderer->getNoRender());
  156. }
  157. /**
  158. * @group ZF-10977
  159. */
  160. public function testEncodeJsonWillAcceptPreencodedJson()
  161. {
  162. $data = $this->helper->encodeJson(Zend_Json::encode(array('f')), false, false);
  163. $this->assertEquals('["f"]', $data);
  164. }
  165. /**
  166. * @group ZF-10977
  167. */
  168. public function testSendJsonWillAcceptPreencodedJson()
  169. {
  170. $data = $this->helper->sendJson(Zend_Json::encode(array('f')), false, false);
  171. $this->assertEquals('["f"]', $data);
  172. }
  173. /**
  174. * @group ZF-10977
  175. */
  176. public function testDirectWillAcceptPreencodedJson()
  177. {
  178. $data = $this->helper->direct(Zend_Json::encode(array('f')), false, false, false);
  179. $this->assertEquals('["f"]', $data);
  180. }
  181. /**
  182. * @group ZF-10977
  183. */
  184. public function testSendingPreencodedJsonViaDirectWillStillSendHeaders()
  185. {
  186. $data = $this->helper->direct(Zend_Json::encode(array('f')), false, false, false);
  187. $this->verifyJsonHeader();
  188. }
  189. /**
  190. * @group ZF-10977
  191. */
  192. public function testSendingPreencodedJsonViaSendJsonWillStillSendHeaders()
  193. {
  194. $data = $this->helper->sendJson(Zend_Json::encode(array('f')), false, false);
  195. $this->verifyJsonHeader();
  196. }
  197. /**
  198. * @group ZF-10977
  199. */
  200. public function testSendingPreencodedJsonViaEncodeJsonWillStillSendHeaders()
  201. {
  202. $data = $this->helper->encodeJson(Zend_Json::encode(array('f')), false, false);
  203. $this->verifyJsonHeader();
  204. }
  205. }
  206. /**
  207. * Zend_Layout subclass to allow resetting MVC instance
  208. */
  209. class Zend_Controller_Action_Helper_JsonTest_Layout extends Zend_Layout
  210. {
  211. public static function resetMvcInstance()
  212. {
  213. self::$_mvcInstance = null;
  214. }
  215. }
  216. // Call Zend_Controller_Action_Helper_JsonTest::main() if this source file is executed directly.
  217. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_JsonTest::main") {
  218. Zend_Controller_Action_Helper_JsonTest::main();
  219. }