Completed
Pull Request — master (#602)
by Tom
07:01
created

TestCase   A

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
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 58
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModuleTest\Framework;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Tools\SchemaTool;
9
use DoctrineORMModuleTest\ServiceManagerFactory;
10
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
11
12
/**
13
 * Base test case for tests using the entity manager
14
 */
15
class TestCase extends PHPUnitTestCase
16
{
17
    protected bool $hasDb = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
18
19
    private EntityManager $entityManager;
20
21
    /**
22
     * Creates a database if not done already.
23
     */
24
    public function createDb() : void
25
    {
26
        if ($this->hasDb) {
27
            return;
28
        }
29
30
        $em   = $this->getEntityManager();
31
        $tool = new SchemaTool($em);
32
        $tool->updateSchema($em->getMetadataFactory()->getAllMetadata());
33
        $this->hasDb = true;
34
    }
35
36
    /**
37
     * Drops existing database
38
     */
39
    public function dropDb() : void
40
    {
41
        $em   = $this->getEntityManager();
42
        $tool = new SchemaTool($em);
43
        $tool->dropSchema($em->getMetadataFactory()->getAllMetadata());
44
        $em->clear();
45
46
        $this->hasDb = false;
47
    }
48
49
    /**
50
     * Get EntityManager.
51
     */
52
    public function getEntityManager() : EntityManager
53
    {
54
        if (isset($this->entityManager)) {
55
            return $this->entityManager;
56
        }
57
58
        $serviceManager = ServiceManagerFactory::getServiceManager();
59
        $serviceManager->get('doctrine.entity_resolver.orm_default');
60
        $this->entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
61
62
        return $this->entityManager;
63
    }
64
}
65