123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
-
-
-
- require_once(dirname(__FILE__) . '/test_case.php');
-
-
-
- class SimpleShell {
- private $output;
-
-
-
- function __construct() {
- $this->output = false;
- }
-
-
-
- function execute($command) {
- $this->output = false;
- exec($command, $this->output, $ret);
- return $ret;
- }
-
-
-
- function getOutput() {
- return implode("\n", $this->output);
- }
-
-
-
- function getOutputAsList() {
- return $this->output;
- }
- }
-
-
- class ShellTestCase extends SimpleTestCase {
- private $current_shell;
- private $last_status;
- private $last_command;
-
-
-
- function __construct($label = false) {
- parent::__construct($label);
- $this->current_shell = $this->createShell();
- $this->last_status = false;
- $this->last_command = '';
- }
-
-
-
- function execute($command) {
- $shell = $this->getShell();
- $this->last_status = $shell->execute($command);
- $this->last_command = $command;
- return ($this->last_status === 0);
- }
-
-
-
- function dumpOutput() {
- $this->dump($this->getOutput());
- }
-
-
-
- function getOutput() {
- $shell = $this->getShell();
- return $shell->getOutput();
- }
-
-
-
- function getOutputAsList() {
- $shell = $this->getShell();
- return $shell->getOutputAsList();
- }
-
-
-
- function assertTrue($result, $message = false) {
- return $this->assert(new TrueExpectation(), $result, $message);
- }
-
-
-
- function assertFalse($result, $message = '%s') {
- return $this->assert(new FalseExpectation(), $result, $message);
- }
-
-
-
- function assertEqual($first, $second, $message = "%s") {
- return $this->assert(
- new EqualExpectation($first),
- $second,
- $message);
- }
-
-
-
- function assertNotEqual($first, $second, $message = "%s") {
- return $this->assert(
- new NotEqualExpectation($first),
- $second,
- $message);
- }
-
-
-
- function assertExitCode($status, $message = "%s") {
- $message = sprintf($message, "Expected status code of [$status] from [" .
- $this->last_command . "], but got [" .
- $this->last_status . "]");
- return $this->assertTrue($status === $this->last_status, $message);
- }
-
-
-
- function assertOutput($expected, $message = "%s") {
- $shell = $this->getShell();
- return $this->assert(
- new EqualExpectation($expected),
- $shell->getOutput(),
- $message);
- }
-
-
-
- function assertOutputPattern($pattern, $message = "%s") {
- $shell = $this->getShell();
- return $this->assert(
- new PatternExpectation($pattern),
- $shell->getOutput(),
- $message);
- }
-
-
-
- function assertNoOutputPattern($pattern, $message = "%s") {
- $shell = $this->getShell();
- return $this->assert(
- new NoPatternExpectation($pattern),
- $shell->getOutput(),
- $message);
- }
-
-
-
- function assertFileExists($path, $message = "%s") {
- $message = sprintf($message, "File [$path] should exist");
- return $this->assertTrue(file_exists($path), $message);
- }
-
-
-
- function assertFileNotExists($path, $message = "%s") {
- $message = sprintf($message, "File [$path] should not exist");
- return $this->assertFalse(file_exists($path), $message);
- }
-
-
-
- function assertFilePattern($pattern, $path, $message = "%s") {
- return $this->assert(
- new PatternExpectation($pattern),
- implode('', file($path)),
- $message);
- }
-
-
-
- function assertNoFilePattern($pattern, $path, $message = "%s") {
- return $this->assert(
- new NoPatternExpectation($pattern),
- implode('', file($path)),
- $message);
- }
-
-
-
- protected function getShell() {
- return $this->current_shell;
- }
-
-
-
- protected function createShell() {
- return new SimpleShell();
- }
- }
- ?>
|