From b46f501869797e3140c991540b2065fda65d45c8 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 10 Oct 2019 14:52:51 -0400 Subject: [PATCH 1/5] Add support for "seeing" WP CLI output and exit codes. Mirrors the native CLI module. --- src/Codeception/Module/WPCLI.php | 64 +++++++++++++++++++++++++++ tests/wpcli_module/SeeMethodsCest.php | 34 ++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 tests/wpcli_module/SeeMethodsCest.php diff --git a/src/Codeception/Module/WPCLI.php b/src/Codeception/Module/WPCLI.php index b7a1844ec..4098c0e7f 100644 --- a/src/Codeception/Module/WPCLI.php +++ b/src/Codeception/Module/WPCLI.php @@ -75,6 +75,14 @@ class WPCLI extends Module * @var int|float|null */ protected $timeout; + /** + * @var string + */ + protected $lastOutput; + /** + * @var int + */ + protected $lastResultCode; /** * WPCLI constructor. @@ -302,6 +310,56 @@ public function cliToString($userCommand) return $return[0]; } + /** + * Checks that output from last command contains text. + * + * @param string $text + */ + public function seeInShellOutput($text) + { + \Codeception\PHPUnit\TestCase::assertStringContainsString($text, $this->lastOutput); + } + + /** + * Checks that output from last command doesn't contain text. + * + * @param string $text + */ + public function dontSeeInShellOutput($text) + { + \PHPUnit\Framework\Assert::assertNotContains($text, $this->lastOutput); + } + + /** + * Checks that output from the last command matches a given regular expression. + * + * @param string $regex + */ + public function seeShellOutputMatches($regex) + { + \PHPUnit\Framework\Assert::assertRegExp($regex, $this->lastOutput); + } + + /** + * Checks the result code from the last command. + * + * @param int $code + */ + public function seeResultCodeIs($code) + { + $this->assertEquals($this->lastResultCode, $code, "result code is $code"); + } + + /** + * Checks the result code from the last command. + * + * @param int $code + */ + public function seeResultCodeIsNot($code) + { + $this->assertNotEquals($this->lastResultCode, $code, "result code is $code"); + } + /** * Builds the process environment from the configuration options. * @@ -367,6 +425,9 @@ protected function run($userCommand) $this->debugSection('command exception', $e->getMessage()); + $this->lastOutput = ''; + $this->lastResultCode = 1; + return ['',1]; } @@ -386,6 +447,9 @@ protected function run($userCommand) $this->evaluateStatus($output, $status); + $this->lastOutput = $output; + $this->lastResultCode = $status; + return [$output, $status]; } } diff --git a/tests/wpcli_module/SeeMethodsCest.php b/tests/wpcli_module/SeeMethodsCest.php new file mode 100644 index 000000000..e974c76df --- /dev/null +++ b/tests/wpcli_module/SeeMethodsCest.php @@ -0,0 +1,34 @@ +cli('eval "echo \'hi\';"'); + $I->seeInShellOutput( 'hi' ); + } + + public function it_should_not_see_correct_output(Wpcli_moduleTester $I) + { + $I->cli('eval "echo \'hi\';"'); + $I->dontSeeInShellOutput( 'hello' ); + } + + public function it_should_see_correct_output_matches(Wpcli_moduleTester $I) + { + $I->cli('eval "echo \'hi\';"'); + $I->seeShellOutputMatches( '\w+' ); + } + + public function it_should_see_correct_result_code(Wpcli_moduleTester $I) + { + $I->cli('eval "echo \'hi\';"'); + $I->seeResultCodeIs(0); + } + + public function it_should_not_see_correct_result_code(Wpcli_moduleTester $I) + { + $I->cli('eval "echo \'hi\';"'); + $I->seeResultCodeIsNot(1); + } +} From e99542f844c53cf729f52d0e98a0b42f1b682fd7 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 10 Oct 2019 15:12:41 -0400 Subject: [PATCH 2/5] Move CLI tests to climodule. Fix regex. --- .../cli}/SeeMethodsCest.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) rename tests/{wpcli_module => climodule/cli}/SeeMethodsCest.php (50%) diff --git a/tests/wpcli_module/SeeMethodsCest.php b/tests/climodule/cli/SeeMethodsCest.php similarity index 50% rename from tests/wpcli_module/SeeMethodsCest.php rename to tests/climodule/cli/SeeMethodsCest.php index e974c76df..15f2ae384 100644 --- a/tests/wpcli_module/SeeMethodsCest.php +++ b/tests/climodule/cli/SeeMethodsCest.php @@ -1,32 +1,33 @@ cli('eval "echo \'hi\';"'); $I->seeInShellOutput( 'hi' ); } - public function it_should_not_see_correct_output(Wpcli_moduleTester $I) + public function it_should_not_see_correct_output(Tester $I) { $I->cli('eval "echo \'hi\';"'); $I->dontSeeInShellOutput( 'hello' ); } - public function it_should_see_correct_output_matches(Wpcli_moduleTester $I) + public function it_should_see_correct_output_matches(Tester $I) { $I->cli('eval "echo \'hi\';"'); - $I->seeShellOutputMatches( '\w+' ); + $I->seeShellOutputMatches( '/\w+/' ); } - public function it_should_see_correct_result_code(Wpcli_moduleTester $I) + public function it_should_see_correct_result_code(Tester $I) { $I->cli('eval "echo \'hi\';"'); $I->seeResultCodeIs(0); } - public function it_should_not_see_correct_result_code(Wpcli_moduleTester $I) + public function it_should_not_see_correct_result_code(Tester $I) { $I->cli('eval "echo \'hi\';"'); $I->seeResultCodeIsNot(1); From fa461590f4f0b3e99579fc0d04851ebda36b9def Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 10 Oct 2019 15:49:39 -0400 Subject: [PATCH 3/5] Add example and param descriptions. --- src/Codeception/Module/WPCLI.php | 45 ++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/Codeception/Module/WPCLI.php b/src/Codeception/Module/WPCLI.php index 4098c0e7f..e386a5473 100644 --- a/src/Codeception/Module/WPCLI.php +++ b/src/Codeception/Module/WPCLI.php @@ -313,7 +313,14 @@ public function cliToString($userCommand) /** * Checks that output from last command contains text. * - * @param string $text + * @param string $text The text to assert is in the output. + * + * @example + * ```php + * // Return the current site administrator email, using string command format. + * $I->cli('option get admin_email'); + * $I->seeInShellOutput('admin@example.org'); + * ``` */ public function seeInShellOutput($text) { @@ -323,7 +330,14 @@ public function seeInShellOutput($text) /** * Checks that output from last command doesn't contain text. * - * @param string $text + * @param string $text The text to assert is not in the output. + * + * @example + * ```php + * // Return the current site administrator email, using string command format. + * $I->cli('plugin list --status=active'); + * $I->dontSeeInShellOutput('my-inactive/plugin.php'); + * ``` */ public function dontSeeInShellOutput($text) { @@ -333,7 +347,14 @@ public function dontSeeInShellOutput($text) /** * Checks that output from the last command matches a given regular expression. * - * @param string $regex + * @param string $regex The regex pattern, including delimiters, to assert the output matches against. + * + * @example + * ```php + * // Return the current site administrator email, using string command format. + * $I->cli('option get admin_email'); + * $I->seeShellOutputMatches('/^\S+@\S+$/'); + * ``` */ public function seeShellOutputMatches($regex) { @@ -343,7 +364,14 @@ public function seeShellOutputMatches($regex) /** * Checks the result code from the last command. * - * @param int $code + * @param int $code The desired result code. + * + * @example + * ```php + * // Return the current site administrator email, using string command format. + * $I->cli('option get admin_email'); + * $I->seeResultCodeIs(0); + * ``` */ public function seeResultCodeIs($code) { @@ -353,7 +381,14 @@ public function seeResultCodeIs($code) /** * Checks the result code from the last command. * - * @param int $code + * @param int $code The result code the command should not have exited with. + * + * @example + * ```php + * // Return the current site administrator email, using string command format. + * $I->cli('invalid command'); + * $I->seeResultCodeIsNot(0); + * ``` */ public function seeResultCodeIsNot($code) { From 281fefa41b8a886c18383b03408f120847cb3432 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 14 Oct 2019 10:38:31 +0200 Subject: [PATCH 4/5] doc(modules) update WPCLI documentation --- docs/modules/WPCLI.md | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/docs/modules/WPCLI.md b/docs/modules/WPCLI.md index ad68fa0bb..8c5aa2eda 100644 --- a/docs/modules/WPCLI.md +++ b/docs/modules/WPCLI.md @@ -98,6 +98,21 @@ modules:
  • cliToString
  • +
  • + dontSeeInShellOutput +
  • +
  • + seeInShellOutput +
  • +
  • + seeResultCodeIs +
  • +
  • + seeResultCodeIsNot +
  • +
  • + seeShellOutputMatches +
  • @@ -166,6 +181,71 @@ modules:

    Parameters

    • string/array $userCommand - The string of command and parameters as it would be passed to wp-cli minus wp.
    + + +

    dontSeeInShellOutput

    + +
    + +

    Checks that output from last command doesn't contain text.

    +
        // Return the current site administrator email, using string command format.
    +    $I->cli('plugin list --status=active');
    +    $I->dontSeeInShellOutput('my-inactive/plugin.php');
    +

    Parameters

    +
      +
    • string $text - The text to assert is not in the output.
    + + +

    seeInShellOutput

    + +
    + +

    Checks that output from last command contains text.

    +
    
    +    // Return the current site administrator email, using string command format.
    +    $I->cli('option get admin_email');
    +

    Parameters

    +
      +
    • string $text - The text to assert is in the output.
    + + +

    seeResultCodeIs

    + +
    + +

    Checks the result code from the last command.

    +
        // Return the current site administrator email, using string command format.
    +    $I->cli('option get admin_email');
    +    $I->seeResultCodeIs(0);
    +

    Parameters

    +
      +
    • int $code - The desired result code.
    + + +

    seeResultCodeIsNot

    + +
    + +

    Checks the result code from the last command.

    +
        // Return the current site administrator email, using string command format.
    +    $I->cli('invalid command');
    +    $I->seeResultCodeIsNot(0);
    +

    Parameters

    +
      +
    • int $code - The result code the command should not have exited with.
    + + +

    seeShellOutputMatches

    + +
    + +

    Checks that output from the last command matches a given regular expression.

    +
    
    +    // Return the current site administrator email, using string command format.
    +    $I->cli('option get admin_email');
    +

    Parameters

    +
      +
    • string $regex - The regex pattern, including delimiters, to assert the output matches against.
    *This class extends \Codeception\Module* From 2c2ef611ce79ebdcaa2cac60bb507845ab93922a Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 14 Oct 2019 10:38:56 +0200 Subject: [PATCH 5/5] doc(CHANGELOG.md) add 2.2.30 changelog entry --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91109ecbd..e1f837594 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +## [2.2.30] 2019-10-14; +### Added +- `dontSeeInShellOutput`, `seeInShellOutput`, `seeResultCodeIs`, `seeResultCodeIsNot` and `seeShellOutputMatches` methods to `WPCLI` module (thanks @TimothyBJacobs)b + ## [2.2.29] 2019-09-24; ### Fixed - `wpbrowser` template class to make sure the environment file name is respected when set to different values @@ -1158,4 +1162,5 @@ This project adheres to [Semantic Versioning](http://semver.org/). [2.2.27]: https://github.com/lucatume/wp-browser/compare/2.2.26...2.2.27 [2.2.28]: https://github.com/lucatume/wp-browser/compare/2.2.27...2.2.28 [2.2.29]: https://github.com/lucatume/wp-browser/compare/2.2.28...2.2.29 -[unreleased]: https://github.com/lucatume/wp-browser/compare/2.2.29...HEAD +[2.2.30]: https://github.com/lucatume/wp-browser/compare/2.2.29...2.2.30 +[unreleased]: https://github.com/lucatume/wp-browser/compare/2.2.30...HEAD