Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update APC adpater

  • Loading branch information...
commit 287cc6f31f9aabed320b217ab6d00f1d0156ab14 1 parent 57831a9
@Herzult Herzult authored
Showing with 33 additions and 108 deletions.
  1. +21 −3 src/Gaufrette/Adapter/Apc.php
  2. +12 −105 tests/Gaufrette/Adapter/ApcTest.php
View
24 src/Gaufrette/Adapter/Apc.php
@@ -3,12 +3,13 @@
namespace Gaufrette\Adapter;
use Gaufrette\Checksum;
+use Gaufrette\Exception;
/**
* Apc adapter, a non-persistent adapter for when this sort of thing is appropriate
*
- * @package Gaufrette
- * @author Alexander Deruwe <alexander.deruwe@gmail.com>
+ * @author Alexander Deruwe <alexander.deruwe@gmail.com>
+ * @author Antoine Hérault <antoine.herault@gmail.com>
*/
class Apc extends Base
{
@@ -25,7 +26,7 @@ class Apc extends Base
public function __construct($prefix, $ttl = 0)
{
if (!extension_loaded('apc')) {
- throw new \RuntimeException('Unable to use Gaufrette\\Adapter\\Apc as the APC extension is not available.');
+ throw new \RuntimeException('Unable to use Gaufrette\Adapter\Apc as the APC extension is not available.');
}
$this->prefix = $prefix;
@@ -37,6 +38,8 @@ public function __construct($prefix, $ttl = 0)
*/
public function read($key)
{
+ $this->assertExists($key);
+
$content = apc_fetch($this->computePath($key));
if (false === $content) {
@@ -94,6 +97,8 @@ public function keys()
*/
public function mtime($key)
{
+ $this->assertExists($key);
+
$pattern = sprintf('/^%s/', preg_quote($this->prefix . $key));
$cachedKeys = iterator_to_array(new \APCIterator('user', $pattern, APC_ITER_MTIME));
@@ -105,6 +110,8 @@ public function mtime($key)
*/
public function checksum($key)
{
+ $this->assertExists($key);
+
return Checksum::fromContent($this->read($key));
}
@@ -113,6 +120,8 @@ public function checksum($key)
*/
public function delete($key)
{
+ $this->assertExists($key);
+
$result = apc_delete($this->computePath($key));
if (false === $result) {
@@ -125,6 +134,8 @@ public function delete($key)
*/
public function rename($key, $new)
{
+ $this->assertExists($key);
+
try {
// TODO: this probably allows for race conditions...
$content = $this->read($key);
@@ -145,4 +156,11 @@ public function computePath($key)
{
return $this->prefix . $key;
}
+
+ private function assertExists($key)
+ {
+ if (!$this->exists($key)) {
+ throw new Exception\FileNotFound($key);
+ }
+ }
}
View
117 tests/Gaufrette/Adapter/ApcTest.php
@@ -2,127 +2,34 @@
namespace Gaufrette\Adapter;
-class ApcTest extends \PHPUnit_Framework_TestCase
+class ApcTest extends FunctionalTestCase
{
- const PREFIX = 'test-suite.';
- const KEY = 'test-key';
- const CONTENT = 'Yummy, some test content!';
-
protected function setUp()
{
if (!extension_loaded('apc')) {
- $this->markTestSkipped('The APC extension is not available.');
+ return $this->markTestSkipped('The APC extension is not available.');
+ } elseif (!ini_get('apc.enabled') || !ini_get('apc.enable_cli')) {
+ return $this->markTestSkipped('The APC extension is available, but not enabled.');
}
- if (!(ini_get('apc.enabled') && ini_get('apc.enable_cli'))) {
- $this->markTestSkipped('The APC extension is available, but not enabled.');
- } else {
- apc_clear_cache('user');
- }
+ apc_clear_cache('user');
+
+ $this->adapter = new Apc('gaufrette-test.');
}
protected function tearDown()
{
- if (ini_get('apc.enabled') && ini_get('apc.enable_cli')) {
- apc_clear_cache('user');
+ if (null === $this->adapter) {
+ return;
}
- }
-
- public function testComputePath()
- {
- $adapter = new Apc(self::PREFIX);
-
- $this->assertEquals(self::PREFIX . 'foobar', $adapter->computePath('foobar'));
- }
-
- /**
- * @depends testComputePath
- */
- public function testWriteAndRead()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write(self::KEY, self::CONTENT);
- $this->assertEquals(self::CONTENT, $adapter->read(self::KEY));
- }
+ apc_clear_cache('user');
- /**
- * @depends testWriteAndRead
- */
- public function testExists()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write(self::KEY, null);
-
- $this->assertTrue($adapter->exists(self::KEY));
- $this->assertFalse($adapter->exists('non-existing-key'));
- }
-
- /**
- * @depends testWriteAndRead
- */
- public function testKeys()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write('test-key1', null);
- $adapter->write('test-key2', null);
- $adapter->write(self::PREFIX, null);
-
- $this->assertEquals(array('test-key1', 'test-key2', self::PREFIX), $adapter->keys());
- }
-
- /**
- * @depends testWriteAndRead
- */
- public function testMtime()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write(self::KEY, null);
-
- $this->assertTrue(time() >= $adapter->mtime(self::KEY));
- }
-
- /**
- * @depends testWriteAndRead
- */
- public function testChecksum()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write(self::KEY, self::CONTENT);
-
- $this->assertEquals(md5(self::CONTENT), $adapter->checksum(self::KEY));
- }
-
- /**
- * @depends testWriteAndRead
- */
- public function testDelete()
- {
- $adapter = new Apc(self::PREFIX);
-
- $adapter->write(self::KEY, null);
- $adapter->delete(self::KEY);
-
- $this->assertFalse($adapter->exists(self::KEY));
- }
-
- /**
- * @depends testWriteAndRead
- */
- public function testRename()
- {
- $adapter = new Apc(self::PREFIX);
- $adapter->write(self::KEY, null);
- $adapter->rename(self::KEY, 'new-key');
-
- $this->assertTrue($adapter->exists('new-key'));
- $this->assertFalse($adapter->exists(self::KEY));
+ $this->adapter = null;
}
public function testSupportsMetadata()
{
- $adapter = new Apc(self::PREFIX);
-
- $this->assertFalse($adapter->supportsMetadata());
+ $this->assertFalse($this->adapter->supportsMetadata());
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.