Added testing
This commit is contained in:
65
tests/ConnectionTest.php
Normal file
65
tests/ConnectionTest.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use ProVM\Database\Connection;
|
||||
use ProVM\Concept;
|
||||
|
||||
class ConnectionTest extends TestCase
|
||||
{
|
||||
protected PDO $pdo;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->pdo = new PDO('sqlite::memory:');
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$this->pdo->query($query);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
unset($this->pdo);
|
||||
}
|
||||
|
||||
public function testConnection()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$this->assertEquals($this->pdo, $connection->connect());
|
||||
}
|
||||
public function testQuery()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$connection->query($query);
|
||||
$query = "SELECT * FROM test_table";
|
||||
$result = $connection->query($query);
|
||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
||||
}
|
||||
public function testPrepare()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$connection->query($query);
|
||||
$query = "SELECT * FROM test_table";
|
||||
$result = $connection->prepare($query);
|
||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
||||
}
|
||||
}
|
32
tests/MySQLTest.php
Normal file
32
tests/MySQLTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\MySQL;
|
||||
|
||||
class MySQLTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = "testhost";
|
||||
$port = 1234;
|
||||
$name = "testdb";
|
||||
$user = "testuser";
|
||||
$pass = "testpass";
|
||||
|
||||
$dsn = "mysql:host={$host};port={$port};dbname={$name}";
|
||||
|
||||
$database = new MySQL();
|
||||
$database->setHost($host);
|
||||
$database->setPort($port);
|
||||
$database->setName($name);
|
||||
$database->setUser($user);
|
||||
$database->setPassword($pass);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertEquals($port, $database->getPort());
|
||||
$this->assertEquals($name, $database->getName());
|
||||
$this->assertEquals($user, $database->getUser());
|
||||
$this->assertEquals($pass, $database->getPassword());
|
||||
$this->assertTrue($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
32
tests/PostgreSQLTest.php
Normal file
32
tests/PostgreSQLTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\PostgreSQL;
|
||||
|
||||
class PostgreSQLTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = "testhost";
|
||||
$port = 1234;
|
||||
$name = "testdb";
|
||||
$user = "testuser";
|
||||
$pass = "testpass";
|
||||
|
||||
$dsn = "pgsql:host={$host};port={$port};dbname={$name};user={$user};password={$pass}";
|
||||
|
||||
$database = new PostgreSQL();
|
||||
$database->setHost($host);
|
||||
$database->setPort($port);
|
||||
$database->setName($name);
|
||||
$database->setUser($user);
|
||||
$database->setPassword($pass);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertEquals($port, $database->getPort());
|
||||
$this->assertEquals($name, $database->getName());
|
||||
$this->assertEquals($user, $database->getUser());
|
||||
$this->assertEquals($pass, $database->getPassword());
|
||||
$this->assertFalse($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
26
tests/ResultSetTest.php
Normal file
26
tests/ResultSetTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use ProVM\Database\ResultSet;
|
||||
|
||||
class ResultSetTest extends TestCase
|
||||
{
|
||||
public function testResultSet()
|
||||
{
|
||||
$result1 = ['col1', 'col2', 'col3'];
|
||||
$result2 = [['col1', 'col2'], ['col3', 'col4']];
|
||||
|
||||
$statement = $this->getMockBuilder(PDOStatement::class)->getMock();
|
||||
$statement->method('execute')->willReturn(true);
|
||||
$statement->method('fetch')->willReturn($result1);
|
||||
$statement->method('fetchAll')->willReturn($result2);
|
||||
$statement->method('rowCount')->willReturn(2);
|
||||
|
||||
$resultSet = new ResultSet($statement);
|
||||
|
||||
$resultSet->execute(['foo' => 'bar']);
|
||||
$this->assertTrue(true);
|
||||
$this->assertEquals($result1, $resultSet->fetchFirst());
|
||||
$this->assertEquals($result2, $resultSet->fetchAll());
|
||||
}
|
||||
}
|
20
tests/SQLiteTest.php
Normal file
20
tests/SQLiteTest.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\SQLite;
|
||||
|
||||
class SQLiteTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = ":memory:";
|
||||
|
||||
$dsn = "sqlite:{$host}";
|
||||
|
||||
$database = new SQLite();
|
||||
$database->setHost($host);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertFalse($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
19
tests/TransactionTest.php
Normal file
19
tests/TransactionTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\Transaction;
|
||||
|
||||
class TransactionTest extends TestCase
|
||||
{
|
||||
public function testTransaction()
|
||||
{
|
||||
$connection = $this->createMock(ProVM\Concept\Database\Connection::class);
|
||||
$transaction = new Transaction($connection);
|
||||
$transaction->begin();
|
||||
$this->assertTrue(true);
|
||||
$transaction->commit();
|
||||
$this->assertTrue(true);
|
||||
$transaction->begin();
|
||||
$transaction->rollback();
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user