HttpTestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Controller_Response_Http
  23. */
  24. require_once 'Zend/Controller/Response/Http.php';
  25. /**
  26. * Zend_Controller_Response_HttpTestCase
  27. *
  28. * @uses Zend_Controller_Response_Http
  29. * @package Zend_Controller
  30. * @subpackage Response
  31. */
  32. class Zend_Controller_Response_HttpTestCase extends Zend_Controller_Response_Http
  33. {
  34. /**
  35. * "send" headers by returning array of all headers that would be sent
  36. *
  37. * @return array
  38. */
  39. public function sendHeaders()
  40. {
  41. $headers = array();
  42. foreach ($this->_headersRaw as $header) {
  43. $headers[] = $header;
  44. }
  45. foreach ($this->_headers as $header) {
  46. $name = $header['name'];
  47. $key = strtolower($name);
  48. if (array_key_exists($name, $headers)) {
  49. if ($header['replace']) {
  50. $headers[$key] = $header['name'] . ': ' . $header['value'];
  51. }
  52. } else {
  53. $headers[$key] = $header['name'] . ': ' . $header['value'];
  54. }
  55. }
  56. return $headers;
  57. }
  58. /**
  59. * Can we send headers?
  60. *
  61. * @param bool $throw
  62. * @return void
  63. */
  64. public function canSendHeaders($throw = false)
  65. {
  66. return true;
  67. }
  68. /**
  69. * Return the concatenated body segments
  70. *
  71. * @return string
  72. */
  73. public function outputBody()
  74. {
  75. $fullContent = '';
  76. foreach ($this->_body as $content) {
  77. $fullContent .= $content;
  78. }
  79. return $fullContent;
  80. }
  81. /**
  82. * Get body and/or body segments
  83. *
  84. * @param bool|string $spec
  85. * @return string|array|null
  86. */
  87. public function getBody($spec = false)
  88. {
  89. if (false === $spec) {
  90. return $this->outputBody();
  91. } elseif (true === $spec) {
  92. return $this->_body;
  93. } elseif (is_string($spec) && isset($this->_body[$spec])) {
  94. return $this->_body[$spec];
  95. }
  96. return null;
  97. }
  98. /**
  99. * "send" Response
  100. *
  101. * Concats all response headers, and then final body (separated by two
  102. * newlines)
  103. *
  104. * @return string
  105. */
  106. public function sendResponse()
  107. {
  108. $headers = $this->sendHeaders();
  109. $content = implode("\n", $headers) . "\n\n";
  110. if ($this->isException() && $this->renderExceptions()) {
  111. $exceptions = '';
  112. foreach ($this->getException() as $e) {
  113. $exceptions .= $e->__toString() . "\n";
  114. }
  115. $content .= $exceptions;
  116. } else {
  117. $content .= $this->outputBody();
  118. }
  119. return $content;
  120. }
  121. }