Skip to content

Commit cb142b3

Browse files
committed
Add ProxyTestCase
Add documentation Fix config and add docs Fix undefined index Fix config key name Fix test Fix fetching container
1 parent cbddbb8 commit cb142b3

File tree

17 files changed

+765
-9
lines changed

17 files changed

+765
-9
lines changed

DependencyInjection/Configuration.php

+102
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

+90-8
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,21 +200,17 @@ 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)
@@ -247,6 +247,73 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
247247
$container->setParameter($this->getAlias() . '.proxy_client.nginx.purge_location', $config['purge_location']);
248248
}
249249

250+
private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
251+
{
252+
$container->setParameter($this->getAlias() . '.test.cache_header', $config['cache_header']);
253+
254+
if ($config['proxy_server']) {
255+
$this->loadProxyServer($container, $loader, $config['proxy_server']);
256+
}
257+
258+
if (isset($config['client']['varnish']['enabled'])
259+
|| isset($config['client']['nginx']['enabled'])) {
260+
$loader->load('test_client.xml');
261+
262+
if ($config['client']['varnish']['enabled']) {
263+
$container->getDefinition($this->getAlias() . '.test.client.varnish')
264+
->setAbstract(false);
265+
}
266+
267+
if ($config['client']['nginx']['enabled']) {
268+
$container->getDefinition($this->getAlias() . '.test.client.nginx')
269+
->setAbstract(false);
270+
}
271+
272+
$container->setAlias(
273+
$this->getAlias() . '.test.default_client',
274+
$this->getAlias() . '.test.client.' . $this->getDefault($config['client'])
275+
);
276+
}
277+
}
278+
279+
private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config)
280+
{
281+
if (isset($config['varnish'])) {
282+
$this->loadVarnishProxyServer($container, $loader, $config['varnish']);
283+
}
284+
285+
if (isset($config['nginx'])) {
286+
$this->loadNginxProxyServer($container, $loader, $config['varnish']);
287+
}
288+
289+
$container->setAlias(
290+
$this->getAlias() . '.test.default_proxy_server',
291+
$this->getAlias() . '.test.proxy_server.' . $this->getDefault($config)
292+
);
293+
}
294+
295+
private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
296+
{
297+
$loader->load('varnish_proxy.xml');
298+
foreach ($config as $key => $value) {
299+
$container->setParameter(
300+
$this->getAlias() . '.test.proxy_server.varnish.' . $key,
301+
$value
302+
);
303+
}
304+
}
305+
306+
private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
307+
{
308+
$loader->load('nginx_proxy.xml');
309+
foreach ($config as $key => $value) {
310+
$container->setParameter(
311+
$this->getAlias() . '.test.proxy_server.nginx.' . $key,
312+
$value
313+
);
314+
}
315+
}
316+
250317
private function loadTagRules(ContainerBuilder $container, array $config)
251318
{
252319
$tagDefinition = $container->getDefinition($this->getAlias() . '.event_listener.tag');
@@ -283,4 +350,19 @@ private function validateUrl($url, $msg)
283350
throw new InvalidConfigurationException(sprintf($msg, $url));
284351
}
285352
}
353+
354+
private function getDefault(array $config)
355+
{
356+
if (isset($config['default'])) {
357+
return $config['default'];
358+
}
359+
360+
if (isset($config['varnish'])) {
361+
return 'varnish';
362+
}
363+
364+
if (isset($config['nginx'])) {
365+
return 'nginx';
366+
}
367+
}
286368
}

Resources/config/nginx.xml

+6
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

+27
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

+23
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

+7
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>

Resources/config/varnish_proxy.xml

+27
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.varnish.class">FOS\HttpCache\Test\Proxy\VarnishProxy</parameter>
9+
</parameters>
10+
11+
<services>
12+
<service id="fos_http_cache.test.proxy_server.varnish"
13+
class="%fos_http_cache.test.proxy_server.varnish.class%">
14+
<argument>%fos_http_cache.test.proxy_server.varnish.config_file%</argument>
15+
<call method="setBinary">
16+
<argument>%fos_http_cache.test.proxy_server.varnish.binary%</argument>
17+
</call>
18+
<call method="setPort">
19+
<argument>%fos_http_cache.test.proxy_server.varnish.port%</argument>
20+
</call>
21+
<call method="setIp">
22+
<argument>%fos_http_cache.test.proxy_server.varnish.ip%</argument>
23+
</call>
24+
</service>
25+
</services>
26+
27+
</container>

Resources/doc/features.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ corresponding reference section.
1111
features/tagging
1212
features/user-context
1313
features/helpers
14+
features/testing

0 commit comments

Comments
 (0)