Skip to content

Commit

Permalink
Merge pull request #2064 from cakephp/revert-2057-improve-create-tests
Browse files Browse the repository at this point in the history
Revert "improve tests for create command"
  • Loading branch information
dereuromark authored Jan 21, 2022
2 parents b8df491 + f3ee4ae commit ad056cf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 173 deletions.
16 changes: 16 additions & 0 deletions src/Phinx/Console/Command/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ protected function getMigrationPath(InputInterface $input, OutputInterface $outp
}

$paths = $this->getConfig()->getMigrationPaths();

// No paths? That's a problem.
if (empty($paths)) {
throw new Exception('No migration paths set in your Phinx configuration file.');
}

$paths = Util::globAll($paths);

if (empty($paths)) {
Expand Down Expand Up @@ -137,6 +143,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
// get the migration path from the config
$path = $this->getMigrationPath($input, $output);

if (!file_exists($path)) {
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = $this->getCreateMigrationDirectoryQuestion();

if ($helper->ask($input, $output, $question)) {
mkdir($path, 0755, true);
}
}

$this->verifyMigrationDirectory($path);

$config = $this->getConfig();
Expand Down
173 changes: 0 additions & 173 deletions tests/Phinx/Console/Command/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,177 +441,4 @@ public function testSimpleTemplateGeneratorsIsCorrectlyPopulated(array $config,
$expectedMigration = "useClassName Phinx\\Migration\\AbstractMigration / className {$commandLine['name']} / version {$match['Version']} / baseClassName AbstractMigration";
$this->assertStringEqualsFile($match['MigrationFilename'], $expectedMigration, 'Failed to create migration file from template generator correctly.');
}

public function testNoMigrationsPathConfig()
{
$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

$this->config->offsetSet('paths', []);
$command->setConfig($this->config);
$command->setManager($managerStub);

$commandTester = new CommandTester($command);

$this->expectException(Exception::class);
$this->expectExceptionMessage('Migrations path missing from config file');

$commandTester->execute(['command' => $command->getName()]);
}

public function testMultipleMigrationsPaths()
{
$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

@mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'othermigrations', 0777, true);
$this->config->offsetSet('paths', [
'migrations' => [
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'migrations',
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'othermigrations',
],
]);
$command->setConfig($this->config);
$command->setManager($managerStub);

$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName()]);

$files = array_diff(scandir($this->config->getMigrationPaths()[0]), ['.', '..']);
$this->assertCount(1, $files);
$fileName = current($files);
$this->assertMatchesRegularExpression('/^[0-9]{14}.php/', $fileName);
$date = substr($fileName, 0, 14);
$this->assertFileExists($this->config->getMigrationPaths()[0]);
$prefix = "<?php\ndeclare(strict_types=1);\n\nuse Phinx\\Migration\\AbstractMigration;\n\nfinal class V{$date} extends AbstractMigration\n";
$this->assertStringStartsWith($prefix, file_get_contents($this->config->getMigrationPaths()[0] . DIRECTORY_SEPARATOR . $fileName));
}

public function testNoMigrationsPath()
{
TestUtils::recursiveRmdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'migrations');

$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

$command->setConfig($this->config);
$command->setManager($managerStub);

$commandTester = new CommandTester($command);

$this->expectException(Exception::class);
$this->expectExceptionMessage('You probably used curly braces to define migration path in your Phinx configuration file, ');

$commandTester->execute(['command' => $command->getName()]);
}

public function testInvalidClassName(): void
{
$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

$command->setConfig($this->config);
$command->setManager($managerStub);

$commandTester = new CommandTester($command);

$this->expectException(Exception::class);
$this->expectExceptionMessage('The migration class name "Th1sI5ÄnInvalüdClaßnam€" is invalid. Please use CamelCase format.');

$commandTester->execute([
'command' => $command->getName(),
'name' => 'Th1sI5ÄnInvalüdClaßnam€',
]);
}

public function testCreateWithPath(): void
{
$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

$command->setConfig($this->config);
$command->setManager($managerStub);

$path = $this->config->getMigrationPaths()[0];

$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(), '--path' => $path]);

$files = array_diff(scandir($path), ['.', '..']);
$this->assertCount(1, $files);
$fileName = current($files);
$this->assertMatchesRegularExpression('/^[0-9]{14}.php/', $fileName);
$date = substr($fileName, 0, 14);
$this->assertFileExists($this->config->getMigrationPaths()[0]);
$prefix = "<?php\ndeclare(strict_types=1);\n\nuse Phinx\\Migration\\AbstractMigration;\n\nfinal class V{$date} extends AbstractMigration\n";
$this->assertStringStartsWith($prefix, file_get_contents($this->config->getMigrationPaths()[0] . DIRECTORY_SEPARATOR . $fileName));
}

public function testNoAltTemplate(): void
{
$application = new PhinxApplication();
$application->add(new Create());

/** @var Create $command */
$command = $application->find('create');

/** @var Manager $managerStub mock the manager class */
$managerStub = $this->getMockBuilder('\Phinx\Migration\Manager')
->setConstructorArgs([$this->config, $this->input, $this->output])
->getMock();

$this->config->offsetSet('templates', [
'file' => 'MyTemplate',
]);

$command->setConfig($this->config);
$command->setManager($managerStub);

$commandTester = new CommandTester($command);

$this->expectException(Exception::class);
$this->expectExceptionMessage('The alternative template file "MyTemplate" does not exist');

$commandTester->execute(['command' => $command->getName()]);
}
}

0 comments on commit ad056cf

Please sign in to comment.