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

src/Testing/Paratest/LaravelRunner.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Tonysm\LaravelParatest\Testing\Paratest;
4
5
use ParaTest\Runners\PHPUnit\Runner;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Facades\Artisan;
8
use ParaTest\Runners\PHPUnit\BaseRunner;
9
use Illuminate\Contracts\Console\Kernel;
10
11
class LaravelRunner extends BaseRunner
0 ignored issues
show
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: beforeLoadChecks, doRun
Loading history...
12
{
13
    /**
14
     * @var \ParaTest\Runners\PHPUnit\Runner
15
     */
16
    protected $innerRunner;
17
18
    public function __construct(array $opts = [])
19
    {
20
        parent::__construct($opts);
0 ignored issues
show
The call to BaseRunner::__construct() misses a required argument $output.

This check looks for function calls that miss required arguments.

Loading history...
$opts is of type array, but the function expects a object<ParaTest\Runners\PHPUnit\Options>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
22
        $this->innerRunner = new Runner($opts);
0 ignored issues
show
The call to Runner::__construct() misses a required argument $output.

This check looks for function calls that miss required arguments.

Loading history...
$opts is of type array, but the function expects a object<ParaTest\Runners\PHPUnit\Options>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
23
    }
24
25
    /**
26
     * @throws \Exception
27
     */
28
    public function run()
29
    {
30
        $this->innerRunner->run();
31
32
        $this->tearDownTestDatabases();
33
    }
34
35
    public function tearDownTestDatabases()
36
    {
37
        $app = $this->createApp();
38
39
        $driver = $app['config']->get('database.default');
40
        $dbName = $app['config']->get("database.connections.{$driver}.database");
41
42
        for ($i = 1; $i <= $this->options->processes; ++$i) {
43
            $this->swapTestingDatabase($app, $driver, sprintf('%s_test_%s', $dbName, $i));
44
            Artisan::call('db:drop');
45
        }
46
    }
47
48
    protected function swapTestingDatabase($app, $driver, $dbName): void
49
    {
50
        // Paratest gives each process a unique TEST_TOKEN env variable.
51
        // When that's not set, we can default to 1 because it's
52
        // probably running on PHPUnit instead.
53
        $app['config']->set([
54
            "database.connections.{$driver}.database" => $dbName,
55
        ]);
56
    }
57
58
    private function createApp(): Application
59
    {
60
        $app = require getcwd().'/bootstrap/app.php';
61
62
        $app->make(Kernel::class)->bootstrap();
63
64
        return $app;
65
    }
66
}
67