|
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( |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
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
|
|
|
|