|
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
|
|
|
* This trait is used to have the same code and behavior between AbstractCacheConstraint and its legacy version. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait AbstractCacheConstraintTrait |
|
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
|
29 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
* |
|
40
|
|
|
* @param ResponseInterface $other The guzzle response object |
|
41
|
|
|
*/ |
|
42
|
29 |
|
public function matches($other) |
|
43
|
|
|
{ |
|
44
|
29 |
|
if (!$other instanceof ResponseInterface) { |
|
45
|
|
|
throw new \RuntimeException(sprintf('Expected a GuzzleHttp\Psr7\Response but got %s', get_class($other))); |
|
46
|
|
|
} |
|
47
|
29 |
|
if (!$other->hasHeader($this->header)) { |
|
48
|
1 |
|
$message = sprintf( |
|
49
|
|
|
'Response has no "%s" header. Configure your caching proxy ' |
|
50
|
1 |
|
.'to set the header with cache hit/miss status.', |
|
51
|
1 |
|
$this->header |
|
52
|
|
|
); |
|
53
|
1 |
|
if (200 !== $other->getStatusCode()) { |
|
54
|
|
|
$message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
$message .= "\nThe response headers are:\n\n"; |
|
58
|
|
|
|
|
59
|
|
|
foreach ($other->getHeaders() as $name => $values) { |
|
60
|
28 |
|
foreach ($values as $value) { |
|
61
|
|
|
$message .= $name.': '.$value; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$body = $other->getBody(); |
|
66
|
2 |
|
$body->rewind(); |
|
67
|
|
|
$message .= sprintf("\nThe response body is:\n\n %s", $body->getContents()); |
|
68
|
2 |
|
|
|
69
|
2 |
|
throw new \RuntimeException($message); |
|
70
|
2 |
|
} |
|
71
|
2 |
|
|
|
72
|
|
|
return false !== strpos((string) $other->getHeaderLine($this->header), $this->getValue()); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function failureDescription($other) |
|
79
|
|
|
{ |
|
80
|
|
|
return sprintf( |
|
81
|
|
|
'response (with status code %s) %s', |
|
82
|
|
|
$other->getStatusCode(), |
|
83
|
|
|
$this->toString() |
|
|
|
|
|
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: