Completed
Push — master ( 82bd6d...98c2fa )
by Luiz
09:12
created

LaravelRunner::getExitCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tonysm\LaravelParatest\Testing\Paratest;
4
5
use Illuminate\Foundation\Application;
6
use Illuminate\Support\Facades\Artisan;
7
use Illuminate\Contracts\Console\Kernel;
8
use ParaTest\Runners\PHPUnit\Options;
9
use ParaTest\Runners\PHPUnit\RunnerInterface;
10
use ParaTest\Runners\PHPUnit\WrapperRunner;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class LaravelRunner implements RunnerInterface
14
{
15
    /**
16
     * @var WrapperRunner
17
     */
18
    private $innerRunner;
19
    /**
20
     * @var Options
21
     */
22
    private $options;
23
24
    public function __construct(Options $options, OutputInterface $output)
25
    {
26
        $this->options = $options;
27
        $this->innerRunner = new WrapperRunner($options, $output);
28
    }
29
30
    public function tearDownTestDatabases()
31
    {
32
        $app = $this->createApp();
33
34
        $driver = $app['config']->get('database.default');
35
        $dbName = $app['config']->get("database.connections.{$driver}.database");
36
37
        for ($i = 1; $i <= $this->options->processes(); ++$i) {
38
            $this->swapTestingDatabase($app, $driver, sprintf('%s_test_%s', $dbName, $i));
39
            Artisan::call('db:drop');
40
        }
41
    }
42
43
    private function swapTestingDatabase($app, $driver, $dbName): void
44
    {
45
        // Paratest gives each process a unique TEST_TOKEN env variable.
46
        // When that's not set, we can default to 1 because it's
47
        // probably running on PHPUnit instead.
48
        $app['config']->set([
49
            "database.connections.{$driver}.database" => $dbName,
50
        ]);
51
    }
52
53
    private function createApp(): Application
54
    {
55
        $app = require getcwd().'/bootstrap/app.php';
56
57
        $app->make(Kernel::class)->bootstrap();
58
59
        return $app;
60
    }
61
62
    public function run(): void
63
    {
64
        $this->innerRunner->run();
65
        $this->tearDownTestDatabases();
66
    }
67
68
    public function getExitCode(): int
69
    {
70
        return $this->innerRunner->getExitCode();
71
    }
72
}
73