JsonTest.php 6.3 KB

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