Skip to content
This repository has been archived by the owner on Jun 27, 2022. It is now read-only.

Commit

Permalink
Adding basic tests. Using constructor injection in CacheStorage for
Browse files Browse the repository at this point in the history
keyPrefix rather than setter injection.
  • Loading branch information
mtdowling committed Oct 29, 2014
1 parent 1380041 commit 9a16633
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 16 deletions.
14 changes: 14 additions & 0 deletions phpunit.xml.dist
@@ -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>
18 changes: 4 additions & 14 deletions src/CacheStorage.php
Expand Up @@ -25,11 +25,13 @@ class CacheStorage implements CacheStorageInterface
private $cache;

/**
* @param Cache $cache Cache backend
* @param Cache $cache Cache backend.
* @param string $keyPrefix Key prefix to add to each key.
*/
public function __construct(Cache $cache)
public function __construct(Cache $cache, $keyPrefix = null)
{
$this->cache = $cache;
$this->keyPrefix = $keyPrefix;
}

public function cache(
Expand Down Expand Up @@ -144,18 +146,6 @@ public function fetch(RequestInterface $request)
return $response;
}

/**
* Set the cache key prefix
*
* @param string $name
*
* @return void
*/
public function setPrefix($name)
{
$this->keyPrefix = $name;
}

/**
* Hash a request URL into a string that returns cache metadata
*
Expand Down
10 changes: 8 additions & 2 deletions src/CacheSubscriber.php
Expand Up @@ -2,7 +2,6 @@
namespace GuzzleHttp\Subscriber\Cache;

use GuzzleHttp\Event\HasEmitterInterface;
use GuzzleHttp\Message\MessageInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Event\BeforeEvent;
use GuzzleHttp\Event\CompleteEvent;
Expand Down Expand Up @@ -70,6 +69,10 @@ public function __construct(
*
* @param HasEmitterInterface $subject Client or request to attach to,
* @param array $options Options used to control the cache.
*
* @return array Returns an associative array containing a 'subscriber' key
* that holds the created CacheSubscriber, and a 'storage'
* key that contains the cache storage used by the subscriber.
*/
public static function attach(
HasEmitterInterface $subject,
Expand All @@ -87,7 +90,8 @@ public static function attach(
}

$emitter = $subject->getEmitter();
$emitter->attach(new self($options['storage'], $options['can_cache']));
$cache = new self($options['storage'], $options['can_cache']);
$emitter->attach($cache);

if (!isset($options['validate']) || $options['validate'] === true) {
$emitter->attach(new ValidationSubscriber(
Expand All @@ -99,6 +103,8 @@ public static function attach(
if (!isset($options['purge']) || $options['purge'] === true) {
$emitter->attach(new PurgeSubscriber($options['storage']));
}

return ['subscriber' => $cache, 'storage' => $options['storage']];
}

public function getEvents()
Expand Down
25 changes: 25 additions & 0 deletions tests/CacheSubscriberTest.php
@@ -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'));
}
}
58 changes: 58 additions & 0 deletions tests/IntegrationTest.php
@@ -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'));
}
}

0 comments on commit 9a16633

Please sign in to comment.