Skip to content

Commit 440f494

Browse files
author
Till Hildebrandt
committed
Wrapped php-http/cache-plugin cache_listeners
- list of classes to match plugins documentation - added to config - added tests
1 parent 6eab39f commit 440f494

File tree

8 files changed

+85
-5
lines changed

8 files changed

+85
-5
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Http\Message\StreamFactory;
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Psr\Log\LoggerInterface;
17+
use ReflectionClass;
1718
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1819
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1920
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -741,9 +742,24 @@ private function createCachePluginNode()
741742
->end()
742743
->end()
743744
->arrayNode('cache_listeners')
744-
->info('An array of services to act on the response based on the results of the cache check. Must implement ' . CacheListener::class . ' Defaults to an empty array.')
745+
->info('An array of classes to act on the response based on the results of the cache check. Must implement ' . CacheListener::class . '. Defaults to an empty array.')
746+
->beforeNormalization()->castToArray()->ifEmpty()->thenUnset()->end()
745747
->defaultValue([])
746748
->prototype('scalar')
749+
->validate()
750+
->ifTrue(function ($v) {
751+
$vs = is_array($v) ? $v : (is_null($v) ? [] : [$v]);
752+
753+
return empty($vs) || array_reduce($vs, function($r, $e) {
754+
return empty($e) || !class_exists($e) || !(new ReflectionClass($e))->implementsInterface(CacheListener::class);
755+
}, false);
756+
})
757+
->thenInvalid('A given listener class does not implement '.CacheListener::class)
758+
->end()
759+
->end()
760+
->validate()
761+
->ifEmpty()
762+
->thenUnset()
747763
->end()
748764
->end()
749765
->scalarNode('respect_cache_headers')

src/DependencyInjection/HttplugExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ private function configurePluginByName($name, Definition $definition, array $con
210210

211211
if (!empty($options['cache_listeners'])) {
212212
foreach ($options['cache_listeners'] as $i => $listener) {
213-
$options['cache_listeners'][$i] = new Reference($listener);
213+
if (!empty($listener)) {
214+
$options['cache_listeners'][$i] = new Definition($listener);
215+
}
214216
}
215217
}
218+
if (! count($options['cache_listeners'])) {
219+
unset($options['cache_listeners']);
220+
}
216221

217222
$definition
218223
->replaceArgument(0, new Reference($config['cache_pool']))

tests/Resources/Fixtures/config/full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
'methods' => ['GET'],
103103
'cache_key_generator' => null,
104104
'respect_response_cache_directives' => ['X-Foo'],
105-
'cache_listeners' => null,
105+
'cache_listeners' => [],
106106
],
107107
],
108108
'cookie' => [

tests/Resources/Fixtures/config/full.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<my_service type="service" service="my_auth_service"/>
5555
</authentication>
5656
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
57-
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null" cache-listeners="null">
57+
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null" cache-listeners="">
5858
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
5959
<method>GET</method>
6060
</config>

tests/Resources/Fixtures/config/full.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ httplug:
7373
cache_key_generator: null
7474
respect_response_cache_directives:
7575
- X-Foo
76-
cache_listeners: null
76+
cache_listeners: []
7777
cookie:
7878
cookie_jar: my_cookie_jar
7979
decoder:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
httplug:
2+
plugins:
3+
cache:
4+
cache_pool: my_cache_pool
5+
config:
6+
cache_listeners: ['NotExistentClass']

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;
44

5+
use Http\Client\Common\Plugin\Cache\Listener\CacheListener;
56
use Http\HttplugBundle\DependencyInjection\Configuration;
67
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
78
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase;
@@ -424,4 +425,14 @@ public function testInvalidCapturedBodyLengthString(): void
424425
$this->expectExceptionMessage('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null');
425426
$this->assertProcessedConfigurationEquals([], [$file]);
426427
}
428+
429+
public function testInvalidCacheConfigCacheListeners(): void
430+
{
431+
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_listener_config.yml';
432+
433+
$this->expectException(InvalidConfigurationException::class);
434+
$this->expectExceptionMessage('A given listener class does not implement '.CacheListener::class);
435+
436+
$this->assertProcessedConfigurationEquals($this->emptyConfig, [$file]);
437+
}
427438
}

tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
88
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
99
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
10+
use Symfony\Component\DependencyInjection\Definition;
1011
use Symfony\Component\DependencyInjection\Reference;
1112
use Symfony\Component\HttpKernel\Kernel;
1213
use Http\Adapter\Guzzle6\Client;
@@ -261,6 +262,47 @@ public function testCachePluginConfigCacheKeyGeneratorReference(): void
261262
$this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
262263
}
263264

265+
public function testCachePluginConfigCacheListenersDefinition(): void
266+
{
267+
$this->load([
268+
'plugins' => [
269+
'cache' => [
270+
'cache_pool' => 'my_cache_pool',
271+
'config' => [
272+
'cache_listeners' => [
273+
'\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener'
274+
],
275+
],
276+
],
277+
],
278+
]);
279+
280+
$cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
281+
282+
$config = $cachePlugin->getArgument(2);
283+
$this->assertArrayHasKey('cache_listeners', $config);
284+
$this->assertContainsOnlyInstancesOf(Definition::class, $config['cache_listeners']);
285+
}
286+
287+
public function testCachePluginInvalidConfigCacheListenersDefinition(): void
288+
{
289+
$this->load([
290+
'plugins' => [
291+
'cache' => [
292+
'cache_pool' => 'my_cache_pool',
293+
'config' => [
294+
'cache_listeners' => [],
295+
],
296+
],
297+
],
298+
]);
299+
300+
$cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
301+
302+
$config = $cachePlugin->getArgument(2);
303+
$this->assertArrayNotHasKey('cache_listeners', $config);
304+
}
305+
264306
public function testContentTypePluginAllowedOptions(): void
265307
{
266308
$this->load([

0 commit comments

Comments
 (0)