1 | <?php |
||
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() |
|
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 | get_class($this), |
||
102 | 4 | $this->getName() |
|
103 | ); |
||
104 | |||
105 | 4 | if (isset($annotations['class']['clearCache']) |
|
106 | 4 | || isset($annotations['method']['clearCache']) |
|
107 | ) { |
||
108 | 1 | $this->getProxy()->clear(); |
|
109 | } |
||
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() |
|
140 | |||
141 | /** |
||
142 | * Get container. |
||
143 | * |
||
144 | * @return ContainerInterface |
||
|
|||
145 | */ |
||
146 | 4 | protected static function getContainer() |
|
150 | } |
||
151 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.