Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ abstract class AbstractCommand extends Command
*/
protected function configure(): void
{
$this->addOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load');
$this->addOption('--parser', '-p', InputOption::VALUE_REQUIRED, 'Parser used to read the config file. Defaults to YAML');
$this->addOption('--no-info', null, InputOption::VALUE_NONE, 'Hides all debug information');
}
Expand Down
25 changes: 20 additions & 5 deletions src/Phinx/Console/Command/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
protected function resolvePath(InputInterface $input, string $format): string
{
// get the migration path from the config
$path = (string)$input->getArgument('path');

if (!in_array($format, static::$supportedFormats, true)) {
throw new InvalidArgumentException(sprintf(
'Invalid format "%s". Format must be either ' . implode(', ', static::$supportedFormats) . '.',
$format,
));
}

// Fallback
// We either get the path to where to create the config path by:
// 1. The path argument if set
// 2. The configuration option if set
// 3. Fallback to a default path of the current directory
$path = (string)$input->getArgument('path');

if (!$path) {
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
$path = $input->hasOption('configuration') ? (string)$input->getOption('configuration') : null;
if ($path) {
$path = (string)$input->getOption('configuration');
if (DIRECTORY_SEPARATOR === '/') {
$isAbsolute = ($path[0] === '/');
} else {
$isAbsolute = (preg_match('/^[a-zA-Z]:\\\\/', $path) === 1 || $path[0] === '\\');
}
if (!$isAbsolute) {
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
}
} else {
$path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
}
}

// Adding file name if necessary
Expand Down
15 changes: 15 additions & 0 deletions src/Phinx/Console/PhinxApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use Phinx\Console\Command\Status;
use Phinx\Console\Command\Test;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -54,6 +56,19 @@ public function __construct()
]);
}

/**
* Setup default input definition.
*
* @return \Symfony\Component\Console\Input\InputDefinition the overridden input definition.
*/
protected function getDefaultInputDefinition(): InputDefinition
{
$definition = parent::getDefaultInputDefinition();
$definition->addOption(new InputOption('--configuration', '-c', InputOption::VALUE_REQUIRED, 'The configuration file to load'));

return $definition;
}

/**
* Runs the current application.
*
Expand Down
52 changes: 48 additions & 4 deletions tests/Phinx/Console/Command/InitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,53 @@ public function testCustomNameConfigIsWritten($format)
$this->writeConfig(uniqid() . $format);
}

public function configurationOptionDataProvider(): array
{
return [
['phinx.php'],
[sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php'],
];
}

/**
* @dataProvider configurationOptionDataProvider
*/
public function testConfigurationOption($configPath): void
{
$currentDir = getcwd();
$expectedPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'phinx.php';
try {
chdir(sys_get_temp_dir());
$application = new PhinxApplication();
$application->add(new Init());
$command = $application->find('init');
$commandTester = new CommandTester($command);

$command = [
'--configuration' => $configPath,
'command' => $command->getName(),
];

$exitCode = $commandTester->execute($command, ['decorated' => false]);
$this->assertEquals(AbstractCommand::CODE_SUCCESS, $exitCode);

$this->assertStringContainsString(
"created $expectedPath",
$commandTester->getDisplay(),
);

$this->assertFileExists(
$expectedPath,
'Phinx configuration not existent',
);
} finally {
chdir($currentDir);
}
}

public function testDefaults()
{
$current_dir = getcwd();
$currentDir = getcwd();

try {
chdir(sys_get_temp_dir());
Expand All @@ -107,13 +151,13 @@ public function testDefaults()
'Phinx configuration not existent',
);
} finally {
chdir($current_dir);
chdir($currentDir);
}
}

public function testYamlFormat()
{
$current_dir = getcwd();
$currentDir = getcwd();

try {
chdir(sys_get_temp_dir());
Expand All @@ -136,7 +180,7 @@ public function testYamlFormat()
'Phinx configuration not existent',
);
} finally {
chdir($current_dir);
chdir($currentDir);
}
}

Expand Down
Loading