|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SymPress\Framework\Tests\Integration; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Group; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SymPress\Framework\ObjectCache\NativeMemcachedObjectCacheAdapter; |
| 10 | +use SymPress\Framework\ObjectCache\NativeRedisObjectCacheAdapter; |
| 11 | + |
| 12 | +#[Group('live-cache')] |
| 13 | +final class NativeObjectCacheBackendSmokeTest extends TestCase |
| 14 | +{ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + if (getenv('SYMPRESS_LIVE_CACHE_TESTS') !== '1') { |
| 18 | + self::markTestSkipped('Set SYMPRESS_LIVE_CACHE_TESTS=1 to test live cache backends.'); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public function testRedisProtocolAndAtomicOperations(): void |
| 23 | + { |
| 24 | + self::assertTrue(class_exists(\Redis::class), 'The redis PHP extension is required.'); |
| 25 | + $redis = new \Redis(); |
| 26 | + $this->connect(fn (): bool => $redis->connect('127.0.0.1', 6379, 1.0)); |
| 27 | + $cache = new NativeRedisObjectCacheAdapter($redis, 'sympress-smoke-' . bin2hex(random_bytes(6))); |
| 28 | + |
| 29 | + self::assertTrue($cache->set('count', 1)); |
| 30 | + self::assertSame(1, $cache->get('count')); |
| 31 | + self::assertSame(3, $cache->incr('count', 2)); |
| 32 | + self::assertSame(0, $cache->decr('count', 9)); |
| 33 | + self::assertTrue($cache->clear()); |
| 34 | + self::assertFalse($cache->get('count')); |
| 35 | + |
| 36 | + $redis->close(); |
| 37 | + } |
| 38 | + |
| 39 | + public function testMemcachedProtocolAndAtomicOperations(): void |
| 40 | + { |
| 41 | + self::assertTrue(class_exists(\Memcached::class), 'The memcached PHP extension is required.'); |
| 42 | + $memcached = new \Memcached(); |
| 43 | + self::assertTrue($memcached->addServer('127.0.0.1', 11211)); |
| 44 | + $this->connect(static fn (): bool => $memcached->getVersion() !== false); |
| 45 | + $cache = new NativeMemcachedObjectCacheAdapter( |
| 46 | + $memcached, |
| 47 | + 'sympress-smoke-' . bin2hex(random_bytes(6)), |
| 48 | + ); |
| 49 | + |
| 50 | + self::assertTrue($cache->set('count', 1)); |
| 51 | + self::assertSame(1, $cache->get('count')); |
| 52 | + self::assertSame(3, $cache->incr('count', 2)); |
| 53 | + self::assertSame(0, $cache->decr('count', 9)); |
| 54 | + self::assertTrue($cache->clear()); |
| 55 | + self::assertFalse($cache->get('count')); |
| 56 | + |
| 57 | + $memcached->quit(); |
| 58 | + } |
| 59 | + |
| 60 | + /** @param callable(): bool $probe */ |
| 61 | + private function connect(callable $probe): void |
| 62 | + { |
| 63 | + for ($attempt = 0; $attempt < 10; $attempt++) { |
| 64 | + if ($probe()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + usleep(250_000); |
| 69 | + } |
| 70 | + |
| 71 | + self::fail('Cache backend did not become ready.'); |
| 72 | + } |
| 73 | +} |
0 commit comments