Skip to content

Commit f9d5ade

Browse files
dbuNyholm
authored andcommitted
Symfony5 (#360)
* Update composer.json * do not fail with twig 3 * adjust for symfony 5
1 parent 3bd469e commit f9d5ade

14 files changed

+95
-53
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ cache:
1010
env:
1111
global:
1212
- TEST_COMMAND="composer test"
13-
- SYMFONY_PHPUNIT_VERSION="6.5"
13+
- SYMFONY_PHPUNIT_VERSION="8"
1414
- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"
15+
- SYMFONY_DEPRECATIONS_HELPER="max[self]=0"
1516

1617
branches:
1718
except:
@@ -25,7 +26,6 @@ matrix:
2526
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak"
2627

2728
# Test the latest stable release
28-
- php: 7.1
2929
- php: 7.2
3030
- php: 7.3
3131
env: COVERAGE=true TEST_COMMAND="composer test-ci" DEPENDENCIES="php-http/vcr-plugin:^1.0@dev"

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
66

77
### Added
88

9+
- Support Symfony 5 and Twig 3.
910
- Configured clients are now tagged with `'httplug.client'`
1011
- Adds a link to profiler page when response is from a Symfony application with
1112
profiler enabled

composer.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"php": "^7.1",
28+
"php": "^7.2",
2929
"php-http/client-common": "^1.9 || ^2.0",
3030
"php-http/client-implementation": "^1.0",
3131
"php-http/discovery": "^1.0",
@@ -35,35 +35,35 @@
3535
"php-http/message-factory": "^1.0.2",
3636
"php-http/stopwatch-plugin": "^1.2",
3737
"psr/http-message": "^1.0",
38-
"symfony/config": "^3.4.20 || ^4.2.1",
39-
"symfony/dependency-injection": "^3.4.20 || ^4.2.1",
40-
"symfony/event-dispatcher": "^3.4.20 || ^4.2.1",
41-
"symfony/http-kernel": "^3.4.20 || ^4.2.1",
42-
"symfony/options-resolver": "^3.4.20 || ^4.2.1"
38+
"symfony/config": "^3.4.20 || ^4.2.1 || ^5.0",
39+
"symfony/dependency-injection": "^3.4.20 || ^4.2.1 || ^5.0",
40+
"symfony/event-dispatcher": "^3.4.20 || ^4.2.1 || ^5.0",
41+
"symfony/http-kernel": "^3.4.20 || ^4.2.1 || ^5.0",
42+
"symfony/options-resolver": "^3.4.20 || ^4.2.1 || ^5.0"
4343
},
4444
"conflict": {
4545
"php-http/guzzle6-adapter": "<1.1",
4646
"php-http/curl-client": "<2.0"
4747
},
4848
"require-dev": {
4949
"guzzlehttp/psr7": "^1.0",
50-
"matthiasnoback/symfony-dependency-injection-test": "^3.0",
50+
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
5151
"nyholm/nsa": "^1.1",
5252
"php-http/cache-plugin": "^1.6",
5353
"php-http/guzzle6-adapter": "^1.1.1 || ^2.0.1",
5454
"php-http/mock-client": "^1.2",
5555
"php-http/promise": "^1.0",
5656
"polishsymfonycommunity/symfony-mocker-container": "^1.0",
57-
"symfony/browser-kit": "^3.4.20 || ^4.2.1",
58-
"symfony/cache": "^3.4.20 || ^4.2.1",
59-
"symfony/dom-crawler": "^3.4.20 || ^4.2.1",
60-
"symfony/framework-bundle": "^3.4.0 || ^4.2",
61-
"symfony/http-foundation": "^3.4.20 || ^4.2.1",
62-
"symfony/phpunit-bridge": "^3.4 || ^4.2",
63-
"symfony/stopwatch": "^3.4.20 || ^4.2.1",
64-
"symfony/twig-bundle": "^3.4.20 || ^4.2.1",
65-
"symfony/web-profiler-bundle": "^3.4.20 || ^4.2.1",
66-
"twig/twig": "^1.41 || ^2.10"
57+
"symfony/browser-kit": "^3.4.20 || ^4.2.1 || ^5.0",
58+
"symfony/cache": "^3.4.20 || ^4.2.1 || ^5.0",
59+
"symfony/dom-crawler": "^3.4.20 || ^4.2.1 || ^5.0",
60+
"symfony/framework-bundle": "^3.4.0 || ^4.2 || ^5.0",
61+
"symfony/http-foundation": "^3.4.20 || ^4.2.1 || ^5.0",
62+
"symfony/phpunit-bridge": "^3.4 || ^4.2 || ^5.0",
63+
"symfony/stopwatch": "^3.4.20 || ^4.2.1 || ^5.0",
64+
"symfony/twig-bundle": "^3.4.20 || ^4.2.1 || ^5.0",
65+
"symfony/web-profiler-bundle": "^3.4.20 || ^4.2.1 || ^5.0",
66+
"twig/twig": "^1.41 || ^2.10 || ^3.0"
6767
},
6868
"suggest": {
6969
"php-http/cache-plugin": "To configure clients that cache responses",

src/Collector/Collector.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Http\HttplugBundle\Collector;
44

5-
use Exception;
5+
use Symfony\Component\HttpKernel\Kernel;
66
use Symfony\Component\HttpFoundation\Request;
77
use Symfony\Component\HttpFoundation\Response;
88
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
9+
use Throwable;
910

1011
/**
1112
* The Collector hold profiled Stacks pushed by StackPlugin. It also have a list of configured clients.
@@ -18,7 +19,8 @@
1819
*
1920
* @internal
2021
*/
21-
class Collector extends DataCollector
22+
// Make this class non-abstract and move collect into it when we drop support for Symfony 4.
23+
abstract class BaseCollector extends DataCollector
2224
{
2325
/**
2426
* @var Stack|null
@@ -30,14 +32,6 @@ public function __construct()
3032
$this->reset();
3133
}
3234

33-
/**
34-
* {@inheritdoc}
35-
*/
36-
public function collect(Request $request, Response $response, Exception $exception = null)
37-
{
38-
// We do not need to collect any data from the Symfony Request and Response
39-
}
40-
4135
/**
4236
* {@inheritdoc}
4337
*/
@@ -199,3 +193,27 @@ public function getTotalDuration()
199193
}, 0);
200194
}
201195
}
196+
197+
if (Kernel::MAJOR_VERSION >= 5) {
198+
class Collector extends BaseCollector
199+
{
200+
/**
201+
* {@inheritdoc}
202+
*/
203+
public function collect(Request $request, Response $response, Throwable $exception = null)
204+
{
205+
// We do not need to collect any data from the Symfony Request and Response
206+
}
207+
}
208+
} else {
209+
class Collector extends BaseCollector
210+
{
211+
/**
212+
* {@inheritdoc}
213+
*/
214+
public function collect(Request $request, Response $response, \Exception $exception = null)
215+
{
216+
// We do not need to collect any data from the Symfony Request and Response
217+
}
218+
}
219+
}

