Skip to content

Commit

Permalink
Merge pull request #5 from fesor/step-run-result
Browse files Browse the repository at this point in the history
Add option to return step execution results
  • Loading branch information
fesor committed Jun 20, 2015
2 parents b306774 + 66cdb3e commit 8d3c76e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 9 additions & 1 deletion src/Controller/SteplerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand All @@ -70,7 +74,11 @@ public function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$output->writeln('<info>Step passed</info>');
if ($input->getOption('return-step-results')) {
$output->writeln(json_encode($result->getCallResult()->getReturn()));
} else {
$output->writeln('<info>Step passed</info>');
}

return 0;
}
Expand Down

0 comments on commit 8d3c76e

Please sign in to comment.