Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
    /**
3
     *	base include file for SimpleTest
4
     *	@package	SimpleTest
5
     *	@subpackage	UnitTester
6
     *	@version	$Id: shell_tester.php 1398 2006-09-08 19:31:03Z xue $
7
     */
8
 
9
    /**#@+
10
     *	include other SimpleTest class files
11
     */
12
    require_once(dirname(__FILE__) . '/test_case.php');
13
    /**#@-*/
14
 
15
    /**
16
     *    Wrapper for exec() functionality.
17
	 *	  @package SimpleTest
18
	 *	  @subpackage UnitTester
19
     */
20
    class SimpleShell {
21
        protected $_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
        protected $_current_shell;
73
        protected $_last_status;
74
        protected $_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
         *    Will trigger a pass if the two parameters have
133
         *    the same value only. Otherwise a fail. This
134
         *    is for testing hand extracted text, etc.
135
         *    @param mixed $first          Value to compare.
136
         *    @param mixed $second         Value to compare.
137
         *    @param string $message       Message to display.
138
         *    @return boolean              True on pass
139
         *    @access public
140
         */
141
        function assertEqual($first, $second, $message = "%s") {
142
            return $this->assert(
143
                    new EqualExpectation($first),
144
                    $second,
145
                    $message);
146
        }
147
 
148
        /**
149
         *    Will trigger a pass if the two parameters have
150
         *    a different value. Otherwise a fail. This
151
         *    is for testing hand extracted text, etc.
152
         *    @param mixed $first           Value to compare.
153
         *    @param mixed $second          Value to compare.
154
         *    @param string $message        Message to display.
155
         *    @return boolean               True on pass
156
         *    @access public
157
         */
158
        function assertNotEqual($first, $second, $message = "%s") {
159
            return $this->assert(
160
                    new NotEqualExpectation($first),
161
                    $second,
162
                    $message);
163
        }
164
 
165
        /**
166
         *    Tests the last status code from the shell.
167
         *    @param integer $status   Expected status of last
168
         *                             command.
169
         *    @param string $message   Message to display.
170
         *    @return boolean          True if pass.
171
         *    @access public
172
         */
173
        function assertExitCode($status, $message = "%s") {
174
            $message = sprintf($message, "Expected status code of [$status] from [" .
175
                    $this->_last_command . "], but got [" .
176
                    $this->_last_status . "]");
177
            return $this->assertTrue($status === $this->_last_status, $message);
178
        }
179
 
180
        /**
181
         *    Attempt to exactly match the combined STDERR and
182
         *    STDOUT output.
183
         *    @param string $expected  Expected output.
184
         *    @param string $message   Message to display.
185
         *    @return boolean          True if pass.
186
         *    @access public
187
         */
188
        function assertOutput($expected, $message = "%s") {
189
            $shell = $this->_getShell();
190
            return $this->assert(
191
                    new EqualExpectation($expected),
192
                    $shell->getOutput(),
193
                    $message);
194
        }
195
 
196
        /**
197
         *    Scans the output for a Perl regex. If found
198
         *    anywhere it passes, else it fails.
199
         *    @param string $pattern    Regex to search for.
200
         *    @param string $message    Message to display.
201
         *    @return boolean           True if pass.
202
         *    @access public
203
         */
204
        function assertOutputPattern($pattern, $message = "%s") {
205
            $shell = $this->_getShell();
206
            return $this->assert(
207
                    new PatternExpectation($pattern),
208
                    $shell->getOutput(),
209
                    $message);
210
        }
211
 
212
        /**
213
         *    If a Perl regex is found anywhere in the current
214
         *    output then a failure is generated, else a pass.
215
         *    @param string $pattern    Regex to search for.
216
         *    @param $message           Message to display.
217
         *    @return boolean           True if pass.
218
         *    @access public
219
         */
220
        function assertNoOutputPattern($pattern, $message = "%s") {
221
            $shell = $this->_getShell();
222
            return $this->assert(
223
                    new NoPatternExpectation($pattern),
224
                    $shell->getOutput(),
225
                    $message);
226
        }
227
 
228
        /**
229
         *    File existence check.
230
         *    @param string $path      Full filename and path.
231
         *    @param string $message   Message to display.
232
         *    @return boolean          True if pass.
233
         *    @access public
234
         */
235
        function assertFileExists($path, $message = "%s") {
236
            $message = sprintf($message, "File [$path] should exist");
237
            return $this->assertTrue(file_exists($path), $message);
238
        }
239
 
240
        /**
241
         *    File non-existence check.
242
         *    @param string $path      Full filename and path.
243
         *    @param string $message   Message to display.
244
         *    @return boolean          True if pass.
245
         *    @access public
246
         */
247
        function assertFileNotExists($path, $message = "%s") {
248
            $message = sprintf($message, "File [$path] should not exist");
249
            return $this->assertFalse(file_exists($path), $message);
250
        }
251
 
252
        /**
253
         *    Scans a file for a Perl regex. If found
254
         *    anywhere it passes, else it fails.
255
         *    @param string $pattern    Regex to search for.
256
         *    @param string $path       Full filename and path.
257
         *    @param string $message    Message to display.
258
         *    @return boolean           True if pass.
259
         *    @access public
260
         */
261
        function assertFilePattern($pattern, $path, $message = "%s") {
262
            $shell = $this->_getShell();
263
            return $this->assert(
264
                    new PatternExpectation($pattern),
265
                    implode('', file($path)),
266
                    $message);
267
        }
268
 
269
        /**
270
         *    If a Perl regex is found anywhere in the named
271
         *    file then a failure is generated, else a pass.
272
         *    @param string $pattern    Regex to search for.
273
         *    @param string $path       Full filename and path.
274
         *    @param string $message    Message to display.
275
         *    @return boolean           True if pass.
276
         *    @access public
277
         */
278
        function assertNoFilePattern($pattern, $path, $message = "%s") {
279
            $shell = $this->_getShell();
280
            return $this->assert(
281
                    new NoPatternExpectation($pattern),
282
                    implode('', file($path)),
283
                    $message);
284
        }
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
        function &_getShell() {
293
            return $this->_current_shell;
294
        }
295
 
296
        /**
297
         *    Factory for the shell to run the command on.
298
         *    @return Shell        New shell object.
299
         *    @access protected
300
         */
301
        function &_createShell() {
302
            $shell = new SimpleShell();
303
            return $shell;
304
        }
305
    }
306
?>