Passed
Pull Request — master (#153)
by Kevin
03:37
created

ORMDatabaseResetter::dropSchema()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Test;
4
5
use Doctrine\Persistence\ManagerRegistry;
6
use Symfony\Bundle\FrameworkBundle\Console\Application;
7
8
/**
9
 * @internal
10
 */
11
final class ORMDatabaseResetter extends AbstractSchemaResetter
12
{
13
    /** @var Application */
14
    private $application;
15
    /** @var ManagerRegistry */
16
    private $registry;
17
18
    public function __construct(Application $application, ManagerRegistry $registry)
19
    {
20
        $this->application = $application;
21
        $this->registry = $registry;
22
    }
23
24
    public function resetDatabase(): void
25
    {
26
        foreach ($this->connectionsToReset() as $connection) {
27
            $dropParams = ['--connection' => $connection, '--force' => true];
28
29
            if ('sqlite' !== $this->registry->getConnection($connection)->getDatabasePlatform()->getName()) {
30
                // sqlite does not support "--if-exists" (ref: https://github.com/doctrine/dbal/pull/2402)
31
                $dropParams['--if-exists'] = true;
32
            }
33
34
            $this->runCommand($this->application, 'doctrine:database:drop', $dropParams);
35
36
            $this->runCommand(
37
                $this->application,
38
                'doctrine:database:create',
39
                [
40
                    '--connection' => $connection,
41
                ]
42
            );
43
        }
44
45
        $this->createSchema();
46
    }
47
48
    public function resetSchema(): void
49
    {
50
        if (DatabaseResetter::isDAMADoctrineTestBundleEnabled()) {
51
            // not required as the DAMADoctrineTestBundle wraps each test in a transaction
52
            return;
53
        }
54
55
        $this->dropSchema();
56
        $this->createSchema();
57
    }
58
59
    private function createSchema(): void
60
    {
61
        if (self::isResetUsingMigrations()) {
62
            $this->runCommand($this->application, 'doctrine:migrations:migrate', ['-n' => true]);
63
64
            return;
65
        }
66
67
        foreach ($this->objectManagersToReset() as $manager) {
68
            $this->runCommand(
69
                $this->application,
70
                'doctrine:schema:create',
71
                [
72
                    '--em' => $manager,
73
                ]
74
            );
75
        }
76
    }
77
78
    private function dropSchema(): void
79
    {
80
        if (self::isResetUsingMigrations()) {
81
            $this->resetDatabase();
82
83
            return;
84
        }
85
86
        foreach ($this->objectManagersToReset() as $manager) {
87
            $this->runCommand(
88
                $this->application,
89
                'doctrine:schema:drop',
90
                [
91
                    '--em' => $manager,
92
                    '--force' => true,
93
                ]
94
            );
95
        }
96
    }
97
98
    /** @return list<string> */
0 ignored issues
show
Bug introduced by
The type Zenstruck\Foundry\Test\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
    private function connectionsToReset(): array
100
    {
101
        if (isset($_SERVER['FOUNDRY_RESET_CONNECTIONS'])) {
102
            return \explode(',', $_SERVER['FOUNDRY_RESET_CONNECTIONS']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode(',', $_SE...RY_RESET_CONNECTIONS']) returns the type string[] which is incompatible with the documented return type Zenstruck\Foundry\Test\list.
Loading history...
103
        }
104
105
        return [$this->registry->getDefaultConnectionName()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->regi...efaultConnectionName()) returns the type array<integer,string> which is incompatible with the documented return type Zenstruck\Foundry\Test\list.
Loading history...
106
    }
107
108
    /** @return list<string> */
109
    private function objectManagersToReset(): array
110
    {
111
        if (isset($_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS'])) {
112
            return \explode(',', $_SERVER['FOUNDRY_RESET_OBJECT_MANAGERS']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode(',', $_SE...ESET_OBJECT_MANAGERS']) returns the type string[] which is incompatible with the documented return type Zenstruck\Foundry\Test\list.
Loading history...
113
        }
114
115
        return [$this->registry->getDefaultManagerName()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($this->regi...etDefaultManagerName()) returns the type array<integer,string> which is incompatible with the documented return type Zenstruck\Foundry\Test\list.
Loading history...
116
    }
117
118
    private static function isResetUsingMigrations(): bool
119
    {
120
        return 'migrate' === ($_SERVER['FOUNDRY_RESET_MODE'] ?? 'schema');
121
    }
122
}
123