diff --git a/src/DependencyInjection/M6WebGuzzleHttpExtension.php b/src/DependencyInjection/M6WebGuzzleHttpExtension.php index 11e8b9d..c720743 100644 --- a/src/DependencyInjection/M6WebGuzzleHttpExtension.php +++ b/src/DependencyInjection/M6WebGuzzleHttpExtension.php @@ -150,24 +150,14 @@ protected function loadClient(ContainerBuilder $container, string $clientId, arr */ protected function setGuzzleProxyHandler(ContainerBuilder $container, ?string $clientId, array $config, bool $isDebugEnabled) { - // arguments (3 and 50) in handler factories below represents the maximum number of idle handles. - // the values are the default defined in guzzle CurlHandler and CurlMultiHandler - $handlerFactorySync = new Definition('%m6web_guzlehttp.handler.curlfactory.class%'); - $handlerFactorySync->setPublic(true); - $handlerFactorySync->setArguments([3]); - - $handlerFactoryNormal = new Definition('%m6web_guzlehttp.handler.curlfactory.class%'); - $handlerFactoryNormal->setPublic(true); - $handlerFactoryNormal->setArguments([50]); - $curlHandler = new Definition('%m6web_guzlehttp.handler.curlhandler.class%'); $curlHandler->setPublic(true); - $curlHandler->setArguments([new Reference('event_dispatcher'), ['handle_factory' => $handlerFactorySync]]); + $curlHandler->setArguments([new Reference('event_dispatcher')]); $curlHandler->addMethodCall('setDebug', [$isDebugEnabled]); $curlMultiHandler = new Definition('%m6web_guzlehttp.handler.curlmultihandler.class%'); $curlMultiHandler->setPublic(true); - $curlMultiHandler->setArguments([new Reference('event_dispatcher'), ['handle_factory' => $handlerFactoryNormal]]); + $curlMultiHandler->setArguments([new Reference('event_dispatcher')]); $curlMultiHandler->addMethodCall('setDebug', [$isDebugEnabled]); if (isset($config['guzzlehttp_cache'])) { diff --git a/src/Handler/CurlFactory.php b/src/Handler/CurlFactory.php deleted file mode 100644 index bf545b8..0000000 --- a/src/Handler/CurlFactory.php +++ /dev/null @@ -1,27 +0,0 @@ - BC is not ensured) -use GuzzleHttp\Handler\EasyHandle; - -/** - * Extends the Guzzle curl factory to set curl info in response - * - * @deprecated the curlInfo dynamic property will be deleted - * in favor of [the native on_stat option](https://docs.guzzlephp.org/en/latest/request-options.html#on-stats) - */ -class CurlFactory extends GuzzleCurlFactory -{ - public function release(EasyHandle $easy): void - { - if (!\is_null($easy->response)) { - $easy->response->curlInfo = curl_getinfo($easy->handle); - } - - parent::release($easy); - } -} diff --git a/src/Handler/CurlHandler.php b/src/Handler/CurlHandler.php index 3392656..3914729 100644 --- a/src/Handler/CurlHandler.php +++ b/src/Handler/CurlHandler.php @@ -20,7 +20,7 @@ class CurlHandler extends GuzzleCurlHandler /** * CurlHandler constructor. */ - public function __construct(EventDispatcherInterface $eventDispatcher, array $options) + public function __construct(EventDispatcherInterface $eventDispatcher, array $options = []) { $this->eventDispatcher = $eventDispatcher; diff --git a/src/Handler/CurlMultiHandler.php b/src/Handler/CurlMultiHandler.php index bf369a0..5070feb 100644 --- a/src/Handler/CurlMultiHandler.php +++ b/src/Handler/CurlMultiHandler.php @@ -20,7 +20,7 @@ class CurlMultiHandler extends GuzzleCurlMultiHandler /** * CurlMultiHandler constructor. */ - public function __construct(EventDispatcherInterface $eventDispatcher, array $options) + public function __construct(EventDispatcherInterface $eventDispatcher, array $options = []) { $this->eventDispatcher = $eventDispatcher; diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 34b736a..11956c4 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -1,5 +1,4 @@ parameters: - m6web_guzlehttp.handler.curlfactory.class: 'M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory' m6web_guzlehttp.handler.curlhandler.class: 'M6Web\Bundle\GuzzleHttpBundle\Handler\CurlHandler' m6web_guzlehttp.handler.curlmultihandler.class: 'M6Web\Bundle\GuzzleHttpBundle\Handler\CurlMultiHandler' diff --git a/tests/Units/Handler/CurlMultiHandler.php b/tests/Units/Handler/CurlMultiHandler.php index 680d08e..b35ff3d 100644 --- a/tests/Units/Handler/CurlMultiHandler.php +++ b/tests/Units/Handler/CurlMultiHandler.php @@ -12,38 +12,29 @@ /** * Class CurlMultiHandler - * Used for testing trait and curlFactory + * Used for testing trait */ class CurlMultiHandler extends \atoum { public function testNoCache() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($request = new Request('GET', 'http://httpbin.org')) ->then ->object($response = $testedClass($request, [])->wait()) ->isInstanceOf('GuzzleHttp\Psr7\Response') ->integer($response->getStatusCode()) ->isEqualTo(200) - ->mock($curlFactoryMock) - ->call('release') - ->once() - ->array($response->curlInfo) - ->isNotEmpty() ; } public function testCacheSet() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request = new Request('GET', 'http://httpbin.org')) ->then @@ -51,11 +42,6 @@ public function testCacheSet() ->isInstanceOf('GuzzleHttp\Psr7\Response') ->integer($response->getStatusCode()) ->isEqualTo(200) - ->mock($curlFactoryMock) - ->call('release') - ->once() - ->array($response->curlInfo) - ->isNotEmpty() ->mock($cacheMock) ->call('set') ->withAtLeastArguments(['1' => $this->getSerializedResponse($response), '2' => 500]) @@ -65,8 +51,6 @@ public function testCacheSet() public function testCacheSetWithHeader() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheData = []; $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->set = function ($key, $value) use (&$cacheData) { @@ -82,7 +66,7 @@ public function testCacheSetWithHeader() // Add Header as part of the cache key but "X-" headers must be ignored $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request1 = new Request('GET', 'http://httpbin.org', ['user-agent' => 'Netscape 1', 'X-Ddos-Me' => uniqid()])) ->and($request2 = new Request('GET', 'http://httpbin.org', ['user-agent' => 'Netscape 1', 'X-Ddos-Me' => uniqid()])) @@ -100,9 +84,6 @@ public function testCacheSetWithHeader() ->isEqualTo(200) ->integer($response3->getStatusCode()) ->isEqualTo(200) - ->mock($curlFactoryMock) - ->call('release') - ->twice() ->mock($cacheMock) ->call('get') ->thrice() @@ -115,7 +96,7 @@ public function testCacheSetWithHeader() // A header in the Vary should be in the cache even if it's an X- $cacheMock->getMockController()->resetCalls(); $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request1 = new Request('GET', 'http://httpbin.org', ['user-agent' => 'Netscape 1', 'X-Important' => 'raoul', 'Vary' => 'X-Important'])) ->and($request2 = new Request('GET', 'http://httpbin.org', ['user-agent' => 'Netscape 1', 'X-Important' => 'raoul2', 'Vary' => 'X-Important'])) @@ -134,8 +115,6 @@ public function testCacheSetWithHeader() public function testCacheGet() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = true; @@ -143,7 +122,7 @@ public function testCacheGet() $cacheMock->getMockController()->ttl = 256; $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($testedClass->setDebug(true)) ->and($request = new Request('GET', 'http://httpbin.org')) @@ -167,9 +146,6 @@ public function testCacheGet() ->call('ttl') ->withAnyArguments() ->once() - ->mock($curlFactoryMock) - ->call('release') - ->never() ; // Test unserialize issue @@ -189,23 +165,18 @@ public function testCacheGet() ->call('ttl') ->withAnyArguments() ->once() - ->mock($curlFactoryMock) - ->call('release') - ->once() ; } public function testForceCache() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = false; $cacheMock->getMockController()->get = null; $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request = new Request('GET', 'http://httpbin.org')) ->then @@ -218,23 +189,18 @@ public function testForceCache() ->call('set') ->withAtLeastArguments(['1' => $this->getSerializedResponse($response), '2' => 500]) ->once() - ->mock($curlFactoryMock) - ->call('release') - ->once() ; } public function testCacheCustomTtl() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = false; $cacheMock->getMockController()->get = null; $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request = new Request('GET', 'http://httpbin.org')) ->then @@ -244,16 +210,11 @@ public function testCacheCustomTtl() ->call('set') ->withAtLeastArguments(['1' => $this->getSerializedResponse($response), '2' => 200]) ->once() - ->mock($curlFactoryMock) - ->call('release') - ->once() ; } public function testCacheUseHeader() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = false; @@ -261,7 +222,7 @@ public function testCacheUseHeader() // use header ttl $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, true)) ->and($request = new Request('GET', 'http://httpbin.org/cache/200')) ->then @@ -271,15 +232,12 @@ public function testCacheUseHeader() ->call('set') ->withAtLeastArguments(['1' => $this->getSerializedResponse($response), '2' => 200]) ->once() - ->mock($curlFactoryMock) - ->call('release') - ->once() ->and($this->resetMock($cacheMock)) ; // 200s of cache but force to 500 $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request = new Request('GET', 'http://httpbin.org/cache/200')) ->then @@ -294,7 +252,7 @@ public function testCacheUseHeader() // use header ttl and no cache $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, true)) ->and($request = new Request('GET', 'http://httpbin.org/cache/0')) ->then @@ -308,7 +266,7 @@ public function testCacheUseHeader() // no cache in header but forced to 500 $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($request = new Request('GET', 'http://httpbin.org/cache/0')) ->then @@ -324,8 +282,6 @@ public function testCacheUseHeader() public function testCacheGetDebugOff() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - $cacheMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Cache\CacheInterface(); $cacheMock->getMockController()->has = true; @@ -333,7 +289,7 @@ public function testCacheGetDebugOff() $cacheMock->getMockController()->ttl = 256; $this - ->if($testedClass = new TestedClass($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock])) + ->if($testedClass = new TestedClass($this->getMockDispatcher())) ->and($testedClass->setCache($cacheMock, 500, false)) ->and($testedClass->setDebug(false)) ->and($request = new Request('GET', 'http://httpbin.org')) @@ -354,9 +310,6 @@ public function testCacheGetDebugOff() ->once() ->call('ttl') ->never() - ->mock($curlFactoryMock) - ->call('release') - ->never() ; } @@ -374,9 +327,7 @@ protected function getSerializedResponse(Response $response) public function testGetKey() { - $curlFactoryMock = new \mock\M6Web\Bundle\GuzzleHttpBundle\Handler\CurlFactory(3); - - $testedClass = new FakeCurlMultiHandler($this->getMockDispatcher(), ['handle_factory' => $curlFactoryMock]); + $testedClass = new FakeCurlMultiHandler($this->getMockDispatcher()); $this->if( $request = new \mock\GuzzleHttp\Psr7\Request(