Skip to content

Commit

Permalink
changed api for running multiple steps
Browse files Browse the repository at this point in the history
  • Loading branch information
mkusher committed Nov 6, 2015
1 parent 8d3c76e commit afe3f71
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
33 changes: 18 additions & 15 deletions src/Controller/SteplerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public function configure(SymfonyCommand $command)
{
$command
->addOption(
'--run-step', null, InputOption::VALUE_REQUIRED,
'Run single step'
'--run-steps',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Run steps'
)
->addOption(
'--return-step-results', null, InputOption::VALUE_NONE,
'--return-steps-results', null, InputOption::VALUE_NONE,
'Returns results of step execution'
);
}
Expand All @@ -62,43 +64,44 @@ public function configure(SymfonyCommand $command)
*/
public function execute(InputInterface $input, OutputInterface $output)
{
if (null === $step = $input->getOption('run-step')) {
$steps = $input->getOption('run-steps');
if (empty($steps) || !is_array($steps)) {
return null;
}

// try to find correct suite
$result = $this->stepRunner->run($step, $this->findSuite());
$result = $this->stepRunner->run($steps, $this->findSuite());
if (!$result->isPassed()) {
$output->writeln('<error>Step not passed</error>');
$output->writeln('<error>Steps not passed</error>');

return 1;
}

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

return 0;
}

private function findSuite()
{
$suites = $this->suiteRepository->getSuites();

if (count($suites) > 0 && null === $this->suite) {
return $suites[0];
}

foreach ($suites as $suite) {
if ($this->suite === $suite->getName()) {
return $suite;
}
}

throw new \RuntimeException(sprintf('Suite %s not found', $this->suite));
}


}
}
22 changes: 14 additions & 8 deletions src/Generator/DummyFeatureGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,27 @@ public function __construct(Parser $parser)
}


public function generate($rawStep)
public function generate($steps)
{
$feature = $this->generateFeatureForStep($rawStep);
$feature = $this->generateFeatureForSteps($steps);
$featureNode = $this->parser->parse($feature);

return new DummyFeatureNode($featureNode);
}
private function generateFeatureForStep($rawStep)

private function generateFeatureForSteps($steps)
{
return implode("\n", [
'Feature: dummy',
'Scenario: dummy',
sprintf('Given %s', $rawStep)
$this->generateRawSteps($steps)
]);
}

}

private function generateRawSteps($steps)
{
return implode("\n", array_map(function($step) {
return sprintf('Given %s', $step);
}, $steps));
}
}
18 changes: 9 additions & 9 deletions src/Runner/StepRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ final class StepRunner
* @param DummyFeatureGenerator $generator
*/
public function __construct(
StepTester $stepTester,
EnvironmentManager $environmentManager,
StepTester $stepTester,
EnvironmentManager $environmentManager,
DummyFeatureGenerator $generator
) {
$this->stepTester = $stepTester;
Expand All @@ -40,25 +40,25 @@ public function __construct(
}

/**
* @param $step
* @param array $steps
* @param Suite $suite
* @return \Behat\Behat\Tester\Result\StepResult
*/
public function run($step, Suite $suite)
public function run($steps, Suite $suite)
{
$env = $this->environmentManager->buildEnvironment($suite);
$env = $this->environmentManager->isolateEnvironment($env);

$dummyFeatureNode = $this->generator->generate($step);
$dummyFeatureNode = $this->generator->generate($steps);

$featureNode = $dummyFeatureNode->getFeatureNode();
$stepNode = $dummyFeatureNode->getStepNode();

$this->stepTester->setUp($env, $featureNode, $stepNode, false);
$result = $this->stepTester->test($env, $featureNode, $stepNode, false);
$this->stepTester->tearDown($env, $featureNode, $stepNode, false, $result);

return $result;
}
}

}

0 comments on commit afe3f71

Please sign in to comment.