1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\Test\PHPUnit; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract cache constraint. |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractCacheConstraint extends \PHPUnit_Framework_Constraint |
20
|
|
|
{ |
21
|
|
|
protected $header = 'X-Cache'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param string $header Cache debug header; defaults to X-Cache-Debug |
27
|
|
|
*/ |
28
|
29 |
|
public function __construct($header = null) |
29
|
|
|
{ |
30
|
29 |
|
if ($header) { |
|
|
|
|
31
|
3 |
|
$this->header = $header; |
32
|
|
|
} |
33
|
|
|
|
34
|
29 |
|
parent::__construct(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get cache header value. |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
abstract public function getValue(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
* |
47
|
|
|
* @param Response $other The guzzle response object |
48
|
|
|
*/ |
49
|
29 |
|
protected function matches($other) |
50
|
|
|
{ |
51
|
29 |
|
if (!$other instanceof ResponseInterface) { |
52
|
|
|
throw new \RuntimeException(sprintf('Expected a GuzzleHttp\Psr7\Response but got %s', get_class($other))); |
53
|
|
|
} |
54
|
29 |
|
if (!$other->hasHeader($this->header)) { |
55
|
1 |
|
$message = sprintf( |
56
|
|
|
'Response has no "%s" header. Configure your caching proxy ' |
57
|
1 |
|
.'to set the header with cache hit/miss status.', |
58
|
1 |
|
$this->header |
59
|
|
|
); |
60
|
1 |
|
if (200 !== $other->getStatusCode()) { |
61
|
|
|
$message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode()); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
throw new \RuntimeException($message); |
65
|
|
|
} |
66
|
|
|
|
67
|
28 |
|
return strpos((string) $other->getHeaderLine($this->header), $this->getValue()) !== false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
protected function failureDescription($other) |
74
|
|
|
{ |
75
|
2 |
|
return sprintf( |
76
|
2 |
|
'response (with status code %s) %s', |
77
|
2 |
|
$other->getStatusCode(), |
78
|
2 |
|
$this->toString() |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: