Skip to content

Commit

Permalink
Update testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
CHH committed Nov 22, 2016
1 parent 9200491 commit dac50d1
Showing 1 changed file with 59 additions and 31 deletions.
90 changes: 59 additions & 31 deletions tests/CHH/Silex/Test/CacheProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,56 @@
namespace CHH\Silex\Test;

use CHH\Silex\CacheServiceProvider;
use CHH\Silex\CacheServiceProvider\CacheNamespace;
use Silex\Application;
use Doctrine\Common\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\FilesystemCache;

class CacheProviderTest extends \PHPUnit_Framework_TestCase
{
function testDefaultCache()
private $app;

function setUp()
{
$app = new Application;
$this->app = new Application;
$this->app->register(new CacheServiceProvider);
}

$app->register(new CacheServiceProvider, array(
'cache.options' => array("default" => array(
function testDefaultCache()
{
$this->app['cache.options'] = array(
"default" => array(
'driver' => "array"
))
));
)
);

$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $app['cache']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $this->app['cache']);
}

function testMultipleCaches()
{
$app = new Application;

$app->register(new CacheServiceProvider, array(
'cache.options' => array(
"default" => array('driver' => "array"),
"foo" => array(
'driver' => FilesystemCache::class,
'directory' => '/tmp'
)
$this->app['cache.options'] = array(
"default" => array('driver' => "array"),
"foo" => array(
'driver' => FilesystemCache::class,
'directory' => '/tmp'
)
));
);

$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\FilesystemCache', $app['caches']['foo']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $app['cache']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\FilesystemCache', $this->app['caches']['foo']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $this->app['cache']);
}

function testCacheFactory()
{
$app = new Application;
$app = $this->app;

$app->register(new CacheServiceProvider, array('cache.options' => array(
'default' => 'array'
)));
$this->app['cache.options'] = [
'default' => 'array',
];

$app['caches'] = $app->extend('caches', function($caches) use ($app) {
$this->app['caches'] = $this->app->extend('caches', function($caches) use ($app) {
$caches['foo'] = $app['cache.factory'](array(
'driver' => 'array'
));
Expand All @@ -61,28 +64,26 @@ function testCacheFactory()
return $caches;
});

$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $app['caches']['foo']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $app['caches']['bar']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $this->app['caches']['foo']);
$this->assertInstanceOf('\\Doctrine\\Common\\Cache\\ArrayCache', $this->app['caches']['bar']);
}

function testNamespaceFactory()
{
$app = new Application;
$app->register(new CacheServiceProvider);
$app = $this->app;

$app['cache.options'] = array('default' => array(
'driver' => 'array'
));

$app['caches']['foo'] = $app['cache.namespace']('foo');

$this->assertInstanceOf('\\CHH\\Silex\\CacheServiceProvider\\CacheNamespace', $app['caches']['foo']);
$this->assertInstanceOf(CacheNamespace::class, $app['caches']['foo']);
}

function testSameNamespaceInDifferentCaches()
{
$app = new Application;
$app->register(new CacheServiceProvider);
$app = $this->app;

$app['cache.options'] = array('default' => array(
'driver' => 'array'
Expand All @@ -98,4 +99,31 @@ function testSameNamespaceInDifferentCaches()
$this->assertFalse($app['caches']['foo']->contains('foo'));
$this->assertEquals('bar', $app['caches']['bar']->fetch('foo'));
}

function testDefaultCacheServiceIsTheSameInstanceInCacheCollection()
{
$app = $this->app;

$app['cache.options'] = [
'default' => [
'driver' => 'array',
],
];

$this->assertEquals($app['caches']['default'], $app['cache']);
}

/**
* @expectedException InvalidArgumentException
*/
function testErrorWhenNoDefaultCacheIsDefineD()
{
$app = $this->app;

$app['cache.options'] = [
'foo' => ['driver' => 'array'],
];

$app['cache'];
}
}

0 comments on commit dac50d1

Please sign in to comment.