Skip to content

Commit

Permalink
Merge pull request #27 from chives/dev
Browse files Browse the repository at this point in the history
Increased code quality and type safety
  • Loading branch information
rn0 authored Mar 19, 2019
2 parents 7f087f7 + 82fb637 commit 19bb4ff
Show file tree
Hide file tree
Showing 46 changed files with 595 additions and 1,029 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ install:

script:
- bin/phpspec run -f pretty
- bin/phpstan analyze --level 5
- bin/behat --no-snippets --verbose
51 changes: 19 additions & 32 deletions Behat/Context/CommandContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,58 +43,55 @@ public function __construct($fixturesPath)
$this->fixturesPath = $fixturesPath;
}

/**
* Sets Kernel instance.
*
* @param KernelInterface $kernel HttpKernel instance
*/
public function setKernel(KernelInterface $kernel)
public function setKernel(KernelInterface $kernel): void
{
$this->kernel = $kernel;
}

/**
* @When /^I successfully run console command "([^"]*)"$/
*/
public function iRunConsoleCommand($command)
public function iRunConsoleCommand(string $command): void
{
$application = new Application($this->kernel);
$tester = new ApplicationTester($application);

expect($tester->run($command))->toBe(0);
expect($tester->run([$command]))->toBe(0);
$this->lastCommandOutput = $tester->getDisplay(true);
}

/**
* @When /^I run console command "([^"]*)" with argument "--([^"]*)=([^"]*)"$/
*/
public function iRunConsoleCommandWithArgument($command, $argument, $value = 1)
public function iRunConsoleCommandWithArgument(string $command, string $argument, string $value = ''): void
{
$application = new Application($this->kernel);
$tester = new ApplicationTester($application);

$value = $this->prepareValue($argument, $value);

$this->lastCommandExitCode = $tester->run(array(
$this->lastCommandExitCode = $tester->run(
[
$command,
$argument => $value
));
]
);

$this->lastCommandOutput = $tester->getDisplay(true);
}

/**
* @When /^I unsuccessfully run console command "([^"]*)" with argument "--([^"]*)=([^"]*)"$/
*/
public function iUnsuccessfullyRunConsoleCommandWithArgument($command, $argument, $value)
public function iUnsuccessfullyRunConsoleCommandWithArgument(string $command, string $argument, string $value): void
{
$application = new Application($this->kernel);
$tester = new ApplicationTester($application);

expect($tester->run(array(
expect($tester->run([
$command,
$argument => $value
)))->toBe(1);
]))->toBe(1);

$this->lastCommandOutput = $tester->getDisplay(true);
}
Expand All @@ -103,17 +100,17 @@ public function iUnsuccessfullyRunConsoleCommandWithArgument($command, $argument
/**
* @When /^I successfully run console command "([^"]*)" with argument "--([^"]*)=([^"]*)"$/
*/
public function iSuccessfullyRunConsoleCommandWithArgument($command, $argument, $value)
public function iSuccessfullyRunConsoleCommandWithArgument(string $command, string $argument, string $value): void
{
$application = new Application($this->kernel);
$tester = new ApplicationTester($application);

$value = $this->prepareValue($argument, $value);

expect($tester->run(array(
expect($tester->run([
$command,
$argument => $value
)))->toBe(0);
]))->toBe(0);

$this->lastCommandOutput = $tester->getDisplay(true);
}
Expand All @@ -122,30 +119,20 @@ public function iSuccessfullyRunConsoleCommandWithArgument($command, $argument,
* @Then /^I should see "([^"]*)" console output$/
* @Given /^I should see "([^"]*)" output at console$/
*/
public function iShouldSeeOutputAtConsole($consoleOutput)
public function iShouldSeeOutputAtConsole(string $consoleOutput): void
{
expect(trim($this->getLastCommandOutput()))->toBe($consoleOutput);
}

/**
* @return mixed
*/
public function getLastCommandOutput()
public function getLastCommandOutput(): string
{
return $this->lastCommandOutput;
}

/**
* @param $argument
* @param $value
* @return string
*/
public function prepareValue($argument, $value)
public function prepareValue(string $argument, string $value): string
{
switch ($argument) {
case 'file':
$value = $this->kernel->getRootDir() . DIRECTORY_SEPARATOR . $value;
break;
if ($argument === 'file') {
return $this->kernel->getRootDir() . DIRECTORY_SEPARATOR . $value;
}

return $value;
Expand Down
60 changes: 16 additions & 44 deletions Behat/Context/Console/ApplicationTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

class ApplicationTester
Expand All @@ -25,7 +26,7 @@ class ApplicationTester
private $application;

/**
* @var StringInput $input
* @var ArrayInput $input
*/
private $input;

Expand All @@ -39,42 +40,25 @@ class ApplicationTester
*/
private $inputStream;

/**
* @param Application $application
*/
public function __construct(Application $application)
{
$this->application = $application;
}

/**
* @param array $input
*
* @return integer
*/
public function run($input = array())
public function run(array $input = []): int
{
$this->input = new ArrayInput((array) $input);
$this->input = new ArrayInput($input);
$this->input->setInteractive(false);

$this->output = new StreamOutput(fopen('php://memory', 'r+', false));

$inputStream = $this->getInputStream();
rewind($inputStream);
$this->output = new StreamOutput(fopen('php://memory', 'rb+', false));

/** @var QuestionHelper $questionHelper */
$questionHelper = $this->application->getHelperSet()->get('question');
$questionHelper->setInputStream($inputStream);
$this->initializeInputStream();
rewind($this->inputStream);

return $this->application->doRun($this->input, $this->output);
}

/**
* @param boolean
*
* @return string
*/
public function getDisplay($normalize = false)
public function getDisplay(bool $normalize = false): string
{
rewind($this->output->getStream());

Expand All @@ -87,39 +71,27 @@ public function getDisplay($normalize = false)
return $display;
}

/**
* @return \Symfony\Component\Console\Input\InputInterface
*/
public function getInput()
public function getInput(): InputInterface
{
return $this->input;
}

/**
* @return \Symfony\Component\Console\Output\OutputInterface
*/
public function getOutput()
public function getOutput(): OutputInterface
{
return $this->output;
}

/**
* @param string $input
*/
public function putToInputStream($input)
public function putToInputStream(string $input): void
{
fputs($this->getInputStream(), $input);
$this->initializeInputStream();

fwrite($this->inputStream, $input);
}

/**
* @return resource
*/
private function getInputStream()
private function initializeInputStream(): void
{
if (null === $this->inputStream) {
$this->inputStream = fopen('php://memory', 'r+', false);
}

return $this->inputStream;
}
}
Loading

0 comments on commit 19bb4ff

Please sign in to comment.