Skip to content

Commit

Permalink
Added custom exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenGDev committed Oct 1, 2016
1 parent 62fe61a commit 3759094
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace MaartenGDev;


use MaartenGDev\Exceptions\BadMethodCallException;
use MaartenGDev\Exceptions\CacheEntryNotFound;
use MaartenGDev\Exceptions\CacheFileNotFoundException;

class Cache implements CacheInterface
Expand Down Expand Up @@ -41,7 +43,7 @@ public function has($key, $callable = null)


if($isCallable && !is_callable($callable)){
return false;
throw new BadMethodCallException('Invalid Callable provided');
}

if (!$this->storage->fileExists($key)) {
Expand All @@ -68,7 +70,7 @@ public function has($key, $callable = null)
public function get($key)
{
if (!$this->isValid($key, $this->expire)) {
throw new \Exception('No Cache Entry found');
throw new CacheEntryNotFound('No cache entry found.');
}

return $this->storage->get($key);
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MaartenGDev\Exceptions;

use Exception;

class BadMethodCallException extends Exception
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/CacheEntryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MaartenGDev\Exceptions;

class CacheEntryNotFound extends \Exception
{

}
1 change: 0 additions & 1 deletion src/Exceptions/CacheFileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace MaartenGDev\Exceptions;


use Exception;

class CacheFileNotFoundException extends Exception
Expand Down
37 changes: 37 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace MaartenGDev\Tests;

use MaartenGDev\Cache;
use MaartenGDev\Exceptions\BadMethodCallException;
use MaartenGDev\Exceptions\CacheEntryNotFound;
use MaartenGDev\Exceptions\CacheFileNotFoundException;
use MaartenGDev\LocalDriver;
use PHPUnit_Framework_TestCase;
Expand Down Expand Up @@ -41,4 +43,39 @@ public function testCacheIsValidWithInvalidItemGivesFileNotFoundException(){

$fileDoesNotExist = $this->cache->isValid('hello_world_invalid',5);
}

public function testCallHasItemInCacheWithInvalidClosure(){

$stored = $this->cache->store('hello_world','Hello world!');

$this->expectException(BadMethodCallException::class);
$this->cache->has('hello_world','invalid closure');
}

public function testHasItemInCacheWithClosureReturnsClosureReturnValue(){

$stored = $this->cache->store('hello_world','Hello world!');

$hasItem = $this->cache->has('hello_world',function(){
return 'correct';
});

$this->assertEquals('correct',$hasItem);
}

public function testGetCacheItemThatDoesNotExist(){

$this->expectException(CacheFileNotFoundException::class);

$this->cache->get('invalid_cache_item_name');
}

public function testGetCacheItemThatDoesExist(){
$stored = $this->cache->store('cache_entry_name','New Entry Here');

$cacheItem = $this->cache->get('cache_entry_name');

$this->assertEquals($cacheItem,'New Entry Here');
}

}

0 comments on commit 3759094

Please sign in to comment.