-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/e2e - Isolate each call to civix. More representative of real u…
…sage.
- Loading branch information
Showing
3 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace CRM\CivixBundle\Test; | ||
|
||
interface CommandTester { | ||
|
||
/** | ||
* Executes the command. | ||
* | ||
* Available execution options: | ||
* | ||
* * interactive: Sets the input interactive flag | ||
* * decorated: Sets the output decorated flag | ||
* * verbosity: Sets the output verbosity flag | ||
* * capture_stderr_separately: Make output of stdOut and stdErr separately available | ||
* | ||
* @param array $input An array of command arguments and options | ||
* @param array $options An array of execution options | ||
* | ||
* @return int The command exit code | ||
*/ | ||
public function execute(array $input, array $options = []); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDisplay(bool $normalize = FALSE); | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStatusCode(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace CRM\CivixBundle\Test; | ||
|
||
use Symfony\Component\Process\Process; | ||
|
||
class SubProcessCommandTester implements CommandTester { | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $baseCommand; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $display; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
protected $statusCode; | ||
|
||
/** | ||
* @param array $baseCommand | ||
*/ | ||
public function __construct(array $baseCommand) { | ||
$this->baseCommand = $baseCommand; | ||
} | ||
|
||
/** | ||
* Executes the command. | ||
* | ||
* @param array $input An array of command arguments and options | ||
* @param array $options An array of execution options | ||
* Ignored | ||
* @return int The command exit code | ||
*/ | ||
public function execute(array $input, array $options = []) { | ||
if (!empty($options)) { | ||
throw new \LogicException(__CLASS__ . " does not implement support for execute() options"); | ||
} | ||
|
||
$command = $this->baseCommand; | ||
foreach ($input as $key => $value) { | ||
if (substr($key, 0, 2) === '--') { | ||
$command[] = "$key=$value"; | ||
} | ||
else { | ||
$command[] = $value; | ||
} | ||
} | ||
|
||
$buffer = fopen('php://memory', 'w+'); | ||
|
||
$p = new Process($command); | ||
$p->run(function ($type, $data) use ($buffer) { | ||
// Default policy - combine STDOUT and STDIN into one continuous stream. | ||
fwrite($buffer, $data); | ||
}); | ||
$this->statusCode = $p->getExitCode(); | ||
|
||
rewind($buffer); | ||
$this->display = stream_get_contents($buffer); | ||
fclose($buffer); | ||
|
||
return $this->statusCode; | ||
} | ||
|
||
public function getDisplay(bool $normalize = FALSE) { | ||
if ($normalize) { | ||
return str_replace(\PHP_EOL, "\n", $this->display); | ||
} | ||
else { | ||
return $this->display; | ||
} | ||
} | ||
|
||
public function getStatusCode(): int { | ||
return $this->statusCode; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters