Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from andreicio/master
Browse files Browse the repository at this point in the history
Injectable naming strategy and extentable services.
  • Loading branch information
csarrazi authored Aug 12, 2019
2 parents a6a8716 + 5e3fc23 commit aa97b59
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 13 deletions.
12 changes: 9 additions & 3 deletions src/Adapter/DoctrineAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Csa\GuzzleHttp\Middleware\Cache\Adapter;

use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\HashNamingStrategy;
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
use Doctrine\Common\Cache\Cache;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
Expand All @@ -23,10 +24,15 @@ class DoctrineAdapter implements StorageAdapterInterface
private $namingStrategy;
private $ttl;

public function __construct(Cache $cache, $ttl = 0)
/**
* @param Cache $cache
* @param int $ttl
* @param NamingStrategyInterface|null $namingStrategy
*/
public function __construct(Cache $cache, $ttl = 0, NamingStrategyInterface $namingStrategy = null)
{
$this->cache = $cache;
$this->namingStrategy = new HashNamingStrategy();
$this->namingStrategy = $namingStrategy ?: new HashNamingStrategy();
$this->ttl = $ttl;
}

Expand All @@ -52,7 +58,7 @@ public function save(RequestInterface $request, ResponseInterface $response)
$data = [
'status' => $response->getStatusCode(),
'headers' => $response->getHeaders(),
'body' => (string) $response->getBody(),
'body' => (string)$response->getBody(),
'version' => $response->getProtocolVersion(),
'reason' => $response->getReasonPhrase(),
];
Expand Down
17 changes: 11 additions & 6 deletions src/Adapter/MockStorageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ class MockStorageAdapter implements StorageAdapterInterface

/**
* @param string $storagePath
* @param array $requestHeadersBlacklist
* @param array $responseHeadersBlacklist
* @param array $requestHeadersBlacklist
* @param array $responseHeadersBlacklist
* @param NamingStrategyInterface|null $namingStrategy
*/
public function __construct($storagePath, array $requestHeadersBlacklist = [], array $responseHeadersBlacklist = [])
public function __construct($storagePath, array $requestHeadersBlacklist = [], array $responseHeadersBlacklist = [], NamingStrategyInterface $namingStrategy = null)
{
$this->storagePath = $storagePath;

$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist);
$this->namingStrategies[] = new LegacyNamingStrategy(true, $requestHeadersBlacklist);
$this->namingStrategies[] = new LegacyNamingStrategy(false, $requestHeadersBlacklist);
if ($namingStrategy) {
$this->namingStrategies[] = $namingStrategy;
} else {
$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist);
$this->namingStrategies[] = new LegacyNamingStrategy(true, $requestHeadersBlacklist);
$this->namingStrategies[] = new LegacyNamingStrategy(false, $requestHeadersBlacklist);
}

if (!empty($responseHeadersBlacklist)) {
$this->responseHeadersBlacklist = $responseHeadersBlacklist;
Expand Down
10 changes: 8 additions & 2 deletions src/Adapter/PsrAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Csa\GuzzleHttp\Middleware\Cache\Adapter;

use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\HashNamingStrategy;
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
use GuzzleHttp\Psr7\Response;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
Expand All @@ -23,10 +24,15 @@ class PsrAdapter implements StorageAdapterInterface
private $namingStrategy;
private $ttl;

public function __construct(CacheItemPoolInterface $cache, $ttl = 0)
/**
* @param CacheItemPoolInterface $cache
* @param int $ttl
* @param NamingStrategyInterface|null $namingStrategy
*/
public function __construct(CacheItemPoolInterface $cache, $ttl = 0, NamingStrategyInterface $namingStrategy = null)
{
$this->cache = $cache;
$this->namingStrategy = new HashNamingStrategy();
$this->namingStrategy = $namingStrategy ?: new HashNamingStrategy();
$this->ttl = $ttl;
}

Expand Down
33 changes: 32 additions & 1 deletion tests/Adapter/DoctrineAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;

use Csa\GuzzleHttp\Middleware\Cache\Adapter\DoctrineAdapter;
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -78,11 +79,41 @@ public function testSave()
10
);
$adapter = new $this->class($cache, 10);
$adapter->save($this->getRequestMock(), new Response(200, [], 'Hello World'));
$adapter->save($this->getRequestMock(), $this->getResponseMock());
}

public function testFetchWithInjectedNamingStrategy()
{
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$adapter = new $this->class($cache, 0, $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->fetch($request);
}

public function testSaveWithInjectedNamingStrategy()
{
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$response = $this->getResponseMock();
$adapter = new $this->class($cache, 0, $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->save($request, $response);
}

private function getRequestMock()
{
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);
}

private function getResponseMock()
{
return new Response(200, [], 'Hello World');
}
}
23 changes: 23 additions & 0 deletions tests/Adapter/MockStorageAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;

use Csa\GuzzleHttp\Middleware\Cache\Adapter\MockStorageAdapter;
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -96,6 +97,28 @@ public function testSaveWithSubResource()
$this->assertFalse($response->hasHeader('X-Foo'));
}

public function testFetchWithInjectedNamingStrategy()
{
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$adapter = new $this->class($this->tmpDir, [], [], $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->fetch($request);
}

public function testSaveWithInjectedNamingStrategy()
{
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$adapter = new $this->class($this->tmpDir, [], [], $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->save($request, new Response());
}

private function getRequestMock()
{
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);
Expand Down
42 changes: 41 additions & 1 deletion tests/Adapter/PsrAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;

use Csa\GuzzleHttp\Middleware\Cache\Adapter\PsrAdapter;
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Cache\CacheItemInterface;
Expand Down Expand Up @@ -97,11 +98,50 @@ public function testSave()
->with($item)
;
$adapter = new PsrAdapter($cache, 10);
$adapter->save($this->getRequestMock(), new Response(200, [], 'Hello World'));
$adapter->save($this->getRequestMock(), $this->getResponseMock());
}

public function testFetchWithInjectedNamingStrategy()
{
$cache = $this->getCacheMock();
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$adapter = new PsrAdapter($cache, 0, $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->fetch($request);
}

public function testSaveWithInjectedNamingStrategy()
{
$cache = $this->getCacheMock();
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
$request = $this->getRequestMock();
$response = $this->getResponseMock();
$adapter = new PsrAdapter($cache, 0, $namingStrategy);

$namingStrategy->expects($this->once())->method('filename')->with($request);

$adapter->save($request, $response);
}

private function getRequestMock()
{
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);
}

private function getResponseMock()
{
return new Response(200, [], 'Hello World');
}

private function getCacheMock()
{
$item = $this->getMock(CacheItemInterface::class);
$cache = $this->getMock(CacheItemPoolInterface::class);
$cache->method('getItem')->willReturn($item);

return $cache;
}
}

0 comments on commit aa97b59

Please sign in to comment.