Skip to content

add support for Gaufrette cache #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 9, 2013
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"require-dev": {
"phpunit/phpunit": ">=3.6.0"
},
"suggest": {
"knplabs/gaufrette": "Needed for optional Gaufrette cache"
},
"autoload": {
"psr-0": { "Github\\": "lib/" }
},
Expand Down
57 changes: 57 additions & 0 deletions lib/Github/HttpClient/Cache/GaufretteCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Github\HttpClient\Cache;

use Guzzle\Http\Message\Response;
use Gaufrette\Filesystem;

/**
* Gaufrette Cache
*
* @author Massimiliano Arione <massimiliano.arione@bee-lab.net>
*/
class GaufretteCache implements CacheInterface
{
/**
* @var Filesystem
*/
protected $filesystem;

/**
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}

/**
* {@inheritdoc}
*/
public function get($id)
{
$content = $this->filesystem->read($id);

return unserialize($content);
}

/**
* {@inheritdoc}
*/
public function set($id, Response $response)
{
$this->filesystem->write($id, serialize($response), true);
}

/**
* {@inheritdoc}
*/
public function getModifiedSince($id)
{
if ($this->filesystem->has($id)) {
return $this->filesystem->mtime($id);
}

return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove that because void functions always are null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just followed FilesystemCache behaviour. Anyway, I agree.

}
}
84 changes: 84 additions & 0 deletions test/Github/Tests/HttpClient/Cache/GaufretteCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Github\Tests\HttpClient\Cache;

use Guzzle\Http\Message\Response;
use Github\HttpClient\Cache\GaufretteCache;

class GaufretteCacheTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!class_exists('Gaufrette\Filesystem')) {
$this->markTestSkipped('Gaufrette not installed.');
}
}

/**
* @test
*/
public function shouldStoreAResponseForAGivenKey()
{
$response = new Response(200);
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
$filesystem
->expects($this->once())
->method('write')
->with('test', serialize($response))
;
$filesystem
->expects($this->once())
->method('read')
->with('test')
->will($this->returnValue('a:0:{}'))
;

$cache = new GaufretteCache($filesystem);
$cache->set('test', $response);
$this->assertNotNull($cache->get('test'));
}

/**
* @test
*/
public function shouldGetATimestampForExistingFile()
{
$response = new Response(200);
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
$filesystem
->expects($this->once())
->method('has')
->with('test')
->will($this->returnValue(true))
;
$filesystem
->expects($this->once())
->method('mtime')
->with('test')
->will($this->returnValue(100))
;

$cache = new GaufretteCache($filesystem);
$cache->set('test', new Response(200));

$this->assertInternalType('int', $cache->getModifiedSince('test'));
}

/**
* @test
*/
public function shouldNotGetATimestampForInexistingFile()
{
$filesystem = $this->getMockBuilder('Gaufrette\Filesystem')->disableOriginalConstructor()->getMock();
$filesystem
->expects($this->once())
->method('has')
->with('test2')
->will($this->returnValue(false))
;

$cache = new GaufretteCache($filesystem);

$this->assertNull($cache->getModifiedSince('test2'));
}
}