From 24d8740d71a2cac18a32834725e10f584283eeae Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 21 Nov 2019 13:51:43 +0100 Subject: [PATCH 01/11] Added cache_listeners to Configuration and Extension --- src/DependencyInjection/Configuration.php | 7 +++++++ src/DependencyInjection/HttplugExtension.php | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f2982d35..6836f54a 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -3,6 +3,7 @@ namespace Http\HttplugBundle\DependencyInjection; use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator; +use Http\Client\Common\Plugin\Cache\Listener\CacheListener; use Http\Client\Common\Plugin\CachePlugin; use Http\Client\Common\Plugin\Journal; use Http\Client\Plugin\Vcr\NamingStrategy\NamingStrategyInterface; @@ -739,6 +740,12 @@ private function createCachePluginNode() ->end() ->end() ->end() + ->arrayNode('cache_listeners') + ->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.') + ->defaultValue([]) + ->prototype('scalar') + ->end() + ->end() ->scalarNode('respect_cache_headers') ->info('Whether we should care about cache headers or not [DEPRECATED]') ->beforeNormalization() diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 61a734b5..21387d2d 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -203,10 +203,17 @@ private function configurePluginByName($name, Definition $definition, array $con switch ($name) { case 'cache': $options = $config['config']; + if (!empty($options['cache_key_generator'])) { $options['cache_key_generator'] = new Reference($options['cache_key_generator']); } + if (!empty($options['cache_listeners'])) { + foreach ($options['cache_listeners'] as $i => $listener) { + $options['cache_listeners'][$i] = new Reference($listener); + } + } + $definition ->replaceArgument(0, new Reference($config['cache_pool'])) ->replaceArgument(1, new Reference($config['stream_factory'])) From 6eab39f4a88b508f2337eb2fbf684363c042b261 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Wed, 27 Nov 2019 11:36:22 +0100 Subject: [PATCH 02/11] Wrapped php-http/cache-plugin Cache Listeners - fixed tests, extended by cache_listeners --- tests/Resources/Fixtures/config/full.php | 1 + tests/Resources/Fixtures/config/full.xml | 2 +- tests/Resources/Fixtures/config/full.yml | 1 + tests/Unit/DependencyInjection/ConfigurationTest.php | 4 ++++ 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 8c9def8a..60a5a6b9 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -102,6 +102,7 @@ 'methods' => ['GET'], 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], + 'cache_listeners' => null, ], ], 'cookie' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 607438de..de9d298b 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -54,7 +54,7 @@ - + X-Foo GET diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index 03c97d9a..eecb6dd6 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -73,6 +73,7 @@ httplug: cache_key_generator: null respect_response_cache_directives: - X-Foo + cache_listeners: null cookie: cookie_jar: my_cookie_jar decoder: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index a9fd4ba4..62bf5fb2 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -43,6 +43,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase 'stream_factory' => 'httplug.stream_factory', 'config' => [ 'methods' => ['GET', 'HEAD'], + 'cache_listeners' => [] ], ], 'cookie' => [ @@ -233,6 +234,7 @@ public function testSupportsAllConfigFormats(): void 'methods' => ['GET'], 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], + 'cache_listeners' => [], ], ], 'cookie' => [ @@ -354,6 +356,7 @@ public function testCacheConfigDeprecationCompatibility(): void 'config' => [ 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => true, + 'cache_listeners' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); @@ -372,6 +375,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void 'config' => [ 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => false, + 'cache_listeners' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); From 440f4949c3f803c67f4ee8fc7a2c8142081672bb Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 28 Nov 2019 08:29:04 +0100 Subject: [PATCH 03/11] Wrapped php-http/cache-plugin cache_listeners - list of classes to match plugins documentation - added to config - added tests --- src/DependencyInjection/Configuration.php | 18 +++++++- src/DependencyInjection/HttplugExtension.php | 7 +++- tests/Resources/Fixtures/config/full.php | 2 +- tests/Resources/Fixtures/config/full.xml | 2 +- tests/Resources/Fixtures/config/full.yml | 2 +- .../config/invalid_cache_listener_config.yml | 6 +++ .../DependencyInjection/ConfigurationTest.php | 11 +++++ .../HttplugExtensionTest.php | 42 +++++++++++++++++++ 8 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 tests/Resources/Fixtures/config/invalid_cache_listener_config.yml diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 6836f54a..e95f428d 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -14,6 +14,7 @@ use Http\Message\StreamFactory; use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; +use ReflectionClass; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; @@ -741,9 +742,24 @@ private function createCachePluginNode() ->end() ->end() ->arrayNode('cache_listeners') - ->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.') + ->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.') + ->beforeNormalization()->castToArray()->ifEmpty()->thenUnset()->end() ->defaultValue([]) ->prototype('scalar') + ->validate() + ->ifTrue(function ($v) { + $vs = is_array($v) ? $v : (is_null($v) ? [] : [$v]); + + return empty($vs) || array_reduce($vs, function($r, $e) { + return empty($e) || !class_exists($e) || !(new ReflectionClass($e))->implementsInterface(CacheListener::class); + }, false); + }) + ->thenInvalid('A given listener class does not implement '.CacheListener::class) + ->end() + ->end() + ->validate() + ->ifEmpty() + ->thenUnset() ->end() ->end() ->scalarNode('respect_cache_headers') diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 21387d2d..08697cd7 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -210,9 +210,14 @@ private function configurePluginByName($name, Definition $definition, array $con if (!empty($options['cache_listeners'])) { foreach ($options['cache_listeners'] as $i => $listener) { - $options['cache_listeners'][$i] = new Reference($listener); + if (!empty($listener)) { + $options['cache_listeners'][$i] = new Definition($listener); + } } } + if (! count($options['cache_listeners'])) { + unset($options['cache_listeners']); + } $definition ->replaceArgument(0, new Reference($config['cache_pool'])) diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 60a5a6b9..d087a24e 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -102,7 +102,7 @@ 'methods' => ['GET'], 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], - 'cache_listeners' => null, + 'cache_listeners' => [], ], ], 'cookie' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index de9d298b..f08f436c 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -54,7 +54,7 @@ - + X-Foo GET diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index eecb6dd6..13165464 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -73,7 +73,7 @@ httplug: cache_key_generator: null respect_response_cache_directives: - X-Foo - cache_listeners: null + cache_listeners: [] cookie: cookie_jar: my_cookie_jar decoder: diff --git a/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml b/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml new file mode 100644 index 00000000..3cfab6c2 --- /dev/null +++ b/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml @@ -0,0 +1,6 @@ +httplug: + plugins: + cache: + cache_pool: my_cache_pool + config: + cache_listeners: ['NotExistentClass'] diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 62bf5fb2..a5d6c884 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -2,6 +2,7 @@ namespace Http\HttplugBundle\Tests\Unit\DependencyInjection; +use Http\Client\Common\Plugin\Cache\Listener\CacheListener; use Http\HttplugBundle\DependencyInjection\Configuration; use Http\HttplugBundle\DependencyInjection\HttplugExtension; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase; @@ -424,4 +425,14 @@ public function testInvalidCapturedBodyLengthString(): void $this->expectExceptionMessage('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null'); $this->assertProcessedConfigurationEquals([], [$file]); } + + public function testInvalidCacheConfigCacheListeners(): void + { + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_listener_config.yml'; + + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionMessage('A given listener class does not implement '.CacheListener::class); + + $this->assertProcessedConfigurationEquals($this->emptyConfig, [$file]); + } } diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index 54f6cc75..a5869a0d 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -7,6 +7,7 @@ use Http\HttplugBundle\Collector\PluginClientFactoryListener; use Http\HttplugBundle\DependencyInjection\HttplugExtension; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; +use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\Kernel; use Http\Adapter\Guzzle6\Client; @@ -261,6 +262,47 @@ public function testCachePluginConfigCacheKeyGeneratorReference(): void $this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']); } + public function testCachePluginConfigCacheListenersDefinition(): void + { + $this->load([ + 'plugins' => [ + 'cache' => [ + 'cache_pool' => 'my_cache_pool', + 'config' => [ + 'cache_listeners' => [ + '\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener' + ], + ], + ], + ], + ]); + + $cachePlugin = $this->container->findDefinition('httplug.plugin.cache'); + + $config = $cachePlugin->getArgument(2); + $this->assertArrayHasKey('cache_listeners', $config); + $this->assertContainsOnlyInstancesOf(Definition::class, $config['cache_listeners']); + } + + public function testCachePluginInvalidConfigCacheListenersDefinition(): void + { + $this->load([ + 'plugins' => [ + 'cache' => [ + 'cache_pool' => 'my_cache_pool', + 'config' => [ + 'cache_listeners' => [], + ], + ], + ], + ]); + + $cachePlugin = $this->container->findDefinition('httplug.plugin.cache'); + + $config = $cachePlugin->getArgument(2); + $this->assertArrayNotHasKey('cache_listeners', $config); + } + public function testContentTypePluginAllowedOptions(): void { $this->load([ From 1bfe50482e154dffb0048ec4fdd41077ac333fba Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 28 Nov 2019 08:34:59 +0100 Subject: [PATCH 04/11] Wrapped php-http/cache-plugin Cache Listeners - updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b72402ff..07b4daed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee - Configured clients are now tagged with `'httplug.client'` - Adds a link to profiler page when response is from a Symfony application with profiler enabled +- Adding `cache_listeners` option of `php-http/cache-plugin` ### Changed From 9acd7b6e56a95b1469842346ad9455d21cb4e593 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 28 Nov 2019 08:46:25 +0100 Subject: [PATCH 05/11] Fixing CI --- src/DependencyInjection/Configuration.php | 4 ++-- src/DependencyInjection/HttplugExtension.php | 2 +- tests/Unit/DependencyInjection/ConfigurationTest.php | 2 +- tests/Unit/DependencyInjection/HttplugExtensionTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index e95f428d..9166eff8 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -742,7 +742,7 @@ private function createCachePluginNode() ->end() ->end() ->arrayNode('cache_listeners') - ->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.') + ->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.') ->beforeNormalization()->castToArray()->ifEmpty()->thenUnset()->end() ->defaultValue([]) ->prototype('scalar') @@ -750,7 +750,7 @@ private function createCachePluginNode() ->ifTrue(function ($v) { $vs = is_array($v) ? $v : (is_null($v) ? [] : [$v]); - return empty($vs) || array_reduce($vs, function($r, $e) { + return empty($vs) || array_reduce($vs, function ($r, $e) { return empty($e) || !class_exists($e) || !(new ReflectionClass($e))->implementsInterface(CacheListener::class); }, false); }) diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 08697cd7..d95c49c9 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -215,7 +215,7 @@ private function configurePluginByName($name, Definition $definition, array $con } } } - if (! count($options['cache_listeners'])) { + if (!count($options['cache_listeners'])) { unset($options['cache_listeners']); } diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index a5d6c884..618eba9d 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -44,7 +44,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase 'stream_factory' => 'httplug.stream_factory', 'config' => [ 'methods' => ['GET', 'HEAD'], - 'cache_listeners' => [] + 'cache_listeners' => [], ], ], 'cookie' => [ diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index a5869a0d..6e70eb43 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -270,7 +270,7 @@ public function testCachePluginConfigCacheListenersDefinition(): void 'cache_pool' => 'my_cache_pool', 'config' => [ 'cache_listeners' => [ - '\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener' + '\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener', ], ], ], From 194e8a5ad967a78fc048d69944cb2973adf93f61 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Mon, 16 Dec 2019 09:29:17 +0100 Subject: [PATCH 06/11] Pull-Request Feedback for "Wrapped php-http/cache-plugin Cache Listeners" - switched from list of classes to list of services - removed class implementation validation in configuration --- src/DependencyInjection/Configuration.php | 21 ++-------- src/DependencyInjection/HttplugExtension.php | 2 +- .../config/invalid_cache_listener_config.yml | 6 --- .../DependencyInjection/ConfigurationTest.php | 39 +++++++------------ .../HttplugExtensionTest.php | 25 ++---------- 5 files changed, 22 insertions(+), 71 deletions(-) delete mode 100644 tests/Resources/Fixtures/config/invalid_cache_listener_config.yml diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 9166eff8..686b0143 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -21,6 +21,8 @@ use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Config\Definition\Exception\InvalidTypeException; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; /** * This class contains the configuration information for the bundle. @@ -742,24 +744,9 @@ private function createCachePluginNode() ->end() ->end() ->arrayNode('cache_listeners') - ->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.') - ->beforeNormalization()->castToArray()->ifEmpty()->thenUnset()->end() - ->defaultValue([]) + ->info('A list of service ids to act on the response based on the results of the cache check. Must implement '.CacheListener::class.'. Defaults to an empty array.') + ->beforeNormalization()->ifNull()->thenEmptyArray()->castToArray()->ifEmpty()->thenUnset()->end() ->prototype('scalar') - ->validate() - ->ifTrue(function ($v) { - $vs = is_array($v) ? $v : (is_null($v) ? [] : [$v]); - - return empty($vs) || array_reduce($vs, function ($r, $e) { - return empty($e) || !class_exists($e) || !(new ReflectionClass($e))->implementsInterface(CacheListener::class); - }, false); - }) - ->thenInvalid('A given listener class does not implement '.CacheListener::class) - ->end() - ->end() - ->validate() - ->ifEmpty() - ->thenUnset() ->end() ->end() ->scalarNode('respect_cache_headers') diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index d95c49c9..e2bd8ba2 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -211,7 +211,7 @@ private function configurePluginByName($name, Definition $definition, array $con if (!empty($options['cache_listeners'])) { foreach ($options['cache_listeners'] as $i => $listener) { if (!empty($listener)) { - $options['cache_listeners'][$i] = new Definition($listener); + $options['cache_listeners'][$i] = new Reference($listener); } } } diff --git a/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml b/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml deleted file mode 100644 index 3cfab6c2..00000000 --- a/tests/Resources/Fixtures/config/invalid_cache_listener_config.yml +++ /dev/null @@ -1,6 +0,0 @@ -httplug: - plugins: - cache: - cache_pool: my_cache_pool - config: - cache_listeners: ['NotExistentClass'] diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 1f010ed2..6002b225 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -2,7 +2,6 @@ namespace Http\HttplugBundle\Tests\Unit\DependencyInjection; -use Http\Client\Common\Plugin\Cache\Listener\CacheListener; use Http\HttplugBundle\DependencyInjection\Configuration; use Http\HttplugBundle\DependencyInjection\HttplugExtension; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase; @@ -97,7 +96,7 @@ protected function getConfiguration(): ConfigurationInterface public function testEmptyConfiguration(): void { $formats = array_map(function ($path) { - return __DIR__.'/../../Resources/Fixtures/'.$path; + return __DIR__ . '/../../Resources/Fixtures/' . $path; }, [ 'config/empty.yml', 'config/empty.xml', @@ -278,7 +277,7 @@ public function testSupportsAllConfigFormats(): void ]; $formats = array_map(function ($path) { - return __DIR__.'/../../Resources/Fixtures/'.$path; + return __DIR__ . '/../../Resources/Fixtures/' . $path; }, [ 'config/full.yml', 'config/full.xml', @@ -292,7 +291,7 @@ public function testSupportsAllConfigFormats(): void public function testMissingClass(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_class.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_class.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Nonexisting\Class'); @@ -301,7 +300,7 @@ public function testMissingClass(): void public function testInvalidPlugin(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_plugin.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_plugin.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Unrecognized option "foobar" under "httplug.clients.acme.plugins.0"'); @@ -310,7 +309,7 @@ public function testInvalidPlugin(): void public function testInvalidAuthentication(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_auth.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_auth.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('password, service, username'); @@ -322,7 +321,7 @@ public function testInvalidAuthentication(): void */ public function testInvalidCacheConfig(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_config.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_cache_config.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Invalid configuration for path "httplug.plugins.cache.config": You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead.'); @@ -335,7 +334,7 @@ public function testInvalidCacheConfig(): void public function testBackwardCompatibility(): void { $formats = array_map(function ($path) { - return __DIR__.'/../../Resources/Fixtures/'.$path; + return __DIR__ . '/../../Resources/Fixtures/' . $path; }, [ 'config/bc/toolbar.yml', 'config/bc/toolbar_auto.yml', @@ -351,7 +350,7 @@ public function testBackwardCompatibility(): void */ public function testCacheConfigDeprecationCompatibility(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/bc/cache_config.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/bc/cache_config.yml'; $config = $this->emptyConfig; $config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ 'enabled' => true, @@ -370,7 +369,7 @@ public function testCacheConfigDeprecationCompatibility(): void */ public function testCacheConfigDeprecationCompatibilityIssue166(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/bc/issue-166.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/bc/issue-166.yml'; $config = $this->emptyConfig; $config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ 'enabled' => true, @@ -386,7 +385,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void public function testProfilingToolbarCollision(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/bc/profiling_toolbar.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/bc/profiling_toolbar.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); @@ -395,7 +394,7 @@ public function testProfilingToolbarCollision(): void public function testClientCacheConfigMustHavePool(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/client_cache_config_with_no_pool.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/client_cache_config_with_no_pool.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "cache_pool" at path "httplug.clients.test.plugins.0.cache" must be configured.'); @@ -404,7 +403,7 @@ public function testClientCacheConfigMustHavePool(): void public function testCacheConfigMustHavePool(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/cache_config_with_no_pool.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/cache_config_with_no_pool.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "cache_pool" at path "httplug.plugins.cache" must be configured.'); @@ -413,7 +412,7 @@ public function testCacheConfigMustHavePool(): void public function testLimitlessCapturedBodyLength(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/limitless_captured_body_length.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/limitless_captured_body_length.yml'; $config = $this->emptyConfig; $config['profiling']['captured_body_length'] = null; $this->assertProcessedConfigurationEquals($config, [$file]); @@ -421,20 +420,10 @@ public function testLimitlessCapturedBodyLength(): void public function testInvalidCapturedBodyLengthString(): void { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_captured_body_length.yml'; + $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_captured_body_length.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null'); $this->assertProcessedConfigurationEquals([], [$file]); } - - public function testInvalidCacheConfigCacheListeners(): void - { - $file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_listener_config.yml'; - - $this->expectException(InvalidConfigurationException::class); - $this->expectExceptionMessage('A given listener class does not implement '.CacheListener::class); - - $this->assertProcessedConfigurationEquals($this->emptyConfig, [$file]); - } } diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index 41013071..f7248992 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -7,7 +7,6 @@ use Http\HttplugBundle\Collector\PluginClientFactoryListener; use Http\HttplugBundle\DependencyInjection\HttplugExtension; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; -use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\Kernel; use Http\Adapter\Guzzle6\Client; @@ -270,7 +269,7 @@ public function testCachePluginConfigCacheListenersDefinition(): void 'cache_pool' => 'my_cache_pool', 'config' => [ 'cache_listeners' => [ - '\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener', + 'httplug.plugin.cache.listeners.add_header', ], ], ], @@ -281,26 +280,8 @@ public function testCachePluginConfigCacheListenersDefinition(): void $config = $cachePlugin->getArgument(2); $this->assertArrayHasKey('cache_listeners', $config); - $this->assertContainsOnlyInstancesOf(Definition::class, $config['cache_listeners']); - } - - public function testCachePluginInvalidConfigCacheListenersDefinition(): void - { - $this->load([ - 'plugins' => [ - 'cache' => [ - 'cache_pool' => 'my_cache_pool', - 'config' => [ - 'cache_listeners' => [], - ], - ], - ], - ]); - - $cachePlugin = $this->container->findDefinition('httplug.plugin.cache'); - - $config = $cachePlugin->getArgument(2); - $this->assertArrayNotHasKey('cache_listeners', $config); + $this->assertContainsOnlyInstancesOf(Reference::class, $config['cache_listeners']); + $this->assertSame('httplug.plugin.cache.listeners.add_header', (string) $config['cache_listeners'][0]); } public function testContentTypePluginAllowedOptions(): void From 65a501441772ce42a16dee6a3c22f1c1436f1bad Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Mon, 16 Dec 2019 09:35:44 +0100 Subject: [PATCH 07/11] Pull-Request Feedback for "Wrapped php-http/cache-plugin Cache Listeners" - Removed obsolete uses --- src/DependencyInjection/Configuration.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 686b0143..07c95ef3 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -14,15 +14,12 @@ use Http\Message\StreamFactory; use Psr\Cache\CacheItemPoolInterface; use Psr\Log\LoggerInterface; -use ReflectionClass; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; use Symfony\Component\Config\Definition\Exception\InvalidTypeException; -use Symfony\Component\DependencyInjection\Definition; -use Symfony\Component\DependencyInjection\Reference; /** * This class contains the configuration information for the bundle. From 1fd748b258668b1e4f619568a58580e3ee62c655 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Mon, 16 Dec 2019 09:39:44 +0100 Subject: [PATCH 08/11] Pull-Request Feedback for "Wrapped php-http/cache-plugin Cache Listeners" - Reverted unwanted style changes (even though a additional space at string concat is really nice ;)) --- .../DependencyInjection/ConfigurationTest.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 6002b225..c0829fa1 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -96,7 +96,7 @@ protected function getConfiguration(): ConfigurationInterface public function testEmptyConfiguration(): void { $formats = array_map(function ($path) { - return __DIR__ . '/../../Resources/Fixtures/' . $path; + return __DIR__.'/../../Resources/Fixtures/'.$path; }, [ 'config/empty.yml', 'config/empty.xml', @@ -277,7 +277,7 @@ public function testSupportsAllConfigFormats(): void ]; $formats = array_map(function ($path) { - return __DIR__ . '/../../Resources/Fixtures/' . $path; + return __DIR__.'/../../Resources/Fixtures/'.$path; }, [ 'config/full.yml', 'config/full.xml', @@ -291,7 +291,7 @@ public function testSupportsAllConfigFormats(): void public function testMissingClass(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_class.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_class.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Nonexisting\Class'); @@ -300,7 +300,7 @@ public function testMissingClass(): void public function testInvalidPlugin(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_plugin.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_plugin.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Unrecognized option "foobar" under "httplug.clients.acme.plugins.0"'); @@ -309,7 +309,7 @@ public function testInvalidPlugin(): void public function testInvalidAuthentication(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_auth.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_auth.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('password, service, username'); @@ -321,7 +321,7 @@ public function testInvalidAuthentication(): void */ public function testInvalidCacheConfig(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_cache_config.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_config.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Invalid configuration for path "httplug.plugins.cache.config": You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead.'); @@ -334,7 +334,7 @@ public function testInvalidCacheConfig(): void public function testBackwardCompatibility(): void { $formats = array_map(function ($path) { - return __DIR__ . '/../../Resources/Fixtures/' . $path; + return __DIR__.'/../../Resources/Fixtures/'.$path; }, [ 'config/bc/toolbar.yml', 'config/bc/toolbar_auto.yml', @@ -350,7 +350,7 @@ public function testBackwardCompatibility(): void */ public function testCacheConfigDeprecationCompatibility(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/bc/cache_config.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/bc/cache_config.yml'; $config = $this->emptyConfig; $config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ 'enabled' => true, @@ -369,7 +369,7 @@ public function testCacheConfigDeprecationCompatibility(): void */ public function testCacheConfigDeprecationCompatibilityIssue166(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/bc/issue-166.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/bc/issue-166.yml'; $config = $this->emptyConfig; $config['plugins']['cache'] = array_merge($config['plugins']['cache'], [ 'enabled' => true, @@ -385,7 +385,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void public function testProfilingToolbarCollision(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/bc/profiling_toolbar.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/bc/profiling_toolbar.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); @@ -394,7 +394,7 @@ public function testProfilingToolbarCollision(): void public function testClientCacheConfigMustHavePool(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/client_cache_config_with_no_pool.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/client_cache_config_with_no_pool.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "cache_pool" at path "httplug.clients.test.plugins.0.cache" must be configured.'); @@ -403,7 +403,7 @@ public function testClientCacheConfigMustHavePool(): void public function testCacheConfigMustHavePool(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/cache_config_with_no_pool.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/cache_config_with_no_pool.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "cache_pool" at path "httplug.plugins.cache" must be configured.'); @@ -412,7 +412,7 @@ public function testCacheConfigMustHavePool(): void public function testLimitlessCapturedBodyLength(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/limitless_captured_body_length.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/limitless_captured_body_length.yml'; $config = $this->emptyConfig; $config['profiling']['captured_body_length'] = null; $this->assertProcessedConfigurationEquals($config, [$file]); @@ -420,7 +420,7 @@ public function testLimitlessCapturedBodyLength(): void public function testInvalidCapturedBodyLengthString(): void { - $file = __DIR__ . '/../../Resources/Fixtures/config/invalid_captured_body_length.yml'; + $file = __DIR__.'/../../Resources/Fixtures/config/invalid_captured_body_length.yml'; $this->expectException(InvalidConfigurationException::class); $this->expectExceptionMessage('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null'); From bd0beb2c6903fa0f3fbda448bc15b4e78b7c5b69 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 9 Jan 2020 12:20:47 +0100 Subject: [PATCH 09/11] Pull-Request-Feedback - removed duplicated cache_listeners node from src/DependencyInjection/Configuration.php - TESTS: added multiple cache_listeners --- src/DependencyInjection/Configuration.php | 4 ---- tests/Resources/Fixtures/config/full.php | 5 ++++- tests/Resources/Fixtures/config/full.xml | 3 ++- tests/Resources/Fixtures/config/full.yml | 4 +++- tests/Unit/DependencyInjection/ConfigurationTest.php | 5 ++++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 800cc9e5..b7bc988c 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -751,10 +751,6 @@ private function createCachePluginNode() ->end() ->end() ->end() - ->arrayNode('cache_listeners') - ->info('A list of service ids to act on the response based on the results of the cache check. Must implement '.CacheListener::class.'. Defaults to an empty array.') - ->beforeNormalization()->ifNull()->thenEmptyArray()->castToArray()->ifEmpty()->thenUnset()->end() - ->end() ->enumNode('hash_algo') ->info('Hashing algorithm to use') ->values(hash_algos()) diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 3647d46c..14573391 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -110,7 +110,10 @@ 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], 'blacklisted_paths' => ['@/path/not-to-be/cached@'], - 'cache_listeners' => [], + 'cache_listeners' => [ + 'my_cache_listener_0', + 'my_cache_listener_1', + ], ], ], 'cookie' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index bdf28942..05c013dc 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -62,7 +62,8 @@ - + my_cache_listener_0 + my_cache_listener_1 X-Foo GET @/path/not-to-be/cached@ diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index 7a91bcf8..1190f9c9 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -82,7 +82,9 @@ httplug: - X-Foo blacklisted_paths: - '@/path/not-to-be/cached@' - cache_listeners: [] + cache_listeners: + - my_cache_listener_0 + - my_cache_listener_1 cookie: cookie_jar: my_cookie_jar decoder: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index bf88a47a..f8cb16b6 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -267,7 +267,10 @@ public function testSupportsAllConfigFormats(): void 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], 'blacklisted_paths' => ['@/path/not-to-be/cached@'], - 'cache_listeners' => [], + 'cache_listeners' => [ + 'my_cache_listener_0', + 'my_cache_listener_1' + ], ], ], 'cookie' => [ From 99029665205824b77a1f9e7dab7be5d50cbc5390 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 9 Jan 2020 14:04:57 +0100 Subject: [PATCH 10/11] Pretty CF --- tests/Unit/DependencyInjection/ConfigurationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index f8cb16b6..8d07b144 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -269,7 +269,7 @@ public function testSupportsAllConfigFormats(): void 'blacklisted_paths' => ['@/path/not-to-be/cached@'], 'cache_listeners' => [ 'my_cache_listener_0', - 'my_cache_listener_1' + 'my_cache_listener_1', ], ], ], From 1b99d943a112f6d7c315e70e45f74db5cb5e2c8a Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Mon, 20 Jan 2020 13:46:13 +0100 Subject: [PATCH 11/11] pull request feedback - fixed cache listener configuration - proper usage of xml configuration --- src/DependencyInjection/Configuration.php | 3 ++- tests/Resources/Fixtures/config/full.xml | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index b7bc988c..d3f44f13 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -718,6 +718,7 @@ private function createCachePluginNode() $config ->fixXmlConfig('method') ->fixXmlConfig('respect_response_cache_directive') + ->fixXmlConfig('cache_listener') ->addDefaultsIfNotSet() ->validate() ->ifTrue(function ($config) { @@ -771,7 +772,7 @@ private function createCachePluginNode() ->end() ->arrayNode('cache_listeners') ->info('A list of service ids to act on the response based on the results of the cache check. Must implement '.CacheListener::class.'. Defaults to an empty array.') - ->beforeNormalization()->ifNull()->thenEmptyArray()->castToArray()->ifEmpty()->thenUnset()->end() + ->beforeNormalization()->castToArray()->end() ->prototype('scalar') ->end() ->end() diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 05c013dc..48449964 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -62,8 +62,8 @@ - my_cache_listener_0 - my_cache_listener_1 + my_cache_listener_0 + my_cache_listener_1 X-Foo GET @/path/not-to-be/cached@