Passed
Pull Request — next (#137)
by Bas
04:08
created

RefreshDatabase::migrateFreshUsing()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 2
rs 10
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) {
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

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