Completed
Push — master ( ccb9d5...d7e68d )
by Alejandro
09:04
created

DatabaseTestCase::getConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\DbUnit;
5
6
use Doctrine\DBAL\Driver\PDOConnection;
7
use Doctrine\ORM\EntityManagerInterface;
8
use PHPUnit\DbUnit\Database\Connection as DbConn;
9
use PHPUnit\DbUnit\DataSet\IDataSet as DataSet;
10
use PHPUnit\DbUnit\TestCase;
11
12
abstract class DatabaseTestCase extends TestCase
13
{
14
    const ENTITIES_TO_EMPTY = [];
15
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    public static $em;
20
    /**
21
     * @var DbConn
22
     */
23
    private static $conn;
24
25
    public function getConnection(): DbConn
26
    {
27
        if (isset(self::$conn)) {
28
            return self::$conn;
29
        }
30
31
        /** @var PDOConnection $pdo */
32
        $pdo = static::$em->getConnection()->getWrappedConnection();
33
        return self::$conn = $this->createDefaultDBConnection($pdo, static::$em->getConnection()->getDatabase());
34
    }
35
36
    public function getDataSet(): DataSet
37
    {
38
        return $this->createArrayDataSet([]);
39
    }
40
41
    protected function getEntityManager(): EntityManagerInterface
42
    {
43
        return static::$em;
44
    }
45
46
    public function tearDown()
47
    {
48
        // Empty all entity tables defined by this test after each test
49
        foreach (static::ENTITIES_TO_EMPTY as $entityClass) {
50
            $qb = $this->getEntityManager()->createQueryBuilder();
51
            $qb->delete($entityClass, 'x');
52
            $qb->getQuery()->execute();
53
        }
54
55
        // Clear entity manager
56
        $this->getEntityManager()->clear();
57
    }
58
}
59