ResponseHeader37.php 14 KB

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