Skip to content

Commit fe8d78e

Browse files
committed
Updated tests for custom drivers
1 parent ac44be7 commit fe8d78e

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

lib/Phpfastcache/CacheManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Phpfastcache\Config\ConfigurationOption;
1919
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
2020
use Phpfastcache\Exceptions\{
21-
PhpfastcacheDeprecatedException, PhpfastcacheDriverCheckException, PhpfastcacheDriverNotFoundException, PhpfastcacheInstanceNotFoundException, PhpfastcacheInvalidArgumentException, PhpfastcacheInvalidConfigurationException, PhpfastcacheLogicException, PhpfastcacheUnsupportedOperationException
21+
PhpfastcacheDeprecatedException, PhpfastcacheDriverCheckException, PhpfastcacheDriverException, PhpfastcacheDriverNotFoundException, PhpfastcacheInstanceNotFoundException, PhpfastcacheInvalidArgumentException, PhpfastcacheInvalidConfigurationException, PhpfastcacheLogicException, PhpfastcacheUnsupportedOperationException
2222
};
2323
use Phpfastcache\Util\ClassNamespaceResolverTrait;
2424

@@ -102,6 +102,7 @@ class CacheManager
102102
* @throws PhpfastcacheInvalidConfigurationException
103103
* @throws PhpfastcacheDriverNotFoundException
104104
* @throws PhpfastcacheInvalidArgumentException
105+
* @throws PhpfastcacheDriverException
105106
*/
106107
public static function getInstance($driver = 'auto', $config = null, $instanceId = null): ExtendedCacheItemPoolInterface
107108
{
@@ -134,6 +135,14 @@ public static function getInstance($driver = 'auto', $config = null, $instanceId
134135
if (!isset(self::$instances[ $instance ])) {
135136
$badPracticeOmeter[ $driver ] = 1;
136137
$driverClass = self::getDriverClass($driver);
138+
139+
if(!is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)){
140+
throw new PhpfastcacheDriverException(sprintf(
141+
'Class "%s" does not implement "%s"',
142+
$driverClass,
143+
ExtendedCacheItemPoolInterface::class
144+
));
145+
}
137146
try {
138147
if (\class_exists($driverClass)) {
139148
$configClass = $driverClass::getConfigClass();

tests/CustomDriver.test.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use Phpfastcache\Drivers\Fakefiles\Config;
1010
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
1111
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
12-
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
13-
use Phpfastcache\Helper\CacheConditionalHelper as CacheConditional;
1412
use Phpfastcache\Helper\TestHelper;
1513

1614
chdir(__DIR__);
@@ -29,33 +27,38 @@
2927
}
3028

3129
try {
32-
CacheManager::addCoreDriverOverride('Fakefiles', \Phpfastcache\Drivers\Fakefiles\Driver::class);
33-
$testHelper->printFailText('No exception thrown while trying to override an non-existing driver');
34-
} catch (PhpfastcacheLogicException $e) {
35-
$testHelper->printPassText('An exception has been thrown while trying to override an non-existing driver');
30+
CacheManager::addCustomDriver('Fakefiles', \Phpfastcache\Drivers\Fakefiles\Driver::class);
31+
$testHelper->printPassText('No exception thrown while trying to add a custom driver');
32+
} catch (\Throwable $e) {
33+
$testHelper->printFailText('An exception has been thrown while trying to add a custom driver');
3634
}
3735

3836
try {
39-
CacheManager::addCoreDriverOverride('', \Phpfastcache\Drivers\Fakefiles\Driver::class);
37+
CacheManager::addCustomDriver('Fakefiles', \Phpfastcache\Drivers\Fakefiles\Driver::class);
38+
$testHelper->printFailText('No exception thrown while trying to re-add a the same custom driver');
39+
} catch (\Throwable $e) {
40+
$testHelper->printPassText('An exception has been thrown while trying to re-add a the same custom driver');
41+
}
42+
43+
try {
44+
CacheManager::addCustomDriver('', \Phpfastcache\Drivers\Fakefiles\Driver::class);
4045
$testHelper->printFailText('No exception thrown while trying to override an empty driver');
4146
} catch (PhpfastcacheInvalidArgumentException $e) {
4247
$testHelper->printPassText('An exception has been thrown while trying to override an empty driver');
4348
}
4449

45-
CacheManager::addCoreDriverOverride('Files', \Phpfastcache\Drivers\Fakefiles\Driver::class);
46-
4750
try{
48-
$cacheInstance = CacheManager::getInstance('Files', new Config(['customOption' => true]));
51+
$cacheInstance = CacheManager::getInstance('Fakefiles', new Config(['customOption' => true]));
4952
$testHelper->printPassText('The custom driver is unavailable at the moment and no exception has been thrown.');
5053
}catch (PhpfastcacheDriverCheckException $e){
5154
$testHelper->printPassText('The custom driver is unavailable at the moment and the exception has been catch.');
5255
}
5356

54-
CacheManager::removeCoreDriverOverride('Files');
57+
CacheManager::removeCustomDriver('Fakefiles');
5558

5659
try{
57-
$cacheInstance = CacheManager::getInstance('Files');
58-
$testHelper->printPassText('The custom driver has been removed but is still.');
60+
$cacheInstance = CacheManager::getInstance('Fakefiles');
61+
$testHelper->printPassText('The custom driver has been removed but is still active.');
5962
}catch (PhpfastcacheDriverCheckException $e){
6063
$testHelper->printPassText('The custom driver is unavailable at the moment and the exception has been catch.');
6164
}

0 commit comments

Comments
 (0)