|
| 1 | +<?php |
| 2 | + |
| 3 | +use CodeIgniter\Cache\Handlers\DummyHandler; |
| 4 | +use CodeIgniter\Psr\Cache\CacheArgumentException; |
| 5 | +use CodeIgniter\Psr\Cache\SimpleCache; |
| 6 | +use CodeIgniter\Test\CIUnitTestCase; |
| 7 | +use CodeIgniter\Test\Mock\MockCache; |
| 8 | +use Config\Cache; |
| 9 | +use Config\Services; |
| 10 | + |
| 11 | +class SupportTraitTest extends CIUnitTestCase |
| 12 | +{ |
| 13 | + public function setUp(): void |
| 14 | + { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + Services::resetSingle('cache'); |
| 18 | + } |
| 19 | + |
| 20 | + public function testDefaultUsesSharedInstance() |
| 21 | + { |
| 22 | + Services::injectMock('cache', new MockCache()); |
| 23 | + |
| 24 | + $psr = new SimpleCache(); |
| 25 | + $result = $this->getPrivateProperty($psr, 'adapter'); |
| 26 | + |
| 27 | + $this->assertInstanceOf(MockCache::class, $result); |
| 28 | + } |
| 29 | + |
| 30 | + public function testUsesConfig() |
| 31 | + { |
| 32 | + $config = new Cache(); |
| 33 | + $config->handler = 'dummy'; |
| 34 | + |
| 35 | + $psr = new SimpleCache($config); |
| 36 | + $result = $this->getPrivateProperty($psr, 'adapter'); |
| 37 | + |
| 38 | + $this->assertInstanceOf(DummyHandler::class, $result); |
| 39 | + } |
| 40 | + |
| 41 | + public function testUsesHandler() |
| 42 | + { |
| 43 | + $psr = new SimpleCache(new MockCache()); |
| 44 | + $result = $this->getPrivateProperty($psr, 'adapter'); |
| 45 | + |
| 46 | + $this->assertInstanceOf(MockCache::class, $result); |
| 47 | + } |
| 48 | + |
| 49 | + public function testThrowsException() |
| 50 | + { |
| 51 | + $this->expectException(CacheArgumentException::class); |
| 52 | + $this->expectExceptionMessage('CodeIgniter\Psr\Cache\SimpleCache constructor only accepts an adapter or configuration'); |
| 53 | + |
| 54 | + $psr = new SimpleCache(42); |
| 55 | + } |
| 56 | +} |
0 commit comments