TestDatabases.php
4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace Illuminate\Testing\Concerns;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\ParallelTesting;
use Illuminate\Support\Facades\Schema;
trait TestDatabases
{
/**
* Indicates if the test database schema is up to date.
*
* @var bool
*/
protected static $schemaIsUpToDate = false;
/**
* Boot a test database.
*
* @return void
*/
protected function bootTestDatabase()
{
ParallelTesting::setUpProcess(function () {
$this->whenNotUsingInMemoryDatabase(function ($database) {
if (ParallelTesting::option('recreate_databases')) {
Schema::dropDatabaseIfExists(
$this->testDatabase($database)
);
}
});
});
ParallelTesting::setUpTestCase(function ($testCase) {
$uses = array_flip(class_uses_recursive(get_class($testCase)));
$databaseTraits = [
Testing\DatabaseMigrations::class,
Testing\DatabaseTransactions::class,
Testing\RefreshDatabase::class,
];
if (Arr::hasAny($uses, $databaseTraits)) {
if (! ParallelTesting::option('without_databases')) {
$this->whenNotUsingInMemoryDatabase(function ($database) use ($uses) {
[$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
$this->switchToDatabase($testDatabase);
if (isset($uses[Testing\DatabaseTransactions::class])) {
$this->ensureSchemaIsUpToDate();
}
if ($created) {
ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
}
});
}
}
});
}
/**
* Ensure a test database exists and returns its name.
*
* @param string $database
* @return array
*/
protected function ensureTestDatabaseExists($database)
{
$testDatabase = $this->testDatabase($database);
try {
$this->usingDatabase($testDatabase, function () {
Schema::hasTable('dummy');
});
} catch (QueryException $e) {
$this->usingDatabase($database, function () use ($testDatabase) {
Schema::dropDatabaseIfExists($testDatabase);
Schema::createDatabase($testDatabase);
});
return [$testDatabase, true];
}
return [$testDatabase, false];
}
/**
* Ensure the current database test schema is up to date.
*
* @return void
*/
protected function ensureSchemaIsUpToDate()
{
if (! static::$schemaIsUpToDate) {
Artisan::call('migrate');
static::$schemaIsUpToDate = true;
}
}
/**
* Runs the given callable using the given database.
*
* @param string $database
* @param callable $callable
* @return void
*/
protected function usingDatabase($database, $callable)
{
$original = DB::getConfig('database');
try {
$this->switchToDatabase($database);
$callable();
} finally {
$this->switchToDatabase($original);
}
}
/**
* Apply the given callback when tests are not using in memory database.
*
* @param callable $callback
* @return void
*/
protected function whenNotUsingInMemoryDatabase($callback)
{
$database = DB::getConfig('database');
if ($database !== ':memory:') {
$callback($database);
}
}
/**
* Switch to the given database.
*
* @param string $database
* @return void
*/
protected function switchToDatabase($database)
{
DB::purge();
$default = config('database.default');
$url = config("database.connections.{$default}.url");
if ($url) {
config()->set(
"database.connections.{$default}.url",
preg_replace('/^(.*)(\/[\w-]*)(\??.*)$/', "$1/{$database}$3", $url),
);
} else {
config()->set(
"database.connections.{$default}.database",
$database,
);
}
}
/**
* Returns the test database name.
*
* @return string
*/
protected function testDatabase($database)
{
$token = ParallelTesting::token();
return "{$database}_test_{$token}";
}
}