Skip to content

Commit

Permalink
Merge pull request #8 from M6Web/fix/redis-calls
Browse files Browse the repository at this point in the history
Disable call to cache ttl method when not in dev environment
  • Loading branch information
mojoLyon committed Jul 17, 2015
2 parents d5c5c8f + d67e70f commit 14a82b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Handler/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ trait CacheTrait
*/
protected $useHeaderTtl;

/**
* @var boolean
*/
protected $debug = false;

/**
* @var array
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
45 changes: 45 additions & 0 deletions tests/Units/Handler/CurlMultiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -90,6 +91,7 @@ public function testCacheGet()
->boolean($response->cached)
->isTrue()
->integer($response->cacheTtl)
->isEqualTo(256)
->mock($cacheMock)
->call('has')
->withArguments(md5($request->getUri()))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 14a82b6

Please sign in to comment.