diff --git a/composer.json b/composer.json index ec95b7e0119..2ef7ee247d3 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,9 @@ "require-dev": { "phpunit/phpunit": ">=3.6.0" }, + "suggest": { + "knplabs/gaufrette": "Needed for optional Gaufrette cache" + }, "autoload": { "psr-0": { "Github\\": "lib/" } }, diff --git a/lib/Github/HttpClient/Cache/GaufretteCache.php b/lib/Github/HttpClient/Cache/GaufretteCache.php new file mode 100644 index 00000000000..d198f3d24ce --- /dev/null +++ b/lib/Github/HttpClient/Cache/GaufretteCache.php @@ -0,0 +1,55 @@ + + */ +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); + } + } +} diff --git a/test/Github/Tests/HttpClient/Cache/GaufretteCacheTest.php b/test/Github/Tests/HttpClient/Cache/GaufretteCacheTest.php new file mode 100644 index 00000000000..2a84f14ad54 --- /dev/null +++ b/test/Github/Tests/HttpClient/Cache/GaufretteCacheTest.php @@ -0,0 +1,84 @@ +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')); + } +}