-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
DiffCommandTest.php
212 lines (159 loc) · 7.91 KB
/
DiffCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Tests\Tools\Console\Command;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Generator\ClassNameGenerator;
use Doctrine\Migrations\Generator\DiffGenerator;
use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
use Doctrine\Migrations\Metadata\ExecutedMigration;
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
use Doctrine\Migrations\Version\MigrationStatusCalculator;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;
use function array_map;
use function explode;
use function sprintf;
use function sys_get_temp_dir;
use function trim;
final class DiffCommandTest extends TestCase
{
/** @var DiffGenerator&MockObject */
private DiffGenerator $migrationDiffGenerator;
/** @var MigrationStatusCalculator&MockObject */
private MigrationStatusCalculator $migrationStatusCalculator;
private Configuration $configuration;
private DiffCommand $diffCommand;
/** @var DependencyFactory&MockObject */
private DependencyFactory $dependencyFactory;
private CommandTester $diffCommandTester;
/** @var ClassNameGenerator&MockObject */
private ClassNameGenerator $classNameGenerator;
public function testExecute(): void
{
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([]));
$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([]));
$this->classNameGenerator->expects(self::once())
->method('generateClassName')
->with('FooNs')
->willReturn('FooNs\\Version1234');
$this->migrationDiffGenerator->expects(self::once())
->method('generate')
->with('FooNs\\Version1234', 'filter expression', true, 80)
->willReturn('/path/to/migration.php');
$this->diffCommandTester->execute([
'--filter-expression' => 'filter expression',
'--formatted' => true,
'--line-length' => 80,
'--allow-empty-diff' => true,
'--check-database-platform' => true,
'--namespace' => 'FooNs',
]);
$output = $this->diffCommandTester->getDisplay(true);
self::assertSame([
'Generated new migration class to "/path/to/migration.php"',
'',
'To run just this migration for testing purposes, you can use migrations:execute --up \'FooNs\\\\Version1234\'',
'',
'To revert the migration you can use migrations:execute --down \'FooNs\\\\Version1234\'',
], array_map(trim(...), explode("\n", trim($output))));
}
public function testAvailableMigrationsCancel(): void
{
$m1 = new AvailableMigration(new Version('A'), self::createStub(AbstractMigration::class));
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([$m1]));
$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([]));
$this->diffCommandTester->setInputs(['no']);
$this->migrationDiffGenerator->expects(self::never())->method('generate');
$statusCode = $this->diffCommandTester->execute([]);
$output = $this->diffCommandTester->getDisplay(true);
self::assertStringContainsString('[WARNING] You have 1 available migrations to execute.', $output);
self::assertStringContainsString('[ERROR] Migration cancelled!', $output);
self::assertSame(3, $statusCode);
}
public function testExecutedUnavailableMigrationsCancel(): void
{
$e1 = new ExecutedMigration(new Version('B'));
$m1 = new AvailableMigration(new Version('A'), self::createStub(AbstractMigration::class));
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([$m1]));
$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([$e1]));
$this->diffCommandTester->setInputs(['no']);
$this->migrationDiffGenerator->expects(self::never())->method('generate');
$statusCode = $this->diffCommandTester->execute([]);
$output = $this->diffCommandTester->getDisplay(true);
self::assertStringContainsString('[WARNING] You have 1 available migrations to execute.', $output);
self::assertStringContainsString('[WARNING] You have 1 previously executed migrations in the database that are not registered migrations.', $output);
self::assertStringContainsString('[ERROR] Migration cancelled!', $output);
self::assertSame(3, $statusCode);
}
/** @return array<string, array{int|null, string}> */
public static function getSelectedNamespace(): array
{
return [
'no' => [null, 'FooNs'],
'first' => [0, 'FooNs'],
'two' => [1, 'FooNs2'],
];
}
/** @dataProvider getSelectedNamespace */
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
{
$this->migrationStatusCalculator
->method('getNewMigrations')
->willReturn(new AvailableMigrationsList([]));
$this->migrationStatusCalculator
->method('getExecutedUnavailableMigrations')
->willReturn(new ExecutedMigrationsList([]));
$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());
$this->diffCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));
$this->migrationDiffGenerator->expects(self::once())->method('generate');
$this->diffCommandTester->setInputs([$input]);
$this->diffCommandTester->execute([]);
$output = $this->diffCommandTester->getDisplay(true);
self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
self::assertStringContainsString('[0] FooNs', $output);
self::assertStringContainsString('[1] FooNs2', $output);
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
}
protected function setUp(): void
{
$this->migrationDiffGenerator = $this->createMock(DiffGenerator::class);
$this->migrationStatusCalculator = $this->createMock(MigrationStatusCalculator::class);
$this->configuration = new Configuration();
$this->configuration->addMigrationsDirectory('FooNs', sys_get_temp_dir());
$this->dependencyFactory = $this->createMock(DependencyFactory::class);
$this->classNameGenerator = $this->createMock(ClassNameGenerator::class);
$this->dependencyFactory
->method('getClassNameGenerator')
->willReturn($this->classNameGenerator);
$this->dependencyFactory
->method('getConfiguration')
->willReturn($this->configuration);
$this->dependencyFactory->expects(self::any())
->method('getDiffGenerator')
->willReturn($this->migrationDiffGenerator);
$this->dependencyFactory->method('getMigrationStatusCalculator')
->willReturn($this->migrationStatusCalculator);
$this->diffCommand = new DiffCommand($this->dependencyFactory);
$this->diffCommandTester = new CommandTester($this->diffCommand);
}
}