PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/share/test/web/response/httpheadersprocessor-test.php

https://github.com/php-tox/tox
PHP | 136 lines | 80 code | 12 blank | 44 comment | 0 complexity | c8d7bdb4bb09af71f9fc8497dca5704c MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Defines the test case for Tox\Web\Response\HTTPHeadersProcessor.
  4. *
  5. * This file is part of Tox.
  6. *
  7. * Tox is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Tox is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Tox. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @copyright Š 2012-2013 PHP-Tox.org
  21. * @license GNU General Public License, version 3
  22. */
  23. namespace Tox\Web\Response;
  24. use PHPUnit_Framework_TestCase;
  25. require_once __DIR__ . '/../../../../src/core/assembly.php';
  26. require_once __DIR__ . '/../../../../src/application/ioutputtask.php';
  27. require_once __DIR__ . '/../../../../src/web/ihttpheadersprocessor.php';
  28. require_once __DIR__ . '/../../../../src/web/response/httpheadersprocessor.php';
  29. require_once __DIR__ . '/../../../../src/core/exception.php';
  30. require_once __DIR__ . '/../../../../src/web/response/responserequiredexception.php';
  31. require_once __DIR__ . '/../../../../src/application/ioutput.php';
  32. require_once __DIR__ . '/../../../../src/application/output/output.php';
  33. require_once __DIR__ . '/../../../../src/web/iresponse.php';
  34. require_once __DIR__ . '/../../../../src/web/response/response.php';
  35. require_once __DIR__ . '/../../../../src/web/ipagecacheagent.php';
  36. require_once __DIR__ . '/../../../../src/web/response/pagecacheagent.php';
  37. require_once __DIR__ . '/../../../../src/application/iview.php';
  38. require_once __DIR__ . '/../../../../src/application/view/view.php';
  39. require_once __DIR__ . '/../../../../src/application/istreamingview.php';
  40. require_once __DIR__ . '/../../../../src/application/view/streamingview.php';
  41. use Exception;
  42. use PHPUnit_Framework_Error;
  43. /**
  44. * Tests Tox\Web\Response\HTTPHeadersProcessor.
  45. *
  46. * @internal
  47. *
  48. * @package tox.web.response
  49. * @author Snakevil Zen <zsnakevil@gmail.com>
  50. */
  51. class HTTPHeadersProcessorTest extends PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * @expectedException Tox\Web\Response\ResponseRequiredException
  55. */
  56. public function testConstructing()
  57. {
  58. new HTTPHeadersProcessor($this->getMock('Tox\\Application\\IOutput'));
  59. }
  60. public function testFetchingHeaders()
  61. {
  62. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('getHeaders'));
  63. $o_out->expects($this->once())->method('getHeaders')->will($this->returnValue(array()));
  64. $o_hhp = new HTTPHeadersProcessor($o_out);
  65. $o_hhp->preOutput();
  66. }
  67. /**
  68. * @depends testFetchingHeaders
  69. */
  70. public function testSendingParsedHeaders()
  71. {
  72. $o_out = new Response;
  73. $o_hhp = $this->getMock('Tox\\Web\\Response\\HTTPHeadersProcessor', array('sendHeader'), array($o_out));
  74. $o_hhp->expects($this->at(0))->method('sendHeader')
  75. ->with($this->equalTo('Date'), $this->equalTo('foo'));
  76. $o_hhp->expects($this->at(1))->method('sendHeader')
  77. ->with($this->equalTo('Date'), $this->equalTo('bar'));
  78. $o_out->addHeader('Date', microtime())->addHeader('Date', 'foo')->addHeader('Date', 'bar', false)
  79. ->setHeadersProcessor($o_hhp);
  80. ob_start();
  81. $o_out->close();
  82. ob_end_clean();
  83. }
  84. /**
  85. * @depends testSendingParsedHeaders
  86. */
  87. public function testCleaningSentHeaders()
  88. {
  89. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('setHeaders'));
  90. $o_out->expects($this->never())->method('setHeaders');
  91. ob_start();
  92. $o_out->close();
  93. ob_end_clean();
  94. $o_out = $this->getMock('Tox\\Web\\Response\\Response', array('setHeaders'));
  95. $o_hhp = $this->getMock('Tox\\Web\\Response\\HTTPHeadersProcessor', array('sendHeader'), array($o_out));
  96. $o_out->setHeadersProcessor($o_hhp)->addHeader('Date', microtime())
  97. ->expects($this->once())->method('setHeaders')->with($this->equalTo(array()));
  98. ob_start();
  99. $o_out->close();
  100. ob_end_clean();
  101. }
  102. /**
  103. * @depends testSendingParsedHeaders
  104. */
  105. public function testHeadersReallySent()
  106. {
  107. $m_value = microtime();
  108. $o_out = new Response;
  109. $o_out->addHeader('X-Foo', $m_value);
  110. try {
  111. ob_start();
  112. $o_out->close();
  113. ob_end_clean();
  114. $this->assertEquals(array('X-Foo: ' . $m_value), xdebug_get_headers());
  115. } catch (PHPUnit_Framework_Error $ex) {
  116. // XXX Uses `Tox\Core\ClassManager' to recognise whether in
  117. // `processIsolation' mode.
  118. $this->assertTrue(class_exists('Tox\\Core\\ClassManager'));
  119. } catch (Exception $ex) {
  120. $this->fail();
  121. }
  122. }
  123. }
  124. // vi:ft=php fenc=utf-8 ff=unix ts=4 sts=4 et sw=4 fen fdm=indent fdl=1 tw=120