JsonTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-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_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 dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once "PHPUnit/Framework/TestCase.php";
  28. require_once "PHPUnit/Framework/TestSuite.php";
  29. require_once 'Zend/View/Helper/Json.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_View_Helper_Json
  36. *
  37. * @category Zend
  38. * @package Zend_View
  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_View
  43. * @group Zend_View_Helper
  44. */
  45. class Zend_View_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. require_once "PHPUnit/TextUI/TestRunner.php";
  55. $suite = new PHPUnit_Framework_TestSuite("Zend_View_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_View_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->helper = new Zend_View_Helper_Json();
  73. }
  74. /**
  75. * Tears down the fixture, for example, close a network connection.
  76. * This method is called after a test is executed.
  77. *
  78. * @return void
  79. */
  80. public function tearDown()
  81. {
  82. }
  83. public function verifyJsonHeader()
  84. {
  85. $headers = $this->response->getHeaders();
  86. $found = false;
  87. foreach ($headers as $header) {
  88. if ('Content-Type' == $header['name']) {
  89. $found = true;
  90. $value = $header['value'];
  91. break;
  92. }
  93. }
  94. $this->assertTrue($found);
  95. $this->assertEquals('application/json', $value);
  96. }
  97. public function testJsonHelperSetsResponseHeader()
  98. {
  99. $this->helper->json('foobar');
  100. $this->verifyJsonHeader();
  101. }
  102. public function testJsonHelperReturnsJsonEncodedString()
  103. {
  104. $data = $this->helper->json(array('foobar'));
  105. $this->assertTrue(is_string($data));
  106. $this->assertEquals(array('foobar'), Zend_Json::decode($data));
  107. }
  108. public function testJsonHelperDisablesLayoutsByDefault()
  109. {
  110. $layout = Zend_Layout::startMvc();
  111. $this->assertTrue($layout->isEnabled());
  112. $this->testJsonHelperReturnsJsonEncodedString();
  113. $this->assertFalse($layout->isEnabled());
  114. }
  115. public function testJsonHelperDoesNotDisableLayoutsWhenKeepLayoutFlagTrue()
  116. {
  117. $layout = Zend_Layout::startMvc();
  118. $this->assertTrue($layout->isEnabled());
  119. $data = $this->helper->json(array('foobar'), true);
  120. $this->assertTrue($layout->isEnabled());
  121. }
  122. }
  123. /**
  124. * Zend_Layout subclass to allow resetting MVC instance
  125. */
  126. class Zend_View_Helper_JsonTest_Layout extends Zend_Layout
  127. {
  128. public static function resetMvcInstance()
  129. {
  130. self::$_mvcInstance = null;
  131. }
  132. }
  133. // Call Zend_View_Helper_JsonTest::main() if this source file is executed directly.
  134. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_JsonTest::main") {
  135. Zend_View_Helper_JsonTest::main();
  136. }