recreate project
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
|
||||
class BaseWebTestCase extends CIUnitTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
// Start session if not already started
|
||||
if (!session()->isStarted()) {
|
||||
session()->start();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set session role and user ID
|
||||
*
|
||||
* @param string $role e.g., 'administrator', 'teacher', 'parent', etc.
|
||||
* @param int $userId Default to 1
|
||||
*/
|
||||
protected function setSessionRole(string $role, int $userId = 1): void
|
||||
{
|
||||
session()->set([
|
||||
'role' => strtolower($role),
|
||||
'user_id' => $userId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support;
|
||||
|
||||
use CodeIgniter\Test\CIUnitTestCase;
|
||||
use Config\Database;
|
||||
|
||||
class DBReset
|
||||
{
|
||||
protected static array $excludeTables = ['migrations'];
|
||||
|
||||
public static function resetDatabase(): void
|
||||
{
|
||||
$db = Database::connect();
|
||||
$db->disableForeignKeyChecks();
|
||||
|
||||
// Get all table names
|
||||
$tables = $db->listTables();
|
||||
|
||||
foreach ($tables as $table) {
|
||||
if (!in_array($table, self::$excludeTables)) {
|
||||
$db->table($table)->truncate();
|
||||
}
|
||||
}
|
||||
|
||||
$db->enableForeignKeyChecks();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class ExampleMigration extends Migration
|
||||
{
|
||||
protected $DBGroup = 'tests';
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
$this->forge->addField('id');
|
||||
$this->forge->addField([
|
||||
'name' => ['type' => 'varchar', 'constraint' => 31],
|
||||
'uid' => ['type' => 'varchar', 'constraint' => 31],
|
||||
'class' => ['type' => 'varchar', 'constraint' => 63],
|
||||
'icon' => ['type' => 'varchar', 'constraint' => 31],
|
||||
'summary' => ['type' => 'varchar', 'constraint' => 255],
|
||||
'created_at' => ['type' => 'datetime', 'null' => true],
|
||||
'updated_at' => ['type' => 'datetime', 'null' => true],
|
||||
'deleted_at' => ['type' => 'datetime', 'null' => true],
|
||||
]);
|
||||
|
||||
$this->forge->addKey('name');
|
||||
$this->forge->addKey('uid');
|
||||
$this->forge->addKey(['deleted_at', 'id']);
|
||||
$this->forge->addKey('created_at');
|
||||
|
||||
$this->forge->createTable('factories');
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->forge->dropTable('factories');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support\Database\Seeds;
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class AttendanceSeeder extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'class_section_id' => 1,
|
||||
'class_section_id' => 1,
|
||||
'student_id' => 1,
|
||||
'date' => '2024-08-20',
|
||||
'status' => 'Absent',
|
||||
'reason' => 'Sick',
|
||||
'modified_by' => 1,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
],
|
||||
// Add more records as necessary
|
||||
];
|
||||
|
||||
// Insert the data into the database
|
||||
$this->db->table('attendance')->insertBatch($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support\Database\Seeds;
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class ClassSeeder extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'class_name' => 'Math 101',
|
||||
'section' => 'A',
|
||||
'schedule' => 'MWF 9:00-10:00',
|
||||
'capacity' => 25,
|
||||
],
|
||||
[
|
||||
'class_name' => 'English 101',
|
||||
'section' => 'B',
|
||||
'schedule' => 'TTh 10:00-11:30',
|
||||
'capacity' => 30,
|
||||
],
|
||||
];
|
||||
|
||||
$this->db->table('classes')->insertBatch($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support\Database\Seeds;
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class ExampleSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$factories = [
|
||||
[
|
||||
'name' => 'Test Factory',
|
||||
'uid' => 'test001',
|
||||
'class' => 'Factories\Tests\NewFactory',
|
||||
'icon' => 'fas fa-puzzle-piece',
|
||||
'summary' => 'Longer sample text for testing',
|
||||
],
|
||||
[
|
||||
'name' => 'Widget Factory',
|
||||
'uid' => 'widget',
|
||||
'class' => 'Factories\Tests\WidgetPlant',
|
||||
'icon' => 'fas fa-puzzle-piece',
|
||||
'summary' => 'Create widgets in your factory',
|
||||
],
|
||||
[
|
||||
'name' => 'Evil Factory',
|
||||
'uid' => 'evil-maker',
|
||||
'class' => 'Factories\Evil\MyFactory',
|
||||
'icon' => 'fas fa-book-dead',
|
||||
'summary' => 'Abandon all hope, ye who enter here',
|
||||
],
|
||||
];
|
||||
|
||||
$builder = $this->db->table('factories');
|
||||
|
||||
foreach ($factories as $factory) {
|
||||
$builder->insert($factory);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
// tests/_support/bootstrap.php
|
||||
|
||||
// Load Composer autoloader
|
||||
require __DIR__ . '/../../vendor/autoload.php';
|
||||
|
||||
// Load CodeIgniter testing bootstrap from Composer package
|
||||
require __DIR__ . '/../../vendor/codeigniter4/framework/system/Test/bootstrap.php';
|
||||
Reference in New Issue
Block a user