shell_tester.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id$
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/test_case.php');
  12. /**#@-*/
  13. /**
  14. * Wrapper for exec() functionality.
  15. * @package SimpleTest
  16. * @subpackage UnitTester
  17. */
  18. class SimpleShell {
  19. private $output;
  20. /**
  21. * Executes the shell comand and stashes the output.
  22. * @access public
  23. */
  24. function __construct() {
  25. $this->output = false;
  26. }
  27. /**
  28. * Actually runs the command. Does not trap the
  29. * error stream output as this need PHP 4.3+.
  30. * @param string $command The actual command line
  31. * to run.
  32. * @return integer Exit code.
  33. * @access public
  34. */
  35. function execute($command) {
  36. $this->output = false;
  37. exec($command, $this->output, $ret);
  38. return $ret;
  39. }
  40. /**
  41. * Accessor for the last output.
  42. * @return string Output as text.
  43. * @access public
  44. */
  45. function getOutput() {
  46. return implode("\n", $this->output);
  47. }
  48. /**
  49. * Accessor for the last output.
  50. * @return array Output as array of lines.
  51. * @access public
  52. */
  53. function getOutputAsList() {
  54. return $this->output;
  55. }
  56. }
  57. /**
  58. * Test case for testing of command line scripts and
  59. * utilities. Usually scripts that are external to the
  60. * PHP code, but support it in some way.
  61. * @package SimpleTest
  62. * @subpackage UnitTester
  63. */
  64. class ShellTestCase extends SimpleTestCase {
  65. private $current_shell;
  66. private $last_status;
  67. private $last_command;
  68. /**
  69. * Creates an empty test case. Should be subclassed
  70. * with test methods for a functional test case.
  71. * @param string $label Name of test case. Will use
  72. * the class name if none specified.
  73. * @access public
  74. */
  75. function __construct($label = false) {
  76. parent::__construct($label);
  77. $this->current_shell = $this->createShell();
  78. $this->last_status = false;
  79. $this->last_command = '';
  80. }
  81. /**
  82. * Executes a command and buffers the results.
  83. * @param string $command Command to run.
  84. * @return boolean True if zero exit code.
  85. * @access public
  86. */
  87. function execute($command) {
  88. $shell = $this->getShell();
  89. $this->last_status = $shell->execute($command);
  90. $this->last_command = $command;
  91. return ($this->last_status === 0);
  92. }
  93. /**
  94. * Dumps the output of the last command.
  95. * @access public
  96. */
  97. function dumpOutput() {
  98. $this->dump($this->getOutput());
  99. }
  100. /**
  101. * Accessor for the last output.
  102. * @return string Output as text.
  103. * @access public
  104. */
  105. function getOutput() {
  106. $shell = $this->getShell();
  107. return $shell->getOutput();
  108. }
  109. /**
  110. * Accessor for the last output.
  111. * @return array Output as array of lines.
  112. * @access public
  113. */
  114. function getOutputAsList() {
  115. $shell = $this->getShell();
  116. return $shell->getOutputAsList();
  117. }
  118. /**
  119. * Called from within the test methods to register
  120. * passes and failures.
  121. * @param boolean $result Pass on true.
  122. * @param string $message Message to display describing
  123. * the test state.
  124. * @return boolean True on pass
  125. * @access public
  126. */
  127. function assertTrue($result, $message = false) {
  128. return $this->assert(new TrueExpectation(), $result, $message);
  129. }
  130. /**
  131. * Will be true on false and vice versa. False
  132. * is the PHP definition of false, so that null,
  133. * empty strings, zero and an empty array all count
  134. * as false.
  135. * @param boolean $result Pass on false.
  136. * @param string $message Message to display.
  137. * @return boolean True on pass
  138. * @access public
  139. */
  140. function assertFalse($result, $message = '%s') {
  141. return $this->assert(new FalseExpectation(), $result, $message);
  142. }
  143. /**
  144. * Will trigger a pass if the two parameters have
  145. * the same value only. Otherwise a fail. This
  146. * is for testing hand extracted text, etc.
  147. * @param mixed $first Value to compare.
  148. * @param mixed $second Value to compare.
  149. * @param string $message Message to display.
  150. * @return boolean True on pass
  151. * @access public
  152. */
  153. function assertEqual($first, $second, $message = "%s") {
  154. return $this->assert(
  155. new EqualExpectation($first),
  156. $second,
  157. $message);
  158. }
  159. /**
  160. * Will trigger a pass if the two parameters have
  161. * a different value. Otherwise a fail. This
  162. * is for testing hand extracted text, etc.
  163. * @param mixed $first Value to compare.
  164. * @param mixed $second Value to compare.
  165. * @param string $message Message to display.
  166. * @return boolean True on pass
  167. * @access public
  168. */
  169. function assertNotEqual($first, $second, $message = "%s") {
  170. return $this->assert(
  171. new NotEqualExpectation($first),
  172. $second,
  173. $message);
  174. }
  175. /**
  176. * Tests the last status code from the shell.
  177. * @param integer $status Expected status of last
  178. * command.
  179. * @param string $message Message to display.
  180. * @return boolean True if pass.
  181. * @access public
  182. */
  183. function assertExitCode($status, $message = "%s") {
  184. $message = sprintf($message, "Expected status code of [$status] from [" .
  185. $this->last_command . "], but got [" .
  186. $this->last_status . "]");
  187. return $this->assertTrue($status === $this->last_status, $message);
  188. }
  189. /**
  190. * Attempt to exactly match the combined STDERR and
  191. * STDOUT output.
  192. * @param string $expected Expected output.
  193. * @param string $message Message to display.
  194. * @return boolean True if pass.
  195. * @access public
  196. */
  197. function assertOutput($expected, $message = "%s") {
  198. $shell = $this->getShell();
  199. return $this->assert(
  200. new EqualExpectation($expected),
  201. $shell->getOutput(),
  202. $message);
  203. }
  204. /**
  205. * Scans the output for a Perl regex. If found
  206. * anywhere it passes, else it fails.
  207. * @param string $pattern Regex to search for.
  208. * @param string $message Message to display.
  209. * @return boolean True if pass.
  210. * @access public
  211. */
  212. function assertOutputPattern($pattern, $message = "%s") {
  213. $shell = $this->getShell();
  214. return $this->assert(
  215. new PatternExpectation($pattern),
  216. $shell->getOutput(),
  217. $message);
  218. }
  219. /**
  220. * If a Perl regex is found anywhere in the current
  221. * output then a failure is generated, else a pass.
  222. * @param string $pattern Regex to search for.
  223. * @param $message Message to display.
  224. * @return boolean True if pass.
  225. * @access public
  226. */
  227. function assertNoOutputPattern($pattern, $message = "%s") {
  228. $shell = $this->getShell();
  229. return $this->assert(
  230. new NoPatternExpectation($pattern),
  231. $shell->getOutput(),
  232. $message);
  233. }
  234. /**
  235. * File existence check.
  236. * @param string $path Full filename and path.
  237. * @param string $message Message to display.
  238. * @return boolean True if pass.
  239. * @access public
  240. */
  241. function assertFileExists($path, $message = "%s") {
  242. $message = sprintf($message, "File [$path] should exist");
  243. return $this->assertTrue(file_exists($path), $message);
  244. }
  245. /**
  246. * File non-existence check.
  247. * @param string $path Full filename and path.
  248. * @param string $message Message to display.
  249. * @return boolean True if pass.
  250. * @access public
  251. */
  252. function assertFileNotExists($path, $message = "%s") {
  253. $message = sprintf($message, "File [$path] should not exist");
  254. return $this->assertFalse(file_exists($path), $message);
  255. }
  256. /**
  257. * Scans a file for a Perl regex. If found
  258. * anywhere it passes, else it fails.
  259. * @param string $pattern Regex to search for.
  260. * @param string $path Full filename and path.
  261. * @param string $message Message to display.
  262. * @return boolean True if pass.
  263. * @access public
  264. */
  265. function assertFilePattern($pattern, $path, $message = "%s") {
  266. return $this->assert(
  267. new PatternExpectation($pattern),
  268. implode('', file($path)),
  269. $message);
  270. }
  271. /**
  272. * If a Perl regex is found anywhere in the named
  273. * file then a failure is generated, else a pass.
  274. * @param string $pattern Regex to search for.
  275. * @param string $path Full filename and path.
  276. * @param string $message Message to display.
  277. * @return boolean True if pass.
  278. * @access public
  279. */
  280. function assertNoFilePattern($pattern, $path, $message = "%s") {
  281. return $this->assert(
  282. new NoPatternExpectation($pattern),
  283. implode('', file($path)),
  284. $message);
  285. }
  286. /**
  287. * Accessor for current shell. Used for testing the
  288. * the tester itself.
  289. * @return Shell Current shell.
  290. * @access protected
  291. */
  292. protected function getShell() {
  293. return $this->current_shell;
  294. }
  295. /**
  296. * Factory for the shell to run the command on.
  297. * @return Shell New shell object.
  298. * @access protected
  299. */
  300. protected function createShell() {
  301. return new SimpleShell();
  302. }
  303. }
  304. ?>