test: setup database for upcoming tests
This commit is contained in:
57
tests/Feature/ExecuteContainerCommandTest.php
Normal file
57
tests/Feature/ExecuteContainerCommandTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Application;
|
||||
use App\Models\Server;
|
||||
use App\Models\User;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
use Tests\Traits\HandlesTestDatabase;
|
||||
|
||||
class ExecuteContainerCommandTest extends TestCase
|
||||
{
|
||||
use HandlesTestDatabase;
|
||||
|
||||
private $user;
|
||||
|
||||
private $team;
|
||||
|
||||
private $server;
|
||||
|
||||
private $application;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Only set up database for tests that need it
|
||||
if ($this->shouldSetUpDatabase()) {
|
||||
$this->setUpTestDatabase();
|
||||
}
|
||||
// Create test data
|
||||
$this->user = User::factory()->create();
|
||||
$this->team = $this->user->teams()->first();
|
||||
$this->server = Server::factory()->create(['team_id' => $this->team->id]);
|
||||
$this->application = Application::factory()->create();
|
||||
|
||||
// Login the user
|
||||
$this->actingAs($this->user);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->shouldSetUpDatabase()) {
|
||||
$this->tearDownTestDatabase();
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
private function shouldSetUpDatabase(): bool
|
||||
{
|
||||
return in_array($this->name(), [
|
||||
'it_allows_valid_container_access',
|
||||
'it_prevents_cross_server_container_access',
|
||||
]);
|
||||
}
|
||||
}
|
78
tests/Traits/HandlesTestDatabase.php
Normal file
78
tests/Traits/HandlesTestDatabase.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
trait HandlesTestDatabase
|
||||
{
|
||||
protected function setUpTestDatabase(): void
|
||||
{
|
||||
try {
|
||||
// Create test database if it doesn't exist
|
||||
$database = config('database.connections.testing.database');
|
||||
$this->createTestDatabase($database);
|
||||
|
||||
// Run migrations
|
||||
Artisan::call('migrate:fresh', [
|
||||
'--database' => 'testing',
|
||||
'--seed' => false,
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->tearDownTestDatabase();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDownTestDatabase(): void
|
||||
{
|
||||
try {
|
||||
// Drop test database
|
||||
$database = config('database.connections.testing.database');
|
||||
$this->dropTestDatabase($database);
|
||||
} catch (\Exception $e) {
|
||||
// Log error but don't throw
|
||||
error_log('Failed to tear down test database: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected function createTestDatabase($database)
|
||||
{
|
||||
try {
|
||||
// Connect to postgres database to create/drop test database
|
||||
config(['database.connections.pgsql.database' => 'postgres']);
|
||||
DB::purge('pgsql');
|
||||
DB::reconnect('pgsql');
|
||||
|
||||
// Drop if exists and create new database
|
||||
DB::connection('pgsql')->statement("DROP DATABASE IF EXISTS $database WITH (FORCE);");
|
||||
DB::connection('pgsql')->statement("CREATE DATABASE $database;");
|
||||
|
||||
// Switch back to testing connection
|
||||
DB::disconnect('pgsql');
|
||||
DB::reconnect('testing');
|
||||
} catch (\Exception $e) {
|
||||
$this->tearDownTestDatabase();
|
||||
throw new \Exception('Could not create test database: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
protected function dropTestDatabase($database)
|
||||
{
|
||||
try {
|
||||
// Connect to postgres database to drop test database
|
||||
config(['database.connections.pgsql.database' => 'postgres']);
|
||||
DB::purge('pgsql');
|
||||
DB::reconnect('pgsql');
|
||||
|
||||
// Drop the test database
|
||||
DB::connection('pgsql')->statement("DROP DATABASE IF EXISTS $database WITH (FORCE);");
|
||||
|
||||
DB::disconnect('pgsql');
|
||||
} catch (\Exception $e) {
|
||||
// Log error but don't throw
|
||||
error_log('Failed to drop test database: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user