1 | <?php |
||
13 | * Base test case for tests using the entity manager |
||
14 | */ |
||
15 | class TestCase extends PHPUnitTestCase |
||
16 | { |
||
17 | protected bool $hasDb = false; |
||
|
|||
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 |