9
9
use Doctrine \Migrations \Configuration \Connection \ExistingConnection ;
10
10
use Doctrine \Migrations \Configuration \Migration \ExistingConfiguration ;
11
11
use Doctrine \Migrations \DependencyFactory ;
12
+ use Doctrine \Migrations \Exception \UnknownMigrationVersion ;
12
13
use Doctrine \Migrations \Metadata \MigrationPlan ;
13
14
use Doctrine \Migrations \Metadata \MigrationPlanList ;
14
15
use Doctrine \Migrations \Metadata \Storage \TableMetadataStorageConfiguration ;
17
18
use Doctrine \Migrations \QueryWriter ;
18
19
use Doctrine \Migrations \Tests \MigrationTestCase ;
19
20
use Doctrine \Migrations \Tools \Console \Command \ExecuteCommand ;
21
+ use Doctrine \Migrations \Version \AliasResolver ;
20
22
use Doctrine \Migrations \Version \Direction ;
21
23
use Doctrine \Migrations \Version \MigrationPlanCalculator ;
22
24
use Doctrine \Migrations \Version \Version ;
@@ -47,6 +49,9 @@ class ExecuteCommandTest extends MigrationTestCase
47
49
/** @var MigrationPlanCalculator|MockObject */
48
50
private $ planCalculator ;
49
51
52
+ /** @var AliasResolver|MockObject */
53
+ private $ versionAliasResolver ;
54
+
50
55
/**
51
56
* @param bool|string|null $arg
52
57
*
@@ -63,6 +68,12 @@ public function testWriteSql(bool $dryRun, $arg, ?string $path): void
63
68
return ['A ' ];
64
69
});
65
70
71
+ $ this ->versionAliasResolver
72
+ ->expects (self ::once ())
73
+ ->method ('resolveVersionAlias ' )
74
+ ->with ('1 ' )
75
+ ->willReturn (new Version ('1 ' ));
76
+
66
77
if ($ arg === false ) {
67
78
$ this ->queryWriter
68
79
->expects (self ::never ())
@@ -116,6 +127,12 @@ public function testExecute(): void
116
127
return ['A ' ];
117
128
});
118
129
130
+ $ this ->versionAliasResolver
131
+ ->expects (self ::once ())
132
+ ->method ('resolveVersionAlias ' )
133
+ ->with ('1 ' )
134
+ ->willReturn (new Version ('1 ' ));
135
+
119
136
$ this ->executeCommandTester ->execute ([
120
137
'versions ' => ['1 ' ],
121
138
'--down ' => true ,
@@ -125,6 +142,63 @@ public function testExecute(): void
125
142
self ::assertStringContainsString ('[notice] Executing 1 up ' , trim ($ this ->executeCommandTester ->getDisplay (true )));
126
143
}
127
144
145
+ public function testExecuteVersionAlias (): void
146
+ {
147
+ $ this ->executeCommandTester ->setInputs (['yes ' ]);
148
+
149
+ $ this ->migrator
150
+ ->expects (self ::once ())
151
+ ->method ('migrate ' )
152
+ ->willReturnCallback (static function (MigrationPlanList $ planList , MigratorConfiguration $ configuration ): array {
153
+ self ::assertFalse ($ configuration ->isDryRun ());
154
+
155
+ return ['A ' ];
156
+ });
157
+
158
+ $ this ->versionAliasResolver
159
+ ->expects (self ::once ())
160
+ ->method ('resolveVersionAlias ' )
161
+ ->with ('current ' )
162
+ ->willReturn (new Version ('1 ' ));
163
+
164
+ $ this ->executeCommandTester ->execute ([
165
+ 'versions ' => ['current ' ],
166
+ '--down ' => true ,
167
+ ]);
168
+
169
+ self ::assertSame (0 , $ this ->executeCommandTester ->getStatusCode ());
170
+ self ::assertStringContainsString ('[notice] Executing 1 up ' , trim ($ this ->executeCommandTester ->getDisplay (true )));
171
+ }
172
+
173
+ public function testExecuteVersionAliasException (): void
174
+ {
175
+ $ this ->executeCommandTester ->setInputs (['yes ' ]);
176
+
177
+ $ this ->migrator
178
+ ->expects (self ::never ())
179
+ ->method ('migrate ' )
180
+ ->willReturnCallback (static function (MigrationPlanList $ planList , MigratorConfiguration $ configuration ): array {
181
+ self ::assertFalse ($ configuration ->isDryRun ());
182
+
183
+ return ['A ' ];
184
+ });
185
+
186
+ $ exception = new UnknownMigrationVersion ('not_a_valid_version_alias ' );
187
+
188
+ $ this ->versionAliasResolver
189
+ ->expects (self ::once ())
190
+ ->method ('resolveVersionAlias ' )
191
+ ->with ('not_a_valid_version_alias ' )
192
+ ->willThrowException ($ exception );
193
+
194
+ self ::expectExceptionObject ($ exception );
195
+
196
+ $ this ->executeCommandTester ->execute ([
197
+ 'versions ' => ['not_a_valid_version_alias ' ],
198
+ '--down ' => true ,
199
+ ]);
200
+ }
201
+
128
202
public function testExecuteMultiple (): void
129
203
{
130
204
$ migration = $ this ->createMock (AbstractMigration::class);
@@ -148,6 +222,12 @@ public function testExecuteMultiple(): void
148
222
return ['A ' ];
149
223
});
150
224
225
+ $ this ->versionAliasResolver
226
+ ->expects (self ::exactly (2 ))
227
+ ->method ('resolveVersionAlias ' )
228
+ ->withConsecutive (['1 ' ], ['2 ' ])
229
+ ->willReturnOnConsecutiveCalls (new Version ('1 ' ), new Version ('2 ' ));
230
+
151
231
$ this ->executeCommandTester ->execute ([
152
232
'versions ' => ['1 ' , '2 ' ],
153
233
'--down ' => true ,
@@ -174,6 +254,12 @@ public function testExecuteCancel(): void
174
254
return ['A ' ];
175
255
});
176
256
257
+ $ this ->versionAliasResolver
258
+ ->expects (self ::once ())
259
+ ->method ('resolveVersionAlias ' )
260
+ ->with ('1 ' )
261
+ ->willReturn (new Version ('1 ' ));
262
+
177
263
$ this ->executeCommandTester ->execute ([
178
264
'versions ' => ['1 ' ],
179
265
'--down ' => true ,
@@ -186,9 +272,10 @@ protected function setUp(): void
186
272
{
187
273
$ connection = $ this ->getSqliteConnection ();
188
274
189
- $ this ->migrator = $ this ->createMock (Migrator::class);
190
- $ this ->queryWriter = $ this ->createMock (QueryWriter::class);
191
- $ migration = $ this ->createMock (AbstractMigration::class);
275
+ $ this ->migrator = $ this ->createMock (Migrator::class);
276
+ $ this ->queryWriter = $ this ->createMock (QueryWriter::class);
277
+ $ this ->versionAliasResolver = $ this ->createMock (AliasResolver::class);
278
+ $ migration = $ this ->createMock (AbstractMigration::class);
192
279
193
280
$ p1 = new MigrationPlan (new Version ('1 ' ), $ migration , Direction::UP );
194
281
$ pl = new MigrationPlanList ([$ p1 ], Direction::UP );
@@ -207,6 +294,7 @@ protected function setUp(): void
207
294
$ this ->dependencyFactory ->setService (Migrator::class, $ this ->migrator );
208
295
$ this ->dependencyFactory ->setService (MigrationPlanCalculator::class, $ this ->planCalculator );
209
296
$ this ->dependencyFactory ->setService (QueryWriter::class, $ this ->queryWriter );
297
+ $ this ->dependencyFactory ->setService (AliasResolver::class, $ this ->versionAliasResolver );
210
298
211
299
$ this ->executeCommand = new ExecuteCommand ($ this ->dependencyFactory );
212
300
0 commit comments