Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 402 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
402 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Testing;
4
 
5
use Illuminate\Contracts\Console\Kernel;
6
use Illuminate\Database\ConnectionInterface;
7
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
8
 
9
trait DatabaseTruncation
10
{
11
    use CanConfigureMigrationCommands;
12
 
13
    /**
14
     * The cached names of the database tables for each connection.
15
     *
16
     * @var array
17
     */
18
    protected static array $allTables;
19
 
20
    /**
21
     * Truncate the database tables for all configured connections.
22
     *
23
     * @return void
24
     */
25
    protected function truncateDatabaseTables(): void
26
    {
27
        // Migrate and seed the database on first run...
28
        if (! RefreshDatabaseState::$migrated) {
29
            $this->artisan('migrate:fresh', $this->migrateFreshUsing());
30
 
31
            $this->app[Kernel::class]->setArtisan(null);
32
 
33
            RefreshDatabaseState::$migrated = true;
34
 
35
            return;
36
        }
37
 
38
        // Always clear any test data on subsequent runs...
39
        $this->truncateTablesForAllConnections();
40
 
41
        if ($seeder = $this->seeder()) {
42
            // Use a specific seeder class...
43
            $this->artisan('db:seed', ['--class' => $seeder]);
44
        } elseif ($this->shouldSeed()) {
45
            // Use the default seeder class...
46
            $this->artisan('db:seed');
47
        }
48
    }
49
 
50
    /**
51
     * Truncate the database tables for all configured connections.
52
     *
53
     * @return void
54
     */
55
    protected function truncateTablesForAllConnections(): void
56
    {
57
        $database = $this->app->make('db');
58
 
59
        collect($this->connectionsToTruncate())
60
            ->each(function ($name) use ($database) {
61
                $connection = $database->connection($name);
62
 
63
                $connection->getSchemaBuilder()->withoutForeignKeyConstraints(
64
                    fn () => $this->truncateTablesForConnection($connection, $name)
65
                );
66
            });
67
    }
68
 
69
    /**
70
     * Truncate the database tables for the given database connection.
71
     *
72
     * @param  \Illuminate\Database\ConnectionInterface  $connection
73
     * @param  string|null  $name
74
     * @return void
75
     */
76
    protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void
77
    {
78
        $dispatcher = $connection->getEventDispatcher();
79
 
80
        $connection->unsetEventDispatcher();
81
 
82
        collect(static::$allTables[$name] ??= $connection->getDoctrineSchemaManager()->listTableNames())
621 lars 83
            ->when(
84
                property_exists($this, 'tablesToTruncate'),
85
                fn ($tables) => $tables->intersect($this->tablesToTruncate),
86
                fn ($tables) => $tables->diff($this->exceptTables($name))
87
            )
402 lars 88
            ->filter(fn ($table) => $connection->table($table)->exists())
89
            ->each(fn ($table) => $connection->table($table)->truncate());
90
 
91
        $connection->setEventDispatcher($dispatcher);
92
    }
93
 
94
    /**
95
     * The database connections that should have their tables truncated.
96
     *
97
     * @return array
98
     */
99
    protected function connectionsToTruncate(): array
100
    {
101
        return property_exists($this, 'connectionsToTruncate')
102
                    ? $this->connectionsToTruncate : [null];
103
    }
104
 
105
    /**
106
     * Get the tables that should not be truncated.
107
     *
108
     * @param  string|null  $connectionName
109
     * @return array
110
     */
111
    protected function exceptTables(?string $connectionName): array
112
    {
113
        if (property_exists($this, 'exceptTables')) {
114
            return array_merge(
115
                $this->exceptTables[$connectionName] ?? [],
116
                [$this->app['config']->get('database.migrations')]
117
            );
118
        }
119
 
120
        return [$this->app['config']->get('database.migrations')];
121
    }
122
}