|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace codecept\Helper; |
|
5
|
|
|
|
|
6
|
|
|
use Codeception\TestInterface; |
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
8
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
9
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\SimpleRegistry; |
|
10
|
|
|
|
|
11
|
|
|
class CleanDoctrine2 extends \Codeception\Module\Doctrine2 |
|
12
|
|
|
{ |
|
13
|
|
|
protected $dependencyMessage = <<<EOF |
|
14
|
|
|
Set a dependent module: |
|
15
|
|
|
|
|
16
|
|
|
modules: |
|
17
|
|
|
enabled: |
|
18
|
|
|
- CleanDoctrine2: |
|
19
|
|
|
depends: ZendExpressive3 |
|
20
|
|
|
EOF; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ManagerRegistry |
|
24
|
|
|
*/ |
|
25
|
|
|
public $registry; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
* @param TestInterface $test |
|
30
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
31
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function _before(TestInterface $test) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->retrieveEntityManager(); |
|
36
|
|
|
if ($this->config['cleanup']) { |
|
37
|
|
|
$schemaManager = $this->em->getConnection()->getSchemaManager(); |
|
38
|
|
|
$currentSchema = $schemaManager->createSchema(); |
|
39
|
|
|
|
|
40
|
|
|
$metadatas = $this->em->getMetadataFactory()->getAllMetadata(); |
|
41
|
|
|
$expectedSchema = (new SchemaTool($this->em))->getSchemaFromMetadata($metadatas); |
|
42
|
|
|
|
|
43
|
|
|
$sql = $currentSchema->getMigrateToSql($expectedSchema, $this->em->getConnection()->getDatabasePlatform()); |
|
44
|
|
|
array_walk($sql, function ($script) { |
|
45
|
|
|
$this->em->getConnection()->executeUpdate($script); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
$this->debugSection('Database', 'Database Created'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function _after(TestInterface $test) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->retrieveEntityManager(); |
|
58
|
|
|
if ($this->config['cleanup']) { |
|
59
|
|
|
$metadatas = $this->em->getMetadataFactory()->getAllMetadata(); |
|
60
|
|
|
(new SchemaTool($this->em))->dropSchema($metadatas); |
|
61
|
|
|
|
|
62
|
|
|
$this->debugSection('Database', 'Database Is Dropped!'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->clean(); |
|
66
|
|
|
$this->em->getConnection()->close(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @throws \Codeception\Exception\ModuleConfigException |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function retrieveEntityManager() |
|
73
|
|
|
{ |
|
74
|
|
|
parent::retrieveEntityManager(); |
|
75
|
|
|
$this->registry = new SimpleRegistry([], [ |
|
76
|
|
|
SimpleRegistry::DEFAULT_MANAGER_NAME => $this->em, |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|