|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\Test; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint; |
|
15
|
|
|
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint; |
|
16
|
|
|
use FOS\HttpCache\Test\Proxy\ProxyInterface; |
|
17
|
|
|
use Guzzle\Http\ClientInterface; |
|
18
|
|
|
use Guzzle\Http\Message\Response; |
|
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Base class that you can extend to run integration tests against a live |
|
24
|
|
|
* caching proxy instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class ProxyTestCase extends WebTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Assert cache hit. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Response $response |
|
32
|
|
|
* @param string|null $message |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function assertHit(Response $response, $message = null) |
|
35
|
|
|
{ |
|
36
|
1 |
|
self::assertThat($response, self::isCacheHit(), $message); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Assert cache miss. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Response $response |
|
43
|
|
|
* @param string|null $message |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function assertMiss(Response $response, $message = null) |
|
46
|
|
|
{ |
|
47
|
1 |
|
self::assertThat($response, self::isCacheMiss(), $message); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get cache hit constraint. |
|
52
|
|
|
* |
|
53
|
|
|
* @return IsCacheHitConstraint |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function isCacheHit() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return new IsCacheHitConstraint(self::getCacheDebugHeader()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get cache miss constraint. |
|
62
|
|
|
* |
|
63
|
|
|
* @return IsCacheMissConstraint |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public static function isCacheMiss() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return new IsCacheMissConstraint(self::getCacheDebugHeader()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get HTTP test client for making requests to your application through a |
|
72
|
|
|
* live caching proxy. |
|
73
|
|
|
* |
|
74
|
|
|
* @return ClientInterface |
|
75
|
|
|
*/ |
|
76
|
1 |
|
protected function getHttpClient() |
|
77
|
|
|
{ |
|
78
|
1 |
|
return static::getContainer()->get('fos_http_cache.test.default_client'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get a response from your application through a live caching proxy. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $url Request URL (absolute or relative) |
|
85
|
|
|
* @param array $headers Request HTTP headers |
|
86
|
|
|
* @param array $options Request options |
|
87
|
|
|
* |
|
88
|
|
|
* @return Response |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getResponse($url, array $headers = array(), $options = array()) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getHttpClient()->get($url, $headers, $options)->send(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Start and clear caching proxy server if test is annotated with @clearCache. |
|
97
|
|
|
*/ |
|
98
|
4 |
|
protected function setUp() |
|
99
|
|
|
{ |
|
100
|
4 |
|
$annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations( |
|
101
|
4 |
|
get_class($this), |
|
102
|
4 |
|
$this->getName() |
|
103
|
4 |
|
); |
|
104
|
|
|
|
|
105
|
4 |
|
if (isset($annotations['class']['clearCache']) |
|
106
|
4 |
|
|| isset($annotations['method']['clearCache']) |
|
107
|
4 |
|
) { |
|
108
|
1 |
|
$this->getProxy()->clear(); |
|
109
|
1 |
|
} |
|
110
|
4 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get proxy server. |
|
114
|
|
|
* |
|
115
|
|
|
* @return ProxyInterface |
|
116
|
|
|
* |
|
117
|
|
|
* @throws \RuntimeException If proxy server is not configured |
|
118
|
|
|
*/ |
|
119
|
1 |
|
protected function getProxy() |
|
120
|
|
|
{ |
|
121
|
1 |
|
if (!static::getContainer()->has('fos_http_cache.test.default_proxy_server')) { |
|
122
|
|
|
throw new \RuntimeException( |
|
123
|
|
|
'Proxy server is not available. Please configure a proxy_server ' |
|
124
|
|
|
.'under test in your application config.' |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
1 |
|
return static::getContainer()->get('fos_http_cache.test.default_proxy_server'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get HTTP header that shows whether the response was a cache hit or miss. |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
2 |
|
protected static function getCacheDebugHeader() |
|
137
|
|
|
{ |
|
138
|
2 |
|
return static::getContainer()->getParameter('fos_http_cache.test.cache_header'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get container. |
|
143
|
|
|
* |
|
144
|
|
|
* @return ContainerInterface |
|
|
|
|
|
|
145
|
|
|
*/ |
|
146
|
4 |
|
protected static function getContainer() |
|
147
|
|
|
{ |
|
148
|
4 |
|
return static::createClient()->getContainer(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.