ResponseHeader37.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2012 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. /** @see PHPUnit_Framework_Constraint */
  23. require_once 'PHPUnit/Framework/Constraint.php';
  24. /**
  25. * Response header PHPUnit Constraint
  26. *
  27. * @uses PHPUnit_Framework_Constraint
  28. * @category Zend
  29. * @package Zend_Test
  30. * @subpackage PHPUnit
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Test_PHPUnit_Constraint_ResponseHeader extends PHPUnit_Framework_Constraint
  35. {
  36. /**#@+
  37. * Assertion type constants
  38. */
  39. const ASSERT_RESPONSE_CODE = 'assertResponseCode';
  40. const ASSERT_HEADER = 'assertHeader';
  41. const ASSERT_HEADER_CONTAINS = 'assertHeaderContains';
  42. const ASSERT_HEADER_REGEX = 'assertHeaderRegex';
  43. /**#@-*/
  44. /**
  45. * Current assertion type
  46. * @var string
  47. */
  48. protected $_assertType = null;
  49. /**
  50. * Available assertion types
  51. * @var array
  52. */
  53. protected $_assertTypes = array(
  54. self::ASSERT_RESPONSE_CODE,
  55. self::ASSERT_HEADER,
  56. self::ASSERT_HEADER_CONTAINS,
  57. self::ASSERT_HEADER_REGEX,
  58. );
  59. /**
  60. * @var int Response code
  61. */
  62. protected $_code = 200;
  63. /**
  64. * @var int Actual response code
  65. */
  66. protected $_actualCode = null;
  67. /**
  68. * @var string Header
  69. */
  70. protected $_header = null;
  71. /**
  72. * @var string pattern against which to compare header content
  73. */
  74. protected $_match = null;
  75. /**
  76. * Whether or not assertion is negated
  77. * @var bool
  78. */
  79. protected $_negate = false;
  80. /**
  81. * Constructor; setup constraint state
  82. *
  83. * @return void
  84. */
  85. public function __construct()
  86. {
  87. }
  88. /**
  89. * Indicate negative match
  90. *
  91. * @param bool $flag
  92. * @return void
  93. */
  94. public function setNegate($flag = true)
  95. {
  96. $this->_negate = $flag;
  97. }
  98. /**
  99. * Evaluate an object to see if it fits the constraints
  100. *
  101. * @param object of Zend_Controller_Response_Abstract to be evaluated
  102. * @param null|string Assertion type
  103. * @param int|string HTTP response code to evaluate against | header string (haystack)
  104. * @param string (optional) match (needle), may be required depending on assertion type
  105. * @return bool
  106. * NOTE:
  107. * Drastic changes up to PHPUnit 3.5.15 this was:
  108. * public function evaluate($other, $assertType = null)
  109. * In PHPUnit 3.6.0 they changed the interface into this:
  110. * public function evaluate($other, $description = '', $returnResult = FALSE)
  111. * We use the new interface for PHP-strict checking, but emulate the old one
  112. */
  113. public function evaluate($response, $assertType = '', $variable = FALSE)
  114. {
  115. if (!$response instanceof Zend_Controller_Response_Abstract) {
  116. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  117. throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object');
  118. }
  119. if (strstr($assertType, 'Not')) {
  120. $this->setNegate(true);
  121. $assertType = str_replace('Not', '', $assertType);
  122. }
  123. if (!in_array($assertType, $this->_assertTypes)) {
  124. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  125. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  126. }
  127. $this->_assertType = $assertType;
  128. $argv = func_get_args();
  129. $argc = func_num_args();
  130. switch ($assertType) {
  131. case self::ASSERT_RESPONSE_CODE:
  132. if (3 > $argc) {
  133. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  134. throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match');
  135. }
  136. $this->_code = $code = $argv[2];
  137. return ($this->_negate)
  138. ? $this->_notCode($response, $code)
  139. : $this->_code($response, $code);
  140. case self::ASSERT_HEADER:
  141. if (3 > $argc) {
  142. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  143. throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match');
  144. }
  145. $this->_header = $header = $argv[2];
  146. return ($this->_negate)
  147. ? $this->_notHeader($response, $header)
  148. : $this->_header($response, $header);
  149. case self::ASSERT_HEADER_CONTAINS:
  150. if (4 > $argc) {
  151. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  152. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . $assertType);
  153. }
  154. $this->_header = $header = $argv[2];
  155. $this->_match = $match = $argv[3];
  156. return ($this->_negate)
  157. ? $this->_notHeaderContains($response, $header, $match)
  158. : $this->_headerContains($response, $header, $match);
  159. case self::ASSERT_HEADER_REGEX:
  160. if (4 > $argc) {
  161. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  162. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . $assertType);
  163. }
  164. $this->_header = $header = $argv[2];
  165. $this->_match = $match = $argv[3];
  166. return ($this->_negate)
  167. ? $this->_notHeaderRegex($response, $header, $match)
  168. : $this->_headerRegex($response, $header, $match);
  169. default:
  170. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  171. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . $assertType);
  172. }
  173. }
  174. /**
  175. * Report Failure
  176. *
  177. * @see PHPUnit_Framework_Constraint for implementation details
  178. * @param mixed CSS selector path
  179. * @param string Failure description
  180. * @param object Cannot be used, null
  181. * @return void
  182. * @throws PHPUnit_Framework_ExpectationFailedException
  183. * NOTE:
  184. * Drastic changes up to PHPUnit 3.5.15 this was:
  185. * public function fail($other, $description, $not = false)
  186. * In PHPUnit 3.6.0 they changed the interface into this:
  187. * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
  188. * We use the new interface for PHP-strict checking
  189. */
  190. public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $cannot_be_used = NULL)
  191. {
  192. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  193. switch ($this->_assertType) {
  194. case self::ASSERT_RESPONSE_CODE:
  195. $failure = 'Failed asserting response code "%s"';
  196. if ($this->_negate) {
  197. $failure = 'Failed asserting response code IS NOT "%s"';
  198. }
  199. $failure = sprintf($failure, $this->_code);
  200. if (!$this->_negate && $this->_actualCode) {
  201. $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode);
  202. }
  203. break;
  204. case self::ASSERT_HEADER:
  205. $failure = 'Failed asserting response header "%s" found';
  206. if ($this->_negate) {
  207. $failure = 'Failed asserting response response header "%s" WAS NOT found';
  208. }
  209. $failure = sprintf($failure, $this->_header);
  210. break;
  211. case self::ASSERT_HEADER_CONTAINS:
  212. $failure = 'Failed asserting response header "%s" exists and contains "%s"';
  213. if ($this->_negate) {
  214. $failure = 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"';
  215. }
  216. $failure = sprintf($failure, $this->_header, $this->_match);
  217. break;
  218. case self::ASSERT_HEADER_REGEX:
  219. $failure = 'Failed asserting response header "%s" exists and matches regex "%s"';
  220. if ($this->_negate) {
  221. $failure = 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"';
  222. }
  223. $failure = sprintf($failure, $this->_header, $this->_match);
  224. break;
  225. default:
  226. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  227. }
  228. if (!empty($description)) {
  229. $failure = $description . "\n" . $failure;
  230. }
  231. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  232. }
  233. /**
  234. * Complete implementation
  235. *
  236. * @return string
  237. */
  238. public function toString()
  239. {
  240. return '';
  241. }
  242. /**
  243. * Compare response code for positive match
  244. *
  245. * @param Zend_Controller_Response_Abstract $response
  246. * @param int $code
  247. * @return bool
  248. */
  249. protected function _code(Zend_Controller_Response_Abstract $response, $code)
  250. {
  251. $test = $this->_getCode($response);
  252. $this->_actualCode = $test;
  253. return ($test == $code);
  254. }
  255. /**
  256. * Compare response code for negative match
  257. *
  258. * @param Zend_Controller_Response_Abstract $response
  259. * @param int $code
  260. * @return bool
  261. */
  262. protected function _notCode(Zend_Controller_Response_Abstract $response, $code)
  263. {
  264. $test = $this->_getCode($response);
  265. return ($test != $code);
  266. }
  267. /**
  268. * Retrieve response code
  269. *
  270. * @param Zend_Controller_Response_Abstract $response
  271. * @return int
  272. */
  273. protected function _getCode(Zend_Controller_Response_Abstract $response)
  274. {
  275. $test = $response->getHttpResponseCode();
  276. if (null === $test) {
  277. $test = 200;
  278. }
  279. return $test;
  280. }
  281. /**
  282. * Positive check for response header presence
  283. *
  284. * @param Zend_Controller_Response_Abstract $response
  285. * @param string $header
  286. * @return bool
  287. */
  288. protected function _header(Zend_Controller_Response_Abstract $response, $header)
  289. {
  290. return (null !== $this->_getHeader($response, $header));
  291. }
  292. /**
  293. * Negative check for response header presence
  294. *
  295. * @param Zend_Controller_Response_Abstract $response
  296. * @param string $header
  297. * @return bool
  298. */
  299. protected function _notHeader(Zend_Controller_Response_Abstract $response, $header)
  300. {
  301. return (null === $this->_getHeader($response, $header));
  302. }
  303. /**
  304. * Retrieve response header
  305. *
  306. * @param Zend_Controller_Response_Abstract $response
  307. * @param string $header
  308. * @return string|null
  309. */
  310. protected function _getHeader(Zend_Controller_Response_Abstract $response, $header)
  311. {
  312. $headers = $response->sendHeaders();
  313. $header = strtolower($header);
  314. if (array_key_exists($header, $headers)) {
  315. return $headers[$header];
  316. }
  317. return null;
  318. }
  319. /**
  320. * Positive check for header contents matching pattern
  321. *
  322. * @param Zend_Controller_Response_Abstract $response
  323. * @param string $header
  324. * @param string $match
  325. * @return bool
  326. */
  327. protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match)
  328. {
  329. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  330. return false;
  331. }
  332. $contents = str_replace($header . ': ', '', $fullHeader);
  333. return (strstr($contents, $match) !== false);
  334. }
  335. /**
  336. * Negative check for header contents matching pattern
  337. *
  338. * @param Zend_Controller_Response_Abstract $response
  339. * @param string $header
  340. * @param string $match
  341. * @return bool
  342. */
  343. protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match)
  344. {
  345. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  346. return true;
  347. }
  348. $contents = str_replace($header . ': ', '', $fullHeader);
  349. return (strstr($contents, $match) === false);
  350. }
  351. /**
  352. * Positive check for header contents matching regex
  353. *
  354. * @param Zend_Controller_Response_Abstract $response
  355. * @param string $header
  356. * @param string $pattern
  357. * @return bool
  358. */
  359. protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  360. {
  361. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  362. return false;
  363. }
  364. $contents = str_replace($header . ': ', '', $fullHeader);
  365. return preg_match($pattern, $contents);
  366. }
  367. /**
  368. * Negative check for header contents matching regex
  369. *
  370. * @param Zend_Controller_Response_Abstract $response
  371. * @param string $header
  372. * @param string $pattern
  373. * @return bool
  374. */
  375. protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  376. {
  377. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  378. return true;
  379. }
  380. $contents = str_replace($header . ': ', '', $fullHeader);
  381. return !preg_match($pattern, $contents);
  382. }
  383. }