diff --git a/src/Command/ChangeLogCommand.php b/src/Command/ChangeLogCommand.php index 87eea1b..785429b 100644 --- a/src/Command/ChangeLogCommand.php +++ b/src/Command/ChangeLogCommand.php @@ -139,7 +139,7 @@ function ($item) use ($major) { $v = substr($milestone, 0, (int)strpos($milestone, '.')); return $v == $major; - } + }, ); } diff --git a/src/Command/ResourcesMigrationCommand.php b/src/Command/ResourcesMigrationCommand.php index 5907aaf..27f7327 100644 --- a/src/Command/ResourcesMigrationCommand.php +++ b/src/Command/ResourcesMigrationCommand.php @@ -16,6 +16,7 @@ use Cake\Console\Arguments; use Cake\Console\ConsoleIo; +use Cake\Console\ConsoleOptionParser; use Migrations\Command\BakeSimpleMigrationCommand; /** @@ -25,6 +26,35 @@ */ class ResourcesMigrationCommand extends BakeSimpleMigrationCommand { + /** + * @inheritDoc + */ + public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser + { + $parser->addArgument('name', [ + 'help' => 'Name of the migration', + 'required' => true, + ]) + ->addOption('force', [ + 'short' => 'f', + 'boolean' => true, + 'default' => 'false', + 'help' => 'Force overwriting existing files without prompting.', + ]) + ->addOption('connection', [ + 'short' => 'c', + 'default' => 'default', + 'help' => 'The datasource connection to get data from.', + ]) + ->addOption('source', [ + 'short' => 's', + 'default' => 'Migrations', + 'help' => 'The migrations folder.', + ]); + + return $parser; + } + /** * Main migration file name. * diff --git a/tests/TestCase/Command/ResourcesMigrationCommandTest.php b/tests/TestCase/Command/ResourcesMigrationCommandTest.php index 564b35a..98fb625 100644 --- a/tests/TestCase/Command/ResourcesMigrationCommandTest.php +++ b/tests/TestCase/Command/ResourcesMigrationCommandTest.php @@ -15,8 +15,10 @@ namespace BEdita\DevTools\Test\TestCase\Shell\Task; use BEdita\DevTools\Command\ResourcesMigrationCommand; +use Cake\Console\Arguments; use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; use Cake\Core\Plugin; +use Cake\Routing\Router; use Cake\TestSuite\StringCompareTrait; use Cake\TestSuite\TestCase; @@ -43,7 +45,9 @@ class ResourcesMigrationCommandTest extends TestCase public function setUp(): void { parent::setUp(); - + Router::reload(); + $this->loadPlugins(['Bake']); + $this->setAppNamespace('BEdita\DevTools\Test\TestApp'); $this->_compareBasePath = Plugin::path('BEdita/DevTools') . 'tests' . DS . 'comparisons' . DS . 'Migrations' . DS; } @@ -81,7 +85,14 @@ public function testName(): void */ public function testFileName(): void { - $command = new ResourcesMigrationCommand(); + $command = new class () extends ResourcesMigrationCommand + { + public function setArgs(Arguments $args): void + { + $this->args = $args; + } + }; + $command->setArgs(new Arguments([['MyMigration']], [], [])); $expected = $command->fileName('MyMigration'); sleep(2); $actual = $command->fileName('MyMigration'); @@ -107,17 +118,19 @@ public function testTemplate(): void * * @return void * @covers ::bake() + * @covers ::buildOptionParser() */ public function testBake(): void { $this->exec('bake resources_migration MyMigration'); $this->assertExitCode(ResourcesMigrationCommand::CODE_SUCCESS); + $basePath = CONFIG . ResourcesMigrationCommand::DEFAULT_MIGRATION_FOLDER . DS; - $file = glob(CONFIG . ResourcesMigrationCommand::DEFAULT_MIGRATION_FOLDER . DS . '*_MyMigration.php'); + $file = glob($basePath . '*_MyMigration.php'); // @phpstan-ignore-next-line $phpFile = current($file); - $file = glob(CONFIG . ResourcesMigrationCommand::DEFAULT_MIGRATION_FOLDER . DS . '*_MyMigration.yml'); + $file = glob($basePath . '*_MyMigration.yml'); // @phpstan-ignore-next-line $yamlFile = current($file); @@ -127,7 +140,21 @@ public function testBake(): void $this->createdFiles[] = $phpFile; $this->createdFiles[] = $yamlFile; - $this->assertSameAsFile('testMyMigration.php', (string)$phpResult); - $this->assertSameAsFile('testMyMigration.yml', (string)$yamlResult); + self::assertSameMigration((string)$phpResult, (string)file_get_contents($this->_compareBasePath . 'testMyMigration.php')); + self::assertSameMigration((string)$yamlResult, (string)file_get_contents($this->_compareBasePath . 'testMyMigration.yml')); + } + + /** + * Assert that two migration files are the same. + * + * @param string $actual The actual migration + * @param string $expected The expected migration + * @return void + */ + private static function assertSameMigration(string $actual, string $expected): void + { + $actual = trim((string)preg_replace('/\s\s+/', ' ', $actual)); + $expected = trim((string)preg_replace('/\s\s+/', ' ', $expected)); + static::assertEquals($actual, $expected); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6b77ca5..609f2ef 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -84,6 +84,7 @@ 'url' => getenv('db_dsn'), 'timezone' => 'UTC', ]); +ConnectionManager::alias('test', 'default'); $app = new Application(dirname(__DIR__) . '/config'); $app->bootstrap();