From 242a64918d4101799c2b1b676349f76731c53088 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Nov 2019 15:23:55 +0000 Subject: [PATCH 1/3] Update composer.json --- composer.json | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index b3c59fd6..bd72dba0 100644 --- a/composer.json +++ b/composer.json @@ -35,11 +35,11 @@ "php-http/message-factory": "^1.0.2", "php-http/stopwatch-plugin": "^1.2", "psr/http-message": "^1.0", - "symfony/config": "^3.4.20 || ^4.2.1", - "symfony/dependency-injection": "^3.4.20 || ^4.2.1", - "symfony/event-dispatcher": "^3.4.20 || ^4.2.1", - "symfony/http-kernel": "^3.4.20 || ^4.2.1", - "symfony/options-resolver": "^3.4.20 || ^4.2.1" + "symfony/config": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/dependency-injection": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/event-dispatcher": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/http-kernel": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/options-resolver": "^3.4.20 || ^4.2.1 || ^5.0" }, "conflict": { "php-http/guzzle6-adapter": "<1.1", @@ -54,15 +54,15 @@ "php-http/mock-client": "^1.2", "php-http/promise": "^1.0", "polishsymfonycommunity/symfony-mocker-container": "^1.0", - "symfony/browser-kit": "^3.4.20 || ^4.2.1", - "symfony/cache": "^3.4.20 || ^4.2.1", - "symfony/dom-crawler": "^3.4.20 || ^4.2.1", - "symfony/framework-bundle": "^3.4.0 || ^4.2", - "symfony/http-foundation": "^3.4.20 || ^4.2.1", - "symfony/phpunit-bridge": "^3.4 || ^4.2", - "symfony/stopwatch": "^3.4.20 || ^4.2.1", - "symfony/twig-bundle": "^3.4.20 || ^4.2.1", - "symfony/web-profiler-bundle": "^3.4.20 || ^4.2.1", + "symfony/browser-kit": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/cache": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/dom-crawler": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/framework-bundle": "^3.4.0 || ^4.2 || ^5.0", + "symfony/http-foundation": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/phpunit-bridge": "^3.4 || ^4.2 || ^5.0", + "symfony/stopwatch": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/twig-bundle": "^3.4.20 || ^4.2.1 || ^5.0", + "symfony/web-profiler-bundle": "^3.4.20 || ^4.2.1 || ^5.0", "twig/twig": "^1.36 || ^2.6" }, "suggest": { From 56b584c424d471eed5a0f679ff11103ec230bc70 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 27 Nov 2019 09:39:21 +0100 Subject: [PATCH 2/3] do not fail with twig 3 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- src/Collector/Twig/HttpMessageMarkupExtension.php | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b72402ff..f9efac77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ## Unreleased +### Fixed + +- Avoid conflict with Twig 3. + ### Added - Configured clients are now tagged with `'httplug.client'` diff --git a/composer.json b/composer.json index 3639041f..859d4239 100644 --- a/composer.json +++ b/composer.json @@ -63,7 +63,7 @@ "symfony/stopwatch": "^3.4.20 || ^4.2.1", "symfony/twig-bundle": "^3.4.20 || ^4.2.1", "symfony/web-profiler-bundle": "^3.4.20 || ^4.2.1", - "twig/twig": "^1.41 || ^2.10" + "twig/twig": "^1.41 || ^2.10 || ^3.0" }, "suggest": { "php-http/cache-plugin": "To configure clients that cache responses", diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index 6101ca66..fe1ef793 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -2,10 +2,20 @@ namespace Http\HttplugBundle\Collector\Twig; +use Twig\Extension\AbstractExtension; +use Twig\TwigFilter; + +if (!\class_exists(AbstractExtension::class)) { + class_alias(\Twig_Extension::class, AbstractExtension::class); +} +if (!\class_exists(TwigFilter::class)) { + class_alias(\Twig_SimpleFilter::class, TwigFilter::class); +} + /** * @author Tobias Nyholm */ -class HttpMessageMarkupExtension extends \Twig_Extension +class HttpMessageMarkupExtension extends AbstractExtension { /** * {@inheritdoc} @@ -15,7 +25,7 @@ class HttpMessageMarkupExtension extends \Twig_Extension public function getFilters() { return [ - new \Twig_SimpleFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]), + new TwigFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]), ]; } From adfc3cffed4972095dabf0e559228891efc08309 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Wed, 27 Nov 2019 10:56:26 +0100 Subject: [PATCH 3/3] adjust for symfony 5 --- .travis.yml | 4 +- CHANGELOG.md | 5 +-- composer.json | 4 +- src/Collector/Collector.php | 38 ++++++++++++++----- src/Collector/PluginClientFactoryListener.php | 14 +++++-- .../Twig/HttpMessageMarkupExtension.php | 7 ---- src/Discovery/ConfiguredClientsStrategy.php | 14 +++++-- tests/Functional/DiscoveredClientsTest.php | 9 +++-- tests/Functional/DiscoveryTest.php | 2 +- tests/Functional/ServiceInstantiationTest.php | 4 +- .../PluginClientFactoryListenerTest.php | 7 +++- tests/Unit/Collector/ProfileClientTest.php | 6 +-- .../DependencyInjection/ConfigurationTest.php | 6 ++- .../HttplugExtensionTest.php | 2 +- 14 files changed, 75 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7d916a46..950b04b4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,9 @@ cache: env: global: - TEST_COMMAND="composer test" - - SYMFONY_PHPUNIT_VERSION="6.5" + - SYMFONY_PHPUNIT_VERSION="8" - SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit" + - SYMFONY_DEPRECATIONS_HELPER="max[self]=0" branches: except: @@ -25,7 +26,6 @@ matrix: env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak" # Test the latest stable release - - php: 7.1 - php: 7.2 - php: 7.3 env: COVERAGE=true TEST_COMMAND="composer test-ci" DEPENDENCIES="php-http/vcr-plugin:^1.0@dev" diff --git a/CHANGELOG.md b/CHANGELOG.md index f9efac77..ff8818a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,9 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee ## Unreleased -### Fixed - -- Avoid conflict with Twig 3. - ### Added +- Support Symfony 5 and Twig 3. - Configured clients are now tagged with `'httplug.client'` - Adds a link to profiler page when response is from a Symfony application with profiler enabled diff --git a/composer.json b/composer.json index e4d50f91..21177bf9 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } ], "require": { - "php": "^7.1", + "php": "^7.2", "php-http/client-common": "^1.9 || ^2.0", "php-http/client-implementation": "^1.0", "php-http/discovery": "^1.0", @@ -47,7 +47,7 @@ }, "require-dev": { "guzzlehttp/psr7": "^1.0", - "matthiasnoback/symfony-dependency-injection-test": "^3.0", + "matthiasnoback/symfony-dependency-injection-test": "^4.0", "nyholm/nsa": "^1.1", "php-http/cache-plugin": "^1.6", "php-http/guzzle6-adapter": "^1.1.1 || ^2.0.1", diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index 6258c1c0..c274507b 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -2,10 +2,11 @@ namespace Http\HttplugBundle\Collector; -use Exception; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; +use Throwable; /** * The Collector hold profiled Stacks pushed by StackPlugin. It also have a list of configured clients. @@ -18,7 +19,8 @@ * * @internal */ -class Collector extends DataCollector +// Make this class non-abstract and move collect into it when we drop support for Symfony 4. +abstract class BaseCollector extends DataCollector { /** * @var Stack|null @@ -30,14 +32,6 @@ public function __construct() $this->reset(); } - /** - * {@inheritdoc} - */ - public function collect(Request $request, Response $response, Exception $exception = null) - { - // We do not need to collect any data from the Symfony Request and Response - } - /** * {@inheritdoc} */ @@ -199,3 +193,27 @@ public function getTotalDuration() }, 0); } } + +if (Kernel::MAJOR_VERSION >= 5) { + class Collector extends BaseCollector + { + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, Throwable $exception = null) + { + // We do not need to collect any data from the Symfony Request and Response + } + } +} else { + class Collector extends BaseCollector + { + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, \Exception $exception = null) + { + // We do not need to collect any data from the Symfony Request and Response + } + } +} diff --git a/src/Collector/PluginClientFactoryListener.php b/src/Collector/PluginClientFactoryListener.php index ce96301e..b86a402f 100644 --- a/src/Collector/PluginClientFactoryListener.php +++ b/src/Collector/PluginClientFactoryListener.php @@ -3,8 +3,16 @@ namespace Http\HttplugBundle\Collector; use Http\Client\Common\PluginClientFactory as DefaultPluginClientFactory; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as LegacyEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Contracts\EventDispatcher\Event; + +if (Kernel::MAJOR_VERSION >= 5) { + \class_alias(Event::class, 'Http\HttplugBundle\Collector\PluginClientFactoryListenerEventClass'); +} else { + \class_alias(LegacyEvent::class, 'Http\HttplugBundle\Collector\PluginClientFactoryListenerEventClass'); +} /** * This subscriber ensures that every PluginClient created when using Http\Client\Common\PluginClientFactory without @@ -32,10 +40,8 @@ public function __construct(PluginClientFactory $factory) /** * Make sure to profile clients created using PluginClientFactory. - * - * @param Event $e */ - public function onEvent(Event $e) + public function onEvent(PluginClientFactoryListenerEventClass $e) { DefaultPluginClientFactory::setFactory([$this->factory, 'createClient']); } diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index fe1ef793..88268793 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -5,13 +5,6 @@ use Twig\Extension\AbstractExtension; use Twig\TwigFilter; -if (!\class_exists(AbstractExtension::class)) { - class_alias(\Twig_Extension::class, AbstractExtension::class); -} -if (!\class_exists(TwigFilter::class)) { - class_alias(\Twig_SimpleFilter::class, TwigFilter::class); -} - /** * @author Tobias Nyholm */ diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index cec82c4b..212e1930 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -6,8 +6,16 @@ use Http\Client\HttpAsyncClient; use Http\Discovery\HttpClientDiscovery; use Http\Discovery\Strategy\DiscoveryStrategy; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as LegacyEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Contracts\EventDispatcher\Event; + +if (Kernel::MAJOR_VERSION >= 5) { + \class_alias(Event::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass'); +} else { + \class_alias(LegacyEvent::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass'); +} /** * A strategy that provide clients configured with HTTPlug bundle. With help from this strategy @@ -60,10 +68,8 @@ public static function getCandidates($type) /** * Make sure to use our custom strategy. - * - * @param Event $e */ - public function onEvent(Event $e) + public function onEvent(ConfiguredClientsStrategyEventClass $e) { HttpClientDiscovery::prependStrategy(self::class); } diff --git a/tests/Functional/DiscoveredClientsTest.php b/tests/Functional/DiscoveredClientsTest.php index 92f9b322..7496c649 100644 --- a/tests/Functional/DiscoveredClientsTest.php +++ b/tests/Functional/DiscoveredClientsTest.php @@ -11,7 +11,9 @@ use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy; use Nyholm\NSA; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Contracts\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as LegacyEvent; class DiscoveredClientsTest extends WebTestCase { @@ -124,8 +126,9 @@ protected function setUp(): void parent::setUp(); // Reset values - $strategy = new ConfiguredClientsStrategy(null, null, null); + $strategy = new ConfiguredClientsStrategy(null, null); HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]); - $strategy->onEvent(new Event()); + $class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class; + $strategy->onEvent(new $class()); } } diff --git a/tests/Functional/DiscoveryTest.php b/tests/Functional/DiscoveryTest.php index 155a577a..25168205 100644 --- a/tests/Functional/DiscoveryTest.php +++ b/tests/Functional/DiscoveryTest.php @@ -30,7 +30,7 @@ protected function setUp(): void /** * {@inheritdoc} */ - protected function getContainerExtensions() + protected function getContainerExtensions(): array { return [ new HttplugExtension(), diff --git a/tests/Functional/ServiceInstantiationTest.php b/tests/Functional/ServiceInstantiationTest.php index eb104bc8..ada23b32 100644 --- a/tests/Functional/ServiceInstantiationTest.php +++ b/tests/Functional/ServiceInstantiationTest.php @@ -17,6 +17,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\HttpKernel\KernelEvents; @@ -116,7 +117,8 @@ protected static function bootKernel(array $options = []) /** @var EventDispatcherInterface $dispatcher */ $dispatcher = static::$kernel->getContainer()->get('event_dispatcher'); - $event = new GetResponseEvent(static::$kernel, new SymfonyRequest(), HttpKernelInterface::MASTER_REQUEST); + $class = (Kernel::MAJOR_VERSION >= 5) ? RequestEvent::class : GetResponseEvent::class; + $event = new $class(static::$kernel, SymfonyRequest::create('/'), HttpKernelInterface::MASTER_REQUEST); if (version_compare(Kernel::VERSION, '4.3.0', '>=')) { $dispatcher->dispatch($event, KernelEvents::REQUEST); diff --git a/tests/Unit/Collector/PluginClientFactoryListenerTest.php b/tests/Unit/Collector/PluginClientFactoryListenerTest.php index 334313e7..54b60152 100644 --- a/tests/Unit/Collector/PluginClientFactoryListenerTest.php +++ b/tests/Unit/Collector/PluginClientFactoryListenerTest.php @@ -9,8 +9,10 @@ use Http\HttplugBundle\Collector\PluginClientFactoryListener; use Nyholm\NSA; use PHPUnit\Framework\TestCase; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as LegacyEvent; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Stopwatch\Stopwatch; +use Symfony\Contracts\EventDispatcher\Event; final class PluginClientFactoryListenerTest extends TestCase { @@ -24,7 +26,8 @@ public function testRegisterPluginClientFactory(): void $listener = new PluginClientFactoryListener($factory); - $listener->onEvent(new Event()); + $class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class; + $listener->onEvent(new $class()); $this->assertTrue(is_callable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory'))); } diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index 2cc7a607..d9694bca 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -141,10 +141,6 @@ public function testSendRequest(): void $this->assertEquals('https', $this->activeStack->getRequestScheme()); } - /** - * @expectedException \Error - * @expectedException "You set string to int prop" - */ public function testSendRequestTypeError() { $this->client @@ -158,6 +154,8 @@ public function testSendRequestTypeError() ->method('formatException') ->with($this->isInstanceOf(\Error::class)); + $this->expectException(\Error::class); + $this->expectExceptionMessage('You set string to int prop'); $this->subject->sendRequest($this->request); } diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index a9fd4ba4..a3de4066 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -9,7 +9,9 @@ use Http\Message\MessageFactory\GuzzleMessageFactory; use Http\Message\UriFactory\GuzzleUriFactory; use Http\Message\StreamFactory\GuzzleStreamFactory; +use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; /** * @author David Buchmann @@ -80,12 +82,12 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase ], ]; - protected function getContainerExtension() + protected function getContainerExtension(): ExtensionInterface { return new HttplugExtension(); } - protected function getConfiguration() + protected function getConfiguration(): ConfigurationInterface { return new Configuration(true); } diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index 54f6cc75..d4b8731c 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -24,7 +24,7 @@ protected function setUp(): void $this->setParameter('kernel.debug', true); } - protected function getContainerExtensions() + protected function getContainerExtensions(): array { return [ new HttplugExtension(),