Passed
Push — master ( 3ee1d1...534f17 )
by Stephen
03:57 queued 58s
created

WithMemoryDbConnection::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Sfneal\Testing\Utils\Traits;
4
5
use Illuminate\Database\Seeder;
6
use Illuminate\Support\Facades\Artisan;
7
use Illuminate\Support\Facades\DB;
8
9
trait WithMemoryDbConnection
10
{
11
    /**
12
     * Setup the test environment.
13
     *
14
     * @return void
15
     */
16
    protected function setUp(): void
17
    {
18
        parent::setUp();
19
20
        DB::purge('testing');
21
        DB::setDefaultConnection('testing');
22
23
        Artisan::call('migrate');
24
25
        foreach ($this->seeders() as $seeder) {
26
            // Instantiated seeder class (likely with constructor params)
27
            if ($seeder instanceof Seeder) {
28
                $seeder->run();
0 ignored issues
show
Bug introduced by
The method run() does not exist on Illuminate\Database\Seeder. ( Ignorable by Annotation )

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

28
                $seeder->/** @scrutinizer ignore-call */ 
29
                         run();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
            }
30
31
            // Seeder class string to be used in Artisan command
32
            else {
33
                $this->seed($seeder);
0 ignored issues
show
Bug introduced by
The method seed() does not exist on Sfneal\Testing\Utils\Traits\WithMemoryDbConnection. Did you maybe mean seeders()? ( Ignorable by Annotation )

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

33
                $this->/** @scrutinizer ignore-call */ 
34
                       seed($seeder);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
            }
35
        }
36
    }
37
38
    /**
39
     * Performs assertions shared by all tests of a test case.
40
     *
41
     * This method is called between setUp() and test.
42
     *
43
     * @return void
44
     */
45
    protected function assertPreConditions(): void
46
    {
47
        $this->assertEquals('testing', DB::getDefaultConnection());
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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

47
        $this->/** @scrutinizer ignore-call */ 
48
               assertEquals('testing', DB::getDefaultConnection());
Loading history...
48
    }
49
50
    /**
51
     * Clean up the testing environment before the next test.
52
     *
53
     * @return void
54
     */
55
    protected function tearDown(): void
56
    {
57
        $this->beforeApplicationDestroyed(function () {
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

57
        $this->/** @scrutinizer ignore-call */ 
58
               beforeApplicationDestroyed(function () {
Loading history...
58
            DB::disconnect('testing');
59
        });
60
61
        parent::tearDown();
62
    }
63
64
    /**
65
     * Retrieve an array of Seeders to be run within the `setUp()` method.
66
     *
67
     * @return array
68
     */
69
    abstract protected function seeders(): array;
70
}
71