/Zend/Test/PHPUnit/Constraint/ResponseHeader.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 406 lines · 202 code · 39 blank · 165 comment · 22 complexity · 0192518ada5a0d6a03d57751afbca99a MD5 · raw file

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