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,13 @@ 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
+ ;
77
+
66
78
if ($ arg === false ) {
67
79
$ this ->queryWriter
68
80
->expects (self ::never ())
@@ -116,6 +128,12 @@ public function testExecute(): void
116
128
return ['A ' ];
117
129
});
118
130
131
+ $ this ->versionAliasResolver
132
+ ->expects (self ::once ())
133
+ ->method ('resolveVersionAlias ' )
134
+ ->with ('1 ' )
135
+ ->willReturn (new Version ('1 ' ));
136
+
119
137
$ this ->executeCommandTester ->execute ([
120
138
'versions ' => ['1 ' ],
121
139
'--down ' => true ,
@@ -125,6 +143,63 @@ public function testExecute(): void
125
143
self ::assertStringContainsString ('[notice] Executing 1 up ' , trim ($ this ->executeCommandTester ->getDisplay (true )));
126
144
}
127
145
146
+ public function testExecuteVersionAlias (): void
147
+ {
148
+ $ this ->executeCommandTester ->setInputs (['yes ' ]);
149
+
150
+ $ this ->migrator
151
+ ->expects (self ::once ())
152
+ ->method ('migrate ' )
153
+ ->willReturnCallback (static function (MigrationPlanList $ planList , MigratorConfiguration $ configuration ): array {
154
+ self ::assertFalse ($ configuration ->isDryRun ());
155
+
156
+ return ['A ' ];
157
+ });
158
+
159
+ $ this ->versionAliasResolver
160
+ ->expects (self ::once ())
161
+ ->method ('resolveVersionAlias ' )
162
+ ->with ('current ' )
163
+ ->willReturn (new Version ('1 ' ));
164
+
165
+ $ this ->executeCommandTester ->execute ([
166
+ 'versions ' => ['current ' ],
167
+ '--down ' => true ,
168
+ ]);
169
+
170
+ self ::assertSame (0 , $ this ->executeCommandTester ->getStatusCode ());
171
+ self ::assertStringContainsString ('[notice] Executing 1 up ' , trim ($ this ->executeCommandTester ->getDisplay (true )));
172
+ }
173
+
174
+ public function testExecuteVersionAliasException (): void
175
+ {
176
+ $ this ->executeCommandTester ->setInputs (['yes ' ]);
177
+
178
+ $ this ->migrator
179
+ ->expects (self ::never ())
180
+ ->method ('migrate ' )
181
+ ->willReturnCallback (static function (MigrationPlanList $ planList , MigratorConfiguration $ configuration ): array {
182
+ self ::assertFalse ($ configuration ->isDryRun ());
183
+
184
+ return ['A ' ];
185
+ });
186
+
187
+ $ exception = new UnknownMigrationVersion ('not_a_valid_version_alias ' );
188
+
189
+ $ this ->versionAliasResolver
190
+ ->expects (self ::once ())
191
+ ->method ('resolveVersionAlias ' )
192
+ ->with ('not_a_valid_version_alias ' )
193
+ ->willThrowException ($ exception );
194
+
195
+ self ::expectExceptionObject ($ exception );
196
+
197
+ $ this ->executeCommandTester ->execute ([
198
+ 'versions ' => ['not_a_valid_version_alias ' ],
199
+ '--down ' => true ,
200
+ ]);
201
+ }
202
+
128
203
public function testExecuteMultiple (): void
129
204
{
130
205
$ migration = $ this ->createMock (AbstractMigration::class);
@@ -148,6 +223,12 @@ public function testExecuteMultiple(): void
148
223
return ['A ' ];
149
224
});
150
225
226
+ $ this ->versionAliasResolver
227
+ ->expects (self ::exactly (2 ))
228
+ ->method ('resolveVersionAlias ' )
229
+ ->withConsecutive (['1 ' ], ['2 ' ])
230
+ ->willReturnOnConsecutiveCalls (new Version ('1 ' ), new Version ('2 ' ));
231
+
151
232
$ this ->executeCommandTester ->execute ([
152
233
'versions ' => ['1 ' , '2 ' ],
153
234
'--down ' => true ,
@@ -174,6 +255,12 @@ public function testExecuteCancel(): void
174
255
return ['A ' ];
175
256
});
176
257
258
+ $ this ->versionAliasResolver
259
+ ->expects (self ::once ())
260
+ ->method ('resolveVersionAlias ' )
261
+ ->with ('1 ' )
262
+ ->willReturn (new Version ('1 ' ));
263
+
177
264
$ this ->executeCommandTester ->execute ([
178
265
'versions ' => ['1 ' ],
179
266
'--down ' => true ,
@@ -186,9 +273,10 @@ protected function setUp(): void
186
273
{
187
274
$ connection = $ this ->getSqliteConnection ();
188
275
189
- $ this ->migrator = $ this ->createMock (Migrator::class);
190
- $ this ->queryWriter = $ this ->createMock (QueryWriter::class);
191
- $ migration = $ this ->createMock (AbstractMigration::class);
276
+ $ this ->migrator = $ this ->createMock (Migrator::class);
277
+ $ this ->queryWriter = $ this ->createMock (QueryWriter::class);
278
+ $ this ->versionAliasResolver = $ this ->createMock (AliasResolver::class);
279
+ $ migration = $ this ->createMock (AbstractMigration::class);
192
280
193
281
$ p1 = new MigrationPlan (new Version ('1 ' ), $ migration , Direction::UP );
194
282
$ pl = new MigrationPlanList ([$ p1 ], Direction::UP );
@@ -207,6 +295,7 @@ protected function setUp(): void
207
295
$ this ->dependencyFactory ->setService (Migrator::class, $ this ->migrator );
208
296
$ this ->dependencyFactory ->setService (MigrationPlanCalculator::class, $ this ->planCalculator );
209
297
$ this ->dependencyFactory ->setService (QueryWriter::class, $ this ->queryWriter );
298
+ $ this ->dependencyFactory ->setService (AliasResolver::class, $ this ->versionAliasResolver );
210
299
211
300
$ this ->executeCommand = new ExecuteCommand ($ this ->dependencyFactory );
212
301
0 commit comments