Completed
Push — master ( 630401...3eb757 )
by Oleg
02:43
created

CleanDoctrine2   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

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\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