Skip to content

Commit

Permalink
test: added more tests for class Database
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 1, 2024
1 parent 797bb57 commit f830e82
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
1 change: 0 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static function factory(string $type): ?DatabaseDriver

if (class_exists($class)) {
self::$databaseDriver = new $class();

return self::$databaseDriver;
}

Expand Down
53 changes: 51 additions & 2 deletions tests/phpMyFAQ/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database\Mysqli;
use phpMyFAQ\Database\Sqlite3;
use PHPUnit\Framework\TestCase;

class DatabaseTest extends TestCase
Expand All @@ -14,8 +15,56 @@ class DatabaseTest extends TestCase
*/
public function testFactoryReturnsInstanceOfDatabaseDriver(): void
{
$type = 'mysqli';
$type = 'sqlite3';
$driver = Database::factory($type);
$this->assertInstanceOf(Mysqli::class, $driver);
$this->assertInstanceOf(Sqlite3::class, $driver);
}

public function testGetInstance(): void
{
$instance = Database::getInstance();

$this->assertInstanceOf(Sqlite3::class, $instance);
$this->assertSame($instance, Database::getInstance());
}

public function testGetType(): void
{
$expectedType = 'sqlite3';

Database::getInstance();

$actualType = Database::getType();
$this->assertEquals($expectedType, $actualType);
}

public function testCheckOnEmptyTable(): void
{
$expected = 0;

Database::getInstance();

$actual = Database::checkOnEmptyTable('faqconfig');
$this->assertEquals($expected, $actual);
}

public function testErrorPage()
{
ob_start();
Database::errorPage('Error message');
$output = ob_get_clean();

$this->assertStringContainsString(
'<title>Fatal phpMyFAQ Error</title>',
$output
);
$this->assertStringContainsString(
'<p class="alert alert-danger">The connection to the database server could not be established.</p>',
$output
);
$this->assertStringContainsString(
'<p class="alert alert-danger">The error message of the database server: Error message</p>',
$output
);
}
}

0 comments on commit f830e82

Please sign in to comment.