| Server IP : / Your IP : 10.244.4.16 [ Web Server : nginx/1.25.3 System : Linux escuela-portal-app-54f56585bc-kst6g 5.15.0-1084-azure #93-Ubuntu SMP Sat Mar 15 14:12:29 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.2.13 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, Domains : 0 Domains MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /proc/789/cwd/app/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ |
Upload File : |
<?php
namespace Illuminate\Foundation\Testing;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
trait DatabaseTruncation
{
use CanConfigureMigrationCommands;
/**
* The cached names of the database tables for each connection.
*
* @var array
*/
protected static array $allTables;
/**
* Truncate the database tables for all configured connections.
*
* @return void
*/
protected function truncateDatabaseTables(): void
{
// Migrate and seed the database on first run...
if (! RefreshDatabaseState::$migrated) {
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
return;
}
// Always clear any test data on subsequent runs...
$this->truncateTablesForAllConnections();
if ($seeder = $this->seeder()) {
// Use a specific seeder class...
$this->artisan('db:seed', ['--class' => $seeder]);
} elseif ($this->shouldSeed()) {
// Use the default seeder class...
$this->artisan('db:seed');
}
}
/**
* Truncate the database tables for all configured connections.
*
* @return void
*/
protected function truncateTablesForAllConnections(): void
{
$database = $this->app->make('db');
collect($this->connectionsToTruncate())
->each(function ($name) use ($database) {
$connection = $database->connection($name);
$connection->getSchemaBuilder()->withoutForeignKeyConstraints(
fn () => $this->truncateTablesForConnection($connection, $name)
);
});
}
/**
* Truncate the database tables for the given database connection.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string|null $name
* @return void
*/
protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void
{
$dispatcher = $connection->getEventDispatcher();
$connection->unsetEventDispatcher();
collect(static::$allTables[$name] ??= $connection->getDoctrineSchemaManager()->listTableNames())
->when(
property_exists($this, 'tablesToTruncate'),
fn ($tables) => $tables->intersect($this->tablesToTruncate),
fn ($tables) => $tables->diff($this->exceptTables($name))
)
->filter(fn ($table) => $connection->table($table)->exists())
->each(fn ($table) => $connection->table($table)->truncate());
$connection->setEventDispatcher($dispatcher);
}
/**
* The database connections that should have their tables truncated.
*
* @return array
*/
protected function connectionsToTruncate(): array
{
return property_exists($this, 'connectionsToTruncate')
? $this->connectionsToTruncate : [null];
}
/**
* Get the tables that should not be truncated.
*
* @param string|null $connectionName
* @return array
*/
protected function exceptTables(?string $connectionName): array
{
if (property_exists($this, 'exceptTables')) {
return array_merge(
$this->exceptTables[$connectionName] ?? [],
[$this->app['config']->get('database.migrations')]
);
}
return [$this->app['config']->get('database.migrations')];
}
}