Skip to content

Commit

Permalink
normalized method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
clphillips committed Mar 6, 2015
1 parent f722190 commit 73ab664
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct($cache_dir, $cache_dir_perms = 0755, $cache_ext = nu
*
* @param string $path The path within cache directory to empty
*/
public function emptyCache($path = null)
public function clear($path = null)
{
if (!($dir = @opendir($this->cache_dir . $path))) {
return;
Expand All @@ -60,7 +60,7 @@ public function emptyCache($path = null)
* @return boolean True if removed, false if no such file exists
* @see Cache::cacheName
*/
public function clearCache($name, $path = null)
public function remove($name, $path = null)
{
$cache = $this->cacheName($name, $path);
if (is_file($cache)) {
Expand All @@ -80,7 +80,7 @@ public function clearCache($name, $path = null)
* @param string $path The path within cache directory to write the file to
* @see Cache::cacheName
*/
public function writeCache($name, $output, $ttl, $path = null)
public function write($name, $output, $ttl, $path = null)
{
$cache = $this->cacheName($name, $path);

Expand All @@ -105,7 +105,7 @@ public function writeCache($name, $output, $ttl, $path = null)
* exists and has not yet expired, false otherwise.
* @see Cache::cacheName
*/
public function fetchCache($name, $path = null)
public function fetch($name, $path = null)
{
$cache = $this->cacheName($name, $path);
if (file_exists($cache) && filemtime($cache) > time()) {
Expand Down
78 changes: 40 additions & 38 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class CacheTest extends PHPUnit_Framework_TestCase
protected function setUp()
{
$this->cache_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "Fixtures" . DIRECTORY_SEPARATOR;
if (!is_dir($this->cache_dir)) {
mkdir($this->cache_dir, 0777, true);
}
$this->cache = new Cache($this->cache_dir);
chmod($this->cache_dir, 0777);
}

/**
Expand All @@ -24,93 +26,93 @@ protected function setUp()
*/
protected function tearDown()
{
$this->cache->emptyCache();
$this->cache->clear();
}

/**
* @covers ::emptyCache
* @covers ::clear
*/
public function testEmptyCache()
public function testClear()
{
file_put_contents($this->cache_dir . "testfile", "CacheTest::testEmptyCache");
file_put_contents($this->cache_dir . "testfile", "CacheTest::testClear");
$this->assertFileExists($this->cache_dir . "testfile");

$this->cache->emptyCache("bad/sub/path/");
$this->cache->clear("bad/sub/path/");
$this->assertFileExists($this->cache_dir . "testfile");

$this->cache->emptyCache();
$this->cache->clear();
$this->assertFileNotExists($this->cache_dir . "testfile");

mkdir($this->cache_dir . "sub/path", 0777, true);
file_put_contents($this->cache_dir . "sub/path/testfile", "CacheTest::testEmptyCache");
$this->assertFileExists($this->cache_dir . "sub/path/testfile");
$this->cache->emptyCache("sub/path/");
$this->cache->clear("sub/path/");
$this->assertFileNotExists($this->cache_dir . "sub/path/testfile");
rmdir($this->cache_dir . "sub/path");
rmdir($this->cache_dir . "sub");
}

/**
* @covers ::clearCache
* @covers ::remove
* @covers ::cacheName
* @uses \minphp\Cache\Cache::fetchCache
* @uses \minphp\Cache\Cache::writeCache
* @uses \minphp\Cache\Cache::fetch
* @uses \minphp\Cache\Cache::write
*/
public function testClearCache()
public function testRemove()
{
$cache_name = "testfile";
$cache_contents = "CacheTest::testClearCache";
$this->assertFalse($this->cache->clearCache("bad_file_name"));
$cache_contents = "CacheTest::testRemove";
$this->assertFalse($this->cache->remove("bad_file_name"));

$this->cache->writeCache($cache_name, $cache_contents, 10);
$this->assertEquals($cache_contents, $this->cache->fetchCache($cache_name));
$this->cache->write($cache_name, $cache_contents, 10);
$this->assertEquals($cache_contents, $this->cache->fetch($cache_name));

$this->assertTrue($this->cache->clearCache($cache_name));
$this->assertTrue($this->cache->remove($cache_name));

$this->assertFalse($this->cache->fetchCache($cache_name));
$this->assertFalse($this->cache->fetch($cache_name));
}

/**
* @covers ::writeCache
* @covers ::write
* @covers ::cacheName
* @uses \minphp\Cache\Cache::fetchCache
* @uses \minphp\Cache\Cache::clearCache
* @uses \minphp\Cache\Cache::fetch
* @uses \minphp\Cache\Cache::remove
*/
public function testWriteCache()
{
$cache_name = "testfile";
$cache_contents = "CacheTest::testWriteCache";
$cache_contents = "CacheTest::testWrite";

$this->cache->writeCache($cache_name, $cache_contents, -1);
$this->assertFalse($this->cache->fetchCache($cache_name));
$this->cache->write($cache_name, $cache_contents, -1);
$this->assertFalse($this->cache->fetch($cache_name));

$this->assertTrue($this->cache->clearCache($cache_name));
$this->assertTrue($this->cache->remove($cache_name));

$this->cache->writeCache($cache_name, $cache_contents, 1);
$this->assertEquals($cache_contents, $this->cache->fetchCache($cache_name));
$this->cache->write($cache_name, $cache_contents, 1);
$this->assertEquals($cache_contents, $this->cache->fetch($cache_name));

$this->assertTrue($this->cache->clearCache($cache_name));
$this->assertTrue($this->cache->remove($cache_name));
}

/**
* @covers ::fetchCache
* @covers ::fetch
* @covers ::cacheName
* @uses \minphp\Cache\Cache::writeCache
* @uses \minphp\Cache\Cache::clearCache
* @uses \minphp\Cache\Cache::write
* @uses \minphp\Cache\Cache::remove
*/
public function testFetchCache()
{
$cache_name = "testfile";
$cache_contents = "CacheTest::testFetchCache";
$cache_contents = "CacheTest::testFetch";

$this->cache->writeCache($cache_name, $cache_contents, -1);
$this->assertFalse($this->cache->fetchCache($cache_name));
$this->cache->write($cache_name, $cache_contents, -1);
$this->assertFalse($this->cache->fetch($cache_name));

$this->assertTrue($this->cache->clearCache($cache_name));
$this->assertTrue($this->cache->remove($cache_name));

$this->cache->writeCache($cache_name, $cache_contents, 1);
$this->assertEquals($cache_contents, $this->cache->fetchCache($cache_name));
$this->cache->write($cache_name, $cache_contents, 1);
$this->assertEquals($cache_contents, $this->cache->fetch($cache_name));

$this->assertTrue($this->cache->clearCache($cache_name));
$this->assertTrue($this->cache->remove($cache_name));
}
}

0 comments on commit 73ab664

Please sign in to comment.