Skip to content

Commit

Permalink
chore: Run CS Fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Apr 28, 2024
1 parent 16f4099 commit 3294e83
Showing 1 changed file with 58 additions and 56 deletions.
114 changes: 58 additions & 56 deletions webfiori/cli/CommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
namespace webfiori\cli;

use webfiori\cli\Runner;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -19,60 +18,17 @@
* @author Ibrahim
*/
class CommandTestCase extends TestCase {
private $exitStatus;
private $outputs;
/**
*
* @var Runner
*/
private $runner;
/**
* End of line character which is used when sending outputs.
*/
const NL = "\n";
private $exitStatus;
private $outputs;
/**
* Returns an array that holds all outputs that was generated by running specific
* command.
*
* @return array If no command was executed, the array will be empty. Other
* than that, the array will hold outputs line by line in each index.
*/
public function getOutput() : array {
if ($this->outputs === null) {
$this->outputs = [];
}

return $this->outputs;
}
/**
* Returns an integer thar represents exit status of running specific command.
*
* @return int Default return value is 0.
*/
public function getExitCode() : int {
if ($this->exitStatus === null) {
$this->exitStatus = 0;
}

return $this->exitStatus;
}
/**
* Returns the instance that the class is using to execute the commands.
*
* @param bool $reset If set to true, input stream, output stream and,
* registered commands of the runner will reset to default.
*
* @return Runner The instance that the class is using to execute the commands.
* @var Runner
*/
public function getRunner(bool $reset = false) : Runner {
if ($this->runner === null) {
$this->runner = new Runner();
}
if ($reset) {
$this->runner->reset();
}
return $this->runner;
}
private $runner;
/**
* Register multiple commands and simulate the process of executing the app
* as if in production environment.
Expand All @@ -96,13 +52,13 @@ public function getRunner(bool $reset = false) : Runner {
*/
public function executeMultiCommand(array $commands, string $default = '', array $argv = [], array $userInputs = []) : array {
$runner = $this->getRunner(true);

foreach ($commands as $command) {
$runner->register($command);
}
$runner->setDefaultCommand($default);
$this->exec($argv, $userInputs);

return $this->getOutput();
}
/**
Expand All @@ -123,13 +79,59 @@ public function executeMultiCommand(array $commands, string $default = '', array
public function executeSingleCommand(CLICommand $command, array $argv = [], array $userInputs = []) : array {
$this->getRunner(true)->register($command);
$this->exec($argv, $userInputs, $command);

return $this->getOutput();
}

/**
* Returns an integer thar represents exit status of running specific command.
*
* @return int Default return value is 0.
*/
public function getExitCode() : int {
if ($this->exitStatus === null) {
$this->exitStatus = 0;
}

return $this->exitStatus;
}
/**
* Returns an array that holds all outputs that was generated by running specific
* command.
*
* @return array If no command was executed, the array will be empty. Other
* than that, the array will hold outputs line by line in each index.
*/
public function getOutput() : array {
if ($this->outputs === null) {
$this->outputs = [];
}

return $this->outputs;
}
/**
* Returns the instance that the class is using to execute the commands.
*
* @param bool $reset If set to true, input stream, output stream and,
* registered commands of the runner will reset to default.
*
* @return Runner The instance that the class is using to execute the commands.
*/
public function getRunner(bool $reset = false) : Runner {
if ($this->runner === null) {
$this->runner = new Runner();
}

if ($reset) {
$this->runner->reset();
}

return $this->runner;
}

private function exec(array $argv, array $userInputs, CLICommand $command = null) {

if ($command !== null) {
$key = array_search($command->getName(), $argv);

if ($key != 0 || $key === false) {
$argv = array_merge(['main.php', $command->getName()], $argv);
} else {
Expand All @@ -139,17 +141,17 @@ private function exec(array $argv, array $userInputs, CLICommand $command = null
$argv = array_merge(['main.php'], $argv);
}
$runner = $this->getRunner();

//Set arguments vector
$runner->setArgsVector($argv);

//Set user inputs.
//Must be called to use Array as input and output stream even if there are no inputs.
$runner->setInputs($userInputs);

//Start the process
$this->exitStatus = $runner->start();

$this->outputs = $runner->getOutput();
}
}

0 comments on commit 3294e83

Please sign in to comment.