From dac50d1cfb6c5c1f61aa6cce316351eb23dcda75 Mon Sep 17 00:00:00 2001 From: Christoph Hochstrasser Date: Tue, 22 Nov 2016 16:05:28 +0100 Subject: [PATCH] Update testcases --- tests/CHH/Silex/Test/CacheProviderTest.php | 90 ++++++++++++++-------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/tests/CHH/Silex/Test/CacheProviderTest.php b/tests/CHH/Silex/Test/CacheProviderTest.php index 725df18..9cf641b 100644 --- a/tests/CHH/Silex/Test/CacheProviderTest.php +++ b/tests/CHH/Silex/Test/CacheProviderTest.php @@ -3,6 +3,7 @@ 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; @@ -10,46 +11,48 @@ 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' )); @@ -61,14 +64,13 @@ 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' @@ -76,13 +78,12 @@ function testNamespaceFactory() $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' @@ -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']; + } }