src/Collector/PluginClientFactoryListener.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
namespace Http\HttplugBundle\Collector;
44

55
use Http\Client\Common\PluginClientFactory as DefaultPluginClientFactory;
6-
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
77
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use Symfony\Component\HttpKernel\Kernel;
9+
use Symfony\Contracts\EventDispatcher\Event;
10+
11+
if (Kernel::MAJOR_VERSION >= 5) {
12+
\class_alias(Event::class, 'Http\HttplugBundle\Collector\PluginClientFactoryListenerEventClass');
13+
} else {
14+
\class_alias(LegacyEvent::class, 'Http\HttplugBundle\Collector\PluginClientFactoryListenerEventClass');
15+
}
816

917
/**
1018
* This subscriber ensures that every PluginClient created when using Http\Client\Common\PluginClientFactory without
@@ -32,10 +40,8 @@ public function __construct(PluginClientFactory $factory)
3240

3341
/**
3442
* Make sure to profile clients created using PluginClientFactory.
35-
*
36-
* @param Event $e
3743
*/
38-
public function onEvent(Event $e)
44+
public function onEvent(PluginClientFactoryListenerEventClass $e)
3945
{
4046
DefaultPluginClientFactory::setFactory([$this->factory, 'createClient']);
4147
}

src/Collector/Twig/HttpMessageMarkupExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Http\HttplugBundle\Collector\Twig;
44

5+
use Twig\Extension\AbstractExtension;
6+
use Twig\TwigFilter;
7+
58
/**
69
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
710
*/
8-
class HttpMessageMarkupExtension extends \Twig_Extension
11+
class HttpMessageMarkupExtension extends AbstractExtension
912
{
1013
/**
1114
* {@inheritdoc}
@@ -15,7 +18,7 @@ class HttpMessageMarkupExtension extends \Twig_Extension
1518
public function getFilters()
1619
{
1720
return [
18-
new \Twig_SimpleFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
21+
new TwigFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
1922
];
2023
}
2124

src/Discovery/ConfiguredClientsStrategy.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
use Http\Client\HttpAsyncClient;
77
use Http\Discovery\HttpClientDiscovery;
88
use Http\Discovery\Strategy\DiscoveryStrategy;
9-
use Symfony\Component\EventDispatcher\Event;
9+
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
1010
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11+
use Symfony\Component\HttpKernel\Kernel;
12+
use Symfony\Contracts\EventDispatcher\Event;
13+
14+
if (Kernel::MAJOR_VERSION >= 5) {
15+
\class_alias(Event::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
16+
} else {
17+
\class_alias(LegacyEvent::class, 'Http\HttplugBundle\Discovery\ConfiguredClientsStrategyEventClass');
18+
}
1119

1220
/**
1321
* A strategy that provide clients configured with HTTPlug bundle. With help from this strategy
@@ -60,10 +68,8 @@ public static function getCandidates($type)
6068

6169
/**
6270
* Make sure to use our custom strategy.
63-
*
64-
* @param Event $e
6571
*/
66-
public function onEvent(Event $e)
72+
public function onEvent(ConfiguredClientsStrategyEventClass $e)
6773
{
6874
HttpClientDiscovery::prependStrategy(self::class);
6975
}

tests/Functional/DiscoveredClientsTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Http\HttplugBundle\Discovery\ConfiguredClientsStrategy;
1212
use Nyholm\NSA;
1313
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
14-
use Symfony\Component\EventDispatcher\Event;
14+
use Symfony\Component\HttpKernel\Kernel;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
1517

1618
class DiscoveredClientsTest extends WebTestCase
1719
{
@@ -124,8 +126,9 @@ protected function setUp(): void
124126
parent::setUp();
125127

126128
// Reset values
127-
$strategy = new ConfiguredClientsStrategy(null, null, null);
129+
$strategy = new ConfiguredClientsStrategy(null, null);
128130
HttpClientDiscovery::setStrategies([CommonClassesStrategy::class]);
129-
$strategy->onEvent(new Event());
131+
$class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class;
132+
$strategy->onEvent(new $class());
130133
}
131134
}

