Skip to content

Commit

Permalink
Merge pull request #122 from FriendsOfSymfony/testcase
Browse files Browse the repository at this point in the history
Add ProxyTestCase
  • Loading branch information
ddeboer committed Aug 10, 2014
2 parents cbddbb8 + a3f47ad commit 9c6c5d4
Show file tree
Hide file tree
Showing 18 changed files with 795 additions and 20 deletions.
102 changes: 102 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ public function getConfigTreeBuilder()
->ifTrue(function ($v) {return $v['invalidation']['rules'] && !$v['invalidation']['enabled'];})
->thenInvalid('You need to enable the cache_manager and invalidation to use rules.')
->end()
->validate()
->ifTrue(function ($v) {
return isset($v['test'])
&& $v['test']['client']['varnish']['enabled']
&& !isset($v['proxy_client']['varnish']);
})
->then(function ($v) {
if ('auto' === $v['test']['client']['varnish']['enabled']) {
$v['test']['client']['varnish']['enabled'] = false;

return $v;
}
throw new InvalidConfigurationException('You need to configure the Varnish proxy_client to use the Varnish test client');
})
->end()
->validate()
->ifTrue(function ($v) {
if (isset($v['test'])) {
return $v['test']['client']['nginx']['enabled'] && !isset($v['proxy_client']['nginx']);
}
})
->then(function ($v) {
if ('auto' === $v['test']['client']['nginx']['enabled']) {
$v['test']['client']['nginx']['enabled'] = false;

return $v;
}
throw new InvalidConfigurationException('You need to configure the Nginx proxy_client to use the Nginx test client');
})
->end()
;

$this->addCacheControlSection($rootNode);
Expand All @@ -100,6 +130,7 @@ public function getConfigTreeBuilder()
$this->addInvalidationSection($rootNode);
$this->addUserContextListenerSection($rootNode);
$this->addFlashMessageSection($rootNode);
$this->addTestSection($rootNode);
$this->addDebugSection($rootNode);

return $treeBuilder;
Expand Down Expand Up @@ -287,6 +318,77 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addTestSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('test')
->children()
->scalarNode('cache_header')
->defaultValue('X-Cache')
->info('HTTP cache hit/miss header')
->end()
->arrayNode('proxy_server')
->info('Configure how caching proxy will be run in your tests')
->children()
->enumNode('default')
->values(array('varnish', 'nginx'))
->info('If you configure more than one proxy server, specify which client is the default.')
->end()
->arrayNode('varnish')
->children()
->scalarNode('config_file')->isRequired()->end()
->scalarNode('binary')->defaultValue('varnishd')->end()
->integerNode('port')->defaultValue(6181)->end()
->scalarNode('ip')->defaultValue('127.0.0.1')->end()
->end()
->end()
->arrayNode('nginx')
->children()
->scalarNode('config_file')->isRequired()->end()
->scalarNode('binary')->defaultValue('nginx')->end()
->integerNode('port')->defaultValue(8080)->end()
->scalarNode('ip')->defaultValue('127.0.0.1')->end()
->end()
->end()
->end()
->end()
->arrayNode('client')
->addDefaultsIfNotSet()
->children()
->enumNode('default')
->values(array('varnish', 'nginx'))
->info('If you configure more than one proxy client, specify which client is the default.')
->end()
->arrayNode('varnish')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->enumNode('enabled')
->values(array(true, false, 'auto'))
->defaultValue('auto')
->info('Whether to enable the Varnish test client.')
->end()
->end()
->end()
->arrayNode('nginx')
->addDefaultsIfNotSet()
->canBeEnabled()
->children()
->enumNode('enabled')
->values(array(true, false, 'auto'))
->defaultValue('auto')
->info('Whether to enable the Nginx test client.')
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end();
}

