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

Commit aa97b59

Browse files
authored
Merge pull request #7 from andreicio/master
Injectable naming strategy and extentable services.
2 parents a6a8716 + 5e3fc23 commit aa97b59

File tree

6 files changed

+124
-13
lines changed

6 files changed

+124
-13
lines changed

src/Adapter/DoctrineAdapter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Csa\GuzzleHttp\Middleware\Cache\Adapter;
1313

1414
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\HashNamingStrategy;
15+
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
1516
use Doctrine\Common\Cache\Cache;
1617
use GuzzleHttp\Psr7\Response;
1718
use Psr\Http\Message\RequestInterface;
@@ -23,10 +24,15 @@ class DoctrineAdapter implements StorageAdapterInterface
2324
private $namingStrategy;
2425
private $ttl;
2526

26-
public function __construct(Cache $cache, $ttl = 0)
27+
/**
28+
* @param Cache $cache
29+
* @param int $ttl
30+
* @param NamingStrategyInterface|null $namingStrategy
31+
*/
32+
public function __construct(Cache $cache, $ttl = 0, NamingStrategyInterface $namingStrategy = null)
2733
{
2834
$this->cache = $cache;
29-
$this->namingStrategy = new HashNamingStrategy();
35+
$this->namingStrategy = $namingStrategy ?: new HashNamingStrategy();
3036
$this->ttl = $ttl;
3137
}
3238

@@ -52,7 +58,7 @@ public function save(RequestInterface $request, ResponseInterface $response)
5258
$data = [
5359
'status' => $response->getStatusCode(),
5460
'headers' => $response->getHeaders(),
55-
'body' => (string) $response->getBody(),
61+
'body' => (string)$response->getBody(),
5662
'version' => $response->getProtocolVersion(),
5763
'reason' => $response->getReasonPhrase(),
5864
];

src/Adapter/MockStorageAdapter.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ class MockStorageAdapter implements StorageAdapterInterface
3333

3434
/**
3535
* @param string $storagePath
36-
* @param array $requestHeadersBlacklist
37-
* @param array $responseHeadersBlacklist
36+
* @param array $requestHeadersBlacklist
37+
* @param array $responseHeadersBlacklist
38+
* @param NamingStrategyInterface|null $namingStrategy
3839
*/
39-
public function __construct($storagePath, array $requestHeadersBlacklist = [], array $responseHeadersBlacklist = [])
40+
public function __construct($storagePath, array $requestHeadersBlacklist = [], array $responseHeadersBlacklist = [], NamingStrategyInterface $namingStrategy = null)
4041
{
4142
$this->storagePath = $storagePath;
4243

43-
$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist);
44-
$this->namingStrategies[] = new LegacyNamingStrategy(true, $requestHeadersBlacklist);
45-
$this->namingStrategies[] = new LegacyNamingStrategy(false, $requestHeadersBlacklist);
44+
if ($namingStrategy) {
45+
$this->namingStrategies[] = $namingStrategy;
46+
} else {
47+
$this->namingStrategies[] = new SubfolderNamingStrategy($requestHeadersBlacklist);
48+
$this->namingStrategies[] = new LegacyNamingStrategy(true, $requestHeadersBlacklist);
49+
$this->namingStrategies[] = new LegacyNamingStrategy(false, $requestHeadersBlacklist);
50+
}
4651

4752
if (!empty($responseHeadersBlacklist)) {
4853
$this->responseHeadersBlacklist = $responseHeadersBlacklist;

src/Adapter/PsrAdapter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Csa\GuzzleHttp\Middleware\Cache\Adapter;
1313

1414
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\HashNamingStrategy;
15+
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
1516
use GuzzleHttp\Psr7\Response;
1617
use Psr\Cache\CacheItemPoolInterface;
1718
use Psr\Http\Message\RequestInterface;
@@ -23,10 +24,15 @@ class PsrAdapter implements StorageAdapterInterface
2324
private $namingStrategy;
2425
private $ttl;
2526

26-
public function __construct(CacheItemPoolInterface $cache, $ttl = 0)
27+
/**
28+
* @param CacheItemPoolInterface $cache
29+
* @param int $ttl
30+
* @param NamingStrategyInterface|null $namingStrategy
31+
*/
32+
public function __construct(CacheItemPoolInterface $cache, $ttl = 0, NamingStrategyInterface $namingStrategy = null)
2733
{
2834
$this->cache = $cache;
29-
$this->namingStrategy = new HashNamingStrategy();
35+
$this->namingStrategy = $namingStrategy ?: new HashNamingStrategy();
3036
$this->ttl = $ttl;
3137
}
3238

tests/Adapter/DoctrineAdapterTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;
1313

1414
use Csa\GuzzleHttp\Middleware\Cache\Adapter\DoctrineAdapter;
15+
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
1516
use GuzzleHttp\Psr7\Request;
1617
use GuzzleHttp\Psr7\Response;
1718
use Psr\Http\Message\ResponseInterface;
@@ -78,11 +79,41 @@ public function testSave()
7879
10
7980
);
8081
$adapter = new $this->class($cache, 10);
81-
$adapter->save($this->getRequestMock(), new Response(200, [], 'Hello World'));
82+
$adapter->save($this->getRequestMock(), $this->getResponseMock());
83+
}
84+
85+
public function testFetchWithInjectedNamingStrategy()
86+
{
87+
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
88+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
89+
$request = $this->getRequestMock();
90+
$adapter = new $this->class($cache, 0, $namingStrategy);
91+
92+
$namingStrategy->expects($this->once())->method('filename')->with($request);
93+
94+
$adapter->fetch($request);
95+
}
96+
97+
public function testSaveWithInjectedNamingStrategy()
98+
{
99+
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
100+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
101+
$request = $this->getRequestMock();
102+
$response = $this->getResponseMock();
103+
$adapter = new $this->class($cache, 0, $namingStrategy);
104+
105+
$namingStrategy->expects($this->once())->method('filename')->with($request);
106+
107+
$adapter->save($request, $response);
82108
}
83109

