PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/devtoannh/cafe
PHP | 400 lines | 200 code | 38 blank | 162 comment | 22 complexity | 21e01eb52ec2abd7e6f117022e184f22 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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 string Header
  65. */
  66. protected $_header = null;
  67. /**
  68. * @var string pattern against which to compare header content
  69. */
  70. protected $_match = null;
  71. /**
  72. * Whether or not assertion is negated
  73. * @var bool
  74. */
  75. protected $_negate = false;
  76. /**
  77. * Constructor; setup constraint state
  78. *
  79. * @return void
  80. */
  81. public function __construct()
  82. {
  83. }
  84. /**
  85. * Indicate negative match
  86. *
  87. * @param bool $flag
  88. * @return void
  89. */
  90. public function setNegate($flag = true)
  91. {
  92. $this->_negate = $flag;
  93. }
  94. /**
  95. * Evaluate an object to see if it fits the constraints
  96. *
  97. * @param Zend_Controller_Response_Abstract $other String to examine
  98. * @param null|string Assertion type
  99. * @return bool
  100. */
  101. public function evaluate($other, $assertType = null)
  102. {
  103. if (!$other instanceof Zend_Controller_Response_Abstract) {
  104. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  105. throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object');
  106. }
  107. if (strstr($assertType, 'Not')) {
  108. $this->setNegate(true);
  109. $assertType = str_replace('Not', '', $assertType);
  110. }
  111. if (!in_array($assertType, $this->_assertTypes)) {
  112. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  113. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  114. }
  115. $this->_assertType = $assertType;
  116. $response = $other;
  117. $argv = func_get_args();
  118. $argc = func_num_args();
  119. switch ($assertType) {
  120. case self::ASSERT_RESPONSE_CODE:
  121. if (3 > $argc) {
  122. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  123. throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match');
  124. }
  125. $this->_code = $code = $argv[2];
  126. return ($this->_negate)
  127. ? $this->_notCode($response, $code)
  128. : $this->_code($response, $code);
  129. case self::ASSERT_HEADER:
  130. if (3 > $argc) {
  131. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  132. throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match');
  133. }
  134. $this->_header = $header = $argv[2];
  135. return ($this->_negate)
  136. ? $this->_notHeader($response, $header)
  137. : $this->_header($response, $header);
  138. case self::ASSERT_HEADER_CONTAINS:
  139. if (4 > $argc) {
  140. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  141. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
  142. }
  143. $this->_header = $header = $argv[2];
  144. $this->_match = $match = $argv[3];
  145. return ($this->_negate)
  146. ? $this->_notHeaderContains($response, $header, $match)
  147. : $this->_headerContains($response, $header, $match);
  148. case self::ASSERT_HEADER_REGEX:
  149. if (4 > $argc) {
  150. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  151. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
  152. }
  153. $this->_header = $header = $argv[2];
  154. $this->_match = $match = $argv[3];
  155. return ($this->_negate)
  156. ? $this->_notHeaderRegex($response, $header, $match)
  157. : $this->_headerRegex($response, $header, $match);
  158. default:
  159. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  160. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  161. }
  162. }
  163. /**
  164. * Report Failure
  165. *
  166. * @see PHPUnit_Framework_Constraint for implementation details
  167. * @param mixed $other
  168. * @param string $description Additional message to display
  169. * @param bool $not
  170. * @return void
  171. * @throws PHPUnit_Framework_ExpectationFailedException
  172. */
  173. public function fail($other, $description, $not = false)
  174. {
  175. require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  176. switch ($this->_assertType) {
  177. case self::ASSERT_RESPONSE_CODE:
  178. $failure = 'Failed asserting response code "%s"';
  179. if ($this->_negate) {
  180. $failure = 'Failed asserting response code IS NOT "%s"';
  181. }
  182. $failure = sprintf($failure, $this->_code);
  183. break;
  184. case self::ASSERT_HEADER:
  185. $failure = 'Failed asserting response header "%s" found';
  186. if ($this->_negate) {
  187. $failure = 'Failed asserting response response header "%s" WAS NOT found';
  188. }
  189. $failure = sprintf($failure, $this->_header);
  190. break;
  191. case self::ASSERT_HEADER_CONTAINS:
  192. $failure = 'Failed asserting response header "%s" exists and contains "%s"';
  193. if ($this->_negate) {
  194. $failure = 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"';
  195. }
  196. $failure = sprintf($failure, $this->_header, $this->_match);
  197. break;
  198. case self::ASSERT_HEADER_REGEX:
  199. $failure = 'Failed asserting response header "%s" exists and matches regex "%s"';
  200. if ($this->_negate) {
  201. $failure = 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"';
  202. }
  203. $failure = sprintf($failure, $this->_header, $this->_match);
  204. break;
  205. default:
  206. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  207. }
  208. if (!empty($description)) {
  209. $failure = $description . "\n" . $failure;
  210. }
  211. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  212. }
  213. /**
  214. * Complete implementation
  215. *
  216. * @return string
  217. */
  218. public function toString()
  219. {
  220. return '';
  221. }
  222. /**
  223. * Compare response code for positive match
  224. *
  225. * @param Zend_Controller_Response_Abstract $response
  226. * @param int $code
  227. * @return bool
  228. */
  229. protected function _code(Zend_Controller_Response_Abstract $response, $code)
  230. {
  231. $test = $this->_getCode($response);
  232. return ($test == $code);
  233. }
  234. /**
  235. * Compare response code for negative match
  236. *
  237. * @param Zend_Controller_Response_Abstract $response
  238. * @param int $code
  239. * @return bool
  240. */
  241. protected function _notCode(Zend_Controller_Response_Abstract $response, $code)
  242. {
  243. $test = $this->_getCode($response);
  244. return ($test != $code);
  245. }
  246. /**
  247. * Retrieve response code
  248. *
  249. * @param Zend_Controller_Response_Abstract $response
  250. * @return int
  251. */
  252. protected function _getCode(Zend_Controller_Response_Abstract $response)
  253. {
  254. $test = $response->getHttpResponseCode();
  255. if (null === $test) {
  256. $test = 200;
  257. }
  258. return $test;
  259. }
  260. /**
  261. * Positive check for response header presence
  262. *
  263. * @param Zend_Controller_Response_Abstract $response
  264. * @param string $header
  265. * @return bool
  266. */
  267. protected function _header(Zend_Controller_Response_Abstract $response, $header)
  268. {
  269. return (null !== $this->_getHeader($response, $header));
  270. }
  271. /**
  272. * Negative check for response header presence
  273. *
  274. * @param Zend_Controller_Response_Abstract $response
  275. * @param string $header
  276. * @return bool
  277. */
  278. protected function _notHeader(Zend_Controller_Response_Abstract $response, $header)
  279. {
  280. return (null === $this->_getHeader($response, $header));
  281. }
  282. /**
  283. * Retrieve response header
  284. *
  285. * @param Zend_Controller_Response_Abstract $response
  286. * @param string $header
  287. * @return string|null
  288. */
  289. protected function _getHeader(Zend_Controller_Response_Abstract $response, $header)
  290. {
  291. $headers = $response->sendHeaders();
  292. $header = strtolower($header);
  293. if (array_key_exists($header, $headers)) {
  294. return $headers[$header];
  295. }
  296. return null;
  297. }
  298. /**
  299. * Positive check for header contents matching pattern
  300. *
  301. * @param Zend_Controller_Response_Abstract $response
  302. * @param string $header
  303. * @param string $match
  304. * @return bool
  305. */
  306. protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match)
  307. {
  308. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  309. return false;
  310. }
  311. $contents = str_replace($header . ': ', '', $fullHeader);
  312. return (strstr($contents, $match));
  313. }
  314. /**
  315. * Negative check for header contents matching pattern
  316. *
  317. * @param Zend_Controller_Response_Abstract $response
  318. * @param string $header
  319. * @param string $match
  320. * @return bool
  321. */
  322. protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match)
  323. {
  324. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  325. return true;
  326. }
  327. $contents = str_replace($header . ': ', '', $fullHeader);
  328. return (!strstr($contents, $match));
  329. }
  330. /**
  331. * Positive check for header contents matching regex
  332. *
  333. * @param Zend_Controller_Response_Abstract $response
  334. * @param string $header
  335. * @param string $pattern
  336. * @return bool
  337. */
  338. protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  339. {
  340. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  341. return false;
  342. }
  343. $contents = str_replace($header . ': ', '', $fullHeader);
  344. return preg_match($pattern, $contents);
  345. }
  346. /**
  347. * Negative check for header contents matching regex
  348. *
  349. * @param Zend_Controller_Response_Abstract $response
  350. * @param string $header
  351. * @param string $pattern
  352. * @return bool
  353. */
  354. protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  355. {
  356. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  357. return true;
  358. }
  359. $contents = str_replace($header . ': ', '', $fullHeader);
  360. return !preg_match($pattern, $contents);
  361. }
  362. }