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