shell_tester.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. 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. var $_current_shell;
  66. var $_last_status;
  67. var $_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 ShellTestCase($label = false) {
  76. $this->SimpleTestCase($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. $shell = &$this->_getShell();
  267. return $this->assert(
  268. new PatternExpectation($pattern),
  269. implode('', file($path)),
  270. $message);
  271. }
  272. /**
  273. * If a Perl regex is found anywhere in the named
  274. * file then a failure is generated, else a pass.
  275. * @param string $pattern Regex to search for.
  276. * @param string $path Full filename and path.
  277. * @param string $message Message to display.
  278. * @return boolean True if pass.
  279. * @access public
  280. */
  281. function assertNoFilePattern($pattern, $path, $message = "%s") {
  282. $shell = &$this->_getShell();
  283. return $this->assert(
  284. new NoPatternExpectation($pattern),
  285. implode('', file($path)),
  286. $message);
  287. }
  288. /**
  289. * Accessor for current shell. Used for testing the
  290. * the tester itself.
  291. * @return Shell Current shell.
  292. * @access protected
  293. */
  294. function &_getShell() {
  295. return $this->_current_shell;
  296. }
  297. /**
  298. * Factory for the shell to run the command on.
  299. * @return Shell New shell object.
  300. * @access protected
  301. */
  302. function &_createShell() {
  303. $shell = &new SimpleShell();
  304. return $shell;
  305. }
  306. }
  307. ?>