Skip to content

Commit 9c6c5d4

Browse files
committed
Merge pull request #122 from FriendsOfSymfony/testcase
Add ProxyTestCase
2 parents cbddbb8 + a3f47ad commit 9c6c5d4

File tree

18 files changed

+795
-20
lines changed

18 files changed

+795
-20
lines changed

DependencyInjection/Configuration.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ public function getConfigTreeBuilder()
9191
->ifTrue(function ($v) {return $v['invalidation']['rules'] && !$v['invalidation']['enabled'];})
9292
->thenInvalid('You need to enable the cache_manager and invalidation to use rules.')
9393
->end()
94+
->validate()
95+
->ifTrue(function ($v) {
96+
return isset($v['test'])
97+
&& $v['test']['client']['varnish']['enabled']
98+
&& !isset($v['proxy_client']['varnish']);
99+
})
100+
->then(function ($v) {
101+
if ('auto' === $v['test']['client']['varnish']['enabled']) {
102+
$v['test']['client']['varnish']['enabled'] = false;
103+
104+
return $v;
105+
}
106+
throw new InvalidConfigurationException('You need to configure the Varnish proxy_client to use the Varnish test client');
107+
})
108+
->end()
109+
->validate()
110+
->ifTrue(function ($v) {
111+
if (isset($v['test'])) {
112+
return $v['test']['client']['nginx']['enabled'] && !isset($v['proxy_client']['nginx']);
113+
}
114+
})
115+
->then(function ($v) {
116+
if ('auto' === $v['test']['client']['nginx']['enabled']) {
117+
$v['test']['client']['nginx']['enabled'] = false;
118+
119+
return $v;
120+
}
121+
throw new InvalidConfigurationException('You need to configure the Nginx proxy_client to use the Nginx test client');
122+
})
123+
->end()
94124
;
95125

96126
$this->addCacheControlSection($rootNode);
@@ -100,6 +130,7 @@ public function getConfigTreeBuilder()
100130
$this->addInvalidationSection($rootNode);
101131
$this->addUserContextListenerSection($rootNode);
102132
$this->addFlashMessageSection($rootNode);
133+
$this->addTestSection($rootNode);
103134
$this->addDebugSection($rootNode);
104135

105136
return $treeBuilder;
@@ -287,6 +318,77 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
287318
->end();
288319
}
289320

