Completed
Push — master ( df6049...d068c5 )
by Antonio
07:08 queued 04:48
created

getPdoQueueStoreConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Da\Mailer\Test;
3
4
use Da\Mailer\Queue\Backend\Pdo\PdoQueueStoreConnection;
5
use Da\Mailer\Test\Fixture\FixtureHelper;
6
use PHPUnit_Extensions_Database_TestCase;
7
8
abstract class AbstractMySqlDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
9
{
10
11
    protected static function getPdoQueueStoreConnection()
12
    {
13
        static $pdoQueue;
14
15
        if ($pdoQueue === null) {
16
            $pdoQueue = new PdoQueueStoreConnection(FixtureHelper::getMySqlConnectionConfiguration());
17
        }
18
19
        return $pdoQueue;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    protected function getConnection()
26
    {
27
        $pdo = self::getPdoQueueStoreConnection();
28
29
        return $this->createDefaultDBConnection($pdo->getInstance());
0 ignored issues
show
Bug introduced by
It seems like $pdo->getInstance() can be null; however, createDefaultDBConnection() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    protected function getDataSet()
36
    {
37
        return $this->createFlatXMLDataSet(__DIR__ . '/data/test.xml');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected function setUp()
44
    {
45
        $sql = file_get_contents(__DIR__ . '/migrations/mysql.sql');
46
47
        $statements = array_map('trim', array_filter(explode(";", $sql)));
48
49
        foreach($statements as $sqlQuery)
50
        {
51
            if(empty($sqlQuery)) {
52
                continue;
53
            }
54
            $query = self::getPdoQueueStoreConnection()->getInstance()->prepare($sqlQuery);
55
            $query->execute();
56
        }
57
58
59
        parent::setUp();
60
    }
61
}
62