Skip to content

Commit

Permalink
Show more help text after generating a migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed May 12, 2018
1 parent ead7322 commit 8f9cd09
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
9 changes: 2 additions & 7 deletions lib/Doctrine/Migrations/MigrationDiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Provider\SchemaProviderInterface;
use RuntimeException;
use function preg_match;
Expand All @@ -17,9 +16,6 @@

class MigrationDiffGenerator
{
/** @var Configuration */
private $configuration;

/** @var DBALConfiguration */
private $dbalConfiguration;

Expand All @@ -39,15 +35,13 @@ class MigrationDiffGenerator
private $migrationSqlGenerator;

public function __construct(
Configuration $configuration,
DBALConfiguration $dbalConfiguration,
AbstractSchemaManager $schemaManager,
SchemaProviderInterface $schemaProvider,
AbstractPlatform $platform,
MigrationGenerator $migrationGenerator,
MigrationSqlGenerator $migrationSqlGenerator
) {
$this->configuration = $configuration;
$this->dbalConfiguration = $dbalConfiguration;
$this->schemaManager = $schemaManager;
$this->schemaProvider = $schemaProvider;
Expand All @@ -57,6 +51,7 @@ public function __construct(
}

public function generate(
string $versionNumber,
?string $filterExpression,
bool $formatted = false,
int $lineLength = 120
Expand Down Expand Up @@ -86,7 +81,7 @@ public function generate(
}

return $this->migrationGenerator->generateMigration(
$this->configuration->generateVersionNumber(),
$versionNumber,
$up,
$down
);
Expand Down
14 changes: 13 additions & 1 deletion lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public function execute(
}
}

$versionNumber = $this->configuration->generateVersionNumber();

$path = $this->createMigrationDiffGenerator()->generate(
$versionNumber,
$filterExpression,
$formatted,
$lineLength
Expand All @@ -102,12 +105,21 @@ public function execute(
}

$output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));

$output->writeln(sprintf(
"\nTo run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>",
$versionNumber
));

$output->writeln(sprintf(
"\nTo revert the migration you can use <info>migrations:execute --down %s</info>",
$versionNumber
));
}

protected function createMigrationDiffGenerator() : MigrationDiffGenerator
{
return new MigrationDiffGenerator(
$this->configuration,
$this->connection->getConfiguration(),
$this->connection->getSchemaManager(),
$this->getSchemaProvider(),
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/Migrations/Tools/Console/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function execute(InputInterface $input, OutputInterface $output) : void
}

$output->writeln(sprintf('Generated new migration class to "<info>%s</info>"', $path));

$output->writeln(sprintf(
"\nTo run just this migration for testing purposes, you can use <info>migrations:execute --up %s</info>",
$versionNumber
));

$output->writeln(sprintf(
"\nTo revert the migration you can use <info>migrations:execute --down %s</info>",
$versionNumber
));
}

protected function procOpen(string $editorCommand, string $path) : void
Expand Down
12 changes: 1 addition & 11 deletions tests/Doctrine/Migrations/Tests/MigrationDiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\MigrationDiffGenerator;
use Doctrine\Migrations\MigrationGenerator;
use Doctrine\Migrations\MigrationSqlGenerator;
Expand All @@ -18,9 +17,6 @@

class MigrationDiffGeneratorTest extends TestCase
{
/** @var Configuration */
private $configuration;

/** @var DBALConfiguration */
private $dbalConfiguration;

Expand Down Expand Up @@ -110,21 +106,16 @@ public function testGenerate() : void
->with(['UPDATE table SET value = 1'], true, 80)
->willReturn('test2');

$this->configuration->expects($this->once())
->method('generateVersionNumber')
->willReturn('1234');

$this->migrationGenerator->expects($this->once())
->method('generateMigration')
->with('1234', 'test1', 'test2')
->willReturn('path');

self::assertEquals('path', $this->migrationDiffGenerator->generate('/table_name1/', true, 80));
self::assertEquals('path', $this->migrationDiffGenerator->generate('1234', '/table_name1/', true, 80));
}

protected function setUp() : void
{
$this->configuration = $this->createMock(Configuration::class);
$this->dbalConfiguration = $this->createMock(DBALConfiguration::class);
$this->schemaManager = $this->createMock(AbstractSchemaManager::class);
$this->schemaProvider = $this->createMock(SchemaProviderInterface::class);
Expand All @@ -134,7 +125,6 @@ protected function setUp() : void
$this->migrationDiffGenerator = $this->createMock(MigrationDiffGenerator::class);

$this->migrationDiffGenerator = new MigrationDiffGenerator(
$this->configuration,
$this->dbalConfiguration,
$this->schemaManager,
$this->schemaProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Migrations\Tests\Tools\Console\Command;

use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\MigrationDiffGenerator;
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
use PHPUnit\Framework\TestCase;
Expand All @@ -15,6 +16,9 @@ final class DiffCommandTest extends TestCase
/** @var MigrationDiffGenerator */
private $migrationDiffGenerator;

/** @var Configuration */
private $configuration;

/** @var DiffCommand */
private $diffCommand;

Expand All @@ -23,6 +27,10 @@ public function testExecute() : void
$input = $this->createMock(InputInterface::class);
$output = $this->createMock(OutputInterface::class);

$this->configuration->expects($this->once())
->method('generateVersionNumber')
->willReturn('1234');

$input->expects($this->at(0))
->method('getOption')
->with('filter-expression')
Expand All @@ -43,30 +51,45 @@ public function testExecute() : void
->with('editor-cmd')
->willReturn('mate');

$this->configuration->expects($this->once())
->method('generateVersionNumber')
->willReturn('1234');

$this->migrationDiffGenerator->expects($this->once())
->method('generate')
->with('filter expression', true, 80)
->with('1234', 'filter expression', true, 80)
->willReturn('/path/to/migration.php');

$this->diffCommand->expects($this->once())
->method('procOpen')
->with('mate', '/path/to/migration.php');

$output->expects($this->once())
$output->expects($this->at(0))
->method('writeln')
->with('Generated new migration class to "<info>/path/to/migration.php</info>"');

$output->expects($this->at(1))
->method('writeln')
->with("\nTo run just this migration for testing purposes, you can use <info>migrations:execute --up 1234</info>");

$output->expects($this->at(2))
->method('writeln')
->with("\nTo revert the migration you can use <info>migrations:execute --down 1234</info>");

$this->diffCommand->execute($input, $output);
}

protected function setUp() : void
{
$this->migrationDiffGenerator = $this->createMock(MigrationDiffGenerator::class);
$this->configuration = $this->createMock(Configuration::class);

$this->diffCommand = $this->getMockBuilder(DiffCommand::class)
->setMethods(['createMigrationDiffGenerator', 'procOpen'])
->getMock();

$this->diffCommand->setMigrationConfiguration($this->configuration);

$this->diffCommand->expects($this->once())
->method('createMigrationDiffGenerator')
->willReturn($this->migrationDiffGenerator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ public function testExecute() : void
->method('procOpen')
->with('mate', '/path/to/migration.php');

$output->expects($this->once())
$output->expects($this->at(0))
->method('writeln')
->with('Generated new migration class to "<info>/path/to/migration.php</info>"');

$output->expects($this->at(1))
->method('writeln')
->with("\nTo run just this migration for testing purposes, you can use <info>migrations:execute --up 1234</info>");

$output->expects($this->at(2))
->method('writeln')
->with("\nTo revert the migration you can use <info>migrations:execute --down 1234</info>");

$this->generateCommand->execute($input, $output);
}

Expand Down

0 comments on commit 8f9cd09

Please sign in to comment.