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
2 changes: 1 addition & 1 deletion src/Command/ChangeLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function ($item) use ($major) {
$v = substr($milestone, 0, (int)strpos($milestone, '.'));

return $v == $major;
}
},
);
}

Expand Down
30 changes: 30 additions & 0 deletions src/Command/ResourcesMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Migrations\Command\BakeSimpleMigrationCommand;

/**
Expand All @@ -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.
*
Expand Down
39 changes: 33 additions & 6 deletions tests/TestCase/Command/ResourcesMigrationCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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');
Expand All @@ -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);

Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
'url' => getenv('db_dsn'),
'timezone' => 'UTC',
]);
ConnectionManager::alias('test', 'default');

$app = new Application(dirname(__DIR__) . '/config');
$app->bootstrap();
Expand Down