SIGN IN SIGN UP
laravel / framework UNCLAIMED

Laravel is a web application framework with expressive, elegant syntax.

<?php
namespace Illuminate\Tests\Cache;
2018-10-26 13:49:24 +01:00
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\CacheManager;
2022-02-15 18:02:22 +03:30
use Illuminate\Cache\NullStore;
2022-02-14 20:57:32 +03:30
use Illuminate\Config\Repository;
use Illuminate\Container\Container;
2022-02-15 18:02:22 +03:30
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Events\Dispatcher as Event;
2022-02-14 20:57:32 +03:30
use InvalidArgumentException;
use Mockery as m;
use PHPUnit\Framework\TestCase;
class CacheManagerTest extends TestCase
{
public function testCustomDriverClosureBoundObjectIsCacheManager()
{
$manager = new CacheManager($this->getApp([
'cache' => [
'stores' => [
__CLASS__ => [
'driver' => __CLASS__,
],
],
],
]));
$manager->extend(__CLASS__, fn () => $this);
$this->assertSame($manager, $manager->store(__CLASS__));
}
2018-10-26 13:40:25 +01:00
2022-02-15 18:02:22 +03:30
public function testCustomDriverOverridesInternalDrivers()
{
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'array',
],
],
],
];
$app = $this->getApp($userConfig);
$cacheManager = new CacheManager($app);
$myArrayDriver = (object) ['flag' => 'mm(u_u)mm'];
$cacheManager->extend('array', fn () => $myArrayDriver);
$driver = $cacheManager->store('my_store');
2022-02-22 15:46:19 +01:00
$this->assertSame('mm(u_u)mm', $driver->flag);
2022-02-15 18:02:22 +03:30
}
public function testItCanBuildRepositories()
{
$app = $this->getApp([]);
$cacheManager = new CacheManager($app);
$arrayCache = $cacheManager->build(['driver' => 'array']);
$nullCache = $cacheManager->build(['driver' => 'null']);
$this->assertInstanceOf(ArrayStore::class, $arrayCache->getStore());
$this->assertInstanceOf(NullStore::class, $nullCache->getStore());
}
2022-02-15 18:02:22 +03:30
public function testItMakesRepositoryWhenContainerHasNoDispatcher()
{
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'array',
],
],
],
];
$app = $this->getApp($userConfig);
$this->assertFalse($app->bound(Dispatcher::class));
$cacheManager = new CacheManager($app);
$repo = $cacheManager->repository($theStore = new NullStore);
2022-02-15 18:02:22 +03:30
$this->assertNull($repo->getEventDispatcher());
$this->assertSame($theStore, $repo->getStore());
// binding dispatcher after the repo's birth will have no effect.
$app->bind(Dispatcher::class, fn () => new Event);
$this->assertNull($repo->getEventDispatcher());
$this->assertSame($theStore, $repo->getStore());
$cacheManager = new CacheManager($app);
$repo = $cacheManager->repository(new NullStore);
2022-02-15 18:02:22 +03:30
// now that the $app has a Dispatcher, the newly born repository will also have one.
$this->assertNotNull($repo->getEventDispatcher());
}
2022-04-01 23:33:50 +04:30
public function testItRefreshesDispatcherOnAllStores()
{
$userConfig = [
'cache' => [
'stores' => [
'store_1' => [
'driver' => 'array',
],
'store_2' => [
'driver' => 'array',
],
],
],
];
$app = $this->getApp($userConfig);
$cacheManager = new CacheManager($app);
$repo1 = $cacheManager->store('store_1');
$repo2 = $cacheManager->store('store_2');
$this->assertNull($repo1->getEventDispatcher());
$this->assertNull($repo2->getEventDispatcher());
$dispatcher = new Event;
$app->bind(Dispatcher::class, fn () => $dispatcher);
$cacheManager->refreshEventDispatcher();
$this->assertNotSame($repo1, $repo2);
$this->assertSame($dispatcher, $repo1->getEventDispatcher());
$this->assertSame($dispatcher, $repo2->getEventDispatcher());
}
public function testItSetsDefaultDriverChangesGlobalConfig()
{
$userConfig = [
'cache' => [
'default' => 'store_1',
'stores' => [
'store_1' => [
'driver' => 'array',
],
'store_2' => [
'driver' => 'array',
],
],
],
];
$app = $this->getApp($userConfig);
$cacheManager = new CacheManager($app);
$cacheManager->setDefaultDriver('><((((@>');
$this->assertEquals('><((((@>', $app->get('config')->get('cache.default'));
}
public function testItPurgesMemoizedStoreObjects()
{
$userConfig = [
'cache' => [
'stores' => [
'store_1' => [
'driver' => 'array',
],
'store_2' => [
'driver' => 'null',
],
],
],
];
$app = $this->getApp($userConfig);
$cacheManager = new CacheManager($app);
$repo1 = $cacheManager->store('store_1');
$repo2 = $cacheManager->store('store_1');
$repo3 = $cacheManager->store('store_2');
$repo4 = $cacheManager->store('store_2');
$repo5 = $cacheManager->store('store_2');
$this->assertSame($repo1, $repo2);
$this->assertSame($repo3, $repo4);
$this->assertSame($repo3, $repo5);
$this->assertNotSame($repo1, $repo5);
$cacheManager->purge('store_1');
// Make sure a now object is built this time.
$repo6 = $cacheManager->store('store_1');
$this->assertNotSame($repo1, $repo6);
// Make sure Purge does not delete all objects.
$repo7 = $cacheManager->store('store_2');
$this->assertSame($repo3, $repo7);
}
2018-10-26 13:34:45 -05:00
public function testForgetDriver()
2018-10-26 13:40:25 +01:00
{
$cacheManager = m::mock(CacheManager::class)
->shouldAllowMockingProtectedMethods()
->makePartial();
$cacheManager->shouldReceive('resolve')
->withArgs(['array'])
->times(4)
->andReturn(new ArrayStore);
2018-10-26 13:40:25 +01:00
$cacheManager->shouldReceive('getDefaultDriver')
->once()
->andReturn('array');
foreach (['array', ['array'], null] as $option) {
$cacheManager->store('array');
$cacheManager->store('array');
2018-10-26 13:34:45 -05:00
$cacheManager->forgetDriver($option);
2018-10-26 13:40:25 +01:00
$cacheManager->store('array');
$cacheManager->store('array');
}
}
2018-10-26 13:34:45 -05:00
public function testForgetDriverForgets()
2018-10-26 13:40:25 +01:00
{
$cacheManager = new CacheManager([
'config' => [
'cache.stores.forget' => [
'driver' => 'forget',
],
],
]);
2018-10-26 13:49:24 +01:00
$cacheManager->extend('forget', function () {
return new ArrayStore;
2018-10-26 13:40:25 +01:00
});
$cacheManager->store('forget')->forever('foo', 'bar');
$this->assertSame('bar', $cacheManager->store('forget')->get('foo'));
2018-10-26 13:34:45 -05:00
$cacheManager->forgetDriver('forget');
2018-10-26 13:40:25 +01:00
$this->assertNull($cacheManager->store('forget')->get('foo'));
}
2022-02-14 20:57:32 +03:30
public function testThrowExceptionWhenUnknownDriverIsUsed()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Driver [unknown_taxi_driver] is not supported.');
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'unknown_taxi_driver',
],
],
],
];
2022-02-15 18:02:22 +03:30
$app = $this->getApp($userConfig);
2022-02-14 20:57:32 +03:30
$cacheManager = new CacheManager($app);
$cacheManager->store('my_store');
}
public function testThrowExceptionWhenUnknownStoreIsUsed()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cache store [alien_store] is not defined.');
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'array',
],
],
],
];
2022-02-15 18:02:22 +03:30
$app = $this->getApp($userConfig);
2022-02-14 20:57:32 +03:30
$cacheManager = new CacheManager($app);
$cacheManager->store('alien_store');
}
2022-02-15 18:02:22 +03:30
public function testMakesRepositoryWithoutDispatcherWhenEventsDisabled()
{
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'array',
],
'my_store_without_events' => [
'driver' => 'array',
'events' => false,
],
],
],
];
$app = $this->getApp($userConfig);
$app->bind(Dispatcher::class, fn () => new Event);
$cacheManager = new CacheManager($app);
// The repository will have an event dispatcher
$repo = $cacheManager->store('my_store');
$this->assertNotNull($repo->getEventDispatcher());
// This repository will not have an event dispatcher as 'with_events' is false
$repoWithoutEvents = $cacheManager->store('my_store_without_events');
$this->assertNull($repoWithoutEvents->getEventDispatcher());
}
2022-02-15 18:02:22 +03:30
protected function getApp(array $userConfig)
{
2022-04-01 23:33:50 +04:30
$app = new Container;
$app->singleton('config', fn () => new Repository($userConfig));
2022-02-15 18:02:22 +03:30
return $app;
}
}