Files
database/tests/ConnectionTest.php

62 lines
1.9 KiB
PHP
Raw Normal View History

2024-04-22 11:56:36 -04:00
<?php
use PHPUnit\Framework\TestCase;
2025-09-30 17:15:39 -03:00
use Database\Implement\Connection;
use Database\Define;
2024-04-22 11:56:36 -04:00
class ConnectionTest extends TestCase
{
2025-09-30 17:15:39 -03:00
protected string $host = '/tmp/test.db3';
protected string $tableName = 'test_table';
2024-04-22 11:56:36 -04:00
protected PDO $pdo;
protected function setUp(): void
{
2025-09-30 17:15:39 -03:00
$this->pdo = new PDO("sqlite:{$this->host}");
$query = "CREATE TABLE IF NOT EXISTS {$this->tableName} (id INTEGER PRIMARY KEY, test TEXT)";
$this->pdo->query($query);
$query = "INSERT INTO {$this->tableName} (test) VALUES ('test')";
2024-04-22 11:56:36 -04:00
$this->pdo->query($query);
}
protected function tearDown(): void
{
unset($this->pdo);
2025-09-30 17:15:39 -03:00
unlink($this->host);
2024-04-22 11:56:36 -04:00
}
2025-09-30 17:15:39 -03:00
protected function getConnection(): Connection
2024-04-22 11:56:36 -04:00
{
2025-09-30 17:15:39 -03:00
$host = $this->host;
2024-04-22 11:56:36 -04:00
2025-09-30 17:15:39 -03:00
$database = $this->getMockBuilder(Define\Database::class)->getMock();
2024-04-22 11:56:36 -04:00
$database->method('getHost')->willReturn($host);
2025-09-30 17:15:39 -03:00
$database->method('getDsn')->willReturn("sqlite:{$host}");
2024-04-22 11:56:36 -04:00
$database->method('needsUser')->willReturn(false);
2025-09-30 17:15:39 -03:00
return new Connection($database);
}
public function testConnection()
{
$connection = $this->getConnection();
$this->assertEquals($this->pdo, $connection->getPDO());
2024-04-22 11:56:36 -04:00
}
public function testQuery()
{
2025-09-30 17:15:39 -03:00
$connection = $this->getConnection();
2024-04-22 11:56:36 -04:00
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
$connection->query($query);
$query = "SELECT * FROM test_table";
2025-09-30 17:15:39 -03:00
$connection->query($query);
$this->assertTrue(true);
2024-04-22 11:56:36 -04:00
}
public function testPrepare()
{
2025-09-30 17:15:39 -03:00
$connection = $this->getConnection();
2024-04-22 11:56:36 -04:00
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
$connection->query($query);
$query = "SELECT * FROM test_table";
2025-09-30 17:15:39 -03:00
$connection->prepare($query);
$this->assertTrue(true);
2024-04-22 11:56:36 -04:00
}
}