Skip to content

Commit

Permalink
Merge branch 'snicco/master'
Browse files Browse the repository at this point in the history
Add the `WPCLI::grabLastShellOutput` command and relative tests.
  • Loading branch information
lucatume committed Apr 28, 2022
2 parents 2012350 + 80305cf commit 5cefdab
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Changed

- Add the `WPCLI::grabLastShellOutput` method (thanks @calvinalkan).

## [3.1.5] 2022-03-16;

## Fixed
Expand Down
25 changes: 21 additions & 4 deletions src/Codeception/Module/WPCLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Codeception\Module;

use RuntimeException;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Codeception\Lib\ModuleContainer;
Expand Down Expand Up @@ -94,11 +95,11 @@ class WPCLI extends Module
*/
protected $timeout;
/**
* @var string
* @var string|null
*/
protected $lastOutput;
/**
* @var int
* @var int|null
*/
protected $lastResultCode;

Expand Down Expand Up @@ -314,6 +315,22 @@ protected function evaluateStatus($output, $status)
}
}

/**
* Returns the shell output of the last command.
*
* @return string The output produced by the last shell command, if any.
*
* @throws ModuleException If no prior command ran.
*/
public function grabLastShellOutput()
{
if (! isset($this->lastOutput)) {
throw new ModuleException($this, 'No output is set yet. Did you forget to run a command?');
}

return $this->lastOutput;
}

/**
* Returns the output of a wp-cli command as an array optionally allowing a callback to process the output.
*
Expand Down Expand Up @@ -439,7 +456,7 @@ public function seeInShellOutput($text)
*/
public function dontSeeInShellOutput($text)
{
Assert::assertStringNotContainsString($text, $this->lastOutput);
Assert::assertStringNotContainsString($text, (string)$this->lastOutput);
}

/**
Expand All @@ -458,7 +475,7 @@ public function dontSeeInShellOutput($text)
*/
public function seeShellOutputMatches($regex)
{
\PHPUnit\Framework\Assert::assertRegExp($regex, $this->lastOutput);
\PHPUnit\Framework\Assert::assertRegExp($regex, (string)$this->lastOutput);
}

/**
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/Codeception/Module/WPCLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
use Codeception\Lib\ModuleContainer;
use http\Exception\RuntimeException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use tad\WPBrowser\Process\Process;
Expand Down Expand Up @@ -826,4 +827,72 @@ protected function buildExpectedCommand(array $arr)
'--path=' . $this->root->url() . '/wp'
], $arr);
}

/**
* It should throw if trying to grab output from last command when no command ever ran
*
* @test
*/
public function should_throw_if_trying_to_grab_output_from_last_command_when_no_command_ever_ran()
{
$this->expectException(ModuleException::class);

$this->makeInstance()->grabLastShellOutput();
}

/**
* It should return empty string when grabbing output of last command that produced no output
*
* @test
*/
public function should_return_empty_string_when_grabbing_output_of_last_command_that_produced_no_output()
{
$this->process = $this->stubProphecy(
Process::class,
[
'getExitCode' => 0,
'getOutput' => '',
'getError' => ''
]
);
$this->process->withCwd(Arg::type('string'))->willReturn($this->process->reveal(true));
$this->process->withCommand(
$this->buildExpectedCommand(['test']),
$this->root->url() . '/wp'
)->willReturn($this->process->reveal(true));

$cli = $this->makeInstance();
$cli->cli(['test']);
$lastOutput = $cli->grabLastShellOutput();

$this->assertEquals('', $lastOutput);
}

/**
* It should allow grabbing the output of the last ran command
*
* @test
*/
public function should_allow_grabbing_the_output_of_the_last_ran_command()
{
$this->process = $this->stubProphecy(
Process::class,
[
'getExitCode' => 0,
'getOutput' => 'some output from the command',
'getError' => ''
]
);
$this->process->withCwd(Arg::type('string'))->willReturn($this->process->reveal(true));
$this->process->withCommand(
$this->buildExpectedCommand(['test']),
$this->root->url() . '/wp'
)->willReturn($this->process->reveal(true));

$cli = $this->makeInstance();
$cli->cli(['test']);
$lastOutput = $cli->grabLastShellOutput();

$this->assertEquals('some output from the command', $lastOutput);
}
}

0 comments on commit 5cefdab

Please sign in to comment.