shell_tester.php 10 KB

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