Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

iFlushFollowingTablesForConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Behat\Features\Dms\MySQL\Repository;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Janisbiz\LightOrm\Connection\ConnectionInterface;
7
use Janisbiz\LightOrm\Dms\MySQL\Connection\Connection as MySQLConnection;
8
use Janisbiz\LightOrm\Dms\MySQL\Generator\DmsFactory;
9
use Janisbiz\LightOrm\Repository\RepositoryInterface;
10
use Monolog\Handler\StreamHandler;
11
use Monolog\Logger;
12
use Psr\Log\LoggerAwareInterface;
13
14
class RepositoryFeatureContext extends AbstractRepositoryFeatureContext
15
{
16
    /**
17
     * @BeforeScenario
18
     */
19
    public function beforeScenario()
20
    {
21
        static::$repositories = [];
22
    }
23
24
    /**
25
     * @Given /^I flush all tables for connection "(.*)"$/
26
     *
27
     * @param string $connectionName
28
     *
29
     * @throws \Exception
30
     */
31
    public function iFlushAllTablesForConnection(string $connectionName)
32
    {
33
        $connection = $this->connectionPool->getConnection($connectionName);
34
35
        $tables = [];
36
        switch (\get_class($connection)) {
37
            case MySQLConnection::class:
38
                $dmsDatabase = (new DmsFactory())->createDmsDatabase($connectionName, $connection);
39
                foreach ($dmsDatabase->getDmsTables() as $dmsTable) {
40
                    $tables[] = $dmsTable->getName();
41
                }
42
43
                break;
44
45
            default:
46
                throw new \Exception(\sprintf('Flush all tables for connection "%s" is no defined!', $connectionName));
47
        }
48
49
        $this->flushTables($connection, $connectionName, $tables);
50
    }
51
52
    /**
53
     * @Given /^I flush following tables for connection "(.*)":$/
54
     *
55
     * @param string $connectionName
56
     * @param TableNode $tables
57
     */
58
    public function iFlushFollowingTablesForConnection(string $connectionName, TableNode $tables)
59
    {
60
        $tablesArray = [];
61
        foreach ($tables->getTable() as $table) {
62
            $tablesArray[] = $table[0];
63
        }
64
65
        $this->flushTables($this->connectionPool->getConnection($connectionName), $connectionName, $tablesArray);
66
    }
67
68
    /**
69
     * @Given /^I reset database for connection "(.*)"$/
70
     *
71
     * @param string $connectionName
72
     */
73
    public function iResetDatabaseForConnection(string $connectionName)
74
    {
75
        $connection = $this->connectionPool->getConnection($connectionName);
76
        switch (\get_class($connection)) {
77
            case MySQLConnection::class:
78
                $connection->exec(\file_get_contents(\implode(
0 ignored issues
show
Bug introduced by
The method exec() does not exist on Janisbiz\LightOrm\Connection\ConnectionInterface. It seems like you code against a sub-type of Janisbiz\LightOrm\Connection\ConnectionInterface such as Janisbiz\LightOrm\Dms\MySQL\Connection\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
                $connection->/** @scrutinizer ignore-call */ 
79
                             exec(\file_get_contents(\implode(
Loading history...
79
                    '',
80
                    [
81
                        JANISBIZ_LIGHT_ORM_ROOT_DIR,
82
                        '.docker',
83
                        DIRECTORY_SEPARATOR,
84
                        'config',
85
                        DIRECTORY_SEPARATOR,
86
                        'mysql',
87
                        DIRECTORY_SEPARATOR,
88
                        'light_orm_mysql.sql',
89
                    ]
90
                )));
91
92
                break;
93
        }
94
    }
95
    
96
    /**
97
     * @Given /^I create repository "(.*)"$/
98
     *
99
     * @param $repositoryClass
100
     *
101
     * @throws \Exception
102
     */
103
    public function iCreateRepository(string $repositoryClass)
104
    {
105
        if (\array_key_exists($repositoryClass, static::$repositories)) {
106
            return;
107
        }
108
109
        $repository = new $repositoryClass;
110
111
        if (!$repository instanceof RepositoryInterface) {
112
            throw new \Exception(\sprintf(
113
                'Class "%s" must implement "%s"',
114
                \get_class($repository),
115
                RepositoryInterface::class
116
            ));
117
        }
118
119
        $logger = new Logger('light-orm-mysql');
120
        $logger->pushHandler(new StreamHandler(\implode(
121
            '',
122
            [
123
                JANISBIZ_LIGHT_ORM_ROOT_DIR,
124
                'var',
125
                DIRECTORY_SEPARATOR,
126
                'log',
127
                DIRECTORY_SEPARATOR,
128
                'light-orm-mysql.log'
129
            ]
130
        )));
131
132
        /** @var LoggerAwareInterface $repository */
133
        $repository->setLogger($logger);
134
135
        static::$repositories[$repositoryClass] = static::$repository = $repository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $repository of type Psr\Log\LoggerAwareInterface is incompatible with the declared type Janisbiz\LightOrm\Repository\RepositoryInterface of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
136
    }
137
138
    /**
139
     * @Given /^I make repository "(.*)" as active repository$/
140
     *
141
     * @param string $repositoryClass
142
     *
143
     * @throws \Exception
144
     */
145
    public function iMakeRepositoryAsActiveRepository(string $repositoryClass)
146
    {
147
        if (!\array_key_exists($repositoryClass, static::$repositories)) {
148
            throw new \Exception(\sprintf(
149
                'There is no present repository "%s"! Please create repository before making it active!',
150
                $repositoryClass
151
            ));
152
        }
153
154
        static::$repository = static::$repositories[$repositoryClass];
155
    }
156
157
    /**
158
     * @Then /^I have exception with message "(.*)"$/
159
     *
160
     * @param string $message
161
     *
162
     * @throws \Exception
163
     */
164
    public function iHaveExceptionWithMessage(string $message)
165
    {
166
        if (null === static::$exception) {
167
            throw new \Exception('There is no expected exception!');
168
        }
169
170
        if ($message !== static::$exception->getMessage()) {
171
            throw new \Exception(\sprintf(
172
                'Expected exception doesn\'t containt message "%s", it contains message "%s"!',
173
                $message,
174
                static::$exception->getMessage()
175
            ));
176
        }
177
    }
178
179
    /**
180
     * @Given /^I begin transaction on connection "(.*)"$/
181
     *
182
     * @param string $connectionName
183
     */
184
    public function iBeginTransactionOnConnection(string $connectionName)
185
    {
186
        $this->connectionPool->getConnection($connectionName)->beginTransaction();
187
    }
188
189
    /**
190
     * @Given /^I commit transaction on connection "(.*)"$/
191
     *
192
     * @param string $connectionName
193
     */
194
    public function iCommitTransactionOnConnection(string $connectionName)
195
    {
196
        $this->connectionPool->getConnection($connectionName)->commit();
0 ignored issues
show
Bug introduced by
The method commit() does not exist on Janisbiz\LightOrm\Connection\ConnectionInterface. It seems like you code against a sub-type of Janisbiz\LightOrm\Connection\ConnectionInterface such as Janisbiz\LightOrm\Dms\MySQL\Connection\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
        $this->connectionPool->getConnection($connectionName)->/** @scrutinizer ignore-call */ commit();
Loading history...
197
    }
198
199
    /**
200
     * @Given /^I rollback transaction on connection "(.*)"$/
201
     *
202
     * @param string $connectionName
203
     */
204
    public function iRollbackTransactionOnConnection(string $connectionName)
205
    {
206
        $this->connectionPool->getConnection($connectionName)->rollBack();
0 ignored issues
show
Bug introduced by
The method rollBack() does not exist on Janisbiz\LightOrm\Connection\ConnectionInterface. It seems like you code against a sub-type of Janisbiz\LightOrm\Connection\ConnectionInterface such as Janisbiz\LightOrm\Dms\MySQL\Connection\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        $this->connectionPool->getConnection($connectionName)->/** @scrutinizer ignore-call */ rollBack();
Loading history...
207
    }
208
209
    /**
210
     * @param ConnectionInterface $connection
211
     * @param string $connectionName
212
     * @param array $tables
213
     *
214
     * @throws \Exception
215
     */
216
    private function flushTables(ConnectionInterface $connection, string $connectionName, array $tables)
217
    {
218
        switch (\get_class($connection)) {
219
            case MySQLConnection::class:
220
                $connection->query('SET SESSION FOREIGN_KEY_CHECKS = 0');
0 ignored issues
show
Bug introduced by
The method query() does not exist on Janisbiz\LightOrm\Connection\ConnectionInterface. It seems like you code against a sub-type of Janisbiz\LightOrm\Connection\ConnectionInterface such as Janisbiz\LightOrm\Dms\MySQL\Connection\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

220
                $connection->/** @scrutinizer ignore-call */ 
221
                             query('SET SESSION FOREIGN_KEY_CHECKS = 0');
Loading history...
221
                foreach ($tables as $table) {
222
                    $connection->query(\sprintf('TRUNCATE TABLE %s', $table));
223
                }
224
                $connection->query('SET SESSION FOREIGN_KEY_CHECKS = 1');
225
226
                break;
227
228
            default:
229
                throw new \Exception(\sprintf('Flush all tables for connection "%s" is no defined!', $connectionName));
230
        }
231
    }
232
}
233