Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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())
83
            ->diff($this->exceptTables($name))
84
            ->filter(fn ($table) => $connection->table($table)->exists())
85
            ->each(fn ($table) => $connection->table($table)->truncate());
86
 
87
        $connection->setEventDispatcher($dispatcher);
88
    }
89
 
90
    /**
91
     * The database connections that should have their tables truncated.
92
     *
93
     * @return array
94
     */
95
    protected function connectionsToTruncate(): array
96
    {
97
        return property_exists($this, 'connectionsToTruncate')
98
                    ? $this->connectionsToTruncate : [null];
99
    }
100
 
101
    /**
102
     * Get the tables that should not be truncated.
103
     *
104
     * @param  string|null  $connectionName
105
     * @return array
106
     */
107
    protected function exceptTables(?string $connectionName): array
108
    {
109
        if (property_exists($this, 'exceptTables')) {
110
            return array_merge(
111
                $this->exceptTables[$connectionName] ?? [],
112
                [$this->app['config']->get('database.migrations')]
113
            );
114
        }
115
 
116
        return [$this->app['config']->get('database.migrations')];
117
    }
118
}