|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Cache\Handlers; |
| 15 | + |
| 16 | +use CodeIgniter\Cache\CacheFactory; |
| 17 | +use CodeIgniter\CLI\CLI; |
| 18 | +use CodeIgniter\I18n\Time; |
| 19 | +use Config\Cache; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | +use PHPUnit\Framework\Attributes\RequiresPhpExtension; |
| 22 | + |
| 23 | +/** |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +#[Group('CacheLive')] |
| 27 | +#[RequiresPhpExtension('apcu')] |
| 28 | +final class ApcuHandlerTest extends AbstractHandlerTestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @return list<string> |
| 32 | + */ |
| 33 | + private static function getKeyArray(): array |
| 34 | + { |
| 35 | + return [ |
| 36 | + self::$key1, |
| 37 | + self::$key2, |
| 38 | + self::$key3, |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + parent::setUp(); |
| 45 | + |
| 46 | + $this->handler = CacheFactory::getHandler(new Cache(), 'apcu'); |
| 47 | + } |
| 48 | + |
| 49 | + protected function tearDown(): void |
| 50 | + { |
| 51 | + foreach (self::getKeyArray() as $key) { |
| 52 | + $this->handler->delete($key); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function testNew(): void |
| 57 | + { |
| 58 | + $this->assertInstanceOf(ApcuHandler::class, $this->handler); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * This test waits for 3 seconds before last assertion so this |
| 63 | + * is naturally a "slow" test on the perspective of the default limit. |
| 64 | + * |
| 65 | + * @timeLimit 3.5 |
| 66 | + */ |
| 67 | + public function testGet(): void |
| 68 | + { |
| 69 | + $this->handler->save(self::$key1, 'value', 2); |
| 70 | + |
| 71 | + $this->assertSame('value', $this->handler->get(self::$key1)); |
| 72 | + $this->assertNull($this->handler->get(self::$dummy)); |
| 73 | + |
| 74 | + CLI::wait(3); |
| 75 | + $this->assertNull($this->handler->get(self::$key1)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * This test waits for 3 seconds before last assertion so this |
| 80 | + * is naturally a "slow" test on the perspective of the default limit. |
| 81 | + * |
| 82 | + * @timeLimit 3.5 |
| 83 | + */ |
| 84 | + public function testRemember(): void |
| 85 | + { |
| 86 | + $this->handler->remember(self::$key1, 2, static fn (): string => 'value'); |
| 87 | + |
| 88 | + $this->assertSame('value', $this->handler->get(self::$key1)); |
| 89 | + $this->assertNull($this->handler->get(self::$dummy)); |
| 90 | + |
| 91 | + CLI::wait(3); |
| 92 | + $this->assertNull($this->handler->get(self::$key1)); |
| 93 | + } |
| 94 | + |
| 95 | + public function testSave(): void |
| 96 | + { |
| 97 | + $this->assertTrue($this->handler->save(self::$key1, 'value')); |
| 98 | + } |
| 99 | + |
| 100 | + public function testSavePermanent(): void |
| 101 | + { |
| 102 | + $this->assertTrue($this->handler->save(self::$key1, 'value', 0)); |
| 103 | + $metaData = $this->handler->getMetaData(self::$key1); |
| 104 | + |
| 105 | + $this->assertNull($metaData['expire']); |
| 106 | + $this->assertLessThanOrEqual(1, $metaData['mtime'] - Time::now()->getTimestamp()); |
| 107 | + $this->assertSame('value', $metaData['data']); |
| 108 | + |
| 109 | + $this->assertTrue($this->handler->delete(self::$key1)); |
| 110 | + } |
| 111 | + |
| 112 | + public function testDelete(): void |
| 113 | + { |
| 114 | + $this->handler->save(self::$key1, 'value'); |
| 115 | + |
| 116 | + $this->assertTrue($this->handler->delete(self::$key1)); |
| 117 | + $this->assertFalse($this->handler->delete(self::$dummy)); |
| 118 | + } |
| 119 | + |
| 120 | + public function testDeleteMatching(): void |
| 121 | + { |
| 122 | + // Save items to match on |
| 123 | + for ($i = 1; $i <= 50; $i++) { |
| 124 | + $this->handler->save('key_' . $i, 'value' . $i); |
| 125 | + } |
| 126 | + |
| 127 | + // Checking that with pattern 'key_1*' only 11 entries deleted: |
| 128 | + // key_1, key_10, key_11, key_12, key_13, key_14, key_15, key_16, key_17, key_18, key_19 |
| 129 | + $this->assertSame(11, $this->handler->deleteMatching('key_1*')); |
| 130 | + |
| 131 | + // Checking that with pattern '*1', only 3 entries deleted: |
| 132 | + // key_21, key_31, key_41 |
| 133 | + $this->assertSame(3, $this->handler->deleteMatching('key_*1')); |
| 134 | + |
| 135 | + // Checking that with pattern '*5*' only 5 entries deleted: |
| 136 | + // key_5, key_25, key_35, key_45, key_50 |
| 137 | + $this->assertSame(5, $this->handler->deleteMatching('*5*')); |
| 138 | + |
| 139 | + // Check final number of cache entries |
| 140 | + $this->assertSame(31, $this->handler->getCacheInfo()['num_entries']); |
| 141 | + } |
| 142 | + |
| 143 | + public function testIncrementAndDecrement(): void |
| 144 | + { |
| 145 | + $this->handler->save('counter', 100); |
| 146 | + |
| 147 | + foreach (range(1, 10) as $step) { |
| 148 | + $this->handler->increment('counter', $step); |
| 149 | + } |
| 150 | + |
| 151 | + $this->assertSame(155, $this->handler->get('counter')); |
| 152 | + |
| 153 | + $this->handler->decrement('counter', 20); |
| 154 | + $this->assertSame(135, $this->handler->get('counter')); |
| 155 | + |
| 156 | + $this->handler->increment('counter', 5); |
| 157 | + $this->assertSame(140, $this->handler->get('counter')); |
| 158 | + } |
| 159 | + |
| 160 | + public function testClean(): void |
| 161 | + { |
| 162 | + $this->handler->save(self::$key1, 1); |
| 163 | + |
| 164 | + $this->assertTrue($this->handler->clean()); |
| 165 | + } |
| 166 | + |
| 167 | + public function testGetCacheInfo(): void |
| 168 | + { |
| 169 | + $this->handler->save(self::$key1, 'value'); |
| 170 | + |
| 171 | + $this->assertIsArray($this->handler->getCacheInfo()); |
| 172 | + } |
| 173 | + |
| 174 | + public function testIsSupported(): void |
| 175 | + { |
| 176 | + $this->assertTrue($this->handler->isSupported()); |
| 177 | + } |
| 178 | +} |
0 commit comments