Skip to content

Commit

Permalink
Disconnection after testing with traits for database testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KentarouTakeda committed Dec 11, 2023
1 parent 4fe214d commit 71c6653
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Foundation/Testing/DatabaseMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function runDatabaseMigrations()
$this->artisan('migrate:rollback');

RefreshDatabaseState::$migrated = false;

$database = $this->app->make('db');
foreach (array_keys($database->getConnections()) as $name) {
$database->purge($name);
}
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/Illuminate/Foundation/Testing/DatabaseTruncation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ protected function truncateDatabaseTables(): void
{
$this->beforeTruncatingDatabase();

$this->beforeApplicationDestroyed(function () {
$database = $this->app->make('db');
foreach (array_keys($database->getConnections()) as $name) {
$database->purge($name);
}
});

// Migrate and seed the database on first run...
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
Expand Down
17 changes: 17 additions & 0 deletions tests/Foundation/Testing/DatabaseMigrationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Illuminate\Foundation\Testing\DatabaseMigrations;
Expand Down Expand Up @@ -104,4 +106,19 @@ public function testRefreshTestDatabaseWithDropTypesOption()

$this->runDatabaseMigrations();
}

public function testDisconnectionAfterTestCompletion()
{
$this->app->instance(ConsoleKernelContract::class, m::spy(ConsoleKernel::class));
$this->app->instance('db', $database = m::mock(DatabaseManager::class));

$database->shouldReceive('getConnections')->once()->andReturn([
'default' => m::mock(ConnectionInterface::class),
'mysql' => m::mock(ConnectionInterface::class),
]);
$database->shouldReceive('purge')->with('default');
$database->shouldReceive('purge')->with('mysql');

$this->runDatabaseMigrations();
}
}
60 changes: 60 additions & 0 deletions tests/Foundation/Testing/DatabaseTruncationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Illuminate\Tests\Foundation\Testing;

use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Mockery as m;
use Orchestra\Testbench\Concerns\ApplicationTestingHooks;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;

use function Orchestra\Testbench\package_path;

class DatabaseTruncationTest extends TestCase
{
use ApplicationTestingHooks;
use DatabaseTruncation;
use InteractsWithConsole;

protected function setUp(): void
{
RefreshDatabaseState::$migrated = false;

$this->setUpTheApplicationTestingHooks();
}

protected function tearDown(): void
{
$this->tearDownTheApplicationTestingHooks();

RefreshDatabaseState::$migrated = false;
}

protected function refreshApplication()
{
$this->app = Testbench::create(
basePath: package_path('vendor/orchestra/testbench-core/laravel'),
);
}

public function testDisconnectionAfterTestCompletion()
{
$this->app->instance(ConsoleKernelContract::class, m::spy(ConsoleKernel::class));
$this->app->instance('db', $database = m::mock(DatabaseManager::class));

$database->shouldReceive('getConnections')->once()->andReturn([
'default' => m::mock(ConnectionInterface::class),
'mysql' => m::mock(ConnectionInterface::class),
]);
$database->shouldReceive('purge')->with('default');
$database->shouldReceive('purge')->with('mysql');

$this->truncateDatabaseTables();
}
}

0 comments on commit 71c6653

Please sign in to comment.