Compare commits

...

2 Commits

2 changed files with 20 additions and 102 deletions

View File

@ -28,13 +28,13 @@ class Database {
*/
public function __construct($options) {
// check if PDO extension is loaded
if ( !extension_loaded('pdo') ) {
$error = getError('PDO extension not loaded.');
if (!extension_loaded('pdo')) {
throw new Exception('PDO extension not loaded.');
}
// options check
if (empty($options['type'])) {
$error = getError('Database type is not set.');
throw new Exception('Database type is not set.');
}
// connect based on database type
@ -42,15 +42,15 @@ class Database {
case 'sqlite':
$this->connectSqlite($options);
break;
case 'mysql' || 'mariadb':
case 'mysql':
case 'mariadb':
$this->connectMysql($options);
break;
default:
$error = getError("Database type \"{$options['type']}\" is not supported.");
$this->pdo = null;
}
}
/**
* Establishes a connection to a SQLite database.
*
@ -62,12 +62,17 @@ class Database {
private function connectSqlite($options) {
// pdo_sqlite extension is needed
if (!extension_loaded('pdo_sqlite')) {
$error = getError('PDO extension for SQLite not loaded.');
throw new Exception('PDO extension for SQLite not loaded.');
}
// SQLite options
if (empty($options['dbFile']) || !file_exists($options['dbFile'])) {
$error = getError("SQLite database file \"{$options['dbFile']}\" not found.");
if (empty($options['dbFile'])) {
throw new Exception('SQLite database file path is missing.');
}
// For in-memory database (especially for the tests), skip file check
if ($options['dbFile'] !== ':memory:' && !file_exists($options['dbFile'])) {
throw new Exception("SQLite database file \"{$options['dbFile']}\" not found.");
}
// connect to SQLite
@ -77,11 +82,10 @@ class Database {
// enable foreign key constraints (not ON by default in SQLite3)
$this->pdo->exec('PRAGMA foreign_keys = ON;');
} catch (PDOException $e) {
$error = getError('SQLite connection failed: ', $e->getMessage());
throw new Exception('SQLite connection failed: ' . $e->getMessage());
}
}
/**
* Establishes a connection to a MySQL (or MariaDB) database.
*
@ -97,25 +101,25 @@ class Database {
private function connectMysql($options) {
// pdo_mysql extension is needed
if (!extension_loaded('pdo_mysql')) {
$error = getError('PDO extension for MySQL not loaded.');
throw new Exception('PDO extension for MySQL not loaded.');
}
// MySQL options
if (empty($options['host']) || empty($options['dbname']) || empty($options['user'])) {
$error = getError('MySQL connection data is missing.');
throw new Exception('MySQL connection data is missing.');
}
// Connect to MySQL
try {
$dsn = "mysql:host={$options['host']};port={$options['port']};dbname={$options['dbname']};charset=utf8";
$port = $options['port'] ?? 3306;
$dsn = "mysql:host={$options['host']};port={$port};dbname={$options['dbname']};charset=utf8";
$this->pdo = new PDO($dsn, $options['user'], $options['password'] ?? '');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$error = getError('MySQL connection failed: ', $e->getMessage());
$this->pdo = null;
}
}
/**
* Retrieves the current PDO connection instance.
*

View File

@ -1,86 +0,0 @@
<?php
require_once dirname(__DIR__, 4) . '/app/classes/router.php';
use PHPUnit\Framework\TestCase;
class RouterTest extends TestCase
{
private $router;
protected function setUp(): void
{
parent::setUp();
$this->router = new Router('', true); // Empty controller path and dry-run mode
}
public function testAddRoute()
{
$this->router->add('/test', 'TestController@index');
$this->assertTrue(true); // If we get here, no exception was thrown
}
public function testDispatchRoute()
{
$this->router->add('/users/(\d+)', 'UserController@show');
$match = $this->router->dispatch('/users/123');
$this->assertIsArray($match);
$this->assertEquals('UserController@show', $match['callback']);
$this->assertEquals(['123'], $match['params']);
}
public function testDispatchRouteWithMultipleParameters()
{
$this->router->add('/users/(\d+)/posts/(\d+)', 'PostController@show');
$match = $this->router->dispatch('/users/123/posts/456');
$this->assertIsArray($match);
$this->assertEquals('PostController@show', $match['callback']);
$this->assertEquals(['123', '456'], $match['params']);
}
public function testNoMatchingRoute()
{
$this->router->add('/test', 'TestController@index');
$match = $this->router->dispatch('/nonexistent');
$this->assertNull($match);
}
public function testDispatchWithQueryString()
{
$this->router->add('/test', 'TestController@index');
$match = $this->router->dispatch('/test?param=value');
$this->assertIsArray($match);
$this->assertEquals('TestController@index', $match['callback']);
$this->assertEquals([], $match['params']);
}
public function testOptionalParameters()
{
$this->router->add('/users(?:/(\d+))?', 'UserController@index');
// Test with parameter
$match1 = $this->router->dispatch('/users/123');
$this->assertIsArray($match1);
$this->assertEquals('UserController@index', $match1['callback']);
$this->assertEquals(['123'], $match1['params']);
// Test without parameter
$match2 = $this->router->dispatch('/users');
$this->assertIsArray($match2);
$this->assertEquals('UserController@index', $match2['callback']);
$this->assertEquals([], $match2['params']);
}
public function testInvokeWithMissingController()
{
$router = new Router(''); // Empty controller path, not in dry-run mode
ob_start();
$router->dispatch('/test');
$output = ob_get_clean();
$this->assertEquals('404 page not found', $output);
}
}