CleanDoctrine2   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _before() 0 18 2
A _after() 0 13 2
A retrieveEntityManager() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace codecept\Helper;
5
6
use Codeception\TestInterface;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry;
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 EntityManagerRegistry
24
     */
25
    public $registry;
26
27
    /**
28
     * @inheritdoc
29
     * @param TestInterface $test
30
     * @throws \Doctrine\DBAL\DBALException
31
     * @throws \Doctrine\ORM\ORMException
32
     * @throws \Codeception\Exception\ModuleConfigException
33
     */
34
    public function _before(TestInterface $test)
35
    {
36
        $this->retrieveEntityManager();
37
        if ($this->config['cleanup']) {
38
            $schemaManager = $this->em->getConnection()->getSchemaManager();
39
            $currentSchema = $schemaManager->createSchema();
40
41
            $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
42
            $expectedSchema = (new SchemaTool($this->em))->getSchemaFromMetadata($metadatas);
43
44
            $sql = $currentSchema->getMigrateToSql($expectedSchema, $this->em->getConnection()->getDatabasePlatform());
45
            array_walk($sql, function ($script) {
46
                $this->em->getConnection()->executeUpdate($script);
47
            });
48
49
            $this->debugSection('Database', 'Database Created');
50
        }
51
    }
52
53
    /**
54
     * @inheritdoc
55
     * @throws \Codeception\Exception\ModuleConfigException
56
     */
57
    public function _after(TestInterface $test)
58
    {
59
        $this->retrieveEntityManager();
60
        if ($this->config['cleanup']) {
61
            $metadatas = $this->em->getMetadataFactory()->getAllMetadata();
62
            (new SchemaTool($this->em))->dropSchema($metadatas);
63
64
            $this->debugSection('Database', 'Database Is Dropped!');
65
        }
66
67
        $this->clean();
68
        $this->em->getConnection()->close();
69
    }
70
71
    /**
72
     * @throws \Codeception\Exception\ModuleConfigException
73
     */
74
    protected function retrieveEntityManager()
75
    {
76
        parent::retrieveEntityManager();
77
        $this->registry = new SimpleRegistry([], [
78
            SimpleRegistry::DEFAULT_MANAGER_NAME => $this->em,
79
        ]);
80
    }
81
}
82