12
12
use Doctrine \DBAL \Connection ;
13
13
use Doctrine \DBAL \Platforms \MySQLPlatform ;
14
14
use Doctrine \DBAL \Result ;
15
+ use Doctrine \DBAL \Schema \ForeignKeyConstraint ;
16
+ use Doctrine \DBAL \Schema \MySQLSchemaManager ;
15
17
use Doctrine \DBAL \Schema \Schema ;
16
18
use PHPUnit \Framework \Attributes \CoversClass ;
19
+ use PHPUnit \Framework \MockObject \MockObject ;
17
20
use PHPUnit \Framework \TestCase ;
18
21
22
+ use function array_keys ;
23
+ use function array_map ;
24
+ use function array_values ;
19
25
use function func_get_arg ;
26
+ use function func_get_args ;
20
27
use function Safe \preg_match ;
21
28
use function str_starts_with ;
22
29
@@ -26,11 +33,13 @@ final class MysqlBasedSchemaStrategyTest extends TestCase
26
33
use SnapshotTrait;
27
34
28
35
private MySQLPlatform $ platform ;
36
+ private MySQLSchemaManager &MockObject $ schemaManager ;
29
37
private SchemaBuilder $ schemaBuilder ;
30
38
31
39
protected function setUp (): void
32
40
{
33
41
$ this ->platform = new MySQLPlatform ();
42
+ $ this ->schemaManager = $ this ->createMock (MySQLSchemaManager::class);
34
43
35
44
$ this ->schemaBuilder = $ this ->createSchemaBuilder ();
36
45
$ this ->schemaBuilder ->foo ();
@@ -41,6 +50,22 @@ public function testApplySchema(): void
41
50
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
42
51
$ queryLog = new ArrayObject ();
43
52
53
+ $ this ->schemaManager ->expects ($ this ->any ())
54
+ ->method ('listTableNames ' )
55
+ ->willReturnCallback (
56
+ static function () use ($ queryLog ) {
57
+ $ result = [];
58
+
59
+ $ queryLog [] = [
60
+ 'function ' => 'listTableNames() ' ,
61
+ 'parameters ' => func_get_args (),
62
+ 'result ' => $ result ,
63
+ ];
64
+
65
+ return $ result ;
66
+ },
67
+ );
68
+
44
69
$ connection = $ this ->createMock (Connection::class);
45
70
$ connection ->expects ($ this ->any ())
46
71
->method ('quoteIdentifier ' )
@@ -51,11 +76,16 @@ public function testApplySchema(): void
51
76
$ connection ->expects ($ this ->any ())
52
77
->method ('getDatabasePlatform ' )
53
78
->willReturn ($ this ->platform );
79
+ $ connection ->expects ($ this ->any ())
80
+ ->method ('createSchemaManager ' )
81
+ ->willReturn ($ this ->schemaManager );
54
82
$ connection ->expects ($ this ->any ())
55
83
->method ('executeStatement ' )
56
84
->willReturnCallback (
57
- static function () use ($ queryLog ): void {
85
+ static function () use ($ queryLog ): int {
58
86
$ queryLog [] = ['statement ' => func_get_arg (0 )];
87
+
88
+ return 0 ;
59
89
},
60
90
);
61
91
$ connection ->expects ($ this ->any ())
@@ -68,7 +98,7 @@ static function () use ($queryLog, $connection) {
68
98
69
99
$ queryLog [] = ['query ' => $ query , 'parameters ' => $ parameters , 'result ' => $ result ];
70
100
71
- return new Result (new ArrayResult ($ result ), $ connection );
101
+ return new Result (self :: createArrayResult ($ result ), $ connection );
72
102
},
73
103
);
74
104
@@ -83,6 +113,57 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void
83
113
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
84
114
$ queryLog = new ArrayObject ();
85
115
116
+ $ this ->schemaManager ->expects ($ this ->once ())
117
+ ->method ('listTableNames ' )
118
+ ->willReturnCallback (static function () use ($ queryLog ) {
119
+ $ result = ['old_table_1 ' , 'old_table_2 ' ];
120
+
121
+ $ queryLog [] = ['function ' => 'listTableNames() ' , 'parameters ' => func_get_args (), 'result ' => $ result ];
122
+
123
+ return $ result ;
124
+ });
125
+
126
+ $ this ->schemaManager ->expects ($ this ->exactly (2 ))
127
+ ->method ('listTableForeignKeys ' )
128
+ ->willReturnCallback (static function ($ tableName ) use ($ queryLog ) {
129
+ $ result = [];
130
+
131
+ if ($ tableName === 'old_table_1 ' ) {
132
+ $ result = [
133
+ new ForeignKeyConstraint ([], '' , [], 'constraint_1 ' ),
134
+ new ForeignKeyConstraint ([], '' , [], 'constraint_2 ' ),
135
+ ];
136
+ }
137
+
138
+ $ queryLog [] = [
139
+ 'function ' => 'listTableForeignKeys() ' ,
140
+ 'parameters ' => func_get_args (),
141
+ 'result ' => array_map (static fn ($ fk ) => $ fk ->getName (), $ result ),
142
+ ];
143
+
144
+ return $ result ;
145
+ });
146
+
147
+ $ this ->schemaManager ->expects ($ this ->any ())
148
+ ->method ('dropForeignKey ' )
149
+ ->willReturnCallback (static function ($ tableName ) use ($ queryLog ) {
150
+ $ result = [];
151
+
152
+ $ queryLog [] = ['function ' => 'dropForeignKey() ' , 'parameters ' => func_get_args (), 'result ' => $ result ];
153
+
154
+ return $ result ;
155
+ });
156
+
157
+ $ this ->schemaManager ->expects ($ this ->any ())
158
+ ->method ('dropTable ' )
159
+ ->willReturnCallback (static function ($ tableName ) use ($ queryLog ) {
160
+ $ result = [];
161
+
162
+ $ queryLog [] = ['function ' => 'dropTable() ' , 'parameters ' => func_get_args (), 'result ' => $ result ];
163
+
164
+ return $ result ;
165
+ });
166
+
86
167
$ connection = $ this ->createMock (Connection::class);
87
168
$ connection ->expects ($ this ->any ())
88
169
->method ('quoteIdentifier ' )
@@ -93,11 +174,16 @@ public function testExistingTablesAreDroppedBeforeCreatingFreshSchema(): void
93
174
$ connection ->expects ($ this ->any ())
94
175
->method ('getDatabasePlatform ' )
95
176
->willReturn ($ this ->platform );
177
+ $ connection ->expects ($ this ->any ())
178
+ ->method ('createSchemaManager ' )
179
+ ->willReturn ($ this ->schemaManager );
96
180
$ connection ->expects ($ this ->any ())
97
181
->method ('executeStatement ' )
98
182
->willReturnCallback (
99
- static function () use ($ queryLog ): void {
183
+ static function () use ($ queryLog ): int {
100
184
$ queryLog [] = ['statement ' => func_get_arg (0 )];
185
+
186
+ return 0 ;
101
187
},
102
188
);
103
189
$ connection ->expects ($ this ->any ())
@@ -118,7 +204,7 @@ static function () use ($queryLog, $connection) {
118
204
119
205
$ queryLog [] = ['query ' => $ query , 'parameters ' => $ parameters , 'result ' => $ result ];
120
206
121
- return new Result (new ArrayResult ($ result ), $ connection );
207
+ return new Result (self :: createArrayResult ($ result ), $ connection );
122
208
},
123
209
);
124
210
@@ -133,6 +219,22 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void
133
219
/** @phpstan-var ArrayObject<string, mixed[]> $queryLog */
134
220
$ queryLog = new ArrayObject ();
135
221
222
+ $ this ->schemaManager ->expects ($ this ->any ())
223
+ ->method ('listTableNames ' )
224
+ ->willReturnCallback (
225
+ static function () use ($ queryLog ) {
226
+ $ result = [];
227
+
228
+ $ queryLog [] = [
229
+ 'function ' => 'listTableNames() ' ,
230
+ 'parameters ' => func_get_args (),
231
+ 'result ' => $ result ,
232
+ ];
233
+
234
+ return $ result ;
235
+ },
236
+ );
237
+
136
238
$ connection = $ this ->createMock (Connection::class);
137
239
$ connection ->expects ($ this ->any ())
138
240
->method ('quoteIdentifier ' )
@@ -143,11 +245,16 @@ public function testSchemaIsReadFromCacheIfDatabaseAndCacheExists(): void
143
245
$ connection ->expects ($ this ->any ())
144
246
->method ('getDatabasePlatform ' )
145
247
->willReturn (new MySQLPlatform ());
248
+ $ connection ->expects ($ this ->any ())
249
+ ->method ('createSchemaManager ' )
250
+ ->willReturn ($ this ->schemaManager );
146
251
$ connection ->expects ($ this ->any ())
147
252
->method ('executeStatement ' )
148
253
->willReturnCallback (
149
- static function () use ($ queryLog ): void {
254
+ static function () use ($ queryLog ): int {
150
255
$ queryLog [] = ['statement ' => func_get_arg (0 )];
256
+
257
+ return 0 ;
151
258
},
152
259
);
153
260
$ connection ->expects ($ this ->any ())
@@ -160,7 +267,7 @@ static function () use ($queryLog, $connection) {
160
267
161
268
$ queryLog [] = ['query ' => $ query , 'parameters ' => $ parameters , 'result ' => $ result ];
162
269
163
- return new Result (new ArrayResult ($ result ), $ connection );
270
+ return new Result (self :: createArrayResult ($ result ), $ connection );
164
271
},
165
272
);
166
273
@@ -194,8 +301,10 @@ public function testDeleteData(): void
194
301
$ connection ->expects ($ this ->any ())
195
302
->method ('executeStatement ' )
196
303
->willReturnCallback (
197
- static function () use ($ queryLog ): void {
304
+ static function () use ($ queryLog ): int {
198
305
$ queryLog [] = ['statement ' => func_get_arg (0 )];
306
+
307
+ return 0 ;
199
308
},
200
309
);
201
310
$ connection ->expects ($ this ->any ())
@@ -213,7 +322,7 @@ static function () use ($queryLog, $connection) {
213
322
214
323
$ queryLog [] = ['query ' => $ query , 'parameters ' => $ parameters , 'result ' => $ result ];
215
324
216
- return new Result (new ArrayResult ($ result ), $ connection );
325
+ return new Result (self :: createArrayResult ($ result ), $ connection );
217
326
},
218
327
);
219
328
@@ -244,8 +353,10 @@ public function testResetSequences(): void
244
353
$ connection ->expects ($ this ->any ())
245
354
->method ('executeStatement ' )
246
355
->willReturnCallback (
247
- static function () use ($ queryLog ): void {
356
+ static function () use ($ queryLog ): int {
248
357
$ queryLog [] = ['statement ' => func_get_arg (0 )];
358
+
359
+ return 0 ;
249
360
},
250
361
);
251
362
$ connection ->expects ($ this ->any ())
@@ -264,7 +375,7 @@ static function () use ($queryLog, $connection) {
264
375
265
376
$ queryLog [] = ['query ' => $ query , 'parameters ' => $ parameters , 'result ' => $ result ];
266
377
267
- return new Result (new ArrayResult ($ result ), $ connection );
378
+ return new Result (self :: createArrayResult ($ result ), $ connection );
268
379
},
269
380
);
270
381
@@ -298,7 +409,7 @@ public function foo(): void
298
409
{
299
410
$ table = $ this ->schema ->createTable ('foo ' );
300
411
$ table ->addColumn ('id ' , 'integer ' , ['autoincrement ' => true ]);
301
- $ table ->addColumn ('bar ' , 'string ' );
412
+ $ table ->addColumn ('bar ' , 'string ' , [ ' length ' => 255 ] );
302
413
}
303
414
};
304
415
}
@@ -307,4 +418,13 @@ private function snapshotPath(): string
307
418
{
308
419
return __DIR__ ;
309
420
}
421
+
422
+ /** @param mixed[] $result */
423
+ private static function createArrayResult (array $ result ): ArrayResult
424
+ {
425
+ $ columnNames = array_keys ($ result [0 ] ?? []);
426
+ $ rows = array_map (array_values (...), $ result );
427
+
428
+ return new ArrayResult ($ columnNames , $ rows );
429
+ }
310
430
}
0 commit comments