Changed namespaces and added Query Builder Co-authored-by: Juan Pablo Vial <jpvial@goacegroup.com> Reviewed-on: #2
62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Database\Implement\Connection;
|
|
use Database\Define;
|
|
|
|
class ConnectionTest extends TestCase
|
|
{
|
|
protected string $host = '/tmp/test.db3';
|
|
protected string $tableName = 'test_table';
|
|
protected PDO $pdo;
|
|
protected function setUp(): void
|
|
{
|
|
$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')";
|
|
$this->pdo->query($query);
|
|
}
|
|
protected function tearDown(): void
|
|
{
|
|
unset($this->pdo);
|
|
unlink($this->host);
|
|
}
|
|
|
|
protected function getConnection(): Connection
|
|
{
|
|
$host = $this->host;
|
|
|
|
$database = $this->getMockBuilder(Define\Database::class)->getMock();
|
|
$database->method('getHost')->willReturn($host);
|
|
$database->method('getDsn')->willReturn("sqlite:{$host}");
|
|
$database->method('needsUser')->willReturn(false);
|
|
|
|
return new Connection($database);
|
|
}
|
|
|
|
public function testConnection()
|
|
{
|
|
$connection = $this->getConnection();
|
|
$this->assertEquals($this->pdo, $connection->getPDO());
|
|
}
|
|
public function testQuery()
|
|
{
|
|
$connection = $this->getConnection();
|
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
|
$connection->query($query);
|
|
$query = "SELECT * FROM test_table";
|
|
$connection->query($query);
|
|
$this->assertTrue(true);
|
|
}
|
|
public function testPrepare()
|
|
{
|
|
$connection = $this->getConnection();
|
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
|
$connection->query($query);
|
|
$query = "SELECT * FROM test_table";
|
|
$connection->prepare($query);
|
|
$this->assertTrue(true);
|
|
}
|
|
}
|