Skip to content

Commit

Permalink
Symfony5 (#360)
Browse files Browse the repository at this point in the history
* Update composer.json

* do not fail with twig 3

* adjust for symfony 5
  • Loading branch information
dbu authored and Nyholm committed Nov 28, 2019
1 parent 3bd469e commit f9d5ade
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 53 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee

### 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
Expand Down
34 changes: 17 additions & 17 deletions composer.json
Expand Up @@ -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",
Expand All @@ -35,35 +35,35 @@
"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",
"php-http/curl-client": "<2.0"
},
"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",
"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",
"twig/twig": "^1.41 || ^2.10"
"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.41 || ^2.10 || ^3.0"
},
"suggest": {
"php-http/cache-plugin": "To configure clients that cache responses",
Expand Down
38 changes: 28 additions & 10 deletions src/Collector/Collector.php
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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
}
}
}
14 changes: 10 additions & 4 deletions src/Collector/PluginClientFactoryListener.php
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Collector/Twig/HttpMessageMarkupExtension.php
Expand Up @@ -2,10 +2,13 @@

namespace Http\HttplugBundle\Collector\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class HttpMessageMarkupExtension extends \Twig_Extension
class HttpMessageMarkupExtension extends AbstractExtension
{
/**
* {@inheritdoc}
Expand All @@ -15,7 +18,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']]),
];
}

Expand Down
14 changes: 10 additions & 4 deletions src/Discovery/ConfiguredClientsStrategy.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 6 additions & 3 deletions tests/Functional/DiscoveredClientsTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/Functional/DiscoveryTest.php
Expand Up @@ -30,7 +30,7 @@ protected function setUp(): void
/**
* {@inheritdoc}
*/
protected function getContainerExtensions()
protected function getContainerExtensions(): array
{
return [
new HttplugExtension(),
Expand Down
4 changes: 3 additions & 1 deletion tests/Functional/ServiceInstantiationTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Collector/PluginClientFactoryListenerTest.php
Expand Up @@ -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
{
Expand All @@ -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')));
}
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/Collector/ProfileClientTest.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Expand Up @@ -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 <mail@davidbu.ch>
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DependencyInjection/HttplugExtensionTest.php
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
$this->setParameter('kernel.debug', true);
}

protected function getContainerExtensions()
protected function getContainerExtensions(): array
{
return [
new HttplugExtension(),
Expand Down

0 comments on commit f9d5ade

Please sign in to comment.