321+
private function addTestSection(ArrayNodeDefinition $rootNode)
322+
{
323+
$rootNode
324+
->children()
325+
->arrayNode('test')
326+
->children()
327+
->scalarNode('cache_header')
328+
->defaultValue('X-Cache')
329+
->info('HTTP cache hit/miss header')
330+
->end()
331+
->arrayNode('proxy_server')
332+
->info('Configure how caching proxy will be run in your tests')
333+
->children()
334+
->enumNode('default')
335+
->values(array('varnish', 'nginx'))
336+
->info('If you configure more than one proxy server, specify which client is the default.')
337+
->end()
338+
->arrayNode('varnish')
339+
->children()
340+
->scalarNode('config_file')->isRequired()->end()
341+
->scalarNode('binary')->defaultValue('varnishd')->end()
342+
->integerNode('port')->defaultValue(6181)->end()
343+
->scalarNode('ip')->defaultValue('127.0.0.1')->end()
344+
->end()
345+
->end()
346+
->arrayNode('nginx')
347+
->children()
348+
->scalarNode('config_file')->isRequired()->end()
349+
->scalarNode('binary')->defaultValue('nginx')->end()
350+
->integerNode('port')->defaultValue(8080)->end()
351+
->scalarNode('ip')->defaultValue('127.0.0.1')->end()
352+
->end()
353+
->end()
354+
->end()
355+
->end()
356+
->arrayNode('client')
357+
->addDefaultsIfNotSet()
358+
->children()
359+
->enumNode('default')
360+
->values(array('varnish', 'nginx'))
361+
->info('If you configure more than one proxy client, specify which client is the default.')
362+
->end()
363+
->arrayNode('varnish')
364+
->addDefaultsIfNotSet()
365+
->canBeEnabled()
366+
->children()
367+
->enumNode('enabled')
368+
->values(array(true, false, 'auto'))
369+
->defaultValue('auto')
370+
->info('Whether to enable the Varnish test client.')
371+
->end()
372+
->end()
373+
->end()
374+
->arrayNode('nginx')
375+
->addDefaultsIfNotSet()
376+
->canBeEnabled()
377+
->children()
378+
->enumNode('enabled')
379+
->values(array(true, false, 'auto'))
380+
->defaultValue('auto')
381+
->info('Whether to enable the Nginx test client.')
382+
->end()
383+
->end()
384+
->end()
385+
->end()
386+
->end()
387+
->end()
388+
->end()
389+
->end();
390+
}
391+
290392
/**
291393
* Cache manager main section
292394
*

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function load(array $configs, ContainerBuilder $container)
5757
$this->loadProxyClient($container, $loader, $config['proxy_client']);
5858
}
5959

60+
if (isset($config['test'])) {
61+
$this->loadTest($container, $loader, $config['test']);
62+
}
63+
6064
if ($config['cache_manager']['enabled']) {
6165
$loader->load('cache_manager.xml');
6266
}
@@ -196,34 +200,33 @@ private function createRequestMatcher(ContainerBuilder $container, $path = null,
196200

197201
private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config)
198202
{
199-
$default = empty($config['default']) ? false : $config['default'];
200203
if (isset($config['varnish'])) {
201204
$this->loadVarnish($container, $loader, $config['varnish']);
202-
if (!$default) {
203-
$default = 'varnish';
204-
}
205205
}
206206
if (isset($config['nginx'])) {
207207
$this->loadNginx($container, $loader, $config['nginx']);
208-
if (!$default) {
209-
$default = 'nginx';
210-
}
211208
}
212209

213-
$container->setAlias($this->getAlias() . '.default_proxy_client', $this->getAlias() . '.proxy_client.' . $default);
210+
$container->setAlias(
211+
$this->getAlias() . '.default_proxy_client',
212+
$this->getAlias() . '.proxy_client.' . $this->getDefault($config)
213+
);
214214
}
215215

216216
private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config)
217217
{
218218
$loader->load('varnish.xml');
219219
foreach ($config['servers'] as $url) {
220-
$this->validateUrl($url, 'Not a valid varnish server address: "%s"');
220+
$this->validateUrl($url, 'Not a valid Varnish server address: "%s"');
221221
}
222222
if (!empty($config['base_url'])) {
223-
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
223+
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
224+
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
225+
} else {
226+
$baseUrl = null;
224227
}
225228
$container->setParameter($this->getAlias() . '.proxy_client.varnish.servers', $config['servers']);
226-
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $config['base_url']);
229+
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $baseUrl);
227230
if ($config['guzzle_client']) {
228231
$container->getDefinition($this->getAlias() . '.proxy_client.varnish')
229232
->addArgument(
@@ -237,16 +240,85 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
237240
{
238241
$loader->load('nginx.xml');
239242
foreach ($config['servers'] as $url) {
240-
$this->validateUrl($url, 'Not a valid nginx server address: "%s"');
243+
$this->validateUrl($url, 'Not a valid Nginx server address: "%s"');
241244
}
242245
if (!empty($config['base_url'])) {
243-
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
246+
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
247+
} else {
248+
$baseUrl = null;
244249
}
245250
$container->setParameter($this->getAlias() . '.proxy_client.nginx.servers', $config['servers']);
246-
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $config['base_url']);
251+
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $baseUrl);
247252
$container->setParameter($this->getAlias() . '.proxy_client.nginx.purge_location', $config['purge_location']);
248253
}
249254

255+
private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
256+
{
257+
$container->setParameter($this->getAlias() . '.test.cache_header', $config['cache_header']);
258+
259+
if ($config['proxy_server']) {
260+
$this->loadProxyServer($container, $loader, $config['proxy_server']);
261+
}
262+
263+
if (isset($config['client']['varnish']['enabled'])
264+
|| isset($config['client']['nginx']['enabled'])) {
265+
$loader->load('test_client.xml');
266+
267+
if ($config['client']['varnish']['enabled']) {
268+
$container->getDefinition($this->getAlias() . '.test.client.varnish')
269+
->setAbstract(false);
270+
}
271+
272+
if ($config['client']['nginx']['enabled']) {
273+
$container->getDefinition($this->getAlias() . '.test.client.nginx')
274+
->setAbstract(false);
275+
}
276+
277+
$container->setAlias(
278+
$this->getAlias() . '.test.default_client',
279+
$this->getAlias() . '.test.client.' . $this->getDefault($config['client'])
280+
);
281+
}
282+
}
283+
284+
private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config)
285+
{
286+
if (isset($config['varnish'])) {
287+
$this->loadVarnishProxyServer($container, $loader, $config['varnish']);
288+
}
289+
290+
if (isset($config['nginx'])) {
291+
$this->loadNginxProxyServer($container, $loader, $config['varnish']);
292+
}
293+
294+
$container->setAlias(
295+
$this->getAlias() . '.test.default_proxy_server',
296+
$this->getAlias() . '.test.proxy_server.' . $this->getDefault($config)
297+
);
298+
}
299+
300+
private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
301+
{
302+
$loader->load('varnish_proxy.xml');
303+
foreach ($config as $key => $value) {
304+
$container->setParameter(
305+
$this->getAlias() . '.test.proxy_server.varnish.' . $key,
306+
$value
307+
);
308+
}
309+
}
310+
311+
private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
312+
{
313+
$loader->load('nginx_proxy.xml');
314+
foreach ($config as $key => $value) {
315+
$container->setParameter(
316+
$this->getAlias() . '.test.proxy_server.nginx.' . $key,
317+
$value
318+
);
319+
}
320+
}
321+
250322
private function loadTagRules(ContainerBuilder $container, array $config)
251323
{
252324
$tagDefinition = $container->getDefinition($this->getAlias() . '.event_listener.tag');
@@ -274,13 +346,36 @@ private function loadInvalidatorRules(ContainerBuilder $container, array $config
274346
}
275347

276348
private function validateUrl($url, $msg)
349+
{
350+
$prefixed = $this->prefixSchema($url);
351+
352+
if (!$parts = parse_url($prefixed)) {
353+
throw new InvalidConfigurationException(sprintf($msg, $url));
354+
}
355+
}
356+
357+
358+
private function prefixSchema($url)
277359
{
278360
if (false === strpos($url, '://')) {
279361
$url = sprintf('%s://%s', 'http', $url);
280362
}
281363

282-
if (!$parts = parse_url($url)) {
283-
throw new InvalidConfigurationException(sprintf($msg, $url));
364+
return $url;
365+
}
366+
367+
private function getDefault(array $config)
368+
{
369+
if (isset($config['default'])) {
370+
return $config['default'];
371+
}
372+
373+
if (isset($config['varnish'])) {
374+
return 'varnish';
375+
}
376+
377+
if (isset($config['nginx'])) {
378+
return 'nginx';
284379
}
285380
}
286381
}

Resources/config/nginx.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<argument>%fos_http_cache.proxy_client.nginx.base_url%</argument>
1616
<argument>%fos_http_cache.proxy_client.nginx.purge_location%</argument>
1717
</service>
18+
19+
<service id="fos_http_cache.test.client.nginx"
20+
parent="fos_http_cache.test.client.abstract"
21+
abstract="true">
22+
<argument index="0">%fos_http_cache.proxy_client.nginx.base_url%</argument>
23+
</service>
1824
</services>
1925

2026
</container>

Resources/config/nginx_proxy.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="fos_http_cache.test.proxy_server.nginx.class">FOS\HttpCache\Test\Proxy\NginxProxy</parameter>
9+
</parameters>
10+
11+
<services>
12+
<service id="fos_http_cache.test.proxy_server.nginx"
13+
class="%fos_http_cache.test.proxy_server.nginx.class%">
14+
<argument on-invalid="ignore">%fos_http_cache.test.proxy_server.nginx.config_file%</argument>
15+
<call method="setBinary">
16+
<argument on-invalid="ignore">%fos_http_cache.test.proxy_server.nginx.binary%</argument>
17+
</call>
18+
<call method="setPort">
19+
<argument>%fos_http_cache.test.proxy_server.nginx.port%</argument>
20+
</call>
21+
<call method="setIp">
22+
<argument>%fos_http_cache.test.proxy_server.nginx.ip%</argument>
23+
</call>
24+
</service>
25+
</services>
26+
27+
</container>

Resources/config/test_client.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="fos_http_cache.test.client.class">Guzzle\Http\Client</parameter>
9+
<parameter key="fos_http_cache.test.client.curlopt_forbid_reuse" type="constant">CURLOPT_FORBID_REUSE</parameter>
10+
</parameters>
11+
12+
<services>
13+
<service id="fos_http_cache.test.client.abstract"
14+
class="%fos_http_cache.test.client.class%"
15+
abstract="true">
16+
<argument /><!-- base_url -->
17+
<argument type="collection">
18+
<argument key="%fos_http_cache.test.client.curlopt_forbid_reuse%">true</argument>
19+
</argument>
20+
</service>
21+
</services>
22+
23+
</container>

Resources/config/varnish.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
<argument>%fos_http_cache.proxy_client.varnish.servers%</argument>
1515
<argument>%fos_http_cache.proxy_client.varnish.base_url%</argument>
1616
</service>
17+
18+
<service id="fos_http_cache.test.client.varnish"
19+
parent="fos_http_cache.test.client.abstract"
20+
abstract="true">
21+
<argument index="0">%fos_http_cache.proxy_client.varnish.base_url%</argument>
22+
</service>
23+
1724
</services>
1825

1926
</container>

0 commit comments

Comments
 (0)