Skip to content

Commit 7780f8d

Browse files
authored
Merge pull request #1414 from Myks92/feat-asked-namespace-when-more-one
Ask for a namespace of when more one path
2 parents 95d3937 + c7474af commit 7780f8d

File tree

8 files changed

+345
-69
lines changed

8 files changed

+345
-69
lines changed

src/Tools/Console/Command/DiffCommand.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
1010
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
1111
use Doctrine\SqlFormatter\SqlFormatter;
12-
use OutOfBoundsException;
1312
use Symfony\Component\Console\Attribute\AsCommand;
1413
use Symfony\Component\Console\Input\InputInterface;
1514
use Symfony\Component\Console\Input\InputOption;
1615
use Symfony\Component\Console\Output\OutputInterface;
1716

1817
use function addslashes;
19-
use function assert;
2018
use function class_exists;
2119
use function count;
2220
use function filter_var;
23-
use function is_string;
24-
use function key;
2521
use function sprintf;
2622

2723
use const FILTER_VALIDATE_BOOLEAN;
@@ -110,10 +106,6 @@ protected function execute(
110106
$allowEmptyDiff = $input->getOption('allow-empty-diff');
111107
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
112108
$fromEmptySchema = $input->getOption('from-empty-schema');
113-
$namespace = $input->getOption('namespace');
114-
if ($namespace === '') {
115-
$namespace = null;
116-
}
117109

118110
if ($formatted) {
119111
if (! class_exists(SqlFormatter::class)) {
@@ -123,16 +115,7 @@ protected function execute(
123115
}
124116
}
125117

126-
$configuration = $this->getDependencyFactory()->getConfiguration();
127-
128-
$dirs = $configuration->getMigrationDirectories();
129-
if ($namespace === null) {
130-
$namespace = key($dirs);
131-
} elseif (! isset($dirs[$namespace])) {
132-
throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
133-
}
134-
135-
assert(is_string($namespace));
118+
$namespace = $this->getNamespace($input, $output);
136119

137120
$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
138121
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();

src/Tools/Console/Command/DoctrineCommand.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@
1010
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
1111
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
1212
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
13+
use Exception;
1314
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
1718
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Question\ChoiceQuestion;
1820
use Symfony\Component\Console\Style\StyleInterface;
1921
use Symfony\Component\Console\Style\SymfonyStyle;
2022

23+
use function array_keys;
2124
use function assert;
25+
use function count;
2226
use function is_string;
27+
use function key;
28+
use function sprintf;
2329

2430
/**
2531
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
@@ -138,4 +144,36 @@ private function setNamedEmOrConnection(InputInterface $input): void
138144
return;
139145
}
140146
}
147+
148+
final protected function getNamespace(InputInterface $input, OutputInterface $output): string
149+
{
150+
$configuration = $this->getDependencyFactory()->getConfiguration();
151+
152+
$namespace = $input->getOption('namespace');
153+
if ($namespace === '') {
154+
$namespace = null;
155+
}
156+
157+
$dirs = $configuration->getMigrationDirectories();
158+
if ($namespace === null && count($dirs) === 1) {
159+
$namespace = key($dirs);
160+
} elseif ($namespace === null && count($dirs) > 1) {
161+
$helper = $this->getHelper('question');
162+
$question = new ChoiceQuestion(
163+
'Please choose a namespace (defaults to the first one)',
164+
array_keys($dirs),
165+
0,
166+
);
167+
$namespace = $helper->ask($input, $output, $question);
168+
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
169+
}
170+
171+
if (! isset($dirs[$namespace])) {
172+
throw new Exception(sprintf('Path not defined for the namespace "%s"', $namespace));
173+
}
174+
175+
assert(is_string($namespace));
176+
177+
return $namespace;
178+
}
141179
}

src/Tools/Console/Command/DumpSchemaCommand.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
use Symfony\Component\Console\Output\OutputInterface;
1414

1515
use function addslashes;
16-
use function assert;
1716
use function class_exists;
18-
use function is_string;
19-
use function key;
2017
use function sprintf;
2118
use function str_contains;
2219

@@ -91,15 +88,7 @@ public function execute(
9188
}
9289
}
9390

94-
$configuration = $this->getDependencyFactory()->getConfiguration();
95-
96-
$namespace = $input->getOption('namespace');
97-
if ($namespace === null) {
98-
$dirs = $configuration->getMigrationDirectories();
99-
$namespace = key($dirs);
100-
}
101-
102-
assert(is_string($namespace));
91+
$namespace = $this->getNamespace($input, $output);
10392

10493
$this->checkNoPreviousDumpExistsForNamespace($namespace);
10594

src/Tools/Console/Command/GenerateCommand.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
namespace Doctrine\Migrations\Tools\Console\Command;
66

7-
use Exception;
87
use Symfony\Component\Console\Attribute\AsCommand;
98
use Symfony\Component\Console\Input\InputInterface;
109
use Symfony\Component\Console\Input\InputOption;
1110
use Symfony\Component\Console\Output\OutputInterface;
1211

13-
use function assert;
14-
use function is_string;
15-
use function key;
1612
use function sprintf;
1713

1814
/**
@@ -47,23 +43,9 @@ protected function configure(): void
4743

4844
protected function execute(InputInterface $input, OutputInterface $output): int
4945
{
50-
$configuration = $this->getDependencyFactory()->getConfiguration();
51-
5246
$migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();
5347

54-
$namespace = $input->getOption('namespace');
55-
if ($namespace === '') {
56-
$namespace = null;
57-
}
58-
59-
$dirs = $configuration->getMigrationDirectories();
60-
if ($namespace === null) {
61-
$namespace = key($dirs);
62-
} elseif (! isset($dirs[$namespace])) {
63-
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
64-
}
65-
66-
assert(is_string($namespace));
48+
$namespace = $this->getNamespace($input, $output);
6749

6850
$fqcn = $this->getDependencyFactory()->getClassNameGenerator()->generateClassName($namespace);
6951

tests/Tools/Console/Command/DiffCommandTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
use Doctrine\Migrations\Version\Version;
1919
use PHPUnit\Framework\MockObject\MockObject;
2020
use PHPUnit\Framework\TestCase;
21+
use Symfony\Component\Console\Helper\HelperSet;
22+
use Symfony\Component\Console\Helper\QuestionHelper;
2123
use Symfony\Component\Console\Tester\CommandTester;
2224

2325
use function array_map;
2426
use function explode;
27+
use function sprintf;
2528
use function sys_get_temp_dir;
2629
use function trim;
2730

@@ -139,6 +142,44 @@ public function testExecutedUnavailableMigrationsCancel(): void
139142
self::assertSame(3, $statusCode);
140143
}
141144

145+
/** @return array<string, array{int|null, string}> */
146+
public static function getSelectedNamespace(): array
147+
{
148+
return [
149+
'no' => [null, 'FooNs'],
150+
'first' => [0, 'FooNs'],
151+
'two' => [1, 'FooNs2'],
152+
];
153+
}
154+
155+
/** @dataProvider getSelectedNamespace */
156+
public function testExecuteWithMultipleDirectories(int|null $input, string $namespace): void
157+
{
158+
$this->migrationStatusCalculator
159+
->method('getNewMigrations')
160+
->willReturn(new AvailableMigrationsList([]));
161+
162+
$this->migrationStatusCalculator
163+
->method('getExecutedUnavailableMigrations')
164+
->willReturn(new ExecutedMigrationsList([]));
165+
166+
$this->configuration->addMigrationsDirectory('FooNs2', sys_get_temp_dir());
167+
168+
$this->diffCommand->setHelperSet(new HelperSet(['question' => new QuestionHelper()]));
169+
170+
$this->migrationDiffGenerator->expects(self::once())->method('generate');
171+
172+
$this->diffCommandTester->setInputs([$input]);
173+
$this->diffCommandTester->execute([]);
174+
175+
$output = $this->diffCommandTester->getDisplay(true);
176+
177+
self::assertStringContainsString('Please choose a namespace (defaults to the first one)', $output);
178+
self::assertStringContainsString('[0] FooNs', $output);
179+
self::assertStringContainsString('[1] FooNs2', $output);
180+
self::assertStringContainsString(sprintf('You have selected the "%s" namespace', $namespace), $output);
181+
}
182+
142183
protected function setUp(): void
143184
{
144185
$this->migrationDiffGenerator = $this->createMock(DiffGenerator::class);

0 commit comments

Comments
 (0)