From 66cdb3ee81a2ff150a0b33af68598eee21b0a71c Mon Sep 17 00:00:00 2001 From: Sergey Protko Date: Sat, 20 Jun 2015 22:59:20 +0300 Subject: [PATCH] Add option to return step execution results --- README.md | 30 ++++++++++++++++++++++++++++ src/Controller/SteplerController.php | 10 +++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3223aed..b6743ef 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,36 @@ $ behat --run-step "Alice have user account: | password | alice_password |" ``` +### Step execution results +Sometime useful to get results of executed step. Given you have step definition, which creates new user. Often you need to get ID of created user (for interaction with API for example). This step definition would looks something like this: + +```php +/** + * @Given :name have account + */ +public function createUser($name) +{ + $user = $this->fakeUserFactory($name); + $this->userRepository->add($user); + + return $user->getDTO(); +} +``` + +Then, if we run this step with `--return-step-results` option: + +``` +$ behat --run-step "Bob have account" --return-step-result +``` + +We will get something like this: + +``` +{"id": 1, "name": "Bob"} +``` + +Data will be serialized with `json_encode`, so you should return some kind of DTO, which can be easly serialized into JSON (arrays, simple objects of objects which implements `JsonSerializable` interface). + ### Hooks Please not that only `BeforeStep` and `AfterStep` hooks are available. If you want to clear your database for example, the workaround for this will be specific step that will purge all test data. diff --git a/src/Controller/SteplerController.php b/src/Controller/SteplerController.php index 34005ba..260f628 100644 --- a/src/Controller/SteplerController.php +++ b/src/Controller/SteplerController.php @@ -50,6 +50,10 @@ public function configure(SymfonyCommand $command) ->addOption( '--run-step', null, InputOption::VALUE_REQUIRED, 'Run single step' + ) + ->addOption( + '--return-step-results', null, InputOption::VALUE_NONE, + 'Returns results of step execution' ); } @@ -70,7 +74,11 @@ public function execute(InputInterface $input, OutputInterface $output) return 1; } - $output->writeln('Step passed'); + if ($input->getOption('return-step-results')) { + $output->writeln(json_encode($result->getCallResult()->getReturn())); + } else { + $output->writeln('Step passed'); + } return 0; }