2
0

JsonTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // Call Zend_View_Helper_JsonTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_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/View/Helper/Json.php';
  10. require_once 'Zend/Controller/Front.php';
  11. require_once 'Zend/Controller/Response/Http.php';
  12. require_once 'Zend/Json.php';
  13. require_once 'Zend/Layout.php';
  14. /**
  15. * Test class for Zend_View_Helper_Json
  16. */
  17. class Zend_View_Helper_JsonTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Runs the test methods of this class.
  21. *
  22. * @return void
  23. */
  24. public static function main()
  25. {
  26. require_once "PHPUnit/TextUI/TestRunner.php";
  27. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_JsonTest");
  28. $result = PHPUnit_TextUI_TestRunner::run($suite);
  29. }
  30. /**
  31. * Sets up the fixture, for example, open a network connection.
  32. * This method is called before a test is executed.
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. Zend_View_Helper_JsonTest_Layout::$_mvcInstance = null;
  39. $this->response = new Zend_Controller_Response_Http();
  40. $this->response->headersSentThrowsException = false;
  41. $front = Zend_Controller_Front::getInstance();
  42. $front->resetInstance();
  43. $front->setResponse($this->response);
  44. $this->helper = new Zend_View_Helper_Json();
  45. }
  46. /**
  47. * Tears down the fixture, for example, close a network connection.
  48. * This method is called after a test is executed.
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. }
  55. public function verifyJsonHeader()
  56. {
  57. $headers = $this->response->getHeaders();
  58. $found = false;
  59. foreach ($headers as $header) {
  60. if ('Content-Type' == $header['name']) {
  61. $found = true;
  62. $value = $header['value'];
  63. break;
  64. }
  65. }
  66. $this->assertTrue($found);
  67. $this->assertEquals('application/json', $value);
  68. }
  69. public function testJsonHelperSetsResponseHeader()
  70. {
  71. $this->helper->json('foobar');
  72. $this->verifyJsonHeader();
  73. }
  74. public function testJsonHelperReturnsJsonEncodedString()
  75. {
  76. $data = $this->helper->json(array('foobar'));
  77. $this->assertTrue(is_string($data));
  78. $this->assertEquals(array('foobar'), Zend_Json::decode($data));
  79. }
  80. public function testJsonHelperDisablesLayoutsByDefault()
  81. {
  82. $layout = Zend_Layout::startMvc();
  83. $this->assertTrue($layout->isEnabled());
  84. $this->testJsonHelperReturnsJsonEncodedString();
  85. $this->assertFalse($layout->isEnabled());
  86. }
  87. public function testJsonHelperDoesNotDisableLayoutsWhenKeepLayoutFlagTrue()
  88. {
  89. $layout = Zend_Layout::startMvc();
  90. $this->assertTrue($layout->isEnabled());
  91. $data = $this->helper->json(array('foobar'), true);
  92. $this->assertTrue($layout->isEnabled());
  93. }
  94. }
  95. /**
  96. * Zend_Layout subclass to allow resetting MVC instance
  97. */
  98. class Zend_View_Helper_JsonTest_Layout extends Zend_Layout
  99. {
  100. public static $_mvcInstance;
  101. }
  102. // Call Zend_View_Helper_JsonTest::main() if this source file is executed directly.
  103. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_JsonTest::main") {
  104. Zend_View_Helper_JsonTest::main();
  105. }