Skip to content

Commit

Permalink
Changed namespace from minphp\Cache to Minphp\Cachel
Browse files Browse the repository at this point in the history
  • Loading branch information
clphillips committed Dec 11, 2015
1 parent 01ba546 commit b5af46c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# minphp/Cache
# Minphp/Cache

[![Build Status](https://travis-ci.org/phillipsdata/minphp-cache.svg?branch=master)](https://travis-ci.org/phillipsdata/minphp-cache)

Expand All @@ -9,7 +9,7 @@ Cache Library.
Install via composer:

```sh
composer require minphp/cache:dev-master
composer require minphp/cache
```

## Basic Usage
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"psr-4": {"minphp\\Cache\\": "src"}
"psr-4": {"Minphp\\Cache\\": "src"}
},
"autoload-dev": {
"psr-4": {"Minphp\\Cache\\Tests\\": "tests"}
}
}
20 changes: 10 additions & 10 deletions src/Cache.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace minphp\Cache;
namespace Minphp\Cache;

/**
* Handles writing to and from the cache
Expand All @@ -18,7 +18,7 @@ class Cache
* @var string The file extension for cache files
*/
protected $cache_ext;

/**
* Initialize the cache environment
*
Expand All @@ -32,7 +32,7 @@ public function __construct($cache_dir, $cache_dir_perms = 0755, $cache_ext = nu
$this->cache_dir_perms = $cache_dir_perms;
$this->cache_ext = $cache_ext;
}

/**
* Empties the entire cache of all files (directories excluded, not recursive)
*
Expand All @@ -43,14 +43,14 @@ public function clear($path = null)
if (!($dir = @opendir($this->cache_dir . $path))) {
return;
}

while ($item = @readdir($dir)) {
if (is_file($this->cache_dir . $path . $item)) {
@unlink($this->cache_dir . $path . $item);
}
}
}

/**
* Removes the given cache file from the cache
*
Expand All @@ -69,7 +69,7 @@ public function remove($name, $path = null)
}
return false;
}

/**
* Writes the given data to the cache using the name given
*
Expand All @@ -83,18 +83,18 @@ public function remove($name, $path = null)
public function write($name, $output, $ttl, $path = null)
{
$cache = $this->cacheName($name, $path);

$cache_dir = dirname($cache);
if (!file_exists($cache_dir)) {
mkdir($cache_dir, $this->cache_dir_perms, true);
}

// Save output to cache file
file_put_contents($cache, $output);
// Set the cache expiration date/time
touch($cache, time()+$ttl);
}

/**
* Fetches the contents of a cache, if it exists and is valid
*
Expand All @@ -114,7 +114,7 @@ public function fetch($name, $path = null)
return false;
}
}

/**
* Builds the file name of the cache file based on the name given
*
Expand Down
44 changes: 22 additions & 22 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
namespace minphp\Cache;
namespace Minphp\Cache\Tests;

use \PHPUnit_Framework_TestCase;
use Minphp\Cache\Cache;
use PHPUnit_Framework_TestCase;

/**
* @coversDefaultClass \minphp\Cache\Cache
* @coversDefaultClass \Minphp\Cache\Cache
*/
class CacheTest extends PHPUnit_Framework_TestCase
{
private $cache_dir;
private $cache;

protected function setUp()
{
$this->cache_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . "Fixtures" . DIRECTORY_SEPARATOR;
Expand All @@ -36,58 +37,57 @@ public function testClear()
{
file_put_contents($this->cache_dir . "testfile", "CacheTest::testClear");
$this->assertFileExists($this->cache_dir . "testfile");

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

$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->clear("sub/path/");
$this->assertFileNotExists($this->cache_dir . "sub/path/testfile");
rmdir($this->cache_dir . "sub/path");
rmdir($this->cache_dir . "sub");
}

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

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

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

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

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

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

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

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

Expand All @@ -97,19 +97,19 @@ public function testWriteCache()
/**
* @covers ::fetch
* @covers ::cacheName
* @uses \minphp\Cache\Cache::write
* @uses \minphp\Cache\Cache::remove
* @uses \Minphp\Cache\Cache::write
* @uses \Minphp\Cache\Cache::remove
*/
public function testFetchCache()
{
$cache_name = "testfile";
$cache_contents = "CacheTest::testFetch";

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

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

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

Expand Down

0 comments on commit b5af46c

Please sign in to comment.