tests/Functional/DiscoveryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected function setUp(): void
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
protected function getContainerExtensions()
33+
protected function getContainerExtensions(): array
3434
{
3535
return [
3636
new HttplugExtension(),

tests/Functional/ServiceInstantiationTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1818
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1919
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
20+
use Symfony\Component\HttpKernel\Event\RequestEvent;
2021
use Symfony\Component\HttpKernel\HttpKernelInterface;
2122
use Symfony\Component\HttpKernel\Kernel;
2223
use Symfony\Component\HttpKernel\KernelEvents;
@@ -116,7 +117,8 @@ protected static function bootKernel(array $options = [])
116117
/** @var EventDispatcherInterface $dispatcher */
117118
$dispatcher = static::$kernel->getContainer()->get('event_dispatcher');
118119

119-
$event = new GetResponseEvent(static::$kernel, new SymfonyRequest(), HttpKernelInterface::MASTER_REQUEST);
120+
$class = (Kernel::MAJOR_VERSION >= 5) ? RequestEvent::class : GetResponseEvent::class;
121+
$event = new $class(static::$kernel, SymfonyRequest::create('/'), HttpKernelInterface::MASTER_REQUEST);
120122

121123
if (version_compare(Kernel::VERSION, '4.3.0', '>=')) {
122124
$dispatcher->dispatch($event, KernelEvents::REQUEST);

tests/Unit/Collector/PluginClientFactoryListenerTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
1010
use Nyholm\NSA;
1111
use PHPUnit\Framework\TestCase;
12-
use Symfony\Component\EventDispatcher\Event;
12+
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
13+
use Symfony\Component\HttpKernel\Kernel;
1314
use Symfony\Component\Stopwatch\Stopwatch;
15+
use Symfony\Contracts\EventDispatcher\Event;
1416

1517
final class PluginClientFactoryListenerTest extends TestCase
1618
{
@@ -24,7 +26,8 @@ public function testRegisterPluginClientFactory(): void
2426

2527
$listener = new PluginClientFactoryListener($factory);
2628

27-
$listener->onEvent(new Event());
29+
$class = (Kernel::MAJOR_VERSION >= 5) ? Event::class : LegacyEvent::class;
30+
$listener->onEvent(new $class());
2831

2932
$this->assertTrue(is_callable(NSA::getProperty(DefaultPluginClientFactory::class, 'factory')));
3033
}

tests/Unit/Collector/ProfileClientTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ public function testSendRequest(): void
141141
$this->assertEquals('https', $this->activeStack->getRequestScheme());
142142
}
143143

144-
/**
145-
* @expectedException \Error
146-
* @expectedException "You set string to int prop"
147-
*/
148144
public function testSendRequestTypeError()
149145
{
150146
$this->client
@@ -158,6 +154,8 @@ public function testSendRequestTypeError()
158154
->method('formatException')
159155
->with($this->isInstanceOf(\Error::class));
160156

157+
$this->expectException(\Error::class);
158+
$this->expectExceptionMessage('You set string to int prop');
161159
$this->subject->sendRequest($this->request);
162160
}
163161

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Http\Message\MessageFactory\GuzzleMessageFactory;
1010
use Http\Message\UriFactory\GuzzleUriFactory;
1111
use Http\Message\StreamFactory\GuzzleStreamFactory;
12+
use Symfony\Component\Config\Definition\ConfigurationInterface;
1213
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
14+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
1315

1416
/**
1517
* @author David Buchmann <mail@davidbu.ch>
@@ -80,12 +82,12 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
8082
],
8183
];
8284

83-
protected function getContainerExtension()
85+
protected function getContainerExtension(): ExtensionInterface
8486
{
8587
return new HttplugExtension();
8688
}
8789

88-
protected function getConfiguration()
90+
protected function getConfiguration(): ConfigurationInterface
8991
{
9092
return new Configuration(true);
9193
}

tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function setUp(): void
2424
$this->setParameter('kernel.debug', true);
2525
}
2626

27-
protected function getContainerExtensions()
27+
protected function getContainerExtensions(): array
2828
{
2929
return [
3030
new HttplugExtension(),

0 commit comments

Comments
 (0)