TestCase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 86
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArrayConfig() 0 8 2
B getEntityManager() 0 27 2
A createDb() 0 11 2
A dropDb() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\DoctrineDDMTest\Framework;
6
7
use Doctrine\Common\EventManager;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Tools\SchemaTool;
10
use Doctrine\ORM\Tools\Setup;
11
use Facile\DoctrineDDM\Factory\MetadataConfigFactory;
12
use Facile\DoctrineDDM\MetadataListener;
13
14
class TestCase extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    protected $entityManager;
20
    /**
21
     * @var bool
22
     */
23
    protected $hasDb = false;
24
    /**
25
     * @var array
26
     */
27
    protected $arrayConfig;
28
29
    /**
30
     * @return array
31
     */
32
    public function getArrayConfig(): array
33
    {
34
        if (! $this->arrayConfig) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->arrayConfig of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
            $this->arrayConfig = include __DIR__.'./../../config/testing.config.php';
36
        }
37
38
        return $this->arrayConfig;
39
    }
40
41
    /**
42
     * @return EntityManager
43
     */
44
    public function getEntityManager(): EntityManager
45
    {
46
        if (! $this->entityManager) {
47
            $metadataConfigFactory = new MetadataConfigFactory();
48
            $eventManager = new EventManager();
49
            $eventManager->addEventSubscriber(
50
                new MetadataListener(
51
                    $metadataConfigFactory->createMetadata($this->getArrayConfig()[MetadataConfigFactory::class])
52
                )
53
            );
54
            $config = Setup::createAnnotationMetadataConfiguration(
55
                [__DIR__.'/../Assets'],
56
                true,
57
                null,
58
                null,
59
                false
60
            );
61
            $connection = [
62
                'driver' => 'pdo_sqlite',
63
                'memory' => true,
64
            ];
65
66
            $this->entityManager = EntityManager::create($connection, $config, $eventManager);
67
        }
68
69
        return $this->entityManager;
70
    }
71
72
    /**
73
     * Creates a database if not done already.
74
     */
75
    public function createDb()
76
    {
77
        if ($this->hasDb) {
78
            return;
79
        }
80
81
        $em = $this->getEntityManager();
82
        $tool = new SchemaTool($em);
83
        $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());
84
        $this->hasDb = true;
85
    }
86
87
    /**
88
     * Drops existing database.
89
     */
90
    public function dropDb()
91
    {
92
        $em = $this->getEntityManager();
93
        $tool = new SchemaTool($em);
94
        $tool->dropSchema($em->getMetadataFactory()->getAllMetadata());
95
        $em->clear();
96
97
        $this->hasDb = false;
98
    }
99
}
100