2022-09-13 13:13:11 +00:00
|
|
|
<?php
|
|
|
|
// DbToolsTest.php
|
|
|
|
// Created: 2021-04-28
|
2024-10-04 22:35:57 +00:00
|
|
|
// Updated: 2024-10-04
|
2022-09-13 13:13:11 +00:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-08-01 23:17:11 +00:00
|
|
|
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
|
2024-10-04 22:35:57 +00:00
|
|
|
use Index\Db\{DbTools,DbType};
|
|
|
|
use Index\Db\MariaDb\MariaDbBackend;
|
|
|
|
use Index\Db\NullDb\NullDbConnection;
|
|
|
|
use Index\Db\Sqlite\SqliteBackend;
|
2022-09-13 13:13:11 +00:00
|
|
|
|
2024-07-31 18:12:46 +00:00
|
|
|
#[CoversClass(DbTools::class)]
|
|
|
|
#[CoversClass(DbType::class)]
|
|
|
|
#[CoversClass(NullDbConnection::class)]
|
2024-10-02 01:35:05 +00:00
|
|
|
#[CoversClass(MariaDbBackend::class)]
|
|
|
|
#[CoversClass(SqliteBackend::class)]
|
2022-09-13 13:13:11 +00:00
|
|
|
final class DbToolsTest extends TestCase {
|
|
|
|
public function testDetectType(): void {
|
|
|
|
$this->assertEquals(DbType::NULL, DbTools::detectType(null));
|
|
|
|
$this->assertEquals(DbType::INTEGER, DbTools::detectType(12345));
|
|
|
|
$this->assertEquals(DbType::FLOAT, DbTools::detectType(123.45));
|
|
|
|
$this->assertEquals(DbType::STRING, DbTools::detectType('This is a string.'));
|
2024-10-02 16:17:48 +00:00
|
|
|
|
|
|
|
$blob = fopen('php://memory', 'r+b');
|
|
|
|
if($blob === false)
|
|
|
|
throw new RuntimeException('failed to fopen a memory stream');
|
|
|
|
|
|
|
|
fwrite($blob, 'This is a string inside a memory stream.');
|
|
|
|
fseek($blob, 0);
|
|
|
|
|
|
|
|
$this->assertEquals(DbType::BLOB, DbTools::detectType($blob));
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDSN(): void {
|
|
|
|
$nullDbConn1 = DbTools::create('null:');
|
|
|
|
$this->assertInstanceOf(NullDbConnection::class, $nullDbConn1);
|
|
|
|
|
2024-10-04 22:35:57 +00:00
|
|
|
$nullDbConn2 = DbTools::create('Index-Db-NullDb-NullDbBackend:');
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertInstanceOf(NullDbConnection::class, $nullDbConn2);
|
|
|
|
|
2024-10-02 01:35:05 +00:00
|
|
|
$maria = new MariaDbBackend;
|
|
|
|
$sqlite = new SqliteBackend;
|
2022-09-13 13:13:11 +00:00
|
|
|
|
|
|
|
// flashii style misuzu connections details
|
|
|
|
$mci1 = $maria->parseDsn('mariadb://flashii:TiFGzCTxzx0n2HYzmNZpa98j255X7W4B@:unix:/flashii/misuzu?socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4&init=SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'');
|
|
|
|
|
|
|
|
$this->assertTrue($mci1->isUnixSocket());
|
|
|
|
$this->assertEquals('/var/run/mysqld/mysqld.sock', (string)$mci1->getEndPoint());
|
|
|
|
$this->assertEquals('', $mci1->getHost());
|
|
|
|
$this->assertEquals(-1, $mci1->getPort());
|
|
|
|
$this->assertEquals('/var/run/mysqld/mysqld.sock', $mci1->getSocketPath());
|
|
|
|
$this->assertEquals('flashii', $mci1->getUserName());
|
|
|
|
$this->assertEquals('TiFGzCTxzx0n2HYzmNZpa98j255X7W4B', $mci1->getPassword());
|
|
|
|
$this->assertEquals('flashii_misuzu', $mci1->getDatabaseName());
|
|
|
|
$this->assertTrue($mci1->hasCharacterSet());
|
|
|
|
$this->assertEquals('utf8mb4', $mci1->getCharacterSet());
|
|
|
|
$this->assertTrue($mci1->hasInitCommand());
|
|
|
|
$this->assertEquals('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\'', $mci1->getInitCommand());
|
|
|
|
$this->assertFalse($mci1->isSecure());
|
|
|
|
$this->assertNull($mci1->getKeyPath());
|
|
|
|
$this->assertNull($mci1->getCertificatePath());
|
|
|
|
$this->assertNull($mci1->getCertificateAuthorityPath());
|
|
|
|
$this->assertNull($mci1->getTrustedCertificatesPath());
|
|
|
|
$this->assertNull($mci1->getCipherAlgorithms());
|
2024-04-10 23:26:13 +00:00
|
|
|
$this->assertTrue($mci1->shouldVerifyCertificate());
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertFalse($mci1->shouldUseCompression());
|
|
|
|
|
|
|
|
// generic ipv4
|
|
|
|
$mci2 = $maria->parseDsn('mariadb://username:password@127.0.0.1/database');
|
|
|
|
$this->assertEquals('127.0.0.1', (string)$mci2->getEndPoint());
|
|
|
|
$this->assertEquals('127.0.0.1', $mci2->getHost());
|
|
|
|
$this->assertEquals(3306, $mci2->getPort());
|
|
|
|
$this->assertEquals('', $mci2->getSocketPath());
|
|
|
|
$this->assertEquals('username', $mci2->getUserName());
|
|
|
|
$this->assertEquals('password', $mci2->getPassword());
|
|
|
|
$this->assertEquals('database', $mci2->getDatabaseName());
|
|
|
|
$this->assertFalse($mci2->hasCharacterSet());
|
|
|
|
$this->assertNull($mci2->getCharacterSet());
|
|
|
|
$this->assertFalse($mci2->hasInitCommand());
|
|
|
|
$this->assertNull($mci2->getInitCommand());
|
|
|
|
$this->assertFalse($mci2->isSecure());
|
|
|
|
$this->assertNull($mci2->getKeyPath());
|
|
|
|
$this->assertNull($mci2->getCertificatePath());
|
|
|
|
$this->assertNull($mci2->getCertificateAuthorityPath());
|
|
|
|
$this->assertNull($mci2->getTrustedCertificatesPath());
|
|
|
|
$this->assertNull($mci2->getCipherAlgorithms());
|
2024-04-10 23:26:13 +00:00
|
|
|
$this->assertTrue($mci2->shouldVerifyCertificate());
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertFalse($mci2->shouldUseCompression());
|
|
|
|
|
|
|
|
// generic ipv6 with port
|
|
|
|
$mci3 = $maria->parseDsn('mysql://username:password@[::1]:9001/database');
|
|
|
|
$this->assertEquals('[::1]:9001', (string)$mci3->getEndPoint());
|
|
|
|
$this->assertEquals('::1', $mci3->getHost());
|
|
|
|
$this->assertEquals(9001, $mci3->getPort());
|
|
|
|
$this->assertEquals('', $mci3->getSocketPath());
|
|
|
|
$this->assertEquals('username', $mci3->getUserName());
|
|
|
|
$this->assertEquals('password', $mci3->getPassword());
|
|
|
|
$this->assertEquals('database', $mci3->getDatabaseName());
|
|
|
|
$this->assertFalse($mci3->hasCharacterSet());
|
|
|
|
$this->assertNull($mci3->getCharacterSet());
|
|
|
|
$this->assertFalse($mci3->hasInitCommand());
|
|
|
|
$this->assertNull($mci3->getInitCommand());
|
|
|
|
$this->assertFalse($mci3->isSecure());
|
|
|
|
$this->assertNull($mci3->getKeyPath());
|
|
|
|
$this->assertNull($mci3->getCertificatePath());
|
|
|
|
$this->assertNull($mci3->getCertificateAuthorityPath());
|
|
|
|
$this->assertNull($mci3->getTrustedCertificatesPath());
|
|
|
|
$this->assertNull($mci3->getCipherAlgorithms());
|
2024-04-10 23:26:13 +00:00
|
|
|
$this->assertTrue($mci3->shouldVerifyCertificate());
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertFalse($mci3->shouldUseCompression());
|
|
|
|
|
|
|
|
// sqlite normal
|
2024-04-10 23:26:13 +00:00
|
|
|
$sql1 = $sqlite->parseDsn('sqlite:/path/to/database.db?key=ebwOKzGkLYIxDGXk&openOnly');
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertEquals('/path/to/database.db', $sql1->getFileName());
|
|
|
|
$this->assertEquals('ebwOKzGkLYIxDGXk', $sql1->getEncryptionKey());
|
|
|
|
$this->assertFalse($sql1->shouldReadOnly());
|
|
|
|
$this->assertFalse($sql1->shouldCreate());
|
|
|
|
|
|
|
|
// sqlite temp file
|
2024-04-10 23:26:13 +00:00
|
|
|
$sql1 = $sqlite->parseDsn('sqlite:?key=Y63vrCttK8bEdPUIXVCOiLQKIgdbUn8V&readOnly');
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertEquals('', $sql1->getFileName());
|
|
|
|
$this->assertEquals('Y63vrCttK8bEdPUIXVCOiLQKIgdbUn8V', $sql1->getEncryptionKey());
|
2024-04-10 23:26:13 +00:00
|
|
|
$this->assertTrue($sql1->shouldReadOnly());
|
2022-09-13 13:13:11 +00:00
|
|
|
$this->assertTrue($sql1->shouldCreate());
|
|
|
|
|
|
|
|
// sqlite memory
|
|
|
|
$sql1 = $sqlite->parseDsn('sqlite::memory:');
|
|
|
|
$this->assertEquals(':memory:', $sql1->getFileName());
|
|
|
|
$this->assertEquals('', $sql1->getEncryptionKey());
|
|
|
|
$this->assertFalse($sql1->shouldReadOnly());
|
|
|
|
$this->assertTrue($sql1->shouldCreate());
|
|
|
|
}
|
|
|
|
}
|