jilo-web/tests/Unit/Classes/DatabaseTest.php

83 lines
2.3 KiB
PHP
Raw Normal View History

2025-02-18 14:36:31 +00:00
<?php
use PHPUnit\Framework\TestCase;
class DatabaseTest extends TestCase
{
private $config;
protected function setUp(): void
{
parent::setUp();
2025-02-19 13:31:01 +00:00
$this->config = test_db_config();
2025-02-18 14:36:31 +00:00
}
public function testDatabaseConnection()
{
$db = new Database($this->config);
$this->assertNotNull($db->getConnection());
}
public function testMysqlAndMariadbEquivalence()
{
$baseConfig = test_db_config();
$mysqlConfig = array_merge($baseConfig, ['type' => 'mysql']);
$mariadbConfig = array_merge($baseConfig, ['type' => 'mariadb']);
// Both should connect successfully
2025-02-18 14:36:31 +00:00
$mysqlDb = new Database($mysqlConfig);
$mariadbDb = new Database($mariadbConfig);
2025-02-19 13:31:01 +00:00
$this->assertNotNull($mysqlDb->getConnection());
$this->assertNotNull($mariadbDb->getConnection());
2025-02-18 14:36:31 +00:00
}
public function testInvalidDatabaseType()
{
require_once dirname(__DIR__, 3) . '/app/includes/errors.php';
global $config;
$config = ['environment' => 'development'];
$invalidConfig = array_merge(test_db_config(), ['type' => 'invalid']);
try {
$db = new Database($invalidConfig);
$connection = $db->getConnection();
$this->assertNull($connection, 'Connection should be null for invalid database type');
} catch (Exception $e) {
// Either an exception or null connection is acceptable
$this->assertTrue(true);
}
2025-02-18 14:36:31 +00:00
}
public function testMysqlConnectionMissingData()
2025-02-18 14:36:31 +00:00
{
$config = test_db_config();
$config['type'] = 'mysql';
$config['user'] = '';
2025-02-18 14:36:31 +00:00
$this->expectException(Exception::class);
$this->expectExceptionMessage('MySQL connection data is missing');
new Database($config);
}
public function testPrepareAndExecute()
{
$db = new Database($this->config);
$pdo = $db->getConnection();
$stmt = $pdo->prepare("SELECT 1");
$this->assertTrue($stmt->execute());
2025-02-18 14:36:31 +00:00
}
public function testTransaction()
{
$db = new Database($this->config);
$pdo = $db->getConnection();
$this->assertTrue($pdo->beginTransaction());
$this->assertTrue($pdo->commit());
2025-02-18 14:36:31 +00:00
}
}