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\CanConfigureMigrationCommands; |
||
10 | use LaravelFreelancerNL\Aranguent\Testing\Concerns\PreparesTestingTransactions; |
||
11 | |||
12 | trait RefreshDatabase |
||
13 | { |
||
14 | use PreparesTestingTransactions; |
||
15 | use CanConfigureMigrationCommands; |
||
16 | use IlluminateRefreshDatabase; |
||
17 | |||
18 | /** |
||
19 | * Begin a database transaction on the testing database. |
||
20 | * |
||
21 | * @return void |
||
22 | */ |
||
23 | 19 | public function beginDatabaseTransaction() |
|
24 | { |
||
25 | 19 | $database = $this->app->make('db'); |
|
26 | |||
27 | 19 | $connections = $this->connectionsToTransact(); |
|
28 | |||
29 | 19 | $this->app->instance('db.transactions', $transactionsManager = new DatabaseTransactionsManager($connections)); |
|
30 | |||
31 | 19 | foreach ($this->connectionsToTransact() as $name) { |
|
32 | 19 | $connection = $database->connection($name); |
|
33 | |||
34 | 19 | $connection->setTransactionManager($transactionsManager); |
|
35 | |||
36 | 19 | $dispatcher = $connection->getEventDispatcher(); |
|
37 | |||
38 | 19 | $connection->unsetEventDispatcher(); |
|
39 | 19 | $connection->beginTransaction($this->transactionCollections); |
|
40 | 19 | $connection->setEventDispatcher($dispatcher); |
|
41 | } |
||
42 | |||
43 | 19 | $this->beforeApplicationDestroyed(function () use ($database) { |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | 19 | foreach ($this->connectionsToTransact() as $name) { |
|
45 | 19 | $connection = $database->connection($name); |
|
46 | 19 | $dispatcher = $connection->getEventDispatcher(); |
|
47 | |||
48 | 19 | $connection->unsetEventDispatcher(); |
|
49 | 19 | $connection->rollBack(); |
|
50 | 19 | $connection->setEventDispatcher($dispatcher); |
|
51 | 19 | $connection->disconnect(); |
|
52 | } |
||
53 | 19 | }); |
|
54 | } |
||
55 | |||
56 | |||
57 | /** |
||
58 | * The parameters that should be used when running "migrate:fresh". |
||
59 | * |
||
60 | * Duplicate code because CanConfigureMigrationCommands has a conflict otherwise. |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | 1 | protected function migrateFreshUsing() |
|
65 | { |
||
66 | 1 | $seeder = $this->seeder(); |
|
67 | |||
68 | 1 | $results = array_merge( |
|
69 | 1 | [ |
|
70 | 1 | '--drop-analyzers' => $this->shouldDropAnalyzers(), |
|
71 | 1 | '--drop-graphs' => $this->shouldDropGraphs(), |
|
72 | 1 | '--drop-views' => $this->shouldDropViews(), |
|
73 | 1 | '--drop-types' => $this->shouldDropTypes(), |
|
74 | 1 | '--drop-all' => $this->shouldDropAll(), |
|
75 | 1 | ], |
|
76 | 1 | $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()], |
|
77 | 1 | $this->setMigrationPaths(), |
|
78 | 1 | ); |
|
79 | |||
80 | 1 | return $results; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Determine if types should be dropped when refreshing the database. |
||
85 | * |
||
86 | * @return array<string, array<string>|string> |
||
87 | */ |
||
88 | 1 | protected function setMigrationPaths() |
|
89 | { |
||
90 | 1 | $migrationSettings = []; |
|
91 | |||
92 | 1 | if (property_exists($this, 'realPath')) { |
|
93 | 1 | $migrationSettings['--realpath'] = $this->realPath ?? false; |
|
94 | } |
||
95 | |||
96 | 1 | if (property_exists($this, 'migrationPaths')) { |
|
97 | 1 | $migrationSettings['--path'] = $this->migrationPaths; |
|
98 | } |
||
99 | |||
100 | 1 | return $migrationSettings; |
|
101 | } |
||
102 | } |
||
103 |