CacheManagerTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Illuminate\Tests\Cache;
  3. use Illuminate\Cache\ArrayStore;
  4. use Illuminate\Cache\CacheManager;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. class CacheManagerTest extends TestCase
  8. {
  9. protected function tearDown(): void
  10. {
  11. m::close();
  12. }
  13. public function testCustomDriverClosureBoundObjectIsCacheManager()
  14. {
  15. $cacheManager = new CacheManager([
  16. 'config' => [
  17. 'cache.stores.'.__CLASS__ => [
  18. 'driver' => __CLASS__,
  19. ],
  20. ],
  21. ]);
  22. $driver = function () {
  23. return $this;
  24. };
  25. $cacheManager->extend(__CLASS__, $driver);
  26. $this->assertEquals($cacheManager, $cacheManager->store(__CLASS__));
  27. }
  28. public function testForgetDriver()
  29. {
  30. $cacheManager = m::mock(CacheManager::class)
  31. ->shouldAllowMockingProtectedMethods()
  32. ->makePartial();
  33. $cacheManager->shouldReceive('resolve')
  34. ->withArgs(['array'])
  35. ->times(4)
  36. ->andReturn(new ArrayStore);
  37. $cacheManager->shouldReceive('getDefaultDriver')
  38. ->once()
  39. ->andReturn('array');
  40. foreach (['array', ['array'], null] as $option) {
  41. $cacheManager->store('array');
  42. $cacheManager->store('array');
  43. $cacheManager->forgetDriver($option);
  44. $cacheManager->store('array');
  45. $cacheManager->store('array');
  46. }
  47. }
  48. public function testForgetDriverForgets()
  49. {
  50. $cacheManager = new CacheManager([
  51. 'config' => [
  52. 'cache.stores.forget' => [
  53. 'driver' => 'forget',
  54. ],
  55. ],
  56. ]);
  57. $cacheManager->extend('forget', function () {
  58. return new ArrayStore;
  59. });
  60. $cacheManager->store('forget')->forever('foo', 'bar');
  61. $this->assertSame('bar', $cacheManager->store('forget')->get('foo'));
  62. $cacheManager->forgetDriver('forget');
  63. $this->assertNull($cacheManager->store('forget')->get('foo'));
  64. }
  65. }