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

AbstractMySqlDatabaseTestCase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPdoQueueStoreConnection() 0 10 2
A getConnection() 0 6 1
A getDataSet() 0 4 1
A setUp() 0 18 3
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