From 414fcb191fd11e2b72c2b0b3f62412e67efec254 Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Sat, 4 May 2019 20:44:59 -0400 Subject: [PATCH] [#13439] - Added more tests for Cache AdapterFactory --- .../Cache/AdapterFactory/ConstructCest.php | 35 +++++++++++ .../Cache/AdapterFactory/GetSetHasCest.php | 49 +++++++++++++++ .../Cache/AdapterFactory/NewInstanceCest.php | 61 +++++++++++++++++++ .../Cache/CacheFactory/NewInstanceCest.php | 10 +-- 4 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 tests/unit/Cache/AdapterFactory/ConstructCest.php create mode 100644 tests/unit/Cache/AdapterFactory/GetSetHasCest.php create mode 100644 tests/unit/Cache/AdapterFactory/NewInstanceCest.php diff --git a/tests/unit/Cache/AdapterFactory/ConstructCest.php b/tests/unit/Cache/AdapterFactory/ConstructCest.php new file mode 100644 index 00000000000..f18bf84fa1b --- /dev/null +++ b/tests/unit/Cache/AdapterFactory/ConstructCest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Cache\AdapterFactory; + +use Phalcon\Cache\AdapterFactory; +use Phalcon\Storage\SerializerFactory; +use UnitTester; + +class ConstructCest +{ + /** + * Tests Phalcon\Cache\AdapterFactory :: __construct() + * + * @author Phalcon Team + * @since 2019-05-04 + */ + public function storageAdapterFactoryConstruct(UnitTester $I) + { + $I->wantToTest('Cache\AdapterFactory - __construct()'); + + $factory = new SerializerFactory(); + $service = new AdapterFactory($factory); + $I->assertInstanceOf(AdapterFactory::class, $service); + } +} diff --git a/tests/unit/Cache/AdapterFactory/GetSetHasCest.php b/tests/unit/Cache/AdapterFactory/GetSetHasCest.php new file mode 100644 index 00000000000..294bb367217 --- /dev/null +++ b/tests/unit/Cache/AdapterFactory/GetSetHasCest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Cache\AdapterFactory; + +use Phalcon\Cache\AdapterFactory; +use Phalcon\Storage\SerializerFactory; +use Phalcon\Test\Fixtures\Cache\Adapter\None; +use UnitTester; + +class GetSetHasCest +{ + /** + * Tests Phalcon\Cache\AdapterFactory :: get() + * + * @author Phalcon Team + * @since 2019-05-04 + */ + public function storageAdapterFactoryGetSetHas(UnitTester $I) + { + $I->wantToTest('Cache\AdapterFactory - get()/set()/has()'); + $I->skipTest('TODO - Check this'); + $mappers = ['none' => 'Phalcon\Test\Fixtures\Cache\Adapter\None']; + $factory = new SerializerFactory(); + $service = new AdapterFactory($factory, $mappers); + + $actual = $service->has('unknown'); + $I->assertFalse($actual); + + $actual = $service->has('none'); + $I->assertTrue($actual); + + $actual = $service->has('redis'); + $I->assertTrue($actual); + + $actual = $service->get('none'); + $class = None::class; + $I->assertInstanceOf($class, $actual); + } +} diff --git a/tests/unit/Cache/AdapterFactory/NewInstanceCest.php b/tests/unit/Cache/AdapterFactory/NewInstanceCest.php new file mode 100644 index 00000000000..d1e388aede6 --- /dev/null +++ b/tests/unit/Cache/AdapterFactory/NewInstanceCest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Cache\AdapterFactory; + +use Phalcon\Service\Exception; +use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\Adapter\Php; +use Phalcon\Cache\AdapterFactory; +use Phalcon\Storage\SerializerFactory; +use UnitTester; + +class NewInstanceCest +{ + /** + * Tests Phalcon\Cache\AdapterFactory :: newInstance() + * + * @author Phalcon Team + * @since 2019-05-04 + */ + public function storageAdapterFactoryNewInstance(UnitTester $I) + { + $I->wantToTest('Cache\AdapterFactory - newInstance()'); + + $factory = new SerializerFactory(); + $service = new AdapterFactory($factory); + + $service = $service->newInstance('apcu'); + $class = Apcu::class; + $I->assertInstanceOf($class, $service); + } + + /** + * Tests Phalcon\Cache\AdapterFactory :: newInstance() - exception + * + * @author Phalcon Team + * @since 2019-05-04 + */ + public function storageAdapterFactoryNewInstanceException(UnitTester $I) + { + $I->wantToTest('Cache\AdapterFactory - newInstance() - exception'); + + $I->expectThrowable( + new Exception('The service unknown has not been found in the locator'), + function () { + $factory = new SerializerFactory(); + $service = new AdapterFactory($factory); + $service = $service->newInstance('unknown'); + } + ); + } +} diff --git a/tests/unit/Cache/CacheFactory/NewInstanceCest.php b/tests/unit/Cache/CacheFactory/NewInstanceCest.php index 6174a897082..e1336a9a8a4 100644 --- a/tests/unit/Cache/CacheFactory/NewInstanceCest.php +++ b/tests/unit/Cache/CacheFactory/NewInstanceCest.php @@ -12,7 +12,7 @@ namespace Phalcon\Test\Unit\Cache\CacheFactory; -use Phalcon\Cache\Adapter\Apcu; +use Phalcon\Cache\AdapterFactory; use Phalcon\Cache\Cache; use Phalcon\Cache\CacheFactory; use Phalcon\Storage\SerializerFactory; @@ -31,10 +31,10 @@ public function cacheCacheFactoryNewInstance(UnitTester $I) { $I->wantToTest('Cache\CacheFactory - newInstance()'); - $serializer = new SerializerFactory(); - $apcu = new Apcu($serializer); - $factory = new CacheFactory(); - $adapter = $factory->newInstance($apcu); + $serializer = new SerializerFactory(); + $adapterFactory = new AdapterFactory($serializer); + $factory = new CacheFactory($adapterFactory); + $adapter = $factory->newInstance('apcu'); $class = Cache::class; $I->assertInstanceOf($class, $adapter);