This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic tests. Using constructor injection in CacheStorage for
keyPrefix rather than setter injection.
- Loading branch information
Showing
5 changed files
with
109 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
colors="true"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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
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,25 @@ | ||
<?php | ||
namespace GuzzleHttp\Tests\Subscriber\Cache; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Subscriber\Cache\CacheSubscriber; | ||
|
||
class CacheSubscriberTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreatesAndAttachedDefaultSubscriber() | ||
{ | ||
$client = new Client(); | ||
$cache = CacheSubscriber::attach($client); | ||
$this->assertArrayHasKey('subscriber', $cache); | ||
$this->assertArrayHasKey('storage', $cache); | ||
$this->assertInstanceOf( | ||
'GuzzleHttp\Subscriber\Cache\CacheStorage', | ||
$cache['storage'] | ||
); | ||
$this->assertInstanceOf( | ||
'GuzzleHttp\Subscriber\Cache\CacheSubscriber', | ||
$cache['subscriber'] | ||
); | ||
$this->assertTrue($client->getEmitter()->hasListeners('error')); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
namespace GuzzleHttp\Tests\Subscriber\Cache; | ||
|
||
require_once __DIR__ . '/../vendor/guzzlehttp/ringphp/tests/Client/Server.php'; | ||
require_once __DIR__ . '/../vendor/guzzlehttp/guzzle/tests/Server.php'; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Message\Response; | ||
use GuzzleHttp\Subscriber\Cache\CacheSubscriber; | ||
use GuzzleHttp\Subscriber\History; | ||
use GuzzleHttp\Tests\Server; | ||
|
||
class IntegrationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public static function setupBeforeClass() | ||
{ | ||
Server::start(); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
Server::stop(); | ||
} | ||
|
||
public function testCachesResponses() | ||
{ | ||
Server::enqueue([ | ||
new Response(200, [ | ||
'Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM', | ||
'Date' => 'Wed, 29 Oct 2014 20:52:15 GMT', | ||
'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate', | ||
'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT', | ||
'Age' => '1277' | ||
]), | ||
new Response(304, [ | ||
'Content-Type' => 'text/html; charset=UTF-8', | ||
'Vary' => 'Accept-Encoding,Cookie,X-Use-HHVM', | ||
'Date' => 'Wed, 29 Oct 2014 20:52:16 GMT', | ||
'Cache-Control' => 'private, s-maxage=0, max-age=0, must-revalidate', | ||
'Last-Modified' => 'Wed, 29 Oct 2014 20:30:57 GMT', | ||
'Age' => '1278' | ||
]) | ||
]); | ||
|
||
$client = new Client(['base_url' => Server::$url]); | ||
CacheSubscriber::attach($client); | ||
$history = new History(); | ||
$client->getEmitter()->attach($history); | ||
$response1 = $client->get('/foo'); | ||
$this->assertEquals(200, $response1->getStatusCode()); | ||
$response2 = $client->get('/foo'); | ||
$this->assertEquals(200, $response2->getStatusCode()); | ||
$this->assertCount(2, Server::received()); | ||
$last = $history->getLastResponse(); | ||
$this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache-Lookup')); | ||
$this->assertEquals('HIT from GuzzleCache', $last->getHeader('X-Cache')); | ||
} | ||
} |