1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Testing; |
6
|
|
|
|
7
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactionsManager; |
8
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase as IlluminateRefreshDatabase; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Testing\Concerns\PreparesTestingTransactions; |
10
|
|
|
|
11
|
|
|
trait RefreshDatabase |
12
|
|
|
{ |
13
|
|
|
use PreparesTestingTransactions; |
14
|
|
|
use IlluminateRefreshDatabase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Begin a database transaction on the testing database. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
16 |
|
public function beginDatabaseTransaction() |
22
|
|
|
{ |
23
|
16 |
|
$database = $this->app->make('db'); |
24
|
|
|
|
25
|
16 |
|
$this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager()); |
26
|
|
|
|
27
|
16 |
|
foreach ($this->connectionsToTransact() as $name) { |
28
|
16 |
|
$connection = $database->connection($name); |
29
|
|
|
|
30
|
16 |
|
$connection->setTransactionManager($transactionsManager); |
31
|
|
|
|
32
|
16 |
|
$dispatcher = $connection->getEventDispatcher(); |
33
|
|
|
|
34
|
16 |
|
$connection->unsetEventDispatcher(); |
35
|
16 |
|
$connection->beginTransaction($this->transactionCollections); |
36
|
16 |
|
$connection->setEventDispatcher($dispatcher); |
37
|
|
|
} |
38
|
|
|
|
39
|
16 |
|
$this->beforeApplicationDestroyed(function () use ($database) { |
|
|
|
|
40
|
16 |
|
foreach ($this->connectionsToTransact() as $name) { |
41
|
16 |
|
$connection = $database->connection($name); |
42
|
16 |
|
$dispatcher = $connection->getEventDispatcher(); |
43
|
|
|
|
44
|
16 |
|
$connection->unsetEventDispatcher(); |
45
|
16 |
|
$connection->rollBack(); |
46
|
16 |
|
$connection->setEventDispatcher($dispatcher); |
47
|
16 |
|
$connection->disconnect(); |
48
|
|
|
} |
49
|
16 |
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The parameters that should be used when running "migrate:fresh". |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
1 |
|
protected function migrateFreshUsing() |
58
|
|
|
{ |
59
|
1 |
|
$seeder = $this->seeder(); |
60
|
|
|
|
61
|
1 |
|
$results = array_merge( |
62
|
1 |
|
[ |
63
|
1 |
|
'--drop-views' => $this->shouldDropViews(), |
64
|
1 |
|
'--drop-types' => $this->shouldDropTypes(), |
65
|
1 |
|
], |
66
|
1 |
|
$seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()], |
67
|
1 |
|
$this->setMigrationPaths(), |
68
|
1 |
|
); |
69
|
|
|
|
70
|
1 |
|
return $results; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Determine if types should be dropped when refreshing the database. |
75
|
|
|
* |
76
|
|
|
* @return array<string, array<string>|string> |
77
|
|
|
*/ |
78
|
1 |
|
protected function setMigrationPaths() |
79
|
|
|
{ |
80
|
1 |
|
$migrationSettings = []; |
81
|
|
|
|
82
|
1 |
|
if(property_exists($this, 'realPath')) { |
83
|
1 |
|
$migrationSettings['--realpath'] = $this->realPath ?? false; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
if (property_exists($this, 'migrationPaths')) { |
87
|
1 |
|
$migrationSettings['--path'] = $this->migrationPaths; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $migrationSettings; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|