Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let commands write errors to STDERR if available (fix issue 2270) #2317

Open
wants to merge 8 commits into
base: 0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
16 changes: 14 additions & 2 deletions src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use UnexpectedValueException;

Expand Down Expand Up @@ -439,7 +440,7 @@
}

if (!$this->getConfig()->hasEnvironment($environment)) {
$output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
self::getErrorOutput($output)->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));

return false;
}
Expand Down Expand Up @@ -478,7 +479,7 @@
}
$output->writeln('<info>using database</info> ' . $name, $this->verbosityLevel);
} else {
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
self::getErrorOutput($output)->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');

Check warning on line 482 in src/Phinx/Console/Command/AbstractCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Phinx/Console/Command/AbstractCommand.php#L482

Added line #L482 was not covered by tests

return false;
}
Expand All @@ -492,4 +493,15 @@

return true;
}

/**
* Returns the error output to use
*
* @param \Symfony\Component\Console\Output\OutputInterface $output Output
* @return \Symfony\Component\Console\Output\OutputInterface
*/
protected static function getErrorOutput(OutputInterface $output): OutputInterface
{
return $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
}
}
18 changes: 10 additions & 8 deletions src/Phinx/Console/Command/ListAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ function ($alias, $class) use ($maxAliasLength, $maxClassLength) {
),
$this->verbosityLevel
);
} else {
$output->writeln(
sprintf(
'<error>No aliases defined in %s</error>',
Util::relativePath($this->config->getConfigFilePath())
)
);

return self::CODE_SUCCESS;
}

return self::CODE_SUCCESS;
self::getErrorOutput($output)->writeln(
sprintf(
'<error>No aliases defined in %s</error>',
Util::relativePath($this->config->getConfigFilePath())
)
);

return self::CODE_ERROR;
InvisibleSmiley marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 1 addition & 6 deletions src/Phinx/Console/Command/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Phinx\Console\Command;

use DateTime;
use Exception;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -92,12 +91,8 @@
$this->getManager()->migrate($environment, $version, $fake);
}
$end = microtime(true);
} catch (Exception $e) {
$output->writeln('<error>' . $e->__toString() . '</error>');

return self::CODE_ERROR;
} catch (Throwable $e) {
$output->writeln('<error>' . $e->__toString() . '</error>');
self::getErrorOutput($output)->writeln('<error>' . $e->__toString() . '</error>');

Check warning on line 95 in src/Phinx/Console/Command/Migrate.php

View check run for this annotation

Codecov / codecov/patch

src/Phinx/Console/Command/Migrate.php#L95

Added line #L95 was not covered by tests

return self::CODE_ERROR;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Phinx/Console/Command/ListAliasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public function testListingAliases($file, $hasAliases)
],
['decorated' => false]
);
$this->assertEquals(AbstractCommand::CODE_SUCCESS, $exitCode);
$expectedExitCode = $hasAliases ? AbstractCommand::CODE_SUCCESS : AbstractCommand::CODE_ERROR;
$this->assertSame($expectedExitCode, $exitCode);

$display = $commandTester->getDisplay(false);

Expand Down
Loading