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

DatabaseTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 10 2
A getDataSet() 0 4 1
A getEntityManager() 0 4 1
A tearDown() 0 12 2
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