RefreshDatabase   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 131
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A refreshDatabase() 0 6 2
A usingInMemoryDatabase() 0 6 1
A refreshInMemoryDatabase() 0 6 1
A refreshTestDatabase() 0 17 3
A beginDatabaseTransaction() 0 25 3
A connectionsToTransact() 0 5 2
A shouldDropViews() 0 5 2
A swapTestingDatabase() 0 16 1
1
<?php
2
3
namespace Tonysm\LaravelParatest\Testing;
4
5
use Illuminate\Contracts\Console\Kernel;
6
7
/**
8
 * Trait RefreshDatabase
9
 *
10
 * Most of the code here was copied from Laravel's RefreshDatabase trait.
11
 *
12
 * @package Tonysm\DbCreateCommand\Testing
13
 *
14
 * @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Foundation/Testing/RefreshDatabase.php
15
 */
16
trait RefreshDatabase
17
{
18
    /**
19
     * Define hooks to migrate the database before and after each test.
20
     *
21
     * @return void
22
     */
23
    public function refreshDatabase()
24
    {
25
        $this->usingInMemoryDatabase()
26
            ? $this->refreshInMemoryDatabase()
27
            : $this->refreshTestDatabase();
28
    }
29
30
    /**
31
     * Determine if an in-memory database is being used.
32
     *
33
     * @return bool
34
     */
35
    protected function usingInMemoryDatabase()
36
    {
37
        $default = config('database.default');
38
39
        return config("database.connections.$default.database") === ':memory:';
40
    }
41
42
    /**
43
     * Refresh the in-memory database.
44
     *
45
     * @return void
46
     */
47
    protected function refreshInMemoryDatabase()
48
    {
49
        $this->artisan('migrate');
0 ignored issues
show
Bug introduced by
It seems like artisan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
51
        $this->app[Kernel::class]->setArtisan(null);
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
    }
53
54
    /**
55
     * Refresh a conventional test database.
56
     *
57
     * @return void
58
     */
59
    protected function refreshTestDatabase()
60
    {
61
        $this->swapTestingDatabase();
62
63
        if (! RefreshDatabaseState::$migrated) {
64
            $this->artisan('db:create');
0 ignored issues
show
Bug introduced by
It seems like artisan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65
            $this->artisan('migrate:fresh', $this->shouldDropViews() ? [
0 ignored issues
show
Bug introduced by
It seems like artisan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
                '--drop-views' => true,
67
            ] : []);
68
69
            $this->app[Kernel::class]->setArtisan(null);
70
71
            RefreshDatabaseState::$migrated = true;
72
        }
73
74
        $this->beginDatabaseTransaction();
75
    }
76
77
    /**
78
     * Begin a database transaction on the testing database.
79
     *
80
     * @return void
81
     */
82
    public function beginDatabaseTransaction()
83
    {
84
        $database = $this->app->make('db');
85
86
        foreach ($this->connectionsToTransact() as $name) {
87
            $connection = $database->connection($name);
88
            $dispatcher = $connection->getEventDispatcher();
89
90
            $connection->unsetEventDispatcher();
91
            $connection->beginTransaction();
92
            $connection->setEventDispatcher($dispatcher);
93
        }
94
95
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
            foreach ($this->connectionsToTransact() as $name) {
97
                $connection = $database->connection($name);
98
                $dispatcher = $connection->getEventDispatcher();
99
100
                $connection->unsetEventDispatcher();
101
                $connection->rollback();
102
                $connection->setEventDispatcher($dispatcher);
103
                $connection->disconnect();
104
            }
105
        });
106
    }
107
108
    /**
109
     * The database connections that should have transactions.
110
     *
111
     * @return array
112
     */
113
    protected function connectionsToTransact()
114
    {
115
        return property_exists($this, 'connectionsToTransact')
116
            ? $this->connectionsToTransact : [null];
0 ignored issues
show
Bug introduced by
The property connectionsToTransact does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
117
    }
118
119
    /**
120
     * Determine if views should be dropped when refreshing the database.
121
     *
122
     * @return bool
123
     */
124
    protected function shouldDropViews()
125
    {
126
        return property_exists($this, 'dropViews')
127
            ? $this->dropViews : false;
0 ignored issues
show
Bug introduced by
The property dropViews does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
128
    }
129
130
    protected function swapTestingDatabase(): void
131
    {
132
        $driver = config('database.default');
133
        $dbName = config("database.connections.{$driver}.database");
134
135
        // Paratest gives each process a unique TEST_TOKEN env variable.
136
        // When that's not set, we can default to 1 because it's
137
        // probably running on PHPUnit instead.
138
        config([
139
            "database.connections.{$driver}.database" => sprintf(
140
                '%s_test_%s',
141
                $dbName,
142
                env('TEST_TOKEN', 1)
143
            ),
144
        ]);
145
    }
146
}
147