/**
* Cache manager main section
*
Expand Down
127 changes: 111 additions & 16 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadProxyClient($container, $loader, $config['proxy_client']);
}

if (isset($config['test'])) {
$this->loadTest($container, $loader, $config['test']);
}

if ($config['cache_manager']['enabled']) {
$loader->load('cache_manager.xml');
}
Expand Down Expand Up @@ -196,34 +200,33 @@ private function createRequestMatcher(ContainerBuilder $container, $path = null,

private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config)
{
$default = empty($config['default']) ? false : $config['default'];
if (isset($config['varnish'])) {
$this->loadVarnish($container, $loader, $config['varnish']);
if (!$default) {
$default = 'varnish';
}
}
if (isset($config['nginx'])) {
$this->loadNginx($container, $loader, $config['nginx']);
if (!$default) {
$default = 'nginx';
}
}

$container->setAlias($this->getAlias() . '.default_proxy_client', $this->getAlias() . '.proxy_client.' . $default);
$container->setAlias(
$this->getAlias() . '.default_proxy_client',
$this->getAlias() . '.proxy_client.' . $this->getDefault($config)
);
}

private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config)
{
$loader->load('varnish.xml');
foreach ($config['servers'] as $url) {
$this->validateUrl($url, 'Not a valid varnish server address: "%s"');
$this->validateUrl($url, 'Not a valid Varnish server address: "%s"');
}
if (!empty($config['base_url'])) {
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
} else {
$baseUrl = null;
}
$container->setParameter($this->getAlias() . '.proxy_client.varnish.servers', $config['servers']);
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $config['base_url']);
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $baseUrl);
if ($config['guzzle_client']) {
$container->getDefinition($this->getAlias() . '.proxy_client.varnish')
->addArgument(
Expand All @@ -237,16 +240,85 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
{
$loader->load('nginx.xml');
foreach ($config['servers'] as $url) {
$this->validateUrl($url, 'Not a valid nginx server address: "%s"');
$this->validateUrl($url, 'Not a valid Nginx server address: "%s"');
}
if (!empty($config['base_url'])) {
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
} else {
$baseUrl = null;
}
$container->setParameter($this->getAlias() . '.proxy_client.nginx.servers', $config['servers']);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $config['base_url']);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $baseUrl);
$container->setParameter($this->getAlias() . '.proxy_client.nginx.purge_location', $config['purge_location']);
}

private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
{
$container->setParameter($this->getAlias() . '.test.cache_header', $config['cache_header']);

if ($config['proxy_server']) {
$this->loadProxyServer($container, $loader, $config['proxy_server']);
}

if (isset($config['client']['varnish']['enabled'])
|| isset($config['client']['nginx']['enabled'])) {
$loader->load('test_client.xml');

if ($config['client']['varnish']['enabled']) {
$container->getDefinition($this->getAlias() . '.test.client.varnish')
->setAbstract(false);
}

if ($config['client']['nginx']['enabled']) {
$container->getDefinition($this->getAlias() . '.test.client.nginx')
->setAbstract(false);
}

$container->setAlias(
$this->getAlias() . '.test.default_client',
$this->getAlias() . '.test.client.' . $this->getDefault($config['client'])
);
}
}

private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config)
{
if (isset($config['varnish'])) {
$this->loadVarnishProxyServer($container, $loader, $config['varnish']);
}

if (isset($config['nginx'])) {
$this->loadNginxProxyServer($container, $loader, $config['varnish']);
}

$container->setAlias(
$this->getAlias() . '.test.default_proxy_server',
$this->getAlias() . '.test.proxy_server.' . $this->getDefault($config)
);
}

private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
{
$loader->load('varnish_proxy.xml');
foreach ($config as $key => $value) {
$container->setParameter(
$this->getAlias() . '.test.proxy_server.varnish.' . $key,
$value
);
}
}

private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
{
$loader->load('nginx_proxy.xml');
foreach ($config as $key => $value) {
$container->setParameter(
$this->getAlias() . '.test.proxy_server.nginx.' . $key,
$value
);
}
}

private function loadTagRules(ContainerBuilder $container, array $config)
{
$tagDefinition = $container->getDefinition($this->getAlias() . '.event_listener.tag');
Expand Down Expand Up @@ -274,13 +346,36 @@ private function loadInvalidatorRules(ContainerBuilder $container, array $config
}

private function validateUrl($url, $msg)
{
$prefixed = $this->prefixSchema($url);

if (!$parts = parse_url($prefixed)) {
throw new InvalidConfigurationException(sprintf($msg, $url));
}
}


private function prefixSchema($url)
{
if (false === strpos($url, '://')) {
$url = sprintf('%s://%s', 'http', $url);
}

if (!$parts = parse_url($url)) {
throw new InvalidConfigurationException(sprintf($msg, $url));
return $url;
}

private function getDefault(array $config)
{
if (isset($config['default'])) {
return $config['default'];
}

if (isset($config['varnish'])) {
return 'varnish';
}

if (isset($config['nginx'])) {
return 'nginx';
}
}
}
6 changes: 6 additions & 0 deletions Resources/config/nginx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<argument>%fos_http_cache.proxy_client.nginx.base_url%</argument>
<argument>%fos_http_cache.proxy_client.nginx.purge_location%</argument>
</service>

<service id="fos_http_cache.test.client.nginx"
parent="fos_http_cache.test.client.abstract"
abstract="true">
<argument index="0">%fos_http_cache.proxy_client.nginx.base_url%</argument>
</service>
</services>

</container>
27 changes: 27 additions & 0 deletions Resources/config/nginx_proxy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_http_cache.test.proxy_server.nginx.class">FOS\HttpCache\Test\Proxy\NginxProxy</parameter>
</parameters>

<services>
<service id="fos_http_cache.test.proxy_server.nginx"
class="%fos_http_cache.test.proxy_server.nginx.class%">
<argument on-invalid="ignore">%fos_http_cache.test.proxy_server.nginx.config_file%</argument>
<call method="setBinary">
<argument on-invalid="ignore">%fos_http_cache.test.proxy_server.nginx.binary%</argument>
</call>
<call method="setPort">
<argument>%fos_http_cache.test.proxy_server.nginx.port%</argument>
</call>
<call method="setIp">
<argument>%fos_http_cache.test.proxy_server.nginx.ip%</argument>
</call>
</service>
</services>

</container>
23 changes: 23 additions & 0 deletions Resources/config/test_client.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_http_cache.test.client.class">Guzzle\Http\Client</parameter>
<parameter key="fos_http_cache.test.client.curlopt_forbid_reuse" type="constant">CURLOPT_FORBID_REUSE</parameter>
</parameters>

<services>
<service id="fos_http_cache.test.client.abstract"
class="%fos_http_cache.test.client.class%"
abstract="true">
<argument /><!-- base_url -->
<argument type="collection">
<argument key="%fos_http_cache.test.client.curlopt_forbid_reuse%">true</argument>
</argument>
</service>
</services>

</container>
7 changes: 7 additions & 0 deletions Resources/config/varnish.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<argument>%fos_http_cache.proxy_client.varnish.servers%</argument>
<argument>%fos_http_cache.proxy_client.varnish.base_url%</argument>
</service>

<service id="fos_http_cache.test.client.varnish"
parent="fos_http_cache.test.client.abstract"
abstract="true">
<argument index="0">%fos_http_cache.proxy_client.varnish.base_url%</argument>
</service>

</services>

</container>
Loading

0 comments on commit 9c6c5d4

Please sign in to comment.