index/tests/CacheBackendsTest.php

193 lines
8.6 KiB
PHP
Raw Normal View History

2024-04-10 22:23:34 +00:00
<?php
2024-10-18 20:56:56 +00:00
// CacheBackendsTest.php
2024-04-10 22:23:34 +00:00
// Created: 2024-04-10
2024-10-18 20:56:56 +00:00
// Updated: 2024-10-18
2024-04-10 22:23:34 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
2024-10-18 20:56:56 +00:00
use Index\Cache\CacheBackends;
2024-04-10 22:23:34 +00:00
use Index\Cache\ArrayCache\{ArrayCacheBackend,ArrayCacheProvider};
use Index\Cache\Memcached\MemcachedBackend;
use Index\Cache\Valkey\ValkeyBackend;
2024-10-02 01:35:05 +00:00
use Index\Net\{DnsEndPoint,IpEndPoint,UnixEndPoint};
2024-04-10 22:23:34 +00:00
2024-10-18 20:56:56 +00:00
#[CoversClass(CacheBackends::class)]
#[CoversClass(ArrayCacheBackend::class)]
#[CoversClass(ArrayCacheProvider::class)]
#[CoversClass(MemcachedBackend::class)]
#[CoversClass(ValkeyBackend::class)]
#[UsesClass(DnsEndPoint::class)]
2024-10-02 01:35:05 +00:00
#[UsesClass(IpEndPoint::class)]
#[UsesClass(UnixEndPoint::class)]
2024-10-18 20:56:56 +00:00
final class CacheBackendsTest extends TestCase {
2024-04-10 22:23:34 +00:00
public function testBasicDSN(): void {
2024-10-18 20:56:56 +00:00
$arrayCache = CacheBackends::create('array:');
2024-04-10 22:23:34 +00:00
$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));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $endPoints[0][0]);
2024-04-10 22:23:34 +00:00
$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));
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $endPoints[0][0]);
2024-04-10 22:23:34 +00:00
$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]);
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $endPoints[1][0]);
2024-04-10 22:23:34 +00:00
$this->assertEquals('127.0.0.1', (string)$endPoints[1][0]->getAddress());
$this->assertEquals(0, $endPoints[1][0]->getPort());
$this->assertEquals(10, $endPoints[1][1]);
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $endPoints[2][0]);
2024-04-10 22:23:34 +00:00
$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');
2024-04-10 22:23:34 +00:00
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $info->getEndPoint());
2024-04-10 22:23:34 +00:00
$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');
2024-04-10 22:23:34 +00:00
2024-10-02 01:35:05 +00:00
$this->assertInstanceOf(IpEndPoint::class, $info->getEndPoint());
2024-04-10 22:23:34 +00:00
$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());
}
}