-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from bpolaszek/null-pattern
Null object pattern
- Loading branch information
Showing
4 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,5 +41,4 @@ public function save($key, CacheEntry $data) | |
|
||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Kevinrob\GuzzleCache\Strategy; | ||
|
||
use Kevinrob\GuzzleCache\CacheEntry; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class NullCacheStrategy implements CacheStrategyInterface | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function fetch(RequestInterface $request) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function cache(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function update(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Kevinrob\GuzzleCache; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\HandlerStack; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Psr7\Response; | ||
use Kevinrob\GuzzleCache\Strategy\NullCacheStrategy; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class NullCacheTest extends TestCase | ||
{ | ||
|
||
public function testCacheIsNeverHit() | ||
{ | ||
$stack = HandlerStack::create(new MockHandler([ | ||
new Response(), | ||
new Response(), | ||
])); | ||
|
||
$stack->push(new CacheMiddleware(new NullCacheStrategy())); | ||
|
||
$request = new Request('GET', '/foo'); | ||
|
||
$client = new Client([ | ||
'handler' => $stack | ||
]); | ||
$client->send($request); // Will not be cached | ||
$response = $client->send($request); // Will not come from cache | ||
|
||
$this->assertEquals(CacheMiddleware::HEADER_CACHE_MISS, $response->getHeaderLine(CacheMiddleware::HEADER_CACHE_INFO)); | ||
} | ||
|
||
} |