84110
private function getRequestMock()
85111
{
86112
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);
87113
}
114+
115+
private function getResponseMock()
116+
{
117+
return new Response(200, [], 'Hello World');
118+
}
88119
}

tests/Adapter/MockStorageAdapterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;
1313

1414
use Csa\GuzzleHttp\Middleware\Cache\Adapter\MockStorageAdapter;
15+
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
1516
use GuzzleHttp\Psr7\Request;
1617
use GuzzleHttp\Psr7\Response;
1718
use Psr\Http\Message\ResponseInterface;
@@ -96,6 +97,28 @@ public function testSaveWithSubResource()
9697
$this->assertFalse($response->hasHeader('X-Foo'));
9798
}
9899

100+
public function testFetchWithInjectedNamingStrategy()
101+
{
102+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
103+
$request = $this->getRequestMock();
104+
$adapter = new $this->class($this->tmpDir, [], [], $namingStrategy);
105+
106+
$namingStrategy->expects($this->once())->method('filename')->with($request);
107+
108+
$adapter->fetch($request);
109+
}
110+
111+
public function testSaveWithInjectedNamingStrategy()
112+
{
113+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
114+
$request = $this->getRequestMock();
115+
$adapter = new $this->class($this->tmpDir, [], [], $namingStrategy);
116+
117+
$namingStrategy->expects($this->once())->method('filename')->with($request);
118+
119+
$adapter->save($request, new Response());
120+
}
121+
99122
private function getRequestMock()
100123
{
101124
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);

tests/Adapter/PsrAdapterTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter;
1313

1414
use Csa\GuzzleHttp\Middleware\Cache\Adapter\PsrAdapter;
15+
use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface;
1516
use GuzzleHttp\Psr7\Request;
1617
use GuzzleHttp\Psr7\Response;
1718
use Psr\Cache\CacheItemInterface;
@@ -97,11 +98,50 @@ public function testSave()
9798
->with($item)
9899
;
99100
$adapter = new PsrAdapter($cache, 10);
100-
$adapter->save($this->getRequestMock(), new Response(200, [], 'Hello World'));
101+
$adapter->save($this->getRequestMock(), $this->getResponseMock());
102+
}
103+
104+
public function testFetchWithInjectedNamingStrategy()
105+
{
106+
$cache = $this->getCacheMock();
107+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
108+
$request = $this->getRequestMock();
109+
$adapter = new PsrAdapter($cache, 0, $namingStrategy);
110+
111+
$namingStrategy->expects($this->once())->method('filename')->with($request);
112+
113+
$adapter->fetch($request);
114+
}
115+
116+
public function testSaveWithInjectedNamingStrategy()
117+
{
118+
$cache = $this->getCacheMock();
119+
$namingStrategy = $this->getMock(NamingStrategyInterface::class);
120+
$request = $this->getRequestMock();
121+
$response = $this->getResponseMock();
122+
$adapter = new PsrAdapter($cache, 0, $namingStrategy);
123+
124+
$namingStrategy->expects($this->once())->method('filename')->with($request);
125+
126+
$adapter->save($request, $response);
101127
}
102128

103129
private function getRequestMock()
104130
{
105131
return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']);
106132
}
133+
134+
private function getResponseMock()
135+
{
136+
return new Response(200, [], 'Hello World');
137+
}
138+
139+
private function getCacheMock()
140+
{
141+
$item = $this->getMock(CacheItemInterface::class);
142+
$cache = $this->getMock(CacheItemPoolInterface::class);
143+
$cache->method('getItem')->willReturn($item);
144+
145+
return $cache;
146+
}
107147
}

0 commit comments

Comments
 (0)