Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Testing;
4
 
5
use Illuminate\Contracts\Console\Kernel;
6
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
7
 
8
trait RefreshDatabase
9
{
10
    use CanConfigureMigrationCommands;
11
 
12
    /**
13
     * Define hooks to migrate the database before and after each test.
14
     *
15
     * @return void
16
     */
17
    public function refreshDatabase()
18
    {
19
        $this->beforeRefreshingDatabase();
20
 
21
        $this->usingInMemoryDatabase()
22
                        ? $this->refreshInMemoryDatabase()
23
                        : $this->refreshTestDatabase();
24
 
25
        $this->afterRefreshingDatabase();
26
    }
27
 
28
    /**
29
     * Determine if an in-memory database is being used.
30
     *
31
     * @return bool
32
     */
33
    protected function usingInMemoryDatabase()
34
    {
35
        $default = config('database.default');
36
 
37
        return config("database.connections.$default.database") === ':memory:';
38
    }
39
 
40
    /**
41
     * Refresh the in-memory database.
42
     *
43
     * @return void
44
     */
45
    protected function refreshInMemoryDatabase()
46
    {
47
        $this->artisan('migrate', $this->migrateUsing());
48
 
49
        $this->app[Kernel::class]->setArtisan(null);
50
    }
51
 
52
    /**
53
     * The parameters that should be used when running "migrate".
54
     *
55
     * @return array
56
     */
57
    protected function migrateUsing()
58
    {
59
        return [
60
            '--seed' => $this->shouldSeed(),
61
            '--seeder' => $this->seeder(),
62
        ];
63
    }
64
 
65
    /**
66
     * Refresh a conventional test database.
67
     *
68
     * @return void
69
     */
70
    protected function refreshTestDatabase()
71
    {
72
        if (! RefreshDatabaseState::$migrated) {
73
            $this->artisan('migrate:fresh', $this->migrateFreshUsing());
74
 
75
            $this->app[Kernel::class]->setArtisan(null);
76
 
77
            RefreshDatabaseState::$migrated = true;
78
        }
79
 
80
        $this->beginDatabaseTransaction();
81
    }
82
 
83
    /**
84
     * Begin a database transaction on the testing database.
85
     *
86
     * @return void
87
     */
88
    public function beginDatabaseTransaction()
89
    {
90
        $database = $this->app->make('db');
91
 
92
        foreach ($this->connectionsToTransact() as $name) {
93
            $connection = $database->connection($name);
94
            $dispatcher = $connection->getEventDispatcher();
95
 
96
            $connection->unsetEventDispatcher();
97
            $connection->beginTransaction();
98
            $connection->setEventDispatcher($dispatcher);
99
 
100
            if ($this->app->resolved('db.transactions')) {
101
                $this->app->make('db.transactions')->callbacksShouldIgnore(
102
                    $this->app->make('db.transactions')->getTransactions()->first()
103
                );
104
            }
105
        }
106
 
107
        $this->beforeApplicationDestroyed(function () use ($database) {
108
            foreach ($this->connectionsToTransact() as $name) {
109
                $connection = $database->connection($name);
110
                $dispatcher = $connection->getEventDispatcher();
111
 
112
                $connection->unsetEventDispatcher();
113
                $connection->rollBack();
114
                $connection->setEventDispatcher($dispatcher);
115
                $connection->disconnect();
116
            }
117
        });
118
    }
119
 
120
    /**
121
     * The database connections that should have transactions.
122
     *
123
     * @return array
124
     */
125
    protected function connectionsToTransact()
126
    {
127
        return property_exists($this, 'connectionsToTransact')
128
                            ? $this->connectionsToTransact : [null];
129
    }
130
 
131
    /**
132
     * Perform any work that should take place before the database has started refreshing.
133
     *
134
     * @return void
135
     */
136
    protected function beforeRefreshingDatabase()
137
    {
138
        // ...
139
    }
140
 
141
    /**
142
     * Perform any work that should take place once the database has finished refreshing.
143
     *
144
     * @return void
145
     */
146
    protected function afterRefreshingDatabase()
147
    {
148
        // ...
149
    }
150
}