|
12 | 12 | namespace Csa\Tests\GuzzleHttp\Middleware\Cache\Adapter; |
13 | 13 |
|
14 | 14 | use Csa\GuzzleHttp\Middleware\Cache\Adapter\PsrAdapter; |
| 15 | +use Csa\GuzzleHttp\Middleware\Cache\NamingStrategy\NamingStrategyInterface; |
15 | 16 | use GuzzleHttp\Psr7\Request; |
16 | 17 | use GuzzleHttp\Psr7\Response; |
17 | 18 | use Psr\Cache\CacheItemInterface; |
@@ -97,11 +98,50 @@ public function testSave() |
97 | 98 | ->with($item) |
98 | 99 | ; |
99 | 100 | $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); |
101 | 127 | } |
102 | 128 |
|
103 | 129 | private function getRequestMock() |
104 | 130 | { |
105 | 131 | return new Request('GET', 'http://google.com/', ['Accept' => 'text/html']); |
106 | 132 | } |
| 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 | + } |
107 | 147 | } |
0 commit comments