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