ResponseHeader41.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_ResponseHeader41 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. * NOTE 2:
  188. * Interface changed again in PHPUnit 4.1.0 because of refactoring to SebastianBergmann\Comparator
  189. */
  190. public function fail($other, $description, \SebastianBergmann\Comparator\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. }