RefreshDatabase::migrateFreshUsing()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 1
nop 0
dl 0
loc 17
ccs 14
cts 14
cp 1
crap 2
rs 9.9332
c 0
b 0
f 0
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
It seems like beforeApplicationDestroyed() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               beforeApplicationDestroyed(function () use ($database) {
Loading history...
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