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) { |
|
|
|
|
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
|
|
|
|
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.