196 lines
8.8 KiB
PHP
196 lines
8.8 KiB
PHP
<?php
|
|
// CacheToolsTest.php
|
|
// Created: 2024-04-10
|
|
// Updated: 2024-07-31
|
|
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\UsesClass;
|
|
use Index\Cache\CacheTools;
|
|
use Index\Cache\ArrayCache\{ArrayCacheBackend,ArrayCacheProvider};
|
|
use Index\Cache\Memcached\MemcachedBackend;
|
|
use Index\Cache\Valkey\ValkeyBackend;
|
|
use Index\Net\{DnsEndPoint,IPEndPoint,UnixEndPoint};
|
|
|
|
#[CoversClass(CacheTools::class)]
|
|
#[CoversClass(ArrayCacheBackend::class)]
|
|
#[CoversClass(ArrayCacheProvider::class)]
|
|
#[CoversClass(MemcachedBackend::class)]
|
|
#[CoversClass(ValkeyBackend::class)]
|
|
#[UsesClass(DnsEndPoint::class)]
|
|
#[UsesClass(IPEndPoint::class)]
|
|
#[UsesClass(UnixEndPoint::class)]
|
|
final class CacheToolsTest extends TestCase {
|
|
public function testBasicDSN(): void {
|
|
$arrayCache = CacheTools::create('array:');
|
|
$this->assertInstanceOf(ArrayCacheProvider::class, $arrayCache);
|
|
|
|
$arrayCache = CacheTools::create('Index-Cache-ArrayCache-ArrayCacheBackend:');
|
|
$this->assertInstanceOf(ArrayCacheProvider::class, $arrayCache);
|
|
}
|
|
|
|
public function testMemcachedDSN(): void {
|
|
$memcached = new MemcachedBackend;
|
|
|
|
// unix socket
|
|
$info = $memcached->parseDsn('memcached://:unix:/prefix/test/?server=/var/run/memcached.sock');
|
|
|
|
$endPoints = $info->getEndPoints();
|
|
$this->assertEquals(1, count($endPoints));
|
|
$this->assertInstanceOf(UnixEndPoint::class, $endPoints[0][0]);
|
|
$this->assertEquals('/var/run/memcached.sock', $endPoints[0][0]->getPath());
|
|
$this->assertEquals(0, $endPoints[0][1]);
|
|
$this->assertEquals('prefix:test:', $info->getPrefixKey());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertNull($info->getPersistentName());
|
|
$this->assertTrue($info->shouldUseBinaryProtocol());
|
|
$this->assertTrue($info->shouldEnableCompression());
|
|
$this->assertTrue($info->shouldTcpNoDelay());
|
|
|
|
// ipv4
|
|
$info = $memcached->parseDsn('memcached://127.0.0.1?compress=no&nodelay=off&persist=test');
|
|
|
|
$endPoints = $info->getEndPoints();
|
|
$this->assertEquals(1, count($endPoints));
|
|
$this->assertInstanceOf(IPEndPoint::class, $endPoints[0][0]);
|
|
$this->assertEquals('127.0.0.1', (string)$endPoints[0][0]->getAddress());
|
|
$this->assertEquals(0, $endPoints[0][0]->getPort());
|
|
$this->assertEquals(0, $endPoints[0][1]);
|
|
$this->assertEquals('', $info->getPrefixKey());
|
|
$this->assertTrue($info->isPersistent());
|
|
$this->assertEquals('test', $info->getPersistentName());
|
|
$this->assertTrue($info->shouldUseBinaryProtocol());
|
|
$this->assertFalse($info->shouldEnableCompression());
|
|
$this->assertFalse($info->shouldTcpNoDelay());
|
|
|
|
// ipv6
|
|
$info = $memcached->parseDsn('memcached://[::1]:9001?proto=ascii');
|
|
|
|
$endPoints = $info->getEndPoints();
|
|
$this->assertEquals(1, count($endPoints));
|
|
$this->assertInstanceOf(IPEndPoint::class, $endPoints[0][0]);
|
|
$this->assertEquals('::1', (string)$endPoints[0][0]->getAddress());
|
|
$this->assertEquals(9001, $endPoints[0][0]->getPort());
|
|
$this->assertEquals(0, $endPoints[0][1]);
|
|
$this->assertEquals('', $info->getPrefixKey());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertNull($info->getPersistentName());
|
|
$this->assertFalse($info->shouldUseBinaryProtocol());
|
|
$this->assertTrue($info->shouldEnableCompression());
|
|
$this->assertTrue($info->shouldTcpNoDelay());
|
|
|
|
// dns
|
|
$info = $memcached->parseDsn('memcache://localhost');
|
|
|
|
$endPoints = $info->getEndPoints();
|
|
$this->assertEquals(1, count($endPoints));
|
|
$this->assertInstanceOf(DnsEndPoint::class, $endPoints[0][0]);
|
|
$this->assertEquals('localhost', $endPoints[0][0]->getHost());
|
|
$this->assertEquals(0, $endPoints[0][0]->getPort());
|
|
$this->assertEquals(0, $endPoints[0][1]);
|
|
$this->assertEquals('', $info->getPrefixKey());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertNull($info->getPersistentName());
|
|
$this->assertTrue($info->shouldUseBinaryProtocol());
|
|
$this->assertTrue($info->shouldEnableCompression());
|
|
$this->assertTrue($info->shouldTcpNoDelay());
|
|
|
|
// pool
|
|
$info = $memcached->parseDsn('memcached://:pool:/?server[]=/var/run/memcached.sock;20&server[]=127.0.0.1;10&server[]=[::1]:9001;5&server[]=localhost');
|
|
|
|
$endPoints = $info->getEndPoints();
|
|
$this->assertEquals(4, count($endPoints));
|
|
|
|
$this->assertInstanceOf(UnixEndPoint::class, $endPoints[0][0]);
|
|
$this->assertEquals('/var/run/memcached.sock', $endPoints[0][0]->getPath());
|
|
$this->assertEquals(20, $endPoints[0][1]);
|
|
|
|
$this->assertInstanceOf(IPEndPoint::class, $endPoints[1][0]);
|
|
$this->assertEquals('127.0.0.1', (string)$endPoints[1][0]->getAddress());
|
|
$this->assertEquals(0, $endPoints[1][0]->getPort());
|
|
$this->assertEquals(10, $endPoints[1][1]);
|
|
|
|
$this->assertInstanceOf(IPEndPoint::class, $endPoints[2][0]);
|
|
$this->assertEquals('::1', (string)$endPoints[2][0]->getAddress());
|
|
$this->assertEquals(9001, $endPoints[2][0]->getPort());
|
|
$this->assertEquals(5, $endPoints[2][1]);
|
|
|
|
$this->assertInstanceOf(DnsEndPoint::class, $endPoints[3][0]);
|
|
$this->assertEquals('localhost', $endPoints[3][0]->getHost());
|
|
$this->assertEquals(0, $endPoints[3][0]->getPort());
|
|
$this->assertEquals(0, $endPoints[3][1]);
|
|
|
|
$this->assertEquals('', $info->getPrefixKey());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertNull($info->getPersistentName());
|
|
$this->assertTrue($info->shouldUseBinaryProtocol());
|
|
$this->assertTrue($info->shouldEnableCompression());
|
|
$this->assertTrue($info->shouldTcpNoDelay());
|
|
}
|
|
|
|
public function testValkeyDSN(): void {
|
|
$valkey = new ValkeyBackend;
|
|
|
|
// unix socket
|
|
$info = $valkey->parseDsn('valkey://:unix:/prefix/test/?socket=/var/run/valkey.sock');
|
|
|
|
$this->assertInstanceOf(UnixEndPoint::class, $info->getEndPoint());
|
|
$this->assertEquals('/var/run/valkey.sock', $info->getServerHost());
|
|
$this->assertEquals(0, $info->getServerPort());
|
|
$this->assertEquals('prefix:test:', $info->getPrefix());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertFalse($info->hasUsername());
|
|
$this->assertEmpty($info->getUsername());
|
|
$this->assertFalse($info->hasPassword());
|
|
$this->assertEmpty($info->getPassword());
|
|
$this->assertFalse($info->hasDatabaseNumber());
|
|
$this->assertEquals(0, $info->getDatabaseNumber());
|
|
|
|
// ipv4
|
|
$info = $valkey->parseDsn('keydb://password@127.0.0.1:6379?db=123');
|
|
|
|
$this->assertInstanceOf(IPEndPoint::class, $info->getEndPoint());
|
|
$this->assertEquals('127.0.0.1', $info->getServerHost());
|
|
$this->assertEquals(6379, $info->getServerPort());
|
|
$this->assertEmpty($info->getPrefix());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertFalse($info->hasUsername());
|
|
$this->assertEmpty($info->getUsername());
|
|
$this->assertTrue($info->hasPassword());
|
|
$this->assertEquals('password', $info->getPassword());
|
|
$this->assertTrue($info->hasDatabaseNumber());
|
|
$this->assertEquals(123, $info->getDatabaseNumber());
|
|
|
|
// ipv6
|
|
$info = $valkey->parseDsn('redis://username:password@[::1]/?persist&db=1');
|
|
|
|
$this->assertInstanceOf(IPEndPoint::class, $info->getEndPoint());
|
|
$this->assertEquals('::1', $info->getServerHost());
|
|
$this->assertEquals(0, $info->getServerPort());
|
|
$this->assertEmpty($info->getPrefix());
|
|
$this->assertTrue($info->isPersistent());
|
|
$this->assertTrue($info->hasUsername());
|
|
$this->assertEquals('username', $info->getUsername());
|
|
$this->assertTrue($info->hasPassword());
|
|
$this->assertEquals('password', $info->getPassword());
|
|
$this->assertTrue($info->hasDatabaseNumber());
|
|
$this->assertEquals(1, $info->getDatabaseNumber());
|
|
|
|
// dns
|
|
$info = $valkey->parseDsn('valkey://username:@misaka.nl/');
|
|
|
|
$this->assertInstanceOf(DnsEndPoint::class, $info->getEndPoint());
|
|
$this->assertEquals('misaka.nl', $info->getServerHost());
|
|
$this->assertEquals(0, $info->getServerPort());
|
|
$this->assertEmpty($info->getPrefix());
|
|
$this->assertFalse($info->isPersistent());
|
|
$this->assertTrue($info->hasUsername());
|
|
$this->assertEquals('username', $info->getUsername());
|
|
$this->assertFalse($info->hasPassword());
|
|
$this->assertEquals('', $info->getPassword());
|
|
$this->assertFalse($info->hasDatabaseNumber());
|
|
$this->assertEquals(0, $info->getDatabaseNumber());
|
|
}
|
|
}
|