JsonTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // Call Zend_Controller_Action_Helper_JsonTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_JsonTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Controller/Action/Helper/Json.php';
  10. require_once 'Zend/Controller/Action/HelperBroker.php';
  11. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  12. require_once 'Zend/Controller/Front.php';
  13. require_once 'Zend/Controller/Response/Http.php';
  14. require_once 'Zend/Json.php';
  15. require_once 'Zend/Layout.php';
  16. /**
  17. * Test class for Zend_Controller_Action_Helper_Json
  18. */
  19. class Zend_Controller_Action_Helper_JsonTest extends PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * Runs the test methods of this class.
  23. *
  24. * @return void
  25. */
  26. public static function main()
  27. {
  28. require_once "PHPUnit/TextUI/TestRunner.php";
  29. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_JsonTest");
  30. $result = PHPUnit_TextUI_TestRunner::run($suite);
  31. }
  32. /**
  33. * Sets up the fixture, for example, open a network connection.
  34. * This method is called before a test is executed.
  35. *
  36. * @return void
  37. */
  38. public function setUp()
  39. {
  40. Zend_Controller_Action_Helper_JsonTest_Layout::$_mvcInstance = null;
  41. $this->response = new Zend_Controller_Response_Http();
  42. $this->response->headersSentThrowsException = false;
  43. $front = Zend_Controller_Front::getInstance();
  44. $front->resetInstance();
  45. $front->setResponse($this->response);
  46. $this->viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  47. Zend_Controller_Action_HelperBroker::addHelper($this->viewRenderer);
  48. $this->helper = new Zend_Controller_Action_Helper_Json();
  49. $this->helper->suppressExit = true;
  50. }
  51. /**
  52. * Tears down the fixture, for example, close a network connection.
  53. * This method is called after a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function tearDown()
  58. {
  59. }
  60. public function verifyJsonHeader()
  61. {
  62. $headers = $this->response->getHeaders();
  63. $found = false;
  64. foreach ($headers as $header) {
  65. if ('Content-Type' == $header['name']) {
  66. $found = true;
  67. $value = $header['value'];
  68. break;
  69. }
  70. }
  71. $this->assertTrue($found);
  72. $this->assertEquals('application/json', $value);
  73. }
  74. public function testJsonHelperSetsResponseHeader()
  75. {
  76. $this->helper->encodeJson('foobar');
  77. $this->verifyJsonHeader();
  78. }
  79. public function testJsonHelperReturnsJsonEncodedString()
  80. {
  81. $data = $this->helper->encodeJson(array('foobar'));
  82. $this->assertTrue(is_string($data));
  83. $this->assertEquals(array('foobar'), Zend_Json::decode($data));
  84. }
  85. public function testJsonHelperDisablesLayoutsAndViewRendererByDefault()
  86. {
  87. $layout = Zend_Layout::startMvc();
  88. $this->assertTrue($layout->isEnabled());
  89. $this->assertFalse($this->viewRenderer->getNoRender());
  90. $this->testJsonHelperReturnsJsonEncodedString();
  91. $this->assertFalse($layout->isEnabled());
  92. $this->assertTrue($this->viewRenderer->getNoRender());
  93. }
  94. public function testJsonHelperDoesNotDisableLayoutsAndViewRendererWhenKeepLayoutFlagTrue()
  95. {
  96. $layout = Zend_Layout::startMvc();
  97. $this->assertTrue($layout->isEnabled());
  98. $this->assertFalse($this->viewRenderer->getNoRender());
  99. $data = $this->helper->encodeJson(array('foobar'), true);
  100. $this->assertTrue($layout->isEnabled());
  101. $this->assertFalse($this->viewRenderer->getNoRender());
  102. }
  103. public function testSendJsonSendsResponse()
  104. {
  105. $this->helper->sendJson(array('foobar'));
  106. $this->verifyJsonHeader();
  107. $response = $this->response->getBody();
  108. $this->assertSame(array('foobar'), Zend_Json::decode($response));
  109. }
  110. public function testDirectProxiesToSendJsonByDefault()
  111. {
  112. $this->helper->direct(array('foobar'));
  113. $this->verifyJsonHeader();
  114. $response = $this->response->getBody();
  115. $this->assertSame(array('foobar'), Zend_Json::decode($response));
  116. }
  117. public function testCanPreventDirectSendingResponse()
  118. {
  119. $data = $this->helper->direct(array('foobar'), false);
  120. $this->assertSame(array('foobar'), Zend_Json::decode($data));
  121. $this->verifyJsonHeader();
  122. $response = $this->response->getBody();
  123. $this->assertTrue(empty($response));
  124. }
  125. public function testCanKeepLayoutsWhenUsingDirect()
  126. {
  127. $layout = Zend_Layout::startMvc();
  128. $data = $this->helper->direct(array('foobar'), false, true);
  129. $this->assertTrue($layout->isEnabled());
  130. $this->assertFalse($this->viewRenderer->getNoRender());
  131. }
  132. }
  133. /**
  134. * Zend_Layout subclass to allow resetting MVC instance
  135. */
  136. class Zend_Controller_Action_Helper_JsonTest_Layout extends Zend_Layout
  137. {
  138. public static $_mvcInstance;
  139. }
  140. // Call Zend_Controller_Action_Helper_JsonTest::main() if this source file is executed directly.
  141. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_JsonTest::main") {
  142. Zend_Controller_Action_Helper_JsonTest::main();
  143. }