1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DummyNamespace\Tests\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
|
7
|
|
|
trait TestCaseTrait |
8
|
|
|
{ |
9
|
|
|
use SeedTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Setup the test environment. |
13
|
|
|
*/ |
14
|
|
|
public function setUp(): void |
15
|
|
|
{ |
16
|
|
|
parent::setUp(); |
17
|
|
|
// $this->loadFactories(); |
18
|
|
|
// $this->cleanUp(); |
19
|
|
|
// $this->publish(); |
20
|
|
|
// $this->clearCaches(); |
21
|
|
|
// $this->migrate(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Do clean up on tear down. |
26
|
|
|
*/ |
27
|
|
|
protected function tearDown(): void |
28
|
|
|
{ |
29
|
|
|
// $this->cleanUp(); |
30
|
|
|
parent::tearDown(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Define environment setup. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Foundation\Application $app |
37
|
|
|
*/ |
38
|
|
|
protected function getEnvironmentSetUp($app) |
39
|
|
|
{ |
40
|
|
|
// Setup default database to use sqlite :memory: |
41
|
|
|
$app['config']->set('database.default', 'testbench'); |
42
|
|
|
$app['config']->set('database.connections.testbench', [ |
43
|
|
|
'driver' => 'sqlite', |
44
|
|
|
'database' => ':memory:', |
45
|
|
|
'prefix' => '', |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Load Factories |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function loadFactories() |
55
|
|
|
{ |
56
|
|
|
$this->withFactories(realpath(__DIR__ . '/../factories')); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Run all migrations. |
61
|
|
|
*/ |
62
|
|
|
public function migrate() |
63
|
|
|
{ |
64
|
|
|
$this->loadLaravelMigrations(['--database' => 'testbench']); |
|
|
|
|
65
|
|
|
$this->artisan('migrate', ['--database' => 'testbench']); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Do test clean up. |
70
|
|
|
*/ |
71
|
|
|
public function cleanUp() |
72
|
|
|
{ |
73
|
|
|
$this->removeMigrationFiles(); |
74
|
|
|
$this->removeSeederFiles(); |
75
|
|
|
// $this->removeIfExist(config_path('config.php')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Clear all caches. |
80
|
|
|
*/ |
81
|
|
|
public function clearCaches() |
82
|
|
|
{ |
83
|
|
|
$this->artisan('config:clear'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Republish assets. |
88
|
|
|
*/ |
89
|
|
|
public function republish() |
90
|
|
|
{ |
91
|
|
|
$this->cleanUp(); |
92
|
|
|
$this->clearCaches(); |
93
|
|
|
$this->publish(); |
94
|
|
|
$this->clearCaches(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Remove all migration files if exist. |
99
|
|
|
*/ |
100
|
|
|
public function removeMigrationFiles() |
101
|
|
|
{ |
102
|
|
|
collect(glob(database_path('migrations/*.php'))) |
103
|
|
|
->each(function ($path) { |
104
|
|
|
$this->removeIfExist($path); |
105
|
|
|
}); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Remove all seed files if exist. |
110
|
|
|
*/ |
111
|
|
|
public function removeSeederFiles() |
112
|
|
|
{ |
113
|
|
|
collect(glob(database_path('seeds/*.php'))) |
114
|
|
|
->each(function ($path) { |
115
|
|
|
$this->removeIfExist($path); |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Remove file if exists. |
121
|
|
|
* |
122
|
|
|
* @param string $path |
123
|
|
|
*/ |
124
|
|
|
public function removeIfExist($path) |
125
|
|
|
{ |
126
|
|
|
if (file_exists($path)) { |
127
|
|
|
unlink($path); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Publish all package assets. |
133
|
|
|
*/ |
134
|
|
|
public function publish() |
135
|
|
|
{ |
136
|
|
|
// $this->artisan('vendor:publish', [ |
137
|
|
|
// '--force' => true, |
138
|
|
|
// '--tag' => 'config', |
139
|
|
|
// ]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Truncate table. |
144
|
|
|
*/ |
145
|
|
|
public function truncateTable($table) |
146
|
|
|
{ |
147
|
|
|
\DB::table($table)->truncate(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
//////////////////////////////////////////////////////// |
151
|
|
|
///////////////// Custom Assertions //////////////////// |
152
|
|
|
//////////////////////////////////////////////////////// |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Assert the current database has table. |
156
|
|
|
* |
157
|
|
|
* @param string $table table name |
158
|
|
|
*/ |
159
|
|
|
protected function assertHasTable($table) |
160
|
|
|
{ |
161
|
|
|
$this->assertTrue(Schema::hasTable($table)); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Assert the table has columns defined. |
166
|
|
|
* |
167
|
|
|
* @param string $table table name |
168
|
|
|
* @param array $columns list of columns |
169
|
|
|
*/ |
170
|
|
|
protected function assertTableHasColumns($table, $columns) |
171
|
|
|
{ |
172
|
|
|
collect($columns)->each(function ($column) use ($table) { |
173
|
|
|
$this->assertTrue(Schema::hasColumn($table, $column)); |
174
|
|
|
}); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Assert has helper. |
179
|
|
|
* |
180
|
|
|
* @param string $helper helper name |
181
|
|
|
*/ |
182
|
|
|
protected function assertHasHelper($helper) |
183
|
|
|
{ |
184
|
|
|
$this->assertTrue(function_exists($helper)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Assert has config. |
189
|
|
|
* |
190
|
|
|
* @param string $config config name |
191
|
|
|
*/ |
192
|
|
|
protected function assertHasConfig($config) |
193
|
|
|
{ |
194
|
|
|
$this->assertFileExists(config_path($config . '.php')); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Assert has migration. |
199
|
|
|
* |
200
|
|
|
* @param string $migration migration name |
201
|
|
|
*/ |
202
|
|
|
protected function assertHasMigration($migration) |
203
|
|
|
{ |
204
|
|
|
$this->assertHasClass($migration); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Assert has class. |
209
|
|
|
* |
210
|
|
|
* @param string $class class name |
211
|
|
|
*/ |
212
|
|
|
protected function assertHasClass($class) |
213
|
|
|
{ |
214
|
|
|
$this->assertTrue(class_exists($class)); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Assert has class method exist. |
219
|
|
|
* |
220
|
|
|
* @param string $object object |
221
|
|
|
* @param string $method method |
222
|
|
|
*/ |
223
|
|
|
protected function assertHasClassMethod($object, $method) |
224
|
|
|
{ |
225
|
|
|
$this->assertTrue(method_exists($object, $method)); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|