Skip to content

Commit

Permalink
feat: remove curl factory override
Browse files Browse the repository at this point in the history
  • Loading branch information
shavounet committed Oct 13, 2023
1 parent c5ab44a commit 0126cff
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 105 deletions.
14 changes: 2 additions & 12 deletions src/DependencyInjection/M6WebGuzzleHttpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
27 changes: 0 additions & 27 deletions src/Handler/CurlFactory.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Handler/CurlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CurlMultiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
77 changes: 14 additions & 63 deletions tests/Units/Handler/CurlMultiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,36 @@

/**
* 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
->object($response = $testedClass($request, [])->wait())
->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])
Expand All @@ -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) {
Expand All @@ -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()]))
Expand All @@ -100,9 +84,6 @@ public function testCacheSetWithHeader()
->isEqualTo(200)
->integer($response3->getStatusCode())
->isEqualTo(200)
->mock($curlFactoryMock)
->call('release')
->twice()
->mock($cacheMock)
->call('get')
->thrice()
Expand All @@ -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']))
Expand All @@ -134,16 +115,14 @@ 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;
$cacheMock->getMockController()->get = $this->getSerializedResponse(new Response(200, [], 'The answer is 42', '1.1', 'OK'));
$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'))
Expand All @@ -167,9 +146,6 @@ public function testCacheGet()
->call('ttl')
->withAnyArguments()
->once()
->mock($curlFactoryMock)
->call('release')
->never()
;

// Test unserialize issue
Expand All @@ -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
Expand All @@ -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
Expand All @@ -244,24 +210,19 @@ 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;
$cacheMock->getMockController()->get = null;

// 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -324,16 +282,14 @@ 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;
$cacheMock->getMockController()->get = $this->getSerializedResponse(new Response(200, [], 'The answer is 42', '1.1', 'OK'));
$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'))
Expand All @@ -354,9 +310,6 @@ public function testCacheGetDebugOff()
->once()
->call('ttl')
->never()
->mock($curlFactoryMock)
->call('release')
->never()
;
}

Expand All @@ -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(
Expand Down

0 comments on commit 0126cff

Please sign in to comment.