DatabaseMigrateTestListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Softonic\DatabaseMigrateTestListener;
4
5
use Composer\Autoload\ClassLoader;
6
use PHPUnit\Framework\TestListener;
7
use PHPUnit\Framework\TestListenerDefaultImplementation;
8
use PHPUnit\Framework\TestSuite;
9
use ReflectionClass;
10
11
class DatabaseMigrateTestListener implements TestListener
0 ignored issues
show
Deprecated Code introduced by
The interface PHPUnit\Framework\TestListener has been deprecated with message: Use the `TestHook` interfaces instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
12
{
13
    use TestListenerDefaultImplementation;
0 ignored issues
show
Deprecated Code introduced by
The trait PHPUnit\Framework\TestLi...erDefaultImplementation has been deprecated with message: The `TestListener` interface is deprecated

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
15
    public $testSuites;
16
17
    public $itShouldSeed;
18
19
    public $connection;
20
21
    public $seeder;
22
23 10
    public function __construct(array $testSuites, bool $itShouldSeed, string $connection = 'sqlite', string $seeder = null)
24
    {
25 10
        $this->testSuites   = $testSuites;
26 10
        $this->itShouldSeed = $itShouldSeed;
27 10
        $this->connection   = $connection;
28 10
        $this->seeder       = $seeder;
29 10
    }
30
31
    /**
32
     * Set up the database for testing.
33
     *
34
     * @param TestSuite $suite
35
     *
36
     * @throws \ReflectionException
37
     */
38 10
    public function startTestSuite(TestSuite $suite): void
39
    {
40 10
        if (!in_array($suite->getName(), $this->testSuites)) {
41 2
            return;
42
        }
43
44 8
        $reflection   = new ReflectionClass(ClassLoader::class);
45 8
        $appDirectory = dirname($reflection->getFileName(), 3);
46 8
        chdir($appDirectory);
47 8
        $seed = $this->itShouldSeed ? '--seed' : '';
48 8
        $seeder = $this->itShouldSeed && !empty($this->seeder) ? "--seeder={$this->seeder}" : '';
49
50 8
        echo shell_exec("php artisan migrate:refresh --database {$this->connection} $seed $seeder");
51 8
    }
52
}
53