PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Test/PHPUnit/Constraint/ResponseHeader34.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 406 lines | 196 code | 38 blank | 172 comment | 24 complexity | bbbe8826ac0d2504141bf92013a85c2a 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-2015 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-2015 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_ResponseHeader34 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 Zend_Controller_Response_Abstract $other String to examine
  100. * @param null|string Assertion type
  101. * @return bool
  102. */
  103. public function evaluate($other, $assertType = null)
  104. {
  105. if (!$other instanceof Zend_Controller_Response_Abstract) {
  106. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  107. throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object');
  108. }
  109. if (strstr($assertType, 'Not')) {
  110. $this->setNegate(true);
  111. $assertType = str_replace('Not', '', $assertType);
  112. }
  113. if (!in_array($assertType, $this->_assertTypes)) {
  114. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  115. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  116. }
  117. $this->_assertType = $assertType;
  118. $response = $other;
  119. $argv = func_get_args();
  120. $argc = func_num_args();
  121. switch ($assertType) {
  122. case self::ASSERT_RESPONSE_CODE:
  123. if (3 > $argc) {
  124. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  125. throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match');
  126. }
  127. $this->_code = $code = $argv[2];
  128. return ($this->_negate)
  129. ? $this->_notCode($response, $code)
  130. : $this->_code($response, $code);
  131. case self::ASSERT_HEADER:
  132. if (3 > $argc) {
  133. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  134. throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match');
  135. }
  136. $this->_header = $header = $argv[2];
  137. return ($this->_negate)
  138. ? $this->_notHeader($response, $header)
  139. : $this->_header($response, $header);
  140. case self::ASSERT_HEADER_CONTAINS:
  141. if (4 > $argc) {
  142. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  143. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
  144. }
  145. $this->_header = $header = $argv[2];
  146. $this->_match = $match = $argv[3];
  147. return ($this->_negate)
  148. ? $this->_notHeaderContains($response, $header, $match)
  149. : $this->_headerContains($response, $header, $match);
  150. case self::ASSERT_HEADER_REGEX:
  151. if (4 > $argc) {
  152. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  153. throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__);
  154. }
  155. $this->_header = $header = $argv[2];
  156. $this->_match = $match = $argv[3];
  157. return ($this->_negate)
  158. ? $this->_notHeaderRegex($response, $header, $match)
  159. : $this->_headerRegex($response, $header, $match);
  160. default:
  161. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  162. throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  163. }
  164. }
  165. /**
  166. * Report Failure
  167. *
  168. * @see PHPUnit_Framework_Constraint for implementation details
  169. * @param mixed $other
  170. * @param string $description Additional message to display
  171. * @param bool $not
  172. * @return void
  173. * @throws PHPUnit_Framework_ExpectationFailedException
  174. */
  175. public function fail($other, $description, $not = false)
  176. {
  177. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  178. switch ($this->_assertType) {
  179. case self::ASSERT_RESPONSE_CODE:
  180. $failure = 'Failed asserting response code "%s"';
  181. if ($this->_negate) {
  182. $failure = 'Failed asserting response code IS NOT "%s"';
  183. }
  184. $failure = sprintf($failure, $this->_code);
  185. if (!$this->_negate && $this->_actualCode) {
  186. $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode);
  187. }
  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 Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__);
  212. }
  213. if (!empty($description)) {
  214. $failure = $description . "\n" . $failure;
  215. }
  216. throw new Zend_Test_PHPUnit_Constraint_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(Zend_Controller_Response_Abstract $response, $code)
  235. {
  236. $test = $this->_getCode($response);
  237. $this->_actualCode = $test;
  238. return ($test == $code);
  239. }
  240. /**
  241. * Compare response code for negative match
  242. *
  243. * @param Zend_Controller_Response_Abstract $response
  244. * @param int $code
  245. * @return bool
  246. */
  247. protected function _notCode(Zend_Controller_Response_Abstract $response, $code)
  248. {
  249. $test = $this->_getCode($response);
  250. return ($test != $code);
  251. }
  252. /**
  253. * Retrieve response code
  254. *
  255. * @param Zend_Controller_Response_Abstract $response
  256. * @return int
  257. */
  258. protected function _getCode(Zend_Controller_Response_Abstract $response)
  259. {
  260. $test = $response->getHttpResponseCode();
  261. if (null === $test) {
  262. $test = 200;
  263. }
  264. return $test;
  265. }
  266. /**
  267. * Positive check for response header presence
  268. *
  269. * @param Zend_Controller_Response_Abstract $response
  270. * @param string $header
  271. * @return bool
  272. */
  273. protected function _header(Zend_Controller_Response_Abstract $response, $header)
  274. {
  275. return (null !== $this->_getHeader($response, $header));
  276. }
  277. /**
  278. * Negative check for response header presence
  279. *
  280. * @param Zend_Controller_Response_Abstract $response
  281. * @param string $header
  282. * @return bool
  283. */
  284. protected function _notHeader(Zend_Controller_Response_Abstract $response, $header)
  285. {
  286. return (null === $this->_getHeader($response, $header));
  287. }
  288. /**
  289. * Retrieve response header
  290. *
  291. * @param Zend_Controller_Response_Abstract $response
  292. * @param string $header
  293. * @return string|null
  294. */
  295. protected function _getHeader(Zend_Controller_Response_Abstract $response, $header)
  296. {
  297. $headers = $response->sendHeaders();
  298. $header = strtolower($header);
  299. if (array_key_exists($header, $headers)) {
  300. return $headers[$header];
  301. }
  302. return null;
  303. }
  304. /**
  305. * Positive check for header contents matching pattern
  306. *
  307. * @param Zend_Controller_Response_Abstract $response
  308. * @param string $header
  309. * @param string $match
  310. * @return bool
  311. */
  312. protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match)
  313. {
  314. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  315. return false;
  316. }
  317. $contents = str_replace($header . ': ', '', $fullHeader);
  318. return (strstr($contents, $match) !== false);
  319. }
  320. /**
  321. * Negative check for header contents matching pattern
  322. *
  323. * @param Zend_Controller_Response_Abstract $response
  324. * @param string $header
  325. * @param string $match
  326. * @return bool
  327. */
  328. protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match)
  329. {
  330. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  331. return true;
  332. }
  333. $contents = str_replace($header . ': ', '', $fullHeader);
  334. return (strstr($contents, $match) === false);
  335. }
  336. /**
  337. * Positive check for header contents matching regex
  338. *
  339. * @param Zend_Controller_Response_Abstract $response
  340. * @param string $header
  341. * @param string $pattern
  342. * @return bool
  343. */
  344. protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  345. {
  346. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  347. return false;
  348. }
  349. $contents = str_replace($header . ': ', '', $fullHeader);
  350. return preg_match($pattern, $contents);
  351. }
  352. /**
  353. * Negative check for header contents matching regex
  354. *
  355. * @param Zend_Controller_Response_Abstract $response
  356. * @param string $header
  357. * @param string $pattern
  358. * @return bool
  359. */
  360. protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern)
  361. {
  362. if (null === ($fullHeader = $this->_getHeader($response, $header))) {
  363. return true;
  364. }
  365. $contents = str_replace($header . ': ', '', $fullHeader);
  366. return !preg_match($pattern, $contents);
  367. }
  368. }