TestCase   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDb() 0 11 2
A dropDb() 0 9 1
A getEntityManager() 0 12 2
1
<?php
2
3
namespace DoctrineORMModuleTest\Framework;
4
5
use DoctrineORMModuleTest\ServiceManagerFactory;
6
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Tools\SchemaTool;
9
10
/**
11
 * Base test case for tests using the entity manager
12
 */
13
class TestCase extends PHPUnitTestCase
14
{
15
    /**
16
     * @var boolean
17
     */
18
    protected $hasDb = false;
19
20
    /**
21
     * @var EntityManager
22
     */
23
    private $entityManager;
24
25
    /**
26
     * Creates a database if not done already.
27
     */
28
    public function createDb()
29
    {
30
        if ($this->hasDb) {
31
            return;
32
        }
33
34
        $em   = $this->getEntityManager();
35
        $tool = new SchemaTool($em);
36
        $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());
37
        $this->hasDb = true;
38
    }
39
40
    /**
41
     * Drops existing database
42
     */
43
    public function dropDb()
44
    {
45
        $em   = $this->getEntityManager();
46
        $tool = new SchemaTool($em);
47
        $tool->dropSchema($em->getMetadataFactory()->getAllMetadata());
48
        $em->clear();
49
50
        $this->hasDb = false;
51
    }
52
53
    /**
54
     * Get EntityManager.
55
     *
56
     * @return EntityManager
57
     */
58
    public function getEntityManager()
59
    {
60
        if ($this->entityManager) {
61
            return $this->entityManager;
62
        }
63
64
        $serviceManager = ServiceManagerFactory::getServiceManager();
65
        $serviceManager->get('doctrine.entity_resolver.orm_default');
66
        $this->entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
67
68
        return $this->entityManager;
69
    }
70
}
71