Skip to content

Commit

Permalink
Merge pull request #1414 from Myks92/feat-asked-namespace-when-more-one
Browse files Browse the repository at this point in the history
Ask for a namespace of when more one path
  • Loading branch information
greg0ire authored Apr 24, 2024
2 parents 95d3937 + c7474af commit 7780f8d
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 69 deletions.
19 changes: 1 addition & 18 deletions src/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Doctrine\SqlFormatter\SqlFormatter;
use OutOfBoundsException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function addslashes;
use function assert;
use function class_exists;
use function count;
use function filter_var;
use function is_string;
use function key;
use function sprintf;

use const FILTER_VALIDATE_BOOLEAN;
Expand Down Expand Up @@ -110,10 +106,6 @@ protected function execute(
$allowEmptyDiff = $input->getOption('allow-empty-diff');
$checkDbPlatform = filter_var($input->getOption('check-database-platform'), FILTER_VALIDATE_BOOLEAN);
$fromEmptySchema = $input->getOption('from-empty-schema');
$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

if ($formatted) {
if (! class_exists(SqlFormatter::class)) {
Expand All @@ -123,16 +115,7 @@ protected function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null) {
$namespace = key($dirs);
} elseif (! isset($dirs[$namespace])) {
throw new OutOfBoundsException(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
$executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations();
Expand Down
38 changes: 38 additions & 0 deletions src/Tools/Console/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function array_keys;
use function assert;
use function count;
use function is_string;
use function key;
use function sprintf;

/**
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
Expand Down Expand Up @@ -138,4 +144,36 @@ private function setNamedEmOrConnection(InputInterface $input): void
return;
}
}

final protected function getNamespace(InputInterface $input, OutputInterface $output): string
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null && count($dirs) === 1) {
$namespace = key($dirs);
} elseif ($namespace === null && count($dirs) > 1) {
$helper = $this->getHelper('question');
$question = new ChoiceQuestion(
'Please choose a namespace (defaults to the first one)',
array_keys($dirs),
0,
);
$namespace = $helper->ask($input, $output, $question);
$this->io->text(sprintf('You have selected the "%s" namespace', $namespace));
}

if (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace "%s"', $namespace));
}

assert(is_string($namespace));

return $namespace;
}
}
13 changes: 1 addition & 12 deletions src/Tools/Console/Command/DumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
use Symfony\Component\Console\Output\OutputInterface;

use function addslashes;
use function assert;
use function class_exists;
use function is_string;
use function key;
use function sprintf;
use function str_contains;

Expand Down Expand Up @@ -91,15 +88,7 @@ public function execute(
}
}

$configuration = $this->getDependencyFactory()->getConfiguration();

$namespace = $input->getOption('namespace');
if ($namespace === null) {
$dirs = $configuration->getMigrationDirectories();
$namespace = key($dirs);
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

$this->checkNoPreviousDumpExistsForNamespace($namespace);

Expand Down
20 changes: 1 addition & 19 deletions src/Tools/Console/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace Doctrine\Migrations\Tools\Console\Command;

use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use function assert;
use function is_string;
use function key;
use function sprintf;

/**
Expand Down Expand Up @@ -47,23 +43,9 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$configuration = $this->getDependencyFactory()->getConfiguration();

$migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator();

$namespace = $input->getOption('namespace');
if ($namespace === '') {
$namespace = null;
}

$dirs = $configuration->getMigrationDirectories();
if ($namespace === null) {
$namespace = key($dirs);
} elseif (! isset($dirs[$namespace])) {
throw new Exception(sprintf('Path not defined for the namespace %s', $namespace));
}

assert(is_string($namespace));
$namespace = $this->getNamespace($input, $output);

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

Expand Down
41 changes: 41 additions & 0 deletions tests/Tools/Console/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
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;

Expand Down Expand Up @@ -139,6 +142,44 @@ public function testExecutedUnavailableMigrationsCancel(): void
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);
Expand Down
Loading

0 comments on commit 7780f8d

Please sign in to comment.