|
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_ResponseHeader34 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 Zend_Controller_Response_Abstract $other String to examine |
|
111 * @param null|string Assertion type |
|
112 * @return bool |
|
113 */ |
|
114 public function evaluate($other, $assertType = null) |
|
115 { |
|
116 if (!$other instanceof Zend_Controller_Response_Abstract) { |
|
117 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
118 throw new Zend_Test_PHPUnit_Constraint_Exception('Header constraint assertions require a response object'); |
|
119 } |
|
120 |
|
121 if (strstr($assertType, 'Not')) { |
|
122 $this->setNegate(true); |
|
123 $assertType = str_replace('Not', '', $assertType); |
|
124 } |
|
125 |
|
126 if (!in_array($assertType, $this->_assertTypes)) { |
|
127 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
128 throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__)); |
|
129 } |
|
130 |
|
131 $this->_assertType = $assertType; |
|
132 |
|
133 $response = $other; |
|
134 $argv = func_get_args(); |
|
135 $argc = func_num_args(); |
|
136 |
|
137 switch ($assertType) { |
|
138 case self::ASSERT_RESPONSE_CODE: |
|
139 if (3 > $argc) { |
|
140 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
141 throw new Zend_Test_PHPUnit_Constraint_Exception('No response code provided against which to match'); |
|
142 } |
|
143 $this->_code = $code = $argv[2]; |
|
144 return ($this->_negate) |
|
145 ? $this->_notCode($response, $code) |
|
146 : $this->_code($response, $code); |
|
147 case self::ASSERT_HEADER: |
|
148 if (3 > $argc) { |
|
149 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
150 throw new Zend_Test_PHPUnit_Constraint_Exception('No header provided against which to match'); |
|
151 } |
|
152 $this->_header = $header = $argv[2]; |
|
153 return ($this->_negate) |
|
154 ? $this->_notHeader($response, $header) |
|
155 : $this->_header($response, $header); |
|
156 case self::ASSERT_HEADER_CONTAINS: |
|
157 if (4 > $argc) { |
|
158 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
159 throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__); |
|
160 } |
|
161 $this->_header = $header = $argv[2]; |
|
162 $this->_match = $match = $argv[3]; |
|
163 return ($this->_negate) |
|
164 ? $this->_notHeaderContains($response, $header, $match) |
|
165 : $this->_headerContains($response, $header, $match); |
|
166 case self::ASSERT_HEADER_REGEX: |
|
167 if (4 > $argc) { |
|
168 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
169 throw new Zend_Test_PHPUnit_Constraint_Exception('Both a header name and content to match are required for ' . __FUNCTION__); |
|
170 } |
|
171 $this->_header = $header = $argv[2]; |
|
172 $this->_match = $match = $argv[3]; |
|
173 return ($this->_negate) |
|
174 ? $this->_notHeaderRegex($response, $header, $match) |
|
175 : $this->_headerRegex($response, $header, $match); |
|
176 default: |
|
177 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
178 throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__); |
|
179 } |
|
180 } |
|
181 |
|
182 /** |
|
183 * Report Failure |
|
184 * |
|
185 * @see PHPUnit_Framework_Constraint for implementation details |
|
186 * @param mixed $other |
|
187 * @param string $description Additional message to display |
|
188 * @param bool $not |
|
189 * @return void |
|
190 * @throws PHPUnit_Framework_ExpectationFailedException |
|
191 */ |
|
192 public function fail($other, $description, $not = false) |
|
193 { |
|
194 require_once 'Zend/Test/PHPUnit/Constraint/Exception.php'; |
|
195 switch ($this->_assertType) { |
|
196 case self::ASSERT_RESPONSE_CODE: |
|
197 $failure = 'Failed asserting response code "%s"'; |
|
198 if ($this->_negate) { |
|
199 $failure = 'Failed asserting response code IS NOT "%s"'; |
|
200 } |
|
201 $failure = sprintf($failure, $this->_code); |
|
202 if (!$this->_negate && $this->_actualCode) { |
|
203 $failure .= sprintf(PHP_EOL . 'Was "%s"', $this->_actualCode); |
|
204 } |
|
205 break; |
|
206 case self::ASSERT_HEADER: |
|
207 $failure = 'Failed asserting response header "%s" found'; |
|
208 if ($this->_negate) { |
|
209 $failure = 'Failed asserting response response header "%s" WAS NOT found'; |
|
210 } |
|
211 $failure = sprintf($failure, $this->_header); |
|
212 break; |
|
213 case self::ASSERT_HEADER_CONTAINS: |
|
214 $failure = 'Failed asserting response header "%s" exists and contains "%s"'; |
|
215 if ($this->_negate) { |
|
216 $failure = 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"'; |
|
217 } |
|
218 $failure = sprintf($failure, $this->_header, $this->_match); |
|
219 break; |
|
220 case self::ASSERT_HEADER_REGEX: |
|
221 $failure = 'Failed asserting response header "%s" exists and matches regex "%s"'; |
|
222 if ($this->_negate) { |
|
223 $failure = 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"'; |
|
224 } |
|
225 $failure = sprintf($failure, $this->_header, $this->_match); |
|
226 break; |
|
227 default: |
|
228 throw new Zend_Test_PHPUnit_Constraint_Exception('Invalid assertion type ' . __FUNCTION__); |
|
229 } |
|
230 |
|
231 if (!empty($description)) { |
|
232 $failure = $description . "\n" . $failure; |
|
233 } |
|
234 |
|
235 throw new Zend_Test_PHPUnit_Constraint_Exception($failure); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Complete implementation |
|
240 * |
|
241 * @return string |
|
242 */ |
|
243 public function toString() |
|
244 { |
|
245 return ''; |
|
246 } |
|
247 |
|
248 /** |
|
249 * Compare response code for positive match |
|
250 * |
|
251 * @param Zend_Controller_Response_Abstract $response |
|
252 * @param int $code |
|
253 * @return bool |
|
254 */ |
|
255 protected function _code(Zend_Controller_Response_Abstract $response, $code) |
|
256 { |
|
257 $test = $this->_getCode($response); |
|
258 $this->_actualCode = $test; |
|
259 return ($test == $code); |
|
260 } |
|
261 |
|
262 /** |
|
263 * Compare response code for negative match |
|
264 * |
|
265 * @param Zend_Controller_Response_Abstract $response |
|
266 * @param int $code |
|
267 * @return bool |
|
268 */ |
|
269 protected function _notCode(Zend_Controller_Response_Abstract $response, $code) |
|
270 { |
|
271 $test = $this->_getCode($response); |
|
272 return ($test != $code); |
|
273 } |
|
274 |
|
275 /** |
|
276 * Retrieve response code |
|
277 * |
|
278 * @param Zend_Controller_Response_Abstract $response |
|
279 * @return int |
|
280 */ |
|
281 protected function _getCode(Zend_Controller_Response_Abstract $response) |
|
282 { |
|
283 $test = $response->getHttpResponseCode(); |
|
284 if (null === $test) { |
|
285 $test = 200; |
|
286 } |
|
287 return $test; |
|
288 } |
|
289 |
|
290 /** |
|
291 * Positive check for response header presence |
|
292 * |
|
293 * @param Zend_Controller_Response_Abstract $response |
|
294 * @param string $header |
|
295 * @return bool |
|
296 */ |
|
297 protected function _header(Zend_Controller_Response_Abstract $response, $header) |
|
298 { |
|
299 return (null !== $this->_getHeader($response, $header)); |
|
300 } |
|
301 |
|
302 /** |
|
303 * Negative check for response header presence |
|
304 * |
|
305 * @param Zend_Controller_Response_Abstract $response |
|
306 * @param string $header |
|
307 * @return bool |
|
308 */ |
|
309 protected function _notHeader(Zend_Controller_Response_Abstract $response, $header) |
|
310 { |
|
311 return (null === $this->_getHeader($response, $header)); |
|
312 } |
|
313 |
|
314 /** |
|
315 * Retrieve response header |
|
316 * |
|
317 * @param Zend_Controller_Response_Abstract $response |
|
318 * @param string $header |
|
319 * @return string|null |
|
320 */ |
|
321 protected function _getHeader(Zend_Controller_Response_Abstract $response, $header) |
|
322 { |
|
323 $headers = $response->sendHeaders(); |
|
324 $header = strtolower($header); |
|
325 if (array_key_exists($header, $headers)) { |
|
326 return $headers[$header]; |
|
327 } |
|
328 return null; |
|
329 } |
|
330 |
|
331 /** |
|
332 * Positive check for header contents matching pattern |
|
333 * |
|
334 * @param Zend_Controller_Response_Abstract $response |
|
335 * @param string $header |
|
336 * @param string $match |
|
337 * @return bool |
|
338 */ |
|
339 protected function _headerContains(Zend_Controller_Response_Abstract $response, $header, $match) |
|
340 { |
|
341 if (null === ($fullHeader = $this->_getHeader($response, $header))) { |
|
342 return false; |
|
343 } |
|
344 |
|
345 $contents = str_replace($header . ': ', '', $fullHeader); |
|
346 |
|
347 return (strstr($contents, $match) !== false); |
|
348 } |
|
349 |
|
350 /** |
|
351 * Negative check for header contents matching pattern |
|
352 * |
|
353 * @param Zend_Controller_Response_Abstract $response |
|
354 * @param string $header |
|
355 * @param string $match |
|
356 * @return bool |
|
357 */ |
|
358 protected function _notHeaderContains(Zend_Controller_Response_Abstract $response, $header, $match) |
|
359 { |
|
360 if (null === ($fullHeader = $this->_getHeader($response, $header))) { |
|
361 return true; |
|
362 } |
|
363 |
|
364 $contents = str_replace($header . ': ', '', $fullHeader); |
|
365 |
|
366 return (strstr($contents, $match) === false); |
|
367 } |
|
368 |
|
369 /** |
|
370 * Positive check for header contents matching regex |
|
371 * |
|
372 * @param Zend_Controller_Response_Abstract $response |
|
373 * @param string $header |
|
374 * @param string $pattern |
|
375 * @return bool |
|
376 */ |
|
377 protected function _headerRegex(Zend_Controller_Response_Abstract $response, $header, $pattern) |
|
378 { |
|
379 if (null === ($fullHeader = $this->_getHeader($response, $header))) { |
|
380 return false; |
|
381 } |
|
382 |
|
383 $contents = str_replace($header . ': ', '', $fullHeader); |
|
384 |
|
385 return preg_match($pattern, $contents); |
|
386 } |
|
387 |
|
388 /** |
|
389 * Negative check for header contents matching regex |
|
390 * |
|
391 * @param Zend_Controller_Response_Abstract $response |
|
392 * @param string $header |
|
393 * @param string $pattern |
|
394 * @return bool |
|
395 */ |
|
396 protected function _notHeaderRegex(Zend_Controller_Response_Abstract $response, $header, $pattern) |
|
397 { |
|
398 if (null === ($fullHeader = $this->_getHeader($response, $header))) { |
|
399 return true; |
|
400 } |
|
401 |
|
402 $contents = str_replace($header . ': ', '', $fullHeader); |
|
403 |
|
404 return !preg_match($pattern, $contents); |
|
405 } |
|
406 } |