@@ -2,19 +2,39 @@
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
use Config\Database;
|
||||
use Throwable;
|
||||
|
||||
class DBReset
|
||||
{
|
||||
private const DB_GROUP = 'tests';
|
||||
|
||||
protected static array $excludeTables = ['migrations'];
|
||||
|
||||
public static function resetDatabase(): void
|
||||
{
|
||||
$db = Database::connect();
|
||||
$config = config('Database');
|
||||
$dbConfig = $config->{self::DB_GROUP} ?? [];
|
||||
|
||||
try {
|
||||
$db = Database::connect(self::DB_GROUP);
|
||||
$db->initialize();
|
||||
} catch (Throwable $exception) {
|
||||
throw new \RuntimeException(
|
||||
sprintf(
|
||||
'Test database connection failed: group=%s host=%s port=%s database=%s',
|
||||
self::DB_GROUP,
|
||||
$dbConfig['hostname'] ?? '',
|
||||
$dbConfig['port'] ?? '',
|
||||
$dbConfig['database'] ?? ''
|
||||
),
|
||||
0,
|
||||
$exception
|
||||
);
|
||||
}
|
||||
|
||||
$db->disableForeignKeyChecks();
|
||||
|
||||
// Get all table names
|
||||
$tables = $db->listTables();
|
||||
|
||||
foreach ($tables as $table) {
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$dumpPath = $argv[1] ?? null;
|
||||
|
||||
if ($dumpPath === null || ! is_file($dumpPath) || ! is_readable($dumpPath)) {
|
||||
fwrite(STDERR, "Usage: php tests/_support/import_baseline.php path/to/dump.sql\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$requiredEnv = [
|
||||
'TEST_DB_HOST',
|
||||
'TEST_DB_USER',
|
||||
'TEST_DB_PASSWORD',
|
||||
'TEST_DB_NAME',
|
||||
'TEST_DB_PORT',
|
||||
];
|
||||
|
||||
foreach ($requiredEnv as $name) {
|
||||
if (getenv($name) === false || getenv($name) === '') {
|
||||
fwrite(STDERR, "Missing required environment variable: {$name}\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
||||
|
||||
$db = new mysqli(
|
||||
getenv('TEST_DB_HOST'),
|
||||
getenv('TEST_DB_USER'),
|
||||
getenv('TEST_DB_PASSWORD'),
|
||||
getenv('TEST_DB_NAME'),
|
||||
(int) getenv('TEST_DB_PORT')
|
||||
);
|
||||
|
||||
$db->set_charset('utf8mb4');
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=0');
|
||||
|
||||
$tables = $db->query('SHOW FULL TABLES WHERE Table_type = "BASE TABLE"')->fetch_all(MYSQLI_NUM);
|
||||
|
||||
foreach ($tables as [$table]) {
|
||||
$db->query('DROP TABLE IF EXISTS `' . str_replace('`', '``', $table) . '`');
|
||||
}
|
||||
|
||||
$db->query('SET FOREIGN_KEY_CHECKS=1');
|
||||
|
||||
$sql = file_get_contents($dumpPath);
|
||||
|
||||
if ($sql === false) {
|
||||
fwrite(STDERR, "Unable to read baseline dump: {$dumpPath}\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$db->multi_query($sql);
|
||||
|
||||
do {
|
||||
if ($result = $db->store_result()) {
|
||||
$result->free();
|
||||
}
|
||||
} while ($db->more_results() && $db->next_result());
|
||||
|
||||
$baselineVersion = '2025-12-07-235959';
|
||||
$migrationFiles = glob(dirname(__DIR__, 2) . '/app/Database/Migrations/*.php') ?: [];
|
||||
$maxId = (int) ($db->query('SELECT COALESCE(MAX(id), 0) AS id FROM migrations')->fetch_assoc()['id'] ?? 0);
|
||||
$maxBatch = (int) ($db->query('SELECT COALESCE(MAX(batch), 0) AS batch FROM migrations')->fetch_assoc()['batch'] ?? 0);
|
||||
|
||||
foreach ($migrationFiles as $file) {
|
||||
$filename = basename($file, '.php');
|
||||
|
||||
if (! preg_match('/^(\d{4}-\d{2}-\d{2}-\d{6})_(.+)$/', $filename, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
[$all, $version] = $matches;
|
||||
|
||||
if ($version > $baselineVersion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$contents = file_get_contents($file);
|
||||
|
||||
if ($contents === false || ! preg_match('/class\s+([A-Za-z_][A-Za-z0-9_]*)\s+extends\s+Migration/', $contents, $classMatch)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$class = 'App\\Database\\Migrations\\' . $classMatch[1];
|
||||
$exists = $db
|
||||
->query(
|
||||
'SELECT 1 FROM migrations WHERE version = "' . $db->real_escape_string($version) . '"'
|
||||
. ' AND class = "' . $db->real_escape_string($class) . '" LIMIT 1'
|
||||
)
|
||||
->num_rows > 0;
|
||||
|
||||
if ($exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$date = DateTimeImmutable::createFromFormat('Y-m-d-His', $version);
|
||||
$time = $date instanceof DateTimeImmutable ? $date->getTimestamp() : time();
|
||||
|
||||
$statement = $db->prepare(
|
||||
'INSERT INTO migrations (id, version, class, `group`, namespace, time, batch) VALUES (?, ?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
|
||||
$id = ++$maxId;
|
||||
$group = 'default';
|
||||
$namespace = 'App';
|
||||
$batch = $maxBatch;
|
||||
$statement->bind_param('issssii', $id, $version, $class, $group, $namespace, $time, $batch);
|
||||
$statement->execute();
|
||||
}
|
||||
|
||||
$count = $db->query('SHOW TABLES')->num_rows;
|
||||
|
||||
if ($count === 0) {
|
||||
fwrite(STDERR, "Baseline import produced no tables\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "Baseline schema imported: {$count} table(s)\n";
|
||||
@@ -16,25 +16,48 @@ class AttendanceDataModelTest extends CIUnitTestCase
|
||||
|
||||
public function testCanInsertAndRetrieve()
|
||||
{
|
||||
$record = fake(AttendanceDataModel::class);
|
||||
$this->assertNotNull($record->id);
|
||||
$fetched = (new AttendanceDataModel())->find($record->id);
|
||||
$this->assertEquals($record->id, $fetched->id);
|
||||
$model = new AttendanceDataModel();
|
||||
$id = $model->insert($this->validAttendanceRow());
|
||||
|
||||
$this->assertNotFalse($id);
|
||||
|
||||
$fetched = $model->find($id);
|
||||
$this->assertSame((int) $id, (int) $fetched['id']);
|
||||
}
|
||||
|
||||
public function testCanUpdate()
|
||||
{
|
||||
$record = fake(AttendanceDataModel::class);
|
||||
$model = new AttendanceDataModel();
|
||||
$model->update($record->id, ['updated_at' => date('Y-m-d H:i:s')]);
|
||||
$this->assertTrue(true); // simple test to verify no exception thrown
|
||||
$id = $model->insert($this->validAttendanceRow());
|
||||
|
||||
$this->assertTrue($model->update($id, ['updated_at' => date('Y-m-d H:i:s')]));
|
||||
}
|
||||
|
||||
public function testCanDelete()
|
||||
{
|
||||
$record = fake(AttendanceDataModel::class);
|
||||
$model = new AttendanceDataModel();
|
||||
$model->delete($record->id);
|
||||
$this->assertNull($model->find($record->id));
|
||||
$id = $model->insert($this->validAttendanceRow());
|
||||
|
||||
$model->delete($id);
|
||||
|
||||
$this->assertNull($model->find($id));
|
||||
}
|
||||
|
||||
private function validAttendanceRow(): array
|
||||
{
|
||||
return [
|
||||
'class_id' => 1,
|
||||
'class_section_id' => 1,
|
||||
'school_id' => 1,
|
||||
'student_id' => 1,
|
||||
'date' => '2026-01-01',
|
||||
'status' => 'present',
|
||||
'reason' => '',
|
||||
'is_reported' => 'no',
|
||||
'is_notified' => 'no',
|
||||
'semester' => 'Fall',
|
||||
'school_year' => '2025-2026',
|
||||
'modified_by' => 1,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user