Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
php: [5.6, 7.0, 7.1, 7.2, 7.3, 7.4]
php: [7.4, 8.0, 8.1]

steps:
- name: Checkout code
Expand Down
24 changes: 13 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"name":"codeception/module-cli",
"description":"Codeception module for testing basic shell commands and shell output",
"keywords":["codeception"],
"homepage":"http://codeception.com/",
"type":"library",
"license":"MIT",
"authors":[
"name": "codeception/module-cli",
"description": "Codeception module for testing basic shell commands and shell output",
"keywords": [ "codeception" ],
"homepage": "https://codeception.com/",
"type": "library",
"license": "MIT",
"authors": [
{
"name":"Michael Bodnarchuk"
"name": "Michael Bodnarchuk"
}
],
"minimum-stability": "RC",
"require": {
"php": ">=5.6.0 <9.0",
"php": "^7.4 || ^8.0",
"codeception/codeception": "*@dev"
},
"conflict": {
"codeception/codeception": "<4.0"
},
"autoload":{
"classmap": ["src/"]
"autoload": {
"classmap": [
"src/"
]
},
"config": {
"classmap-authoritative": true
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ A Codeception module for testing basic shell commands and shell output.
[![Total Downloads](https://poser.pugx.org/codeception/module-cli/downloads)](https://packagist.org/packages/codeception/module-cli)
[![License](https://poser.pugx.org/codeception/module-cli/license)](/LICENSE)

## Requirements

* `PHP 7.4` or higher.

## Installation

```
Expand Down
64 changes: 28 additions & 36 deletions src/Codeception/Module/Cli.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

declare(strict_types=1);

namespace Codeception\Module;

use Codeception\Module as CodeceptionModule;
use Codeception\Module;
use Codeception\PHPUnit\TestCase;
use Codeception\TestInterface;
use PHPUnit\Framework\Assert;

/**
* Wrapper for basic shell commands and shell output
Expand All @@ -14,13 +19,13 @@
*
* *Please review the code of non-stable modules and provide patches if you have issues.*
*/
class Cli extends CodeceptionModule
class Cli extends Module
{
public $output = '';
public string $output = '';

public $result = null;
public int $result;

public function _before(TestInterface $test)
public function _before(TestInterface $test): void
{
$this->output = '';
}
Expand All @@ -36,59 +41,50 @@ public function _before(TestInterface $test)
* // do not fail test when command fails
* $I->runShellCommand('phpunit', false);
* ```
*
* @param $command
* @param bool $failNonZero
*/
public function runShellCommand($command, $failNonZero = true)
public function runShellCommand(string $command, bool $failNonZero = true): void
{
$data = [];
exec("$command", $data, $resultCode);
exec("{$command}", $data, $resultCode);
$this->result = $resultCode;
$this->output = implode("\n", $data);
if ($this->output === null) {
\PHPUnit\Framework\Assert::fail("$command can't be executed");
Assert::fail("{$command} can't be executed");
}

if ($resultCode !== 0 && $failNonZero) {
\PHPUnit\Framework\Assert::fail("Result code was $resultCode.\n\n" . $this->output);
Assert::fail("Result code was {$resultCode}.\n\n" . $this->output);
}
$this->debug(preg_replace('~s/\e\[\d+(?>(;\d+)*)m//g~', '', $this->output));

$this->debug(preg_replace('#s/\e\[\d+(?>(;\d+)*)m//g#', '', $this->output));
}

/**
* Checks that output from last executed command contains text
*
* @param $text
*/
public function seeInShellOutput($text)
public function seeInShellOutput(string $text): void
{
\Codeception\PHPUnit\TestCase::assertStringContainsString($text, $this->output);
TestCase::assertStringContainsString($text, $this->output);
}

/**
* Checks that output from latest command doesn't contain text
*
* @param $text
*
*/
public function dontSeeInShellOutput($text)
public function dontSeeInShellOutput(string $text): void
{
$this->debug($this->output);
\Codeception\PHPUnit\TestCase::assertStringNotContainsString($text, $this->output);
TestCase::assertStringNotContainsString($text, $this->output);
}

/**
* @param $regex
*/
public function seeShellOutputMatches($regex)
public function seeShellOutputMatches(string $regex): void
{
\Codeception\PHPUnit\TestCase::assertRegExp($regex, $this->output);
TestCase::assertMatchesRegularExpression($regex, $this->output);
}

/**
* Returns the output from latest command
*/
public function grabShellOutput()
public function grabShellOutput(): string
{
return $this->output;
}
Expand All @@ -100,12 +96,10 @@ public function grabShellOutput()
* <?php
* $I->seeResultCodeIs(0);
* ```
*
* @param $code
*/
public function seeResultCodeIs($code)
public function seeResultCodeIs(int $code): void
{
$this->assertEquals($this->result, $code, "result code is $code");
$this->assertEquals($this->result, $code, "result code is {$code}");
}

/**
Expand All @@ -115,11 +109,9 @@ public function seeResultCodeIs($code)
* <?php
* $I->seeResultCodeIsNot(0);
* ```
*
* @param $code
*/
public function seeResultCodeIsNot($code)
public function seeResultCodeIsNot(int $code): void
{
$this->assertNotEquals($this->result, $code, "result code is $code");
$this->assertNotEquals($this->result, $code, "result code is {$code}");
}
}