Skip to content

Commit

Permalink
Added date and time, inline error
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Jagiello committed Dec 9, 2015
1 parent 229e9cb commit ffc5116
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"simple-bus/symfony-bridge": "^4.1",
"ramsey/uuid": "~3.0",
"symfony/yaml": "~2.5",
"matthiasnoback/symfony-console-form": "^1.2"
"matthiasnoback/symfony-console-form": "^1.2",
"nesbot/carbon": "^1.21"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
13 changes: 6 additions & 7 deletions features/handle_command.feature
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
Feature: Handle command

Scenario: Successfully handle command
When I run command "command-bus:successful-command --no-interaction"
When I run command "command-bus:successful-command --no-interaction" on "2015-12-09 11:00:59"
Then command should end successfully
And the output should be
"""
The tests\Clearcode\CommandBusConsole\CommandBus\SuccessfulCommand executed with success.
[2015-12-09 11:00:59] The tests\Clearcode\CommandBusConsole\CommandBus\SuccessfulCommand executed with success.
"""

Scenario: Unsuccessfully handle command
When I run command "command-bus:unsuccessful-command --no-interaction"
When I run command "command-bus:unsuccessful-command --no-interaction" on "2015-12-09 11:00:59"
Then command should end unsuccessfully
And the output should be
"""
The tests\Clearcode\CommandBusConsole\CommandBus\UnsuccessfulCommand failed to execute
Unsuccessful command execution.
[2015-12-09 11:00:59] The command "tests\Clearcode\CommandBusConsole\CommandBus\UnsuccessfulCommand" with arguments [] has failed to execute. Exception "DomainException" was thrown with message: "Unsuccessful command execution."
"""

Scenario: Successfully handle command with argument
When I run command "command-bus:command-with-argument --no-interaction --id=1234"
When I run command "command-bus:command-with-argument --no-interaction --id=1234" on "2015-12-09 11:00:59"
Then command should end successfully
And the output should be
"""
The tests\Clearcode\CommandBusConsole\CommandBus\CommandWithArgument executed with success.
[2015-12-09 11:00:59] The tests\Clearcode\CommandBusConsole\CommandBus\CommandWithArgument executed with success.
"""

Scenario: Unsuccessfully handle command with argument if argument is missing
Expand Down
26 changes: 18 additions & 8 deletions src/Bundle/Command/CommandBusHandleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Matthias\SymfonyConsoleForm\Console\Command\InteractiveFormContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Carbon\Carbon;

class CommandBusHandleCommand extends InteractiveFormContainerAwareCommand
{
Expand Down Expand Up @@ -64,22 +65,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
private function handleException(OutputInterface $output, \Exception $exception)
{
$command = $this->formData();
$arguments = $this->getArgumentsString($command);

$output->writeln(sprintf('<error>The %s failed to execute</error>', get_class($command)));

foreach (get_object_vars($command) as $propertyName => $propertyValue) {
$output->writeln(sprintf('<error>%s => %s</error>', $propertyName, $propertyValue));
}

$output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
$output->writeln(sprintf(
'<error>[%s] The command "%s" with arguments [%s] has failed to execute. Exception "%s" was thrown with message: "%s"</error>',
Carbon::now(), get_class($command), $arguments, get_class($exception), $exception->getMessage()
));

return self::ERROR_CODE;
}

private function handleSuccess(OutputInterface $output, $commandToLunch)
{
$output->writeln(sprintf('The <info>%s</info> executed with success.', $commandToLunch));
$output->writeln(sprintf('[%s] The <info>%s</info> executed with success.', Carbon::now(), $commandToLunch));

return self::SUCCESS_CODE;
}

private function getArgumentsString($command)
{
$argumentsString = "";

foreach (get_object_vars($command) as $propertyName => $propertyValue) {
$argumentsString .= $propertyName . " => '" . $propertyValue . "', ";
}

return substr($argumentsString, 0, -2);
}
}
7 changes: 6 additions & 1 deletion tests/Contexts/CLIContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Carbon\Carbon;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use tests\Clearcode\CommandBusConsole\TestKernel;
use tests\Clearcode\CommandBusConsole\Helper\ApplicationTester;
Expand All @@ -25,10 +26,14 @@ public function __construct()
/**
* @param string $command
*
* @When I run command :command on :date
* @When I run command :command
*/
public function iRunCommand($command)
public function iRunCommand($command, $date = null)
{
$testNow = $date ? new Carbon($date) : Carbon::now();
Carbon::setTestNow($testNow);

$this->runCommandWithNonInteractiveInput($command);
}

Expand Down

0 comments on commit ffc5116

Please sign in to comment.