|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tonysm\LaravelParatest\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Console\Kernel; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait RefreshDatabase |
|
9
|
|
|
* |
|
10
|
|
|
* Most of the code here was copied from Laravel's RefreshDatabase trait. |
|
11
|
|
|
* |
|
12
|
|
|
* @package Tonysm\DbCreateCommand\Testing |
|
13
|
|
|
* |
|
14
|
|
|
* @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Testing/RefreshDatabase.php |
|
15
|
|
|
*/ |
|
16
|
|
|
trait RefreshDatabase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Define hooks to migrate the database before and after each test. |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
public function refreshDatabase() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->usingInMemoryDatabase() |
|
26
|
|
|
? $this->refreshInMemoryDatabase() |
|
27
|
|
|
: $this->refreshTestDatabase(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Determine if an in-memory database is being used. |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function usingInMemoryDatabase() |
|
36
|
|
|
{ |
|
37
|
|
|
$default = config('database.default'); |
|
38
|
|
|
|
|
39
|
|
|
return config("database.connections.$default.database") === ':memory:'; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Refresh the in-memory database. |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function refreshInMemoryDatabase() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->artisan('migrate'); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$this->app[Kernel::class]->setArtisan(null); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Refresh a conventional test database. |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function refreshTestDatabase() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->swapTestingDatabase(); |
|
62
|
|
|
|
|
63
|
|
|
if (! RefreshDatabaseState::$migrated) { |
|
64
|
|
|
$this->artisan('db:create'); |
|
|
|
|
|
|
65
|
|
|
$this->artisan('migrate:fresh', $this->shouldDropViews() ? [ |
|
|
|
|
|
|
66
|
|
|
'--drop-views' => true, |
|
67
|
|
|
] : []); |
|
68
|
|
|
|
|
69
|
|
|
$this->app[Kernel::class]->setArtisan(null); |
|
70
|
|
|
|
|
71
|
|
|
RefreshDatabaseState::$migrated = true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->beginDatabaseTransaction(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Begin a database transaction on the testing database. |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function beginDatabaseTransaction() |
|
83
|
|
|
{ |
|
84
|
|
|
$database = $this->app->make('db'); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->connectionsToTransact() as $name) { |
|
87
|
|
|
$connection = $database->connection($name); |
|
88
|
|
|
$dispatcher = $connection->getEventDispatcher(); |
|
89
|
|
|
|
|
90
|
|
|
$connection->unsetEventDispatcher(); |
|
91
|
|
|
$connection->beginTransaction(); |
|
92
|
|
|
$connection->setEventDispatcher($dispatcher); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->beforeApplicationDestroyed(function () use ($database) { |
|
|
|
|
|
|
96
|
|
|
foreach ($this->connectionsToTransact() as $name) { |
|
97
|
|
|
$connection = $database->connection($name); |
|
98
|
|
|
$dispatcher = $connection->getEventDispatcher(); |
|
99
|
|
|
|
|
100
|
|
|
$connection->unsetEventDispatcher(); |
|
101
|
|
|
$connection->rollback(); |
|
102
|
|
|
$connection->setEventDispatcher($dispatcher); |
|
103
|
|
|
$connection->disconnect(); |
|
104
|
|
|
} |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* The database connections that should have transactions. |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function connectionsToTransact() |
|
114
|
|
|
{ |
|
115
|
|
|
return property_exists($this, 'connectionsToTransact') |
|
116
|
|
|
? $this->connectionsToTransact : [null]; |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Determine if views should be dropped when refreshing the database. |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function shouldDropViews() |
|
125
|
|
|
{ |
|
126
|
|
|
return property_exists($this, 'dropViews') |
|
127
|
|
|
? $this->dropViews : false; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
protected function swapTestingDatabase(): void |
|
131
|
|
|
{ |
|
132
|
|
|
$driver = config('database.default'); |
|
133
|
|
|
$dbName = config("database.connections.{$driver}.database"); |
|
134
|
|
|
|
|
135
|
|
|
// Paratest gives each process a unique TEST_TOKEN env variable. |
|
136
|
|
|
// When that's not set, we can default to 1 because it's |
|
137
|
|
|
// probably running on PHPUnit instead. |
|
138
|
|
|
config([ |
|
139
|
|
|
"database.connections.{$driver}.database" => sprintf( |
|
140
|
|
|
'%s_test_%s', |
|
141
|
|
|
$dbName, |
|
142
|
|
|
env('TEST_TOKEN', 1) |
|
143
|
|
|
), |
|
144
|
|
|
]); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.