JsonTest.php 6.5 KB

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