Skip to content

Commit

Permalink
mark console commands as final classes
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Mar 12, 2020
1 parent 05628ad commit 19780b9
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 169 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ocramius/package-versions": "^1.3",
"ocramius/proxy-manager": "^2.0.2",
"symfony/console": "^3.4||^4.0||^5.0",
"symfony/process": "^3.4||^4.0||^5.0",
"symfony/stopwatch": "^3.4||^4.0||^5.0"
},
"require-dev": {
Expand All @@ -30,7 +31,6 @@
"phpstan/phpstan-strict-rules": "^0.11",
"phpstan/phpstan-symfony": "^0.11.6",
"phpunit/phpunit": "^8.3",
"symfony/process": "^3.4||^4.0||^5.0",
"symfony/yaml": "^3.4||^4.0||^5.0"
},
"suggest": {
Expand Down
107 changes: 56 additions & 51 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* The DiffCommand class is responsible for generating a migration by comparing your current database schema to
* your mapping information.
*/
class DiffCommand extends DoctrineCommand
final class DiffCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:diff';
Expand Down Expand Up @@ -149,7 +149,7 @@ public function execute(

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
$this->procOpen($output, $editorCommand, $path);
}

$output->writeln([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use function escapeshellarg;
use Symfony\Component\Process\Process;
use function is_string;
use function proc_open;

/**
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
Expand Down Expand Up @@ -105,8 +104,12 @@ protected function canExecute(
return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output);
}

protected function procOpen(string $editorCommand, string $path) : void
protected function procOpen(OutputInterface $output, string $editorCommand, string $path) : void
{
proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes);
$process = new Process([$editorCommand, $path]);
$process->setTty(true);
$this->getHelper('process')->mustRun($output, $process, null, static function ($type, $buffer) : void {
echo $buffer;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @see Doctrine\Migrations\Tools\Console\Command\RollupCommand
*/
class DumpSchemaCommand extends DoctrineCommand
final class DumpSchemaCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:dump-schema';
Expand Down Expand Up @@ -121,7 +121,7 @@ public function execute(

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
$this->procOpen($output, $editorCommand, $path);
}

$output->writeln([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* The ExecuteCommand class is responsible for executing migration versions up or down manually.
*/
class ExecuteCommand extends DoctrineCommand
final class ExecuteCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:execute';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* The GenerateCommand class is responsible for generating a blank migration class for you to modify to your needs.
*/
class GenerateCommand extends DoctrineCommand
final class GenerateCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:generate';
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : ?in

if ($editorCommand !== null) {
assert(is_string($editorCommand));
$this->procOpen($editorCommand, $path);
$this->procOpen($output, $editorCommand, $path);
}

$output->writeln([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* The LatestCommand class is responsible for outputting what your latest version is.
*/
class LatestCommand extends DoctrineCommand
final class LatestCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:latest';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* The MigrateCommand class is responsible for executing a migration from the current version to another
* version up or down. It will calculate all the migration versions that need to be executed and execute them.
*/
class MigrateCommand extends DoctrineCommand
final class MigrateCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:migrate';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* The RollupCommand class is responsible for deleting all previously executed migrations from the versions table
* and marking the freshly dumped schema migration (that was created with DumpSchemaCommand) as migrated.
*/
class RollupCommand extends DoctrineCommand
final class RollupCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:rollup';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* The StatusCommand class is responsible for outputting what the current state is of all your migrations. It shows
* what your current version is, how many new versions you have to execute, etc. and details about each of your migrations.
*/
class StatusCommand extends DoctrineCommand
final class StatusCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:status';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SyncMetadataCommand extends DoctrineCommand
final class SyncMetadataCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:sync-metadata-storage';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* The UpToDateCommand class outputs if your database is up to date or if there are new migrations
* that need to be executed.
*/
class UpToDateCommand extends DoctrineCommand
final class UpToDateCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:up-to-date';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* The VersionCommand class is responsible for manually adding and deleting migration versions from the tracking table.
*/
class VersionCommand extends DoctrineCommand
final class VersionCommand extends DoctrineCommand
{
/** @var string */
protected static $defaultName = 'migrations:version';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
use Doctrine\Migrations\Tools\Console\Command\DiffCommand;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\ProcessHelper;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use function sys_get_temp_dir;

final class DiffCommandTest extends TestCase
Expand All @@ -23,12 +27,18 @@ final class DiffCommandTest extends TestCase
/** @var Configuration */
private $configuration;

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

/** @var MockObject */
/** @var MockObject|DependencyFactory */
private $dependencyFactory;

/** @var MockObject|QuestionHelper */
private $questions;

/** @var MockObject|ProcessHelper */
private $process;

public function testExecute() : void
{
$input = $this->createMock(InputInterface::class);
Expand Down Expand Up @@ -74,9 +84,12 @@ public function testExecute() : void
->with('FooNs\\Version1234', 'filter expression', true, 80)
->willReturn('/path/to/migration.php');

$this->diffCommand->expects(self::once())
->method('procOpen')
->with('mate', '/path/to/migration.php');
$this->process->expects(self::once())
->method('mustRun')
->willReturnCallback(static function ($output, Process $process, $err, $callback) : void {
self::assertSame("'mate' '/path/to/migration.php'", $process->getCommandLine());
self::assertNotNull($callback);
});

$output->expects(self::once())
->method('writeln')
Expand Down Expand Up @@ -117,9 +130,11 @@ protected function setUp() : void
->method('getDiffGenerator')
->willReturn($this->migrationDiffGenerator);

$this->diffCommand = $this->getMockBuilder(DiffCommand::class)
->setConstructorArgs([$this->dependencyFactory])
->onlyMethods(['procOpen'])
->getMock();
$this->diffCommand = new DiffCommand($this->dependencyFactory);

$this->process = $this->createMock(ProcessHelper::class);
$this->questions = $this->createMock(QuestionHelper::class);

$this->diffCommand->setHelperSet(new HelperSet(['question' => $this->questions, 'process' => $this->process]));
}
}
Loading

0 comments on commit 19780b9

Please sign in to comment.