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