diff --git a/src/Handler/CacheTrait.php b/src/Handler/CacheTrait.php index 3757d8c..e1142ab 100644 --- a/src/Handler/CacheTrait.php +++ b/src/Handler/CacheTrait.php @@ -24,6 +24,11 @@ trait CacheTrait */ protected $useHeaderTtl; + /** + * @var boolean + */ + protected $debug = false; + /** * @var array */ @@ -41,6 +46,16 @@ public function setCache(CacheInterface $cache, $defaultTtl, $useHeaderTtl) $this->useHeaderTtl = $useHeaderTtl; } + /** + * Set the debug mode + * + * @param boolean $debug + */ + public function setDebug($debug) + { + $this->debug = $debug; + } + /** * Get cache key * @@ -125,7 +140,12 @@ protected function getCached(RequestInterface $request) $response = new Response($cached[0], $cached[1], $cached[2], $cached[3], $cached[4]); $response->cached = true; - $response->cacheTtl = $this->cache->ttl($cacheKey); + + // set ttl information only on debug mode + if ($this->debug) { + $response->cacheTtl = $this->cache->ttl($cacheKey); + } + return $response; } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index abe8519..70c5b43 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -22,10 +22,12 @@ services: m6web_guzlehttp.handler.curlhandler: class: %m6web_guzlehttp.handler.curlhandler.class% arguments: [ {handle_factory: "@m6web_guzlehttp.handler.curlfactory.sync"} ] + calls: [ ['setDebug', [%kernel.debug%]] ] m6web_guzlehttp.handler.curlmultihandler: class: %m6web_guzlehttp.handler.curlmultihandler.class% arguments: [ {handle_factory : "@m6web_guzlehttp.handler.curlfactory.normal"} ] + calls: [ ['setDebug', [%kernel.debug%]] ] m6web_guzzlehttp.guzzle.proxyhandler: class: %m6web_guzzlehttp.guzzle.proxyhandler.class% diff --git a/tests/Units/Handler/CurlMultiHandler.php b/tests/Units/Handler/CurlMultiHandler.php index edaf7b9..6b16844 100644 --- a/tests/Units/Handler/CurlMultiHandler.php +++ b/tests/Units/Handler/CurlMultiHandler.php @@ -77,6 +77,7 @@ public function testCacheGet() $this ->if($testedClass = new TestedClass(['handle_factory' => $curlFactoryMock])) ->and($testedClass->setCache($cacheMock, 500, false)) + ->and($testedClass->setDebug(true)) ->and($request = new Request('GET', 'http://httpbin.org')) ->then ->object($response = $testedClass($request, [])->wait()) @@ -90,6 +91,7 @@ public function testCacheGet() ->boolean($response->cached) ->isTrue() ->integer($response->cacheTtl) + ->isEqualTo(256) ->mock($cacheMock) ->call('has') ->withArguments(md5($request->getUri())) @@ -192,6 +194,49 @@ 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(['handle_factory' => $curlFactoryMock])) + ->and($testedClass->setCache($cacheMock, 500, false)) + ->and($testedClass->setDebug(false)) + ->and($request = new Request('GET', 'http://httpbin.org')) + ->then + ->object($response = $testedClass($request, [])->wait()) + ->isInstanceOf('GuzzleHttp\Psr7\Response') + ->integer($response->getStatuscode()) + ->isEqualTo(200) + ->string($response->getReasonPhrase()) + ->isEqualTo('OK') + ->string($response->getBody()->__toString()) + ->isEqualTo('The answer is 42') + ->boolean($response->cached) + ->isTrue() + ->mock($cacheMock) + ->call('has') + ->withArguments(md5($request->getUri())) + ->once() + ->call('get') + ->withArguments(md5($request->getUri())) + ->once() + ->call('ttl') + ->never() + ->mock($curlFactoryMock) + ->call('release') + ->never() + ; + + } + protected function getSerializedResponse(Response $response) { $cached = new \SplFixedArray(5);