@@ -1,305 +1,305 @@ |
||
| 1 | 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 | - */ |
|
| 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 | 8 | |
| 9 | - /**#@+ |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include other SimpleTest class files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/test_case.php'); |
|
| 13 | - /**#@-*/ |
|
| 12 | + require_once(dirname(__FILE__) . '/test_case.php'); |
|
| 13 | + /**#@-*/ |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Wrapper for exec() functionality. |
|
| 15 | + /** |
|
| 16 | + * Wrapper for exec() functionality. |
|
| 17 | 17 | * @package SimpleTest |
| 18 | 18 | * @subpackage UnitTester |
| 19 | - */ |
|
| 20 | - class SimpleShell { |
|
| 21 | - protected $_output; |
|
| 19 | + */ |
|
| 20 | + class SimpleShell { |
|
| 21 | + protected $_output; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Executes the shell comand and stashes the output. |
|
| 25 | - * @access public |
|
| 26 | - */ |
|
| 27 | - function SimpleShell() { |
|
| 28 | - $this->_output = false; |
|
| 29 | - } |
|
| 23 | + /** |
|
| 24 | + * Executes the shell comand and stashes the output. |
|
| 25 | + * @access public |
|
| 26 | + */ |
|
| 27 | + function SimpleShell() { |
|
| 28 | + $this->_output = false; |
|
| 29 | + } |
|
| 30 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 53 | |
| 54 | - /** |
|
| 55 | - * Accessor for the last output. |
|
| 56 | - * @return array Output as array of lines. |
|
| 57 | - * @access public |
|
| 58 | - */ |
|
| 54 | + /** |
|
| 55 | + * Accessor for the last output. |
|
| 56 | + * @return array Output as array of lines. |
|
| 57 | + * @access public |
|
| 58 | + */ |
|
| 59 | 59 | function getOutputAsList() { |
| 60 | 60 | return $this->_output; |
| 61 | 61 | } |
| 62 | - } |
|
| 62 | + } |
|
| 63 | 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. |
|
| 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 | 68 | * @package SimpleTest |
| 69 | 69 | * @subpackage UnitTester |
| 70 | - */ |
|
| 71 | - class ShellTestCase extends SimpleTestCase { |
|
| 72 | - protected $_current_shell; |
|
| 73 | - protected $_last_status; |
|
| 74 | - protected $_last_command; |
|
| 70 | + */ |
|
| 71 | + class ShellTestCase extends SimpleTestCase { |
|
| 72 | + protected $_current_shell; |
|
| 73 | + protected $_last_status; |
|
| 74 | + protected $_last_command; |
|
| 75 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 102 | |
| 103 | - /** |
|
| 104 | - * Dumps the output of the last command. |
|
| 105 | - * @access public |
|
| 106 | - */ |
|
| 107 | - function dumpOutput() { |
|
| 108 | - $this->dump($this->getOutput()); |
|
| 109 | - } |
|
| 103 | + /** |
|
| 104 | + * Dumps the output of the last command. |
|
| 105 | + * @access public |
|
| 106 | + */ |
|
| 107 | + function dumpOutput() { |
|
| 108 | + $this->dump($this->getOutput()); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Accessor for the last output. |
|
| 113 | - * @return string Output as text. |
|
| 114 | - * @access public |
|
| 115 | - */ |
|
| 111 | + /** |
|
| 112 | + * Accessor for the last output. |
|
| 113 | + * @return string Output as text. |
|
| 114 | + * @access public |
|
| 115 | + */ |
|
| 116 | 116 | function getOutput() { |
| 117 | - $shell = $this->_getShell(); |
|
| 118 | - return $shell->getOutput(); |
|
| 117 | + $shell = $this->_getShell(); |
|
| 118 | + return $shell->getOutput(); |
|
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Accessor for the last output. |
|
| 123 | - * @return array Output as array of lines. |
|
| 124 | - * @access public |
|
| 125 | - */ |
|
| 121 | + /** |
|
| 122 | + * Accessor for the last output. |
|
| 123 | + * @return array Output as array of lines. |
|
| 124 | + * @access public |
|
| 125 | + */ |
|
| 126 | 126 | function getOutputAsList() { |
| 127 | - $shell = $this->_getShell(); |
|
| 128 | - return $shell->getOutputAsList(); |
|
| 127 | + $shell = $this->_getShell(); |
|
| 128 | + return $shell->getOutputAsList(); |
|
| 129 | 129 | } |
| 130 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | - } |
|
| 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 | 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 | 296 | \ No newline at end of file |
| 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 | 307 | \ No newline at end of file |
@@ -1,689 +1,689 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * Base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage UnitTester |
|
| 6 | - * @version $Id: test_case.php 2309 2007-10-08 03:24:07Z wei $ |
|
| 7 | - */ |
|
| 8 | - |
|
| 9 | - /**#@+ |
|
| 2 | + /** |
|
| 3 | + * Base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage UnitTester |
|
| 6 | + * @version $Id: test_case.php 2309 2007-10-08 03:24:07Z wei $ |
|
| 7 | + */ |
|
| 8 | + |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * Includes SimpleTest files and defined the root constant |
| 11 | 11 | * for dependent libraries. |
| 12 | 12 | */ |
| 13 | - require_once(dirname(__FILE__) . '/invoker.php'); |
|
| 14 | - require_once(dirname(__FILE__) . '/errors.php'); |
|
| 15 | - require_once(dirname(__FILE__) . '/compatibility.php'); |
|
| 16 | - require_once(dirname(__FILE__) . '/scorer.php'); |
|
| 17 | - require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 18 | - require_once(dirname(__FILE__) . '/dumper.php'); |
|
| 19 | - require_once(dirname(__FILE__) . '/simpletest.php'); |
|
| 20 | - if (version_compare(phpversion(), '5') >= 0) { |
|
| 21 | - require_once(dirname(__FILE__) . '/exceptions.php'); |
|
| 22 | - require_once(dirname(__FILE__) . '/reflection_php5.php'); |
|
| 23 | - } else { |
|
| 24 | - require_once(dirname(__FILE__) . '/reflection_php4.php'); |
|
| 25 | - } |
|
| 26 | - if (! defined('SIMPLE_TEST')) { |
|
| 27 | - /** |
|
| 28 | - * @ignore |
|
| 29 | - */ |
|
| 30 | - define('SIMPLE_TEST', dirname(__FILE__) . '/'); |
|
| 31 | - } |
|
| 32 | - /**#@-*/ |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Basic test case. This is the smallest unit of a test |
|
| 36 | - * suite. It searches for |
|
| 37 | - * all methods that start with the the string "test" and |
|
| 38 | - * runs them. Working test cases extend this class. |
|
| 13 | + require_once(dirname(__FILE__) . '/invoker.php'); |
|
| 14 | + require_once(dirname(__FILE__) . '/errors.php'); |
|
| 15 | + require_once(dirname(__FILE__) . '/compatibility.php'); |
|
| 16 | + require_once(dirname(__FILE__) . '/scorer.php'); |
|
| 17 | + require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 18 | + require_once(dirname(__FILE__) . '/dumper.php'); |
|
| 19 | + require_once(dirname(__FILE__) . '/simpletest.php'); |
|
| 20 | + if (version_compare(phpversion(), '5') >= 0) { |
|
| 21 | + require_once(dirname(__FILE__) . '/exceptions.php'); |
|
| 22 | + require_once(dirname(__FILE__) . '/reflection_php5.php'); |
|
| 23 | + } else { |
|
| 24 | + require_once(dirname(__FILE__) . '/reflection_php4.php'); |
|
| 25 | + } |
|
| 26 | + if (! defined('SIMPLE_TEST')) { |
|
| 27 | + /** |
|
| 28 | + * @ignore |
|
| 29 | + */ |
|
| 30 | + define('SIMPLE_TEST', dirname(__FILE__) . '/'); |
|
| 31 | + } |
|
| 32 | + /**#@-*/ |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Basic test case. This is the smallest unit of a test |
|
| 36 | + * suite. It searches for |
|
| 37 | + * all methods that start with the the string "test" and |
|
| 38 | + * runs them. Working test cases extend this class. |
|
| 39 | 39 | * @package SimpleTest |
| 40 | 40 | * @subpackage UnitTester |
| 41 | - */ |
|
| 42 | - class SimpleTestCase { |
|
| 43 | - protected $_label = false; |
|
| 44 | - protected $_reporter; |
|
| 45 | - protected $_observers; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Sets up the test with no display. |
|
| 49 | - * @param string $label If no test name is given then |
|
| 50 | - * the class name is used. |
|
| 51 | - * @access public |
|
| 52 | - */ |
|
| 53 | - function SimpleTestCase($label = false) { |
|
| 54 | - if ($label) { |
|
| 55 | - $this->_label = $label; |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Accessor for the test name for subclasses. |
|
| 61 | - * @return string Name of the test. |
|
| 62 | - * @access public |
|
| 63 | - */ |
|
| 64 | - function getLabel() { |
|
| 65 | - return $this->_label ? $this->_label : get_class($this); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Used to invoke the single tests. |
|
| 70 | - * @return SimpleInvoker Individual test runner. |
|
| 71 | - * @access public |
|
| 72 | - */ |
|
| 73 | - function createInvoker() { |
|
| 74 | - $invoker = new SimpleErrorTrappingInvoker(new SimpleInvoker($this)); |
|
| 75 | - if (version_compare(phpversion(), '5') >= 0) { |
|
| 76 | - $invoker = new SimpleExceptionTrappingInvoker($invoker); |
|
| 77 | - } |
|
| 78 | - return $invoker; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Uses reflection to run every method within itself |
|
| 83 | - * starting with the string "test" unless a method |
|
| 84 | - * is specified. |
|
| 85 | - * @param SimpleReporter $reporter Current test reporter. |
|
| 86 | - * @access public |
|
| 87 | - */ |
|
| 88 | - function run($reporter) { |
|
| 89 | - SimpleTest::setCurrent($this); |
|
| 90 | - $this->_reporter = $reporter; |
|
| 91 | - $this->_reporter->paintCaseStart($this->getLabel()); |
|
| 92 | - foreach ($this->getTests() as $method) { |
|
| 93 | - if ($this->_reporter->shouldInvoke($this->getLabel(), $method)) { |
|
| 94 | - $invoker = $this->_reporter->createInvoker($this->createInvoker()); |
|
| 95 | - $invoker->before($method); |
|
| 96 | - $invoker->invoke($method); |
|
| 97 | - $invoker->after($method); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - $this->_reporter->paintCaseEnd($this->getLabel()); |
|
| 101 | - unset($this->_reporter); |
|
| 102 | - return $reporter->getStatus(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Gets a list of test names. Normally that will |
|
| 107 | - * be all internal methods that start with the |
|
| 108 | - * name "test". This method should be overridden |
|
| 109 | - * if you want a different rule. |
|
| 110 | - * @return array List of test names. |
|
| 111 | - * @access public |
|
| 112 | - */ |
|
| 113 | - function getTests() { |
|
| 114 | - $methods = array(); |
|
| 115 | - foreach (get_class_methods(get_class($this)) as $method) { |
|
| 116 | - if ($this->_isTest($method)) { |
|
| 117 | - $methods[] = $method; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - return $methods; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Tests to see if the method is a test that should |
|
| 125 | - * be run. Currently any method that starts with 'test' |
|
| 126 | - * is a candidate unless it is the constructor. |
|
| 127 | - * @param string $method Method name to try. |
|
| 128 | - * @return boolean True if test method. |
|
| 129 | - * @access protected |
|
| 130 | - */ |
|
| 131 | - function _isTest($method) { |
|
| 132 | - if (strtolower(substr($method, 0, 4)) == 'test') { |
|
| 133 | - return ! SimpleTestCompatibility::isA($this, strtolower($method)); |
|
| 134 | - } |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Announces the start of the test. |
|
| 140 | - * @param string $method Test method just started. |
|
| 141 | - * @access public |
|
| 142 | - */ |
|
| 143 | - function before($method) { |
|
| 144 | - $this->_reporter->paintMethodStart($method); |
|
| 145 | - $this->_observers = array(); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Sets up unit test wide variables at the start |
|
| 150 | - * of each test method. To be overridden in |
|
| 151 | - * actual user test cases. |
|
| 152 | - * @access public |
|
| 153 | - */ |
|
| 154 | - function setUp() { |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Clears the data set in the setUp() method call. |
|
| 159 | - * To be overridden by the user in actual user test cases. |
|
| 160 | - * @access public |
|
| 161 | - */ |
|
| 162 | - function tearDown() { |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Announces the end of the test. Includes private clean up. |
|
| 167 | - * @param string $method Test method just finished. |
|
| 168 | - * @access public |
|
| 169 | - */ |
|
| 170 | - function after($method) { |
|
| 171 | - for ($i = 0; $i < count($this->_observers); $i++) { |
|
| 172 | - $this->_observers[$i]->atTestEnd($method); |
|
| 173 | - } |
|
| 174 | - $this->_reporter->paintMethodEnd($method); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Sets up an observer for the test end. |
|
| 179 | - * @param object $observer Must have atTestEnd() |
|
| 180 | - * method. |
|
| 181 | - * @access public |
|
| 182 | - */ |
|
| 183 | - function tell($observer) { |
|
| 184 | - $this->_observers[] = $observer; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Sends a pass event with a message. |
|
| 189 | - * @param string $message Message to send. |
|
| 190 | - * @access public |
|
| 191 | - */ |
|
| 192 | - function pass($message = "Pass") { |
|
| 193 | - if (! isset($this->_reporter)) { |
|
| 194 | - trigger_error('Can only make assertions within test methods'); |
|
| 195 | - } |
|
| 196 | - $this->_reporter->paintPass( |
|
| 197 | - $message . $this->getAssertionLine()); |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Sends a fail event with a message. |
|
| 203 | - * @param string $message Message to send. |
|
| 204 | - * @access public |
|
| 205 | - */ |
|
| 206 | - function fail($message = "Fail") { |
|
| 207 | - if (! isset($this->_reporter)) { |
|
| 208 | - trigger_error('Can only make assertions within test methods'); |
|
| 209 | - } |
|
| 210 | - $this->_reporter->paintFail( |
|
| 211 | - $message . $this->getAssertionLine()); |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Formats a PHP error and dispatches it to the |
|
| 217 | - * reporter. |
|
| 218 | - * @param integer $severity PHP error code. |
|
| 219 | - * @param string $message Text of error. |
|
| 220 | - * @param string $file File error occoured in. |
|
| 221 | - * @param integer $line Line number of error. |
|
| 222 | - * @access public |
|
| 223 | - */ |
|
| 224 | - function error($severity, $message, $file, $line) { |
|
| 225 | - if (! isset($this->_reporter)) { |
|
| 226 | - trigger_error('Can only make assertions within test methods'); |
|
| 227 | - } |
|
| 228 | - $this->_reporter->paintError( |
|
| 229 | - "Unexpected PHP error [$message] severity [$severity] in [$file] line [$line]"); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Formats an exception and dispatches it to the |
|
| 234 | - * reporter. |
|
| 235 | - * @param Exception $exception Object thrown. |
|
| 236 | - * @access public |
|
| 237 | - */ |
|
| 238 | - function exception($exception) { |
|
| 239 | - $this->_reporter->paintError( |
|
| 240 | - 'Unexpected exception of type [' . get_class($exception) . |
|
| 241 | - '] with message ['. $exception->getMessage() . |
|
| 242 | - '] in ['. $exception->getFile() . |
|
| 243 | - '] line [' . $exception->getLine() . |
|
| 41 | + */ |
|
| 42 | + class SimpleTestCase { |
|
| 43 | + protected $_label = false; |
|
| 44 | + protected $_reporter; |
|
| 45 | + protected $_observers; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Sets up the test with no display. |
|
| 49 | + * @param string $label If no test name is given then |
|
| 50 | + * the class name is used. |
|
| 51 | + * @access public |
|
| 52 | + */ |
|
| 53 | + function SimpleTestCase($label = false) { |
|
| 54 | + if ($label) { |
|
| 55 | + $this->_label = $label; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Accessor for the test name for subclasses. |
|
| 61 | + * @return string Name of the test. |
|
| 62 | + * @access public |
|
| 63 | + */ |
|
| 64 | + function getLabel() { |
|
| 65 | + return $this->_label ? $this->_label : get_class($this); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Used to invoke the single tests. |
|
| 70 | + * @return SimpleInvoker Individual test runner. |
|
| 71 | + * @access public |
|
| 72 | + */ |
|
| 73 | + function createInvoker() { |
|
| 74 | + $invoker = new SimpleErrorTrappingInvoker(new SimpleInvoker($this)); |
|
| 75 | + if (version_compare(phpversion(), '5') >= 0) { |
|
| 76 | + $invoker = new SimpleExceptionTrappingInvoker($invoker); |
|
| 77 | + } |
|
| 78 | + return $invoker; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Uses reflection to run every method within itself |
|
| 83 | + * starting with the string "test" unless a method |
|
| 84 | + * is specified. |
|
| 85 | + * @param SimpleReporter $reporter Current test reporter. |
|
| 86 | + * @access public |
|
| 87 | + */ |
|
| 88 | + function run($reporter) { |
|
| 89 | + SimpleTest::setCurrent($this); |
|
| 90 | + $this->_reporter = $reporter; |
|
| 91 | + $this->_reporter->paintCaseStart($this->getLabel()); |
|
| 92 | + foreach ($this->getTests() as $method) { |
|
| 93 | + if ($this->_reporter->shouldInvoke($this->getLabel(), $method)) { |
|
| 94 | + $invoker = $this->_reporter->createInvoker($this->createInvoker()); |
|
| 95 | + $invoker->before($method); |
|
| 96 | + $invoker->invoke($method); |
|
| 97 | + $invoker->after($method); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + $this->_reporter->paintCaseEnd($this->getLabel()); |
|
| 101 | + unset($this->_reporter); |
|
| 102 | + return $reporter->getStatus(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Gets a list of test names. Normally that will |
|
| 107 | + * be all internal methods that start with the |
|
| 108 | + * name "test". This method should be overridden |
|
| 109 | + * if you want a different rule. |
|
| 110 | + * @return array List of test names. |
|
| 111 | + * @access public |
|
| 112 | + */ |
|
| 113 | + function getTests() { |
|
| 114 | + $methods = array(); |
|
| 115 | + foreach (get_class_methods(get_class($this)) as $method) { |
|
| 116 | + if ($this->_isTest($method)) { |
|
| 117 | + $methods[] = $method; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + return $methods; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Tests to see if the method is a test that should |
|
| 125 | + * be run. Currently any method that starts with 'test' |
|
| 126 | + * is a candidate unless it is the constructor. |
|
| 127 | + * @param string $method Method name to try. |
|
| 128 | + * @return boolean True if test method. |
|
| 129 | + * @access protected |
|
| 130 | + */ |
|
| 131 | + function _isTest($method) { |
|
| 132 | + if (strtolower(substr($method, 0, 4)) == 'test') { |
|
| 133 | + return ! SimpleTestCompatibility::isA($this, strtolower($method)); |
|
| 134 | + } |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Announces the start of the test. |
|
| 140 | + * @param string $method Test method just started. |
|
| 141 | + * @access public |
|
| 142 | + */ |
|
| 143 | + function before($method) { |
|
| 144 | + $this->_reporter->paintMethodStart($method); |
|
| 145 | + $this->_observers = array(); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Sets up unit test wide variables at the start |
|
| 150 | + * of each test method. To be overridden in |
|
| 151 | + * actual user test cases. |
|
| 152 | + * @access public |
|
| 153 | + */ |
|
| 154 | + function setUp() { |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Clears the data set in the setUp() method call. |
|
| 159 | + * To be overridden by the user in actual user test cases. |
|
| 160 | + * @access public |
|
| 161 | + */ |
|
| 162 | + function tearDown() { |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Announces the end of the test. Includes private clean up. |
|
| 167 | + * @param string $method Test method just finished. |
|
| 168 | + * @access public |
|
| 169 | + */ |
|
| 170 | + function after($method) { |
|
| 171 | + for ($i = 0; $i < count($this->_observers); $i++) { |
|
| 172 | + $this->_observers[$i]->atTestEnd($method); |
|
| 173 | + } |
|
| 174 | + $this->_reporter->paintMethodEnd($method); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Sets up an observer for the test end. |
|
| 179 | + * @param object $observer Must have atTestEnd() |
|
| 180 | + * method. |
|
| 181 | + * @access public |
|
| 182 | + */ |
|
| 183 | + function tell($observer) { |
|
| 184 | + $this->_observers[] = $observer; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Sends a pass event with a message. |
|
| 189 | + * @param string $message Message to send. |
|
| 190 | + * @access public |
|
| 191 | + */ |
|
| 192 | + function pass($message = "Pass") { |
|
| 193 | + if (! isset($this->_reporter)) { |
|
| 194 | + trigger_error('Can only make assertions within test methods'); |
|
| 195 | + } |
|
| 196 | + $this->_reporter->paintPass( |
|
| 197 | + $message . $this->getAssertionLine()); |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Sends a fail event with a message. |
|
| 203 | + * @param string $message Message to send. |
|
| 204 | + * @access public |
|
| 205 | + */ |
|
| 206 | + function fail($message = "Fail") { |
|
| 207 | + if (! isset($this->_reporter)) { |
|
| 208 | + trigger_error('Can only make assertions within test methods'); |
|
| 209 | + } |
|
| 210 | + $this->_reporter->paintFail( |
|
| 211 | + $message . $this->getAssertionLine()); |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Formats a PHP error and dispatches it to the |
|
| 217 | + * reporter. |
|
| 218 | + * @param integer $severity PHP error code. |
|
| 219 | + * @param string $message Text of error. |
|
| 220 | + * @param string $file File error occoured in. |
|
| 221 | + * @param integer $line Line number of error. |
|
| 222 | + * @access public |
|
| 223 | + */ |
|
| 224 | + function error($severity, $message, $file, $line) { |
|
| 225 | + if (! isset($this->_reporter)) { |
|
| 226 | + trigger_error('Can only make assertions within test methods'); |
|
| 227 | + } |
|
| 228 | + $this->_reporter->paintError( |
|
| 229 | + "Unexpected PHP error [$message] severity [$severity] in [$file] line [$line]"); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Formats an exception and dispatches it to the |
|
| 234 | + * reporter. |
|
| 235 | + * @param Exception $exception Object thrown. |
|
| 236 | + * @access public |
|
| 237 | + */ |
|
| 238 | + function exception($exception) { |
|
| 239 | + $this->_reporter->paintError( |
|
| 240 | + 'Unexpected exception of type [' . get_class($exception) . |
|
| 241 | + '] with message ['. $exception->getMessage() . |
|
| 242 | + '] in ['. $exception->getFile() . |
|
| 243 | + '] line [' . $exception->getLine() . |
|
| 244 | 244 | '] stack [' . $exception->getTraceAsString() .']'); |
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Sends a user defined event to the test reporter. |
|
| 249 | - * This is for small scale extension where |
|
| 250 | - * both the test case and either the reporter or |
|
| 251 | - * display are subclassed. |
|
| 252 | - * @param string $type Type of event. |
|
| 253 | - * @param mixed $payload Object or message to deliver. |
|
| 254 | - * @access public |
|
| 255 | - */ |
|
| 256 | - function signal($type, $payload) { |
|
| 257 | - if (! isset($this->_reporter)) { |
|
| 258 | - trigger_error('Can only make assertions within test methods'); |
|
| 259 | - } |
|
| 260 | - $this->_reporter->paintSignal($type, $payload); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Cancels any outstanding errors. |
|
| 265 | - * @access public |
|
| 266 | - */ |
|
| 267 | - function swallowErrors() { |
|
| 268 | - $queue = &SimpleErrorQueue::instance(); |
|
| 269 | - $queue->clear(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Runs an expectation directly, for extending the |
|
| 274 | - * tests with new expectation classes. |
|
| 275 | - * @param SimpleExpectation $expectation Expectation subclass. |
|
| 276 | - * @param mixed $compare Value to compare. |
|
| 277 | - * @param string $message Message to display. |
|
| 278 | - * @return boolean True on pass |
|
| 279 | - * @access public |
|
| 280 | - */ |
|
| 281 | - function assert($expectation, $compare, $message = '%s') { |
|
| 282 | - return $this->assertTrue( |
|
| 283 | - $expectation->test($compare), |
|
| 284 | - sprintf($message, $expectation->overlayMessage($compare))); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * @deprecated |
|
| 289 | - */ |
|
| 290 | - function assertExpectation($expectation, $compare, $message = '%s') { |
|
| 291 | - return $this->assert($expectation, $compare, $message); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Called from within the test methods to register |
|
| 296 | - * passes and failures. |
|
| 297 | - * @param boolean $result Pass on true. |
|
| 298 | - * @param string $message Message to display describing |
|
| 299 | - * the test state. |
|
| 300 | - * @return boolean True on pass |
|
| 301 | - * @access public |
|
| 302 | - */ |
|
| 303 | - function assertTrue($result, $message = false) { |
|
| 304 | - if (! $message) { |
|
| 305 | - $message = 'True assertion got ' . ($result ? 'True' : 'False'); |
|
| 306 | - } |
|
| 307 | - if ($result) { |
|
| 308 | - return $this->pass($message); |
|
| 309 | - } else { |
|
| 310 | - return $this->fail($message); |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Will be true on false and vice versa. False |
|
| 316 | - * is the PHP definition of false, so that null, |
|
| 317 | - * empty strings, zero and an empty array all count |
|
| 318 | - * as false. |
|
| 319 | - * @param boolean $result Pass on false. |
|
| 320 | - * @param string $message Message to display. |
|
| 321 | - * @return boolean True on pass |
|
| 322 | - * @access public |
|
| 323 | - */ |
|
| 324 | - function assertFalse($result, $message = false) { |
|
| 325 | - if (! $message) { |
|
| 326 | - $message = 'False assertion got ' . ($result ? 'True' : 'False'); |
|
| 327 | - } |
|
| 328 | - return $this->assertTrue(! $result, $message); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Uses a stack trace to find the line of an assertion. |
|
| 333 | - * @param string $format String formatting. |
|
| 334 | - * @param array $stack Stack frames top most first. Only |
|
| 335 | - * needed if not using the PHP |
|
| 336 | - * backtrace function. |
|
| 337 | - * @return string Line number of first assert* |
|
| 338 | - * method embedded in format string. |
|
| 339 | - * @access public |
|
| 340 | - */ |
|
| 341 | - function getAssertionLine($stack = false) { |
|
| 342 | - if ($stack === false) { |
|
| 343 | - $stack = SimpleTestCompatibility::getStackTrace(); |
|
| 344 | - } |
|
| 345 | - return SimpleDumper::getFormattedAssertionLine($stack); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Sends a formatted dump of a variable to the |
|
| 350 | - * test suite for those emergency debugging |
|
| 351 | - * situations. |
|
| 352 | - * @param mixed $variable Variable to display. |
|
| 353 | - * @param string $message Message to display. |
|
| 354 | - * @return mixed The original variable. |
|
| 355 | - * @access public |
|
| 356 | - */ |
|
| 357 | - function dump($variable, $message = false) { |
|
| 358 | - $formatted = SimpleDumper::dump($variable); |
|
| 359 | - if ($message) { |
|
| 360 | - $formatted = $message . "\n" . $formatted; |
|
| 361 | - } |
|
| 362 | - $this->_reporter->paintFormattedMessage($formatted); |
|
| 363 | - return $variable; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Dispatches a text message straight to the |
|
| 368 | - * test suite. Useful for status bar displays. |
|
| 369 | - * @param string $message Message to show. |
|
| 370 | - * @access public |
|
| 371 | - */ |
|
| 372 | - function sendMessage($message) { |
|
| 373 | - $this->_reporter->PaintMessage($message); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Accessor for the number of subtests. |
|
| 378 | - * @return integer Number of test cases. |
|
| 379 | - * @access public |
|
| 380 | - * @static |
|
| 381 | - */ |
|
| 382 | - static function getSize() { |
|
| 383 | - return 1; |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * This is a composite test class for combining |
|
| 389 | - * test cases and other RunnableTest classes into |
|
| 390 | - * a group test. |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Sends a user defined event to the test reporter. |
|
| 249 | + * This is for small scale extension where |
|
| 250 | + * both the test case and either the reporter or |
|
| 251 | + * display are subclassed. |
|
| 252 | + * @param string $type Type of event. |
|
| 253 | + * @param mixed $payload Object or message to deliver. |
|
| 254 | + * @access public |
|
| 255 | + */ |
|
| 256 | + function signal($type, $payload) { |
|
| 257 | + if (! isset($this->_reporter)) { |
|
| 258 | + trigger_error('Can only make assertions within test methods'); |
|
| 259 | + } |
|
| 260 | + $this->_reporter->paintSignal($type, $payload); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Cancels any outstanding errors. |
|
| 265 | + * @access public |
|
| 266 | + */ |
|
| 267 | + function swallowErrors() { |
|
| 268 | + $queue = &SimpleErrorQueue::instance(); |
|
| 269 | + $queue->clear(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Runs an expectation directly, for extending the |
|
| 274 | + * tests with new expectation classes. |
|
| 275 | + * @param SimpleExpectation $expectation Expectation subclass. |
|
| 276 | + * @param mixed $compare Value to compare. |
|
| 277 | + * @param string $message Message to display. |
|
| 278 | + * @return boolean True on pass |
|
| 279 | + * @access public |
|
| 280 | + */ |
|
| 281 | + function assert($expectation, $compare, $message = '%s') { |
|
| 282 | + return $this->assertTrue( |
|
| 283 | + $expectation->test($compare), |
|
| 284 | + sprintf($message, $expectation->overlayMessage($compare))); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * @deprecated |
|
| 289 | + */ |
|
| 290 | + function assertExpectation($expectation, $compare, $message = '%s') { |
|
| 291 | + return $this->assert($expectation, $compare, $message); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Called from within the test methods to register |
|
| 296 | + * passes and failures. |
|
| 297 | + * @param boolean $result Pass on true. |
|
| 298 | + * @param string $message Message to display describing |
|
| 299 | + * the test state. |
|
| 300 | + * @return boolean True on pass |
|
| 301 | + * @access public |
|
| 302 | + */ |
|
| 303 | + function assertTrue($result, $message = false) { |
|
| 304 | + if (! $message) { |
|
| 305 | + $message = 'True assertion got ' . ($result ? 'True' : 'False'); |
|
| 306 | + } |
|
| 307 | + if ($result) { |
|
| 308 | + return $this->pass($message); |
|
| 309 | + } else { |
|
| 310 | + return $this->fail($message); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Will be true on false and vice versa. False |
|
| 316 | + * is the PHP definition of false, so that null, |
|
| 317 | + * empty strings, zero and an empty array all count |
|
| 318 | + * as false. |
|
| 319 | + * @param boolean $result Pass on false. |
|
| 320 | + * @param string $message Message to display. |
|
| 321 | + * @return boolean True on pass |
|
| 322 | + * @access public |
|
| 323 | + */ |
|
| 324 | + function assertFalse($result, $message = false) { |
|
| 325 | + if (! $message) { |
|
| 326 | + $message = 'False assertion got ' . ($result ? 'True' : 'False'); |
|
| 327 | + } |
|
| 328 | + return $this->assertTrue(! $result, $message); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Uses a stack trace to find the line of an assertion. |
|
| 333 | + * @param string $format String formatting. |
|
| 334 | + * @param array $stack Stack frames top most first. Only |
|
| 335 | + * needed if not using the PHP |
|
| 336 | + * backtrace function. |
|
| 337 | + * @return string Line number of first assert* |
|
| 338 | + * method embedded in format string. |
|
| 339 | + * @access public |
|
| 340 | + */ |
|
| 341 | + function getAssertionLine($stack = false) { |
|
| 342 | + if ($stack === false) { |
|
| 343 | + $stack = SimpleTestCompatibility::getStackTrace(); |
|
| 344 | + } |
|
| 345 | + return SimpleDumper::getFormattedAssertionLine($stack); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Sends a formatted dump of a variable to the |
|
| 350 | + * test suite for those emergency debugging |
|
| 351 | + * situations. |
|
| 352 | + * @param mixed $variable Variable to display. |
|
| 353 | + * @param string $message Message to display. |
|
| 354 | + * @return mixed The original variable. |
|
| 355 | + * @access public |
|
| 356 | + */ |
|
| 357 | + function dump($variable, $message = false) { |
|
| 358 | + $formatted = SimpleDumper::dump($variable); |
|
| 359 | + if ($message) { |
|
| 360 | + $formatted = $message . "\n" . $formatted; |
|
| 361 | + } |
|
| 362 | + $this->_reporter->paintFormattedMessage($formatted); |
|
| 363 | + return $variable; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Dispatches a text message straight to the |
|
| 368 | + * test suite. Useful for status bar displays. |
|
| 369 | + * @param string $message Message to show. |
|
| 370 | + * @access public |
|
| 371 | + */ |
|
| 372 | + function sendMessage($message) { |
|
| 373 | + $this->_reporter->PaintMessage($message); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Accessor for the number of subtests. |
|
| 378 | + * @return integer Number of test cases. |
|
| 379 | + * @access public |
|
| 380 | + * @static |
|
| 381 | + */ |
|
| 382 | + static function getSize() { |
|
| 383 | + return 1; |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * This is a composite test class for combining |
|
| 389 | + * test cases and other RunnableTest classes into |
|
| 390 | + * a group test. |
|
| 391 | 391 | * @package SimpleTest |
| 392 | 392 | * @subpackage UnitTester |
| 393 | - */ |
|
| 394 | - class GroupTest { |
|
| 395 | - protected $_label; |
|
| 396 | - protected $_test_cases; |
|
| 397 | - protected $_old_track_errors; |
|
| 398 | - protected $_xdebug_is_enabled; |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Sets the name of the test suite. |
|
| 402 | - * @param string $label Name sent at the start and end |
|
| 403 | - * of the test. |
|
| 404 | - * @access public |
|
| 405 | - */ |
|
| 406 | - function GroupTest($label = false) { |
|
| 407 | - $this->_label = $label ? $label : get_class($this); |
|
| 408 | - $this->_test_cases = array(); |
|
| 409 | - $this->_old_track_errors = ini_get('track_errors'); |
|
| 410 | - $this->_xdebug_is_enabled = function_exists('xdebug_is_enabled') ? |
|
| 411 | - xdebug_is_enabled() : false; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Accessor for the test name for subclasses. |
|
| 416 | - * @return string Name of the test. |
|
| 417 | - * @access public |
|
| 418 | - */ |
|
| 419 | - function getLabel() { |
|
| 420 | - return $this->_label; |
|
| 421 | - } |
|
| 393 | + */ |
|
| 394 | + class GroupTest { |
|
| 395 | + protected $_label; |
|
| 396 | + protected $_test_cases; |
|
| 397 | + protected $_old_track_errors; |
|
| 398 | + protected $_xdebug_is_enabled; |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Sets the name of the test suite. |
|
| 402 | + * @param string $label Name sent at the start and end |
|
| 403 | + * of the test. |
|
| 404 | + * @access public |
|
| 405 | + */ |
|
| 406 | + function GroupTest($label = false) { |
|
| 407 | + $this->_label = $label ? $label : get_class($this); |
|
| 408 | + $this->_test_cases = array(); |
|
| 409 | + $this->_old_track_errors = ini_get('track_errors'); |
|
| 410 | + $this->_xdebug_is_enabled = function_exists('xdebug_is_enabled') ? |
|
| 411 | + xdebug_is_enabled() : false; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Accessor for the test name for subclasses. |
|
| 416 | + * @return string Name of the test. |
|
| 417 | + * @access public |
|
| 418 | + */ |
|
| 419 | + function getLabel() { |
|
| 420 | + return $this->_label; |
|
| 421 | + } |
|
| 422 | 422 | |
| 423 | 423 | function setLabel($value) |
| 424 | 424 | { |
| 425 | 425 | $this->_label = $value; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | - /** |
|
| 429 | - * Adds a test into the suite. Can be either a group |
|
| 430 | - * test or some other unit test. |
|
| 431 | - * @param SimpleTestCase $test_case Suite or individual test |
|
| 432 | - * case implementing the |
|
| 433 | - * runnable test interface. |
|
| 434 | - * @access public |
|
| 435 | - */ |
|
| 436 | - function addTestCase($test_case) { |
|
| 437 | - $this->_test_cases[] = $test_case; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Adds a test into the suite by class name. The class will |
|
| 442 | - * be instantiated as needed. |
|
| 443 | - * @param SimpleTestCase $test_case Suite or individual test |
|
| 444 | - * case implementing the |
|
| 445 | - * runnable test interface. |
|
| 446 | - * @access public |
|
| 447 | - */ |
|
| 448 | - function addTestClass($class) { |
|
| 449 | - if ($this->_getBaseTestCase($class) == 'grouptest') { |
|
| 450 | - $this->_test_cases[] = new $class(); |
|
| 451 | - } else { |
|
| 452 | - $this->_test_cases[] = $class; |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Builds a group test from a library of test cases. |
|
| 458 | - * The new group is composed into this one. |
|
| 459 | - * @param string $test_file File name of library with |
|
| 460 | - * test case classes. |
|
| 461 | - * @access public |
|
| 462 | - */ |
|
| 463 | - function addTestFile($test_file) { |
|
| 464 | - $existing_classes = get_declared_classes(); |
|
| 465 | - if ($error = $this->_requireWithError($test_file)) { |
|
| 466 | - $this->addTestCase(new BadGroupTest($test_file, $error)); |
|
| 467 | - return; |
|
| 468 | - } |
|
| 469 | - $classes = $this->_selectRunnableTests($existing_classes, get_declared_classes()); |
|
| 470 | - if (count($classes) == 0) { |
|
| 471 | - $this->addTestCase(new BadGroupTest($test_file, "No runnable test cases in [$test_file]")); |
|
| 472 | - return; |
|
| 473 | - } |
|
| 474 | - $group = $this->_createGroupFromClasses($test_file, $classes); |
|
| 475 | - $this->addTestCase($group); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Requires a source file recording any syntax errors. |
|
| 480 | - * @param string $file File name to require in. |
|
| 481 | - * @return string/boolean An error message on failure or false |
|
| 482 | - * if no errors. |
|
| 483 | - * @access private |
|
| 484 | - */ |
|
| 485 | - function _requireWithError($file) { |
|
| 486 | - $this->_enableErrorReporting(); |
|
| 487 | - include_once($file); |
|
| 488 | - $error = isset($php_errormsg) ? $php_errormsg : false; |
|
| 489 | - $this->_disableErrorReporting(); |
|
| 490 | - $self_inflicted_errors = array( |
|
| 491 | - 'Assigning the return value of new by reference is deprecated', |
|
| 492 | - 'var: Deprecated. Please use the public/private/protected modifiers'); |
|
| 493 | - if (in_array($error, $self_inflicted_errors)) { |
|
| 494 | - return false; |
|
| 495 | - } |
|
| 496 | - return $error; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Sets up detection of parse errors. Note that XDebug |
|
| 501 | - * interferes with this and has to be disabled. This is |
|
| 502 | - * to make sure the correct error code is returned |
|
| 503 | - * from unattended scripts. |
|
| 504 | - * @access private |
|
| 505 | - */ |
|
| 506 | - function _enableErrorReporting() { |
|
| 507 | - if ($this->_xdebug_is_enabled) { |
|
| 508 | - xdebug_disable(); |
|
| 509 | - } |
|
| 510 | - ini_set('track_errors', true); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - /** |
|
| 514 | - * Resets detection of parse errors to their old values. |
|
| 515 | - * This is to make sure the correct error code is returned |
|
| 516 | - * from unattended scripts. |
|
| 517 | - * @access private |
|
| 518 | - */ |
|
| 519 | - function _disableErrorReporting() { |
|
| 520 | - ini_set('track_errors', $this->_old_track_errors); |
|
| 521 | - if ($this->_xdebug_is_enabled) { |
|
| 522 | - xdebug_enable(); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Calculates the incoming test cases from a before |
|
| 528 | - * and after list of loaded classes. Skips abstract |
|
| 529 | - * classes. |
|
| 530 | - * @param array $existing_classes Classes before require(). |
|
| 531 | - * @param array $new_classes Classes after require(). |
|
| 532 | - * @return array New classes which are test |
|
| 533 | - * cases that shouldn't be ignored. |
|
| 534 | - * @access private |
|
| 535 | - */ |
|
| 536 | - function _selectRunnableTests($existing_classes, $new_classes) { |
|
| 537 | - $classes = array(); |
|
| 538 | - foreach ($new_classes as $class) { |
|
| 539 | - if (in_array($class, $existing_classes)) { |
|
| 540 | - continue; |
|
| 541 | - } |
|
| 542 | - if ($this->_getBaseTestCase($class)) { |
|
| 543 | - $reflection = new SimpleReflection($class); |
|
| 544 | - if ($reflection->isAbstract()) { |
|
| 545 | - SimpleTest::ignore($class); |
|
| 546 | - } |
|
| 547 | - $classes[] = $class; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - return $classes; |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Builds a group test from a class list. |
|
| 555 | - * @param string $title Title of new group. |
|
| 556 | - * @param array $classes Test classes. |
|
| 557 | - * @return GroupTest Group loaded with the new |
|
| 558 | - * test cases. |
|
| 559 | - * @access private |
|
| 560 | - */ |
|
| 561 | - function &_createGroupFromClasses($title, $classes) { |
|
| 562 | - SimpleTest::ignoreParentsIfIgnored($classes); |
|
| 563 | - $group = new GroupTest($title); |
|
| 564 | - foreach ($classes as $class) { |
|
| 565 | - if (! SimpleTest::isIgnored($class)) { |
|
| 566 | - $group->addTestClass($class); |
|
| 567 | - } |
|
| 568 | - } |
|
| 569 | - return $group; |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Test to see if a class is derived from the |
|
| 574 | - * SimpleTestCase class. |
|
| 575 | - * @param string $class Class name. |
|
| 576 | - * @access private |
|
| 577 | - */ |
|
| 578 | - function _getBaseTestCase($class) { |
|
| 579 | - while ($class = get_parent_class($class)) { |
|
| 580 | - $class = strtolower($class); |
|
| 581 | - if ($class == "simpletestcase" || $class == "grouptest") { |
|
| 582 | - return $class; |
|
| 583 | - } |
|
| 584 | - } |
|
| 585 | - return false; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Delegates to a visiting collector to add test |
|
| 590 | - * files. |
|
| 591 | - * @param string $path Path to scan from. |
|
| 592 | - * @param SimpleCollector $collector Directory scanner. |
|
| 593 | - * @access public |
|
| 594 | - */ |
|
| 595 | - function collect($path, $collector) { |
|
| 596 | - $collector->collect($this, $path); |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Invokes run() on all of the held test cases, instantiating |
|
| 601 | - * them if necessary. |
|
| 602 | - * @param SimpleReporter $reporter Current test reporter. |
|
| 603 | - * @access public |
|
| 604 | - */ |
|
| 605 | - function run($reporter) { |
|
| 606 | - $reporter->paintGroupStart($this->getLabel(), $this->getSize()); |
|
| 607 | - for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) { |
|
| 608 | - if (is_string($this->_test_cases[$i])) { |
|
| 609 | - $class = $this->_test_cases[$i]; |
|
| 610 | - $test = new $class(); |
|
| 611 | - $test->run($reporter); |
|
| 612 | - } else { |
|
| 613 | - $this->_test_cases[$i]->run($reporter); |
|
| 614 | - } |
|
| 615 | - } |
|
| 616 | - $reporter->paintGroupEnd($this->getLabel()); |
|
| 617 | - return $reporter->getStatus(); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Number of contained test cases. |
|
| 622 | - * @return integer Total count of cases in the group. |
|
| 623 | - * @access public |
|
| 624 | - */ |
|
| 625 | - function getSize() { |
|
| 626 | - $count = 0; |
|
| 627 | - foreach ($this->_test_cases as $case) { |
|
| 628 | - if (is_string($case)) { |
|
| 629 | - $count++; |
|
| 630 | - } else { |
|
| 631 | - $count += $case->getSize(); |
|
| 632 | - } |
|
| 633 | - } |
|
| 634 | - return $count; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * This is a failing group test for when a test suite hasn't |
|
| 640 | - * loaded properly. |
|
| 428 | + /** |
|
| 429 | + * Adds a test into the suite. Can be either a group |
|
| 430 | + * test or some other unit test. |
|
| 431 | + * @param SimpleTestCase $test_case Suite or individual test |
|
| 432 | + * case implementing the |
|
| 433 | + * runnable test interface. |
|
| 434 | + * @access public |
|
| 435 | + */ |
|
| 436 | + function addTestCase($test_case) { |
|
| 437 | + $this->_test_cases[] = $test_case; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Adds a test into the suite by class name. The class will |
|
| 442 | + * be instantiated as needed. |
|
| 443 | + * @param SimpleTestCase $test_case Suite or individual test |
|
| 444 | + * case implementing the |
|
| 445 | + * runnable test interface. |
|
| 446 | + * @access public |
|
| 447 | + */ |
|
| 448 | + function addTestClass($class) { |
|
| 449 | + if ($this->_getBaseTestCase($class) == 'grouptest') { |
|
| 450 | + $this->_test_cases[] = new $class(); |
|
| 451 | + } else { |
|
| 452 | + $this->_test_cases[] = $class; |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Builds a group test from a library of test cases. |
|
| 458 | + * The new group is composed into this one. |
|
| 459 | + * @param string $test_file File name of library with |
|
| 460 | + * test case classes. |
|
| 461 | + * @access public |
|
| 462 | + */ |
|
| 463 | + function addTestFile($test_file) { |
|
| 464 | + $existing_classes = get_declared_classes(); |
|
| 465 | + if ($error = $this->_requireWithError($test_file)) { |
|
| 466 | + $this->addTestCase(new BadGroupTest($test_file, $error)); |
|
| 467 | + return; |
|
| 468 | + } |
|
| 469 | + $classes = $this->_selectRunnableTests($existing_classes, get_declared_classes()); |
|
| 470 | + if (count($classes) == 0) { |
|
| 471 | + $this->addTestCase(new BadGroupTest($test_file, "No runnable test cases in [$test_file]")); |
|
| 472 | + return; |
|
| 473 | + } |
|
| 474 | + $group = $this->_createGroupFromClasses($test_file, $classes); |
|
| 475 | + $this->addTestCase($group); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Requires a source file recording any syntax errors. |
|
| 480 | + * @param string $file File name to require in. |
|
| 481 | + * @return string/boolean An error message on failure or false |
|
| 482 | + * if no errors. |
|
| 483 | + * @access private |
|
| 484 | + */ |
|
| 485 | + function _requireWithError($file) { |
|
| 486 | + $this->_enableErrorReporting(); |
|
| 487 | + include_once($file); |
|
| 488 | + $error = isset($php_errormsg) ? $php_errormsg : false; |
|
| 489 | + $this->_disableErrorReporting(); |
|
| 490 | + $self_inflicted_errors = array( |
|
| 491 | + 'Assigning the return value of new by reference is deprecated', |
|
| 492 | + 'var: Deprecated. Please use the public/private/protected modifiers'); |
|
| 493 | + if (in_array($error, $self_inflicted_errors)) { |
|
| 494 | + return false; |
|
| 495 | + } |
|
| 496 | + return $error; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + /** |
|
| 500 | + * Sets up detection of parse errors. Note that XDebug |
|
| 501 | + * interferes with this and has to be disabled. This is |
|
| 502 | + * to make sure the correct error code is returned |
|
| 503 | + * from unattended scripts. |
|
| 504 | + * @access private |
|
| 505 | + */ |
|
| 506 | + function _enableErrorReporting() { |
|
| 507 | + if ($this->_xdebug_is_enabled) { |
|
| 508 | + xdebug_disable(); |
|
| 509 | + } |
|
| 510 | + ini_set('track_errors', true); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + /** |
|
| 514 | + * Resets detection of parse errors to their old values. |
|
| 515 | + * This is to make sure the correct error code is returned |
|
| 516 | + * from unattended scripts. |
|
| 517 | + * @access private |
|
| 518 | + */ |
|
| 519 | + function _disableErrorReporting() { |
|
| 520 | + ini_set('track_errors', $this->_old_track_errors); |
|
| 521 | + if ($this->_xdebug_is_enabled) { |
|
| 522 | + xdebug_enable(); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Calculates the incoming test cases from a before |
|
| 528 | + * and after list of loaded classes. Skips abstract |
|
| 529 | + * classes. |
|
| 530 | + * @param array $existing_classes Classes before require(). |
|
| 531 | + * @param array $new_classes Classes after require(). |
|
| 532 | + * @return array New classes which are test |
|
| 533 | + * cases that shouldn't be ignored. |
|
| 534 | + * @access private |
|
| 535 | + */ |
|
| 536 | + function _selectRunnableTests($existing_classes, $new_classes) { |
|
| 537 | + $classes = array(); |
|
| 538 | + foreach ($new_classes as $class) { |
|
| 539 | + if (in_array($class, $existing_classes)) { |
|
| 540 | + continue; |
|
| 541 | + } |
|
| 542 | + if ($this->_getBaseTestCase($class)) { |
|
| 543 | + $reflection = new SimpleReflection($class); |
|
| 544 | + if ($reflection->isAbstract()) { |
|
| 545 | + SimpleTest::ignore($class); |
|
| 546 | + } |
|
| 547 | + $classes[] = $class; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + return $classes; |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * Builds a group test from a class list. |
|
| 555 | + * @param string $title Title of new group. |
|
| 556 | + * @param array $classes Test classes. |
|
| 557 | + * @return GroupTest Group loaded with the new |
|
| 558 | + * test cases. |
|
| 559 | + * @access private |
|
| 560 | + */ |
|
| 561 | + function &_createGroupFromClasses($title, $classes) { |
|
| 562 | + SimpleTest::ignoreParentsIfIgnored($classes); |
|
| 563 | + $group = new GroupTest($title); |
|
| 564 | + foreach ($classes as $class) { |
|
| 565 | + if (! SimpleTest::isIgnored($class)) { |
|
| 566 | + $group->addTestClass($class); |
|
| 567 | + } |
|
| 568 | + } |
|
| 569 | + return $group; |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Test to see if a class is derived from the |
|
| 574 | + * SimpleTestCase class. |
|
| 575 | + * @param string $class Class name. |
|
| 576 | + * @access private |
|
| 577 | + */ |
|
| 578 | + function _getBaseTestCase($class) { |
|
| 579 | + while ($class = get_parent_class($class)) { |
|
| 580 | + $class = strtolower($class); |
|
| 581 | + if ($class == "simpletestcase" || $class == "grouptest") { |
|
| 582 | + return $class; |
|
| 583 | + } |
|
| 584 | + } |
|
| 585 | + return false; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Delegates to a visiting collector to add test |
|
| 590 | + * files. |
|
| 591 | + * @param string $path Path to scan from. |
|
| 592 | + * @param SimpleCollector $collector Directory scanner. |
|
| 593 | + * @access public |
|
| 594 | + */ |
|
| 595 | + function collect($path, $collector) { |
|
| 596 | + $collector->collect($this, $path); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Invokes run() on all of the held test cases, instantiating |
|
| 601 | + * them if necessary. |
|
| 602 | + * @param SimpleReporter $reporter Current test reporter. |
|
| 603 | + * @access public |
|
| 604 | + */ |
|
| 605 | + function run($reporter) { |
|
| 606 | + $reporter->paintGroupStart($this->getLabel(), $this->getSize()); |
|
| 607 | + for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) { |
|
| 608 | + if (is_string($this->_test_cases[$i])) { |
|
| 609 | + $class = $this->_test_cases[$i]; |
|
| 610 | + $test = new $class(); |
|
| 611 | + $test->run($reporter); |
|
| 612 | + } else { |
|
| 613 | + $this->_test_cases[$i]->run($reporter); |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + $reporter->paintGroupEnd($this->getLabel()); |
|
| 617 | + return $reporter->getStatus(); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Number of contained test cases. |
|
| 622 | + * @return integer Total count of cases in the group. |
|
| 623 | + * @access public |
|
| 624 | + */ |
|
| 625 | + function getSize() { |
|
| 626 | + $count = 0; |
|
| 627 | + foreach ($this->_test_cases as $case) { |
|
| 628 | + if (is_string($case)) { |
|
| 629 | + $count++; |
|
| 630 | + } else { |
|
| 631 | + $count += $case->getSize(); |
|
| 632 | + } |
|
| 633 | + } |
|
| 634 | + return $count; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * This is a failing group test for when a test suite hasn't |
|
| 640 | + * loaded properly. |
|
| 641 | 641 | * @package SimpleTest |
| 642 | 642 | * @subpackage UnitTester |
| 643 | - */ |
|
| 644 | - class BadGroupTest { |
|
| 645 | - protected $_label; |
|
| 646 | - protected $_error; |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * Sets the name of the test suite and error message. |
|
| 650 | - * @param string $label Name sent at the start and end |
|
| 651 | - * of the test. |
|
| 652 | - * @access public |
|
| 653 | - */ |
|
| 654 | - function BadGroupTest($label, $error) { |
|
| 655 | - $this->_label = $label; |
|
| 656 | - $this->_error = $error; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Accessor for the test name for subclasses. |
|
| 661 | - * @return string Name of the test. |
|
| 662 | - * @access public |
|
| 663 | - */ |
|
| 664 | - function getLabel() { |
|
| 665 | - return $this->_label; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Sends a single error to the reporter. |
|
| 670 | - * @param SimpleReporter $reporter Current test reporter. |
|
| 671 | - * @access public |
|
| 672 | - */ |
|
| 673 | - function run($reporter) { |
|
| 674 | - $reporter->paintGroupStart($this->getLabel(), $this->getSize()); |
|
| 675 | - $reporter->paintFail('Bad GroupTest [' . $this->getLabel() . |
|
| 676 | - '] with error [' . $this->_error . ']'); |
|
| 677 | - $reporter->paintGroupEnd($this->getLabel()); |
|
| 678 | - return $reporter->getStatus(); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Number of contained test cases. Always zero. |
|
| 683 | - * @return integer Total count of cases in the group. |
|
| 684 | - * @access public |
|
| 685 | - */ |
|
| 686 | - function getSize() { |
|
| 687 | - return 0; |
|
| 688 | - } |
|
| 689 | - } |
|
| 643 | + */ |
|
| 644 | + class BadGroupTest { |
|
| 645 | + protected $_label; |
|
| 646 | + protected $_error; |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * Sets the name of the test suite and error message. |
|
| 650 | + * @param string $label Name sent at the start and end |
|
| 651 | + * of the test. |
|
| 652 | + * @access public |
|
| 653 | + */ |
|
| 654 | + function BadGroupTest($label, $error) { |
|
| 655 | + $this->_label = $label; |
|
| 656 | + $this->_error = $error; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Accessor for the test name for subclasses. |
|
| 661 | + * @return string Name of the test. |
|
| 662 | + * @access public |
|
| 663 | + */ |
|
| 664 | + function getLabel() { |
|
| 665 | + return $this->_label; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Sends a single error to the reporter. |
|
| 670 | + * @param SimpleReporter $reporter Current test reporter. |
|
| 671 | + * @access public |
|
| 672 | + */ |
|
| 673 | + function run($reporter) { |
|
| 674 | + $reporter->paintGroupStart($this->getLabel(), $this->getSize()); |
|
| 675 | + $reporter->paintFail('Bad GroupTest [' . $this->getLabel() . |
|
| 676 | + '] with error [' . $this->_error . ']'); |
|
| 677 | + $reporter->paintGroupEnd($this->getLabel()); |
|
| 678 | + return $reporter->getStatus(); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Number of contained test cases. Always zero. |
|
| 683 | + * @return integer Total count of cases in the group. |
|
| 684 | + * @access public |
|
| 685 | + */ |
|
| 686 | + function getSize() { |
|
| 687 | + return 0; |
|
| 688 | + } |
|
| 689 | + } |
|
@@ -1,524 +1,524 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage WebTester |
|
| 6 | - * @version $Id: url.php 1532 2006-12-01 12:28:55Z xue $ |
|
| 7 | - */ |
|
| 8 | - |
|
| 9 | - /**#@+ |
|
| 2 | + /** |
|
| 3 | + * base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage WebTester |
|
| 6 | + * @version $Id: url.php 1532 2006-12-01 12:28:55Z xue $ |
|
| 7 | + */ |
|
| 8 | + |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include other SimpleTest class files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 13 | - /**#@-*/ |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * URL parser to replace parse_url() PHP function which |
|
| 17 | - * got broken in PHP 4.3.0. Adds some browser specific |
|
| 18 | - * functionality such as expandomatics. |
|
| 19 | - * Guesses a bit trying to separate the host from |
|
| 20 | - * the path and tries to keep a raw, possibly unparsable, |
|
| 21 | - * request string as long as possible. |
|
| 12 | + require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 13 | + /**#@-*/ |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * URL parser to replace parse_url() PHP function which |
|
| 17 | + * got broken in PHP 4.3.0. Adds some browser specific |
|
| 18 | + * functionality such as expandomatics. |
|
| 19 | + * Guesses a bit trying to separate the host from |
|
| 20 | + * the path and tries to keep a raw, possibly unparsable, |
|
| 21 | + * request string as long as possible. |
|
| 22 | 22 | * @package SimpleTest |
| 23 | 23 | * @subpackage WebTester |
| 24 | - */ |
|
| 25 | - class SimpleUrl { |
|
| 26 | - protected $_scheme; |
|
| 27 | - protected $_username; |
|
| 28 | - protected $_password; |
|
| 29 | - protected $_host; |
|
| 30 | - protected $_port; |
|
| 31 | - protected $_path; |
|
| 32 | - protected $_request; |
|
| 33 | - protected $_fragment; |
|
| 34 | - protected $_x; |
|
| 35 | - protected $_y; |
|
| 36 | - protected $_target; |
|
| 37 | - protected $_raw = false; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Constructor. Parses URL into sections. |
|
| 41 | - * @param string $url Incoming URL. |
|
| 42 | - * @access public |
|
| 43 | - */ |
|
| 44 | - function SimpleUrl($url) { |
|
| 45 | - list($x, $y) = $this->_chompCoordinates($url); |
|
| 46 | - $this->setCoordinates($x, $y); |
|
| 47 | - $this->_scheme = $this->_chompScheme($url); |
|
| 48 | - list($this->_username, $this->_password) = $this->_chompLogin($url); |
|
| 49 | - $this->_host = $this->_chompHost($url); |
|
| 50 | - $this->_port = false; |
|
| 51 | - if (preg_match('/(.*?):(.*)/', $this->_host, $host_parts)) { |
|
| 52 | - $this->_host = $host_parts[1]; |
|
| 53 | - $this->_port = (integer)$host_parts[2]; |
|
| 54 | - } |
|
| 55 | - $this->_path = $this->_chompPath($url); |
|
| 56 | - $this->_request = $this->_parseRequest($this->_chompRequest($url)); |
|
| 57 | - $this->_fragment = (strncmp($url, "#", 1) == 0 ? substr($url, 1) : false); |
|
| 58 | - $this->_target = false; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Extracts the X, Y coordinate pair from an image map. |
|
| 63 | - * @param string $url URL so far. The coordinates will be |
|
| 64 | - * removed. |
|
| 65 | - * @return array X, Y as a pair of integers. |
|
| 66 | - * @access private |
|
| 67 | - */ |
|
| 68 | - function _chompCoordinates($url) { |
|
| 69 | - if (preg_match('/(.*)\?(\d+),(\d+)$/', $url, $matches)) { |
|
| 70 | - $url = $matches[1]; |
|
| 71 | - return array((integer)$matches[2], (integer)$matches[3]); |
|
| 72 | - } |
|
| 73 | - return array(false, false); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Extracts the scheme part of an incoming URL. |
|
| 78 | - * @param string $url URL so far. The scheme will be |
|
| 79 | - * removed. |
|
| 80 | - * @return string Scheme part or false. |
|
| 81 | - * @access private |
|
| 82 | - */ |
|
| 83 | - function _chompScheme($url) { |
|
| 84 | - if (preg_match('/(.*?):(\/\/)(.*)/', $url, $matches)) { |
|
| 85 | - $url = $matches[2] . $matches[3]; |
|
| 86 | - return $matches[1]; |
|
| 87 | - } |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Extracts the username and password from the |
|
| 93 | - * incoming URL. The // prefix will be reattached |
|
| 94 | - * to the URL after the doublet is extracted. |
|
| 95 | - * @param string $url URL so far. The username and |
|
| 96 | - * password are removed. |
|
| 97 | - * @return array Two item list of username and |
|
| 98 | - * password. Will urldecode() them. |
|
| 99 | - * @access private |
|
| 100 | - */ |
|
| 101 | - function _chompLogin($url) { |
|
| 102 | - $prefix = ''; |
|
| 103 | - if (preg_match('/^(\/\/)(.*)/', $url, $matches)) { |
|
| 104 | - $prefix = $matches[1]; |
|
| 105 | - $url = $matches[2]; |
|
| 106 | - } |
|
| 107 | - if (preg_match('/(.*?)@(.*)/', $url, $matches)) { |
|
| 108 | - $url = $prefix . $matches[2]; |
|
| 109 | - $parts = split(":", $matches[1]); |
|
| 110 | - return array( |
|
| 111 | - urldecode($parts[0]), |
|
| 112 | - isset($parts[1]) ? urldecode($parts[1]) : false); |
|
| 113 | - } |
|
| 114 | - $url = $prefix . $url; |
|
| 115 | - return array(false, false); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Extracts the host part of an incoming URL. |
|
| 120 | - * Includes the port number part. Will extract |
|
| 121 | - * the host if it starts with // or it has |
|
| 122 | - * a top level domain or it has at least two |
|
| 123 | - * dots. |
|
| 124 | - * @param string $url URL so far. The host will be |
|
| 125 | - * removed. |
|
| 126 | - * @return string Host part guess or false. |
|
| 127 | - * @access private |
|
| 128 | - */ |
|
| 129 | - function _chompHost($url) { |
|
| 130 | - if (preg_match('/^(\/\/)(.*?)(\/.*|\?.*|#.*|$)/', $url, $matches)) { |
|
| 131 | - $url = $matches[3]; |
|
| 132 | - return $matches[2]; |
|
| 133 | - } |
|
| 134 | - if (preg_match('/(.*?)(\.\.\/|\.\/|\/|\?|#|$)(.*)/', $url, $matches)) { |
|
| 135 | - $tlds = SimpleUrl::getAllTopLevelDomains(); |
|
| 136 | - if (preg_match('/[a-z0-9\-]+\.(' . $tlds . ')/i', $matches[1])) { |
|
| 137 | - $url = $matches[2] . $matches[3]; |
|
| 138 | - return $matches[1]; |
|
| 139 | - } elseif (preg_match('/[a-z0-9\-]+\.[a-z0-9\-]+\.[a-z0-9\-]+/i', $matches[1])) { |
|
| 140 | - $url = $matches[2] . $matches[3]; |
|
| 141 | - return $matches[1]; |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Extracts the path information from the incoming |
|
| 149 | - * URL. Strips this path from the URL. |
|
| 150 | - * @param string $url URL so far. The host will be |
|
| 151 | - * removed. |
|
| 152 | - * @return string Path part or '/'. |
|
| 153 | - * @access private |
|
| 154 | - */ |
|
| 155 | - function _chompPath($url) { |
|
| 156 | - if (preg_match('/(.*?)(\?|#|$)(.*)/', $url, $matches)) { |
|
| 157 | - $url = $matches[2] . $matches[3]; |
|
| 158 | - return ($matches[1] ? $matches[1] : ''); |
|
| 159 | - } |
|
| 160 | - return ''; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Strips off the request data. |
|
| 165 | - * @param string $url URL so far. The request will be |
|
| 166 | - * removed. |
|
| 167 | - * @return string Raw request part. |
|
| 168 | - * @access private |
|
| 169 | - */ |
|
| 170 | - function _chompRequest($url) { |
|
| 171 | - if (preg_match('/\?(.*?)(#|$)(.*)/', $url, $matches)) { |
|
| 172 | - $url = $matches[2] . $matches[3]; |
|
| 173 | - return $matches[1]; |
|
| 174 | - } |
|
| 175 | - return ''; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Breaks the request down into an object. |
|
| 180 | - * @param string $raw Raw request. |
|
| 181 | - * @return SimpleFormEncoding Parsed data. |
|
| 182 | - * @access private |
|
| 183 | - */ |
|
| 184 | - function _parseRequest($raw) { |
|
| 185 | - $this->_raw = $raw; |
|
| 186 | - $request = new SimpleGetEncoding(); |
|
| 187 | - foreach (split("&", $raw) as $pair) { |
|
| 188 | - if (preg_match('/(.*?)=(.*)/', $pair, $matches)) { |
|
| 189 | - $request->add($matches[1], urldecode($matches[2])); |
|
| 190 | - } elseif ($pair) { |
|
| 191 | - $request->add($pair, ''); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - return $request; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Accessor for protocol part. |
|
| 199 | - * @param string $default Value to use if not present. |
|
| 200 | - * @return string Scheme name, e.g "http". |
|
| 201 | - * @access public |
|
| 202 | - */ |
|
| 203 | - function getScheme($default = false) { |
|
| 204 | - return $this->_scheme ? $this->_scheme : $default; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Accessor for user name. |
|
| 209 | - * @return string Username preceding host. |
|
| 210 | - * @access public |
|
| 211 | - */ |
|
| 212 | - function getUsername() { |
|
| 213 | - return $this->_username; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Accessor for password. |
|
| 218 | - * @return string Password preceding host. |
|
| 219 | - * @access public |
|
| 220 | - */ |
|
| 221 | - function getPassword() { |
|
| 222 | - return $this->_password; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Accessor for hostname and port. |
|
| 227 | - * @param string $default Value to use if not present. |
|
| 228 | - * @return string Hostname only. |
|
| 229 | - * @access public |
|
| 230 | - */ |
|
| 231 | - function getHost($default = false) { |
|
| 232 | - return $this->_host ? $this->_host : $default; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Accessor for top level domain. |
|
| 237 | - * @return string Last part of host. |
|
| 238 | - * @access public |
|
| 239 | - */ |
|
| 240 | - function getTld() { |
|
| 241 | - $path_parts = pathinfo($this->getHost()); |
|
| 242 | - return (isset($path_parts['extension']) ? $path_parts['extension'] : false); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Accessor for port number. |
|
| 247 | - * @return integer TCP/IP port number. |
|
| 248 | - * @access public |
|
| 249 | - */ |
|
| 250 | - function getPort() { |
|
| 251 | - return $this->_port; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Accessor for path. |
|
| 256 | - * @return string Full path including leading slash if implied. |
|
| 257 | - * @access public |
|
| 258 | - */ |
|
| 259 | - function getPath() { |
|
| 260 | - if (! $this->_path && $this->_host) { |
|
| 261 | - return '/'; |
|
| 262 | - } |
|
| 263 | - return $this->_path; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Accessor for page if any. This may be a |
|
| 268 | - * directory name if ambiguious. |
|
| 269 | - * @return Page name. |
|
| 270 | - * @access public |
|
| 271 | - */ |
|
| 272 | - function getPage() { |
|
| 273 | - if (! preg_match('/([^\/]*?)$/', $this->getPath(), $matches)) { |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - return $matches[1]; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Gets the path to the page. |
|
| 281 | - * @return string Path less the page. |
|
| 282 | - * @access public |
|
| 283 | - */ |
|
| 284 | - function getBasePath() { |
|
| 285 | - if (! preg_match('/(.*\/)[^\/]*?$/', $this->getPath(), $matches)) { |
|
| 286 | - return false; |
|
| 287 | - } |
|
| 288 | - return $matches[1]; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Accessor for fragment at end of URL after the "#". |
|
| 293 | - * @return string Part after "#". |
|
| 294 | - * @access public |
|
| 295 | - */ |
|
| 296 | - function getFragment() { |
|
| 297 | - return $this->_fragment; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Sets image coordinates. Set to false to clear |
|
| 302 | - * them. |
|
| 303 | - * @param integer $x Horizontal position. |
|
| 304 | - * @param integer $y Vertical position. |
|
| 305 | - * @access public |
|
| 306 | - */ |
|
| 307 | - function setCoordinates($x = false, $y = false) { |
|
| 308 | - if (($x === false) || ($y === false)) { |
|
| 309 | - $this->_x = $this->_y = false; |
|
| 310 | - return; |
|
| 311 | - } |
|
| 312 | - $this->_x = (integer)$x; |
|
| 313 | - $this->_y = (integer)$y; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Accessor for horizontal image coordinate. |
|
| 318 | - * @return integer X value. |
|
| 319 | - * @access public |
|
| 320 | - */ |
|
| 321 | - function getX() { |
|
| 322 | - return $this->_x; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Accessor for vertical image coordinate. |
|
| 327 | - * @return integer Y value. |
|
| 328 | - * @access public |
|
| 329 | - */ |
|
| 330 | - function getY() { |
|
| 331 | - return $this->_y; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Accessor for current request parameters |
|
| 336 | - * in URL string form. Will return teh original request |
|
| 337 | - * if at all possible even if it doesn't make much |
|
| 338 | - * sense. |
|
| 339 | - * @return string Form is string "?a=1&b=2", etc. |
|
| 340 | - * @access public |
|
| 341 | - */ |
|
| 342 | - function getEncodedRequest() { |
|
| 343 | - if ($this->_raw) { |
|
| 344 | - $encoded = $this->_raw; |
|
| 345 | - } else { |
|
| 346 | - $encoded = $this->_request->asUrlRequest(); |
|
| 347 | - } |
|
| 348 | - if ($encoded) { |
|
| 349 | - return '?' . preg_replace('/^\?/', '', $encoded); |
|
| 350 | - } |
|
| 351 | - return ''; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Adds an additional parameter to the request. |
|
| 356 | - * @param string $key Name of parameter. |
|
| 357 | - * @param string $value Value as string. |
|
| 358 | - * @access public |
|
| 359 | - */ |
|
| 360 | - function addRequestParameter($key, $value) { |
|
| 361 | - $this->_raw = false; |
|
| 362 | - $this->_request->add($key, $value); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Adds additional parameters to the request. |
|
| 367 | - * @param hash/SimpleFormEncoding $parameters Additional |
|
| 368 | - * parameters. |
|
| 369 | - * @access public |
|
| 370 | - */ |
|
| 371 | - function addRequestParameters($parameters) { |
|
| 372 | - $this->_raw = false; |
|
| 373 | - $this->_request->merge($parameters); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Clears down all parameters. |
|
| 378 | - * @access public |
|
| 379 | - */ |
|
| 380 | - function clearRequest() { |
|
| 381 | - $this->_raw = false; |
|
| 382 | - $this->_request = new SimpleGetEncoding(); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Gets the frame target if present. Although |
|
| 387 | - * not strictly part of the URL specification it |
|
| 388 | - * acts as similarily to the browser. |
|
| 389 | - * @return boolean/string Frame name or false if none. |
|
| 390 | - * @access public |
|
| 391 | - */ |
|
| 392 | - function getTarget() { |
|
| 393 | - return $this->_target; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Attaches a frame target. |
|
| 398 | - * @param string $frame Name of frame. |
|
| 399 | - * @access public |
|
| 400 | - */ |
|
| 401 | - function setTarget($frame) { |
|
| 402 | - $this->_raw = false; |
|
| 403 | - $this->_target = $frame; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Renders the URL back into a string. |
|
| 408 | - * @return string URL in canonical form. |
|
| 409 | - * @access public |
|
| 410 | - */ |
|
| 411 | - function asString() { |
|
| 412 | - $scheme = $identity = $host = $path = $encoded = $fragment = ''; |
|
| 413 | - if ($this->_username && $this->_password) { |
|
| 414 | - $identity = $this->_username . ':' . $this->_password . '@'; |
|
| 415 | - } |
|
| 416 | - if ($this->getHost()) { |
|
| 417 | - $scheme = $this->getScheme() ? $this->getScheme() : 'http'; |
|
| 418 | - $host = $this->getHost(); |
|
| 419 | - } |
|
| 420 | - if (substr($this->_path, 0, 1) == '/') { |
|
| 421 | - $path = $this->normalisePath($this->_path); |
|
| 422 | - } |
|
| 423 | - $encoded = $this->getEncodedRequest(); |
|
| 424 | - $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; |
|
| 425 | - $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); |
|
| 426 | - return "$scheme://$identity$host$path$encoded$fragment$coords"; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Replaces unknown sections to turn a relative |
|
| 431 | - * URL into an absolute one. The base URL can |
|
| 432 | - * be either a string or a SimpleUrl object. |
|
| 433 | - * @param string/SimpleUrl $base Base URL. |
|
| 434 | - * @access public |
|
| 435 | - */ |
|
| 436 | - function makeAbsolute($base) { |
|
| 437 | - if (! is_object($base)) { |
|
| 438 | - $base = new SimpleUrl($base); |
|
| 439 | - } |
|
| 440 | - $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme(); |
|
| 441 | - if ($this->getHost()) { |
|
| 442 | - $host = $this->getHost(); |
|
| 443 | - $port = $this->getPort() ? ':' . $this->getPort() : ''; |
|
| 444 | - $identity = $this->getIdentity() ? $this->getIdentity() . '@' : ''; |
|
| 445 | - if (! $identity) { |
|
| 446 | - $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; |
|
| 447 | - } |
|
| 448 | - } else { |
|
| 449 | - $host = $base->getHost(); |
|
| 450 | - $port = $base->getPort() ? ':' . $base->getPort() : ''; |
|
| 451 | - $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; |
|
| 452 | - } |
|
| 453 | - $path = $this->normalisePath($this->_extractAbsolutePath($base)); |
|
| 454 | - $encoded = $this->getEncodedRequest(); |
|
| 455 | - $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; |
|
| 456 | - $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); |
|
| 457 | - return new SimpleUrl("$scheme://$identity$host$port$path$encoded$fragment$coords"); |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * Replaces unknown sections of the path with base parts |
|
| 462 | - * to return a complete absolute one. |
|
| 463 | - * @param string/SimpleUrl $base Base URL. |
|
| 464 | - * @param string Absolute path. |
|
| 465 | - * @access private |
|
| 466 | - */ |
|
| 467 | - function _extractAbsolutePath($base) { |
|
| 468 | - if ($this->getHost()) { |
|
| 469 | - return $this->_path; |
|
| 470 | - } |
|
| 471 | - if (! $this->_isRelativePath($this->_path)) { |
|
| 472 | - return $this->_path; |
|
| 473 | - } |
|
| 474 | - if ($this->_path) { |
|
| 475 | - return $base->getBasePath() . $this->_path; |
|
| 476 | - } |
|
| 477 | - return $base->getPath(); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Simple test to see if a path part is relative. |
|
| 482 | - * @param string $path Path to test. |
|
| 483 | - * @return boolean True if starts with a "/". |
|
| 484 | - * @access private |
|
| 485 | - */ |
|
| 486 | - function _isRelativePath($path) { |
|
| 487 | - return (substr($path, 0, 1) != '/'); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Extracts the username and password for use in rendering |
|
| 492 | - * a URL. |
|
| 493 | - * @return string/boolean Form of username:password or false. |
|
| 494 | - * @access public |
|
| 495 | - */ |
|
| 496 | - function getIdentity() { |
|
| 497 | - if ($this->_username && $this->_password) { |
|
| 498 | - return $this->_username . ':' . $this->_password; |
|
| 499 | - } |
|
| 500 | - return false; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * Replaces . and .. sections of the path. |
|
| 505 | - * @param string $path Unoptimised path. |
|
| 506 | - * @return string Path with dots removed if possible. |
|
| 507 | - * @access public |
|
| 508 | - */ |
|
| 509 | - function normalisePath($path) { |
|
| 510 | - $path = preg_replace('|/[^/]+/\.\./|', '/', $path); |
|
| 511 | - return preg_replace('|/\./|', '/', $path); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * A pipe seperated list of all TLDs that result in two part |
|
| 516 | - * domain names. |
|
| 517 | - * @return string Pipe separated list. |
|
| 518 | - * @access public |
|
| 519 | - * @static |
|
| 520 | - */ |
|
| 521 | - static function getAllTopLevelDomains() { |
|
| 522 | - return 'com|edu|net|org|gov|mil|int|biz|info|name|pro|aero|coop|museum'; |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | 24 | \ No newline at end of file |
| 25 | + */ |
|
| 26 | + class SimpleUrl { |
|
| 27 | + protected $_scheme; |
|
| 28 | + protected $_username; |
|
| 29 | + protected $_password; |
|
| 30 | + protected $_host; |
|
| 31 | + protected $_port; |
|
| 32 | + protected $_path; |
|
| 33 | + protected $_request; |
|
| 34 | + protected $_fragment; |
|
| 35 | + protected $_x; |
|
| 36 | + protected $_y; |
|
| 37 | + protected $_target; |
|
| 38 | + protected $_raw = false; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Constructor. Parses URL into sections. |
|
| 42 | + * @param string $url Incoming URL. |
|
| 43 | + * @access public |
|
| 44 | + */ |
|
| 45 | + function SimpleUrl($url) { |
|
| 46 | + list($x, $y) = $this->_chompCoordinates($url); |
|
| 47 | + $this->setCoordinates($x, $y); |
|
| 48 | + $this->_scheme = $this->_chompScheme($url); |
|
| 49 | + list($this->_username, $this->_password) = $this->_chompLogin($url); |
|
| 50 | + $this->_host = $this->_chompHost($url); |
|
| 51 | + $this->_port = false; |
|
| 52 | + if (preg_match('/(.*?):(.*)/', $this->_host, $host_parts)) { |
|
| 53 | + $this->_host = $host_parts[1]; |
|
| 54 | + $this->_port = (integer)$host_parts[2]; |
|
| 55 | + } |
|
| 56 | + $this->_path = $this->_chompPath($url); |
|
| 57 | + $this->_request = $this->_parseRequest($this->_chompRequest($url)); |
|
| 58 | + $this->_fragment = (strncmp($url, "#", 1) == 0 ? substr($url, 1) : false); |
|
| 59 | + $this->_target = false; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Extracts the X, Y coordinate pair from an image map. |
|
| 64 | + * @param string $url URL so far. The coordinates will be |
|
| 65 | + * removed. |
|
| 66 | + * @return array X, Y as a pair of integers. |
|
| 67 | + * @access private |
|
| 68 | + */ |
|
| 69 | + function _chompCoordinates($url) { |
|
| 70 | + if (preg_match('/(.*)\?(\d+),(\d+)$/', $url, $matches)) { |
|
| 71 | + $url = $matches[1]; |
|
| 72 | + return array((integer)$matches[2], (integer)$matches[3]); |
|
| 73 | + } |
|
| 74 | + return array(false, false); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Extracts the scheme part of an incoming URL. |
|
| 79 | + * @param string $url URL so far. The scheme will be |
|
| 80 | + * removed. |
|
| 81 | + * @return string Scheme part or false. |
|
| 82 | + * @access private |
|
| 83 | + */ |
|
| 84 | + function _chompScheme($url) { |
|
| 85 | + if (preg_match('/(.*?):(\/\/)(.*)/', $url, $matches)) { |
|
| 86 | + $url = $matches[2] . $matches[3]; |
|
| 87 | + return $matches[1]; |
|
| 88 | + } |
|
| 89 | + return false; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Extracts the username and password from the |
|
| 94 | + * incoming URL. The // prefix will be reattached |
|
| 95 | + * to the URL after the doublet is extracted. |
|
| 96 | + * @param string $url URL so far. The username and |
|
| 97 | + * password are removed. |
|
| 98 | + * @return array Two item list of username and |
|
| 99 | + * password. Will urldecode() them. |
|
| 100 | + * @access private |
|
| 101 | + */ |
|
| 102 | + function _chompLogin($url) { |
|
| 103 | + $prefix = ''; |
|
| 104 | + if (preg_match('/^(\/\/)(.*)/', $url, $matches)) { |
|
| 105 | + $prefix = $matches[1]; |
|
| 106 | + $url = $matches[2]; |
|
| 107 | + } |
|
| 108 | + if (preg_match('/(.*?)@(.*)/', $url, $matches)) { |
|
| 109 | + $url = $prefix . $matches[2]; |
|
| 110 | + $parts = split(":", $matches[1]); |
|
| 111 | + return array( |
|
| 112 | + urldecode($parts[0]), |
|
| 113 | + isset($parts[1]) ? urldecode($parts[1]) : false); |
|
| 114 | + } |
|
| 115 | + $url = $prefix . $url; |
|
| 116 | + return array(false, false); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Extracts the host part of an incoming URL. |
|
| 121 | + * Includes the port number part. Will extract |
|
| 122 | + * the host if it starts with // or it has |
|
| 123 | + * a top level domain or it has at least two |
|
| 124 | + * dots. |
|
| 125 | + * @param string $url URL so far. The host will be |
|
| 126 | + * removed. |
|
| 127 | + * @return string Host part guess or false. |
|
| 128 | + * @access private |
|
| 129 | + */ |
|
| 130 | + function _chompHost($url) { |
|
| 131 | + if (preg_match('/^(\/\/)(.*?)(\/.*|\?.*|#.*|$)/', $url, $matches)) { |
|
| 132 | + $url = $matches[3]; |
|
| 133 | + return $matches[2]; |
|
| 134 | + } |
|
| 135 | + if (preg_match('/(.*?)(\.\.\/|\.\/|\/|\?|#|$)(.*)/', $url, $matches)) { |
|
| 136 | + $tlds = SimpleUrl::getAllTopLevelDomains(); |
|
| 137 | + if (preg_match('/[a-z0-9\-]+\.(' . $tlds . ')/i', $matches[1])) { |
|
| 138 | + $url = $matches[2] . $matches[3]; |
|
| 139 | + return $matches[1]; |
|
| 140 | + } elseif (preg_match('/[a-z0-9\-]+\.[a-z0-9\-]+\.[a-z0-9\-]+/i', $matches[1])) { |
|
| 141 | + $url = $matches[2] . $matches[3]; |
|
| 142 | + return $matches[1]; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + return false; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Extracts the path information from the incoming |
|
| 150 | + * URL. Strips this path from the URL. |
|
| 151 | + * @param string $url URL so far. The host will be |
|
| 152 | + * removed. |
|
| 153 | + * @return string Path part or '/'. |
|
| 154 | + * @access private |
|
| 155 | + */ |
|
| 156 | + function _chompPath($url) { |
|
| 157 | + if (preg_match('/(.*?)(\?|#|$)(.*)/', $url, $matches)) { |
|
| 158 | + $url = $matches[2] . $matches[3]; |
|
| 159 | + return ($matches[1] ? $matches[1] : ''); |
|
| 160 | + } |
|
| 161 | + return ''; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Strips off the request data. |
|
| 166 | + * @param string $url URL so far. The request will be |
|
| 167 | + * removed. |
|
| 168 | + * @return string Raw request part. |
|
| 169 | + * @access private |
|
| 170 | + */ |
|
| 171 | + function _chompRequest($url) { |
|
| 172 | + if (preg_match('/\?(.*?)(#|$)(.*)/', $url, $matches)) { |
|
| 173 | + $url = $matches[2] . $matches[3]; |
|
| 174 | + return $matches[1]; |
|
| 175 | + } |
|
| 176 | + return ''; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Breaks the request down into an object. |
|
| 181 | + * @param string $raw Raw request. |
|
| 182 | + * @return SimpleFormEncoding Parsed data. |
|
| 183 | + * @access private |
|
| 184 | + */ |
|
| 185 | + function _parseRequest($raw) { |
|
| 186 | + $this->_raw = $raw; |
|
| 187 | + $request = new SimpleGetEncoding(); |
|
| 188 | + foreach (split("&", $raw) as $pair) { |
|
| 189 | + if (preg_match('/(.*?)=(.*)/', $pair, $matches)) { |
|
| 190 | + $request->add($matches[1], urldecode($matches[2])); |
|
| 191 | + } elseif ($pair) { |
|
| 192 | + $request->add($pair, ''); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + return $request; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Accessor for protocol part. |
|
| 200 | + * @param string $default Value to use if not present. |
|
| 201 | + * @return string Scheme name, e.g "http". |
|
| 202 | + * @access public |
|
| 203 | + */ |
|
| 204 | + function getScheme($default = false) { |
|
| 205 | + return $this->_scheme ? $this->_scheme : $default; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Accessor for user name. |
|
| 210 | + * @return string Username preceding host. |
|
| 211 | + * @access public |
|
| 212 | + */ |
|
| 213 | + function getUsername() { |
|
| 214 | + return $this->_username; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Accessor for password. |
|
| 219 | + * @return string Password preceding host. |
|
| 220 | + * @access public |
|
| 221 | + */ |
|
| 222 | + function getPassword() { |
|
| 223 | + return $this->_password; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Accessor for hostname and port. |
|
| 228 | + * @param string $default Value to use if not present. |
|
| 229 | + * @return string Hostname only. |
|
| 230 | + * @access public |
|
| 231 | + */ |
|
| 232 | + function getHost($default = false) { |
|
| 233 | + return $this->_host ? $this->_host : $default; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Accessor for top level domain. |
|
| 238 | + * @return string Last part of host. |
|
| 239 | + * @access public |
|
| 240 | + */ |
|
| 241 | + function getTld() { |
|
| 242 | + $path_parts = pathinfo($this->getHost()); |
|
| 243 | + return (isset($path_parts['extension']) ? $path_parts['extension'] : false); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Accessor for port number. |
|
| 248 | + * @return integer TCP/IP port number. |
|
| 249 | + * @access public |
|
| 250 | + */ |
|
| 251 | + function getPort() { |
|
| 252 | + return $this->_port; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Accessor for path. |
|
| 257 | + * @return string Full path including leading slash if implied. |
|
| 258 | + * @access public |
|
| 259 | + */ |
|
| 260 | + function getPath() { |
|
| 261 | + if (! $this->_path && $this->_host) { |
|
| 262 | + return '/'; |
|
| 263 | + } |
|
| 264 | + return $this->_path; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Accessor for page if any. This may be a |
|
| 269 | + * directory name if ambiguious. |
|
| 270 | + * @return Page name. |
|
| 271 | + * @access public |
|
| 272 | + */ |
|
| 273 | + function getPage() { |
|
| 274 | + if (! preg_match('/([^\/]*?)$/', $this->getPath(), $matches)) { |
|
| 275 | + return false; |
|
| 276 | + } |
|
| 277 | + return $matches[1]; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Gets the path to the page. |
|
| 282 | + * @return string Path less the page. |
|
| 283 | + * @access public |
|
| 284 | + */ |
|
| 285 | + function getBasePath() { |
|
| 286 | + if (! preg_match('/(.*\/)[^\/]*?$/', $this->getPath(), $matches)) { |
|
| 287 | + return false; |
|
| 288 | + } |
|
| 289 | + return $matches[1]; |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Accessor for fragment at end of URL after the "#". |
|
| 294 | + * @return string Part after "#". |
|
| 295 | + * @access public |
|
| 296 | + */ |
|
| 297 | + function getFragment() { |
|
| 298 | + return $this->_fragment; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Sets image coordinates. Set to false to clear |
|
| 303 | + * them. |
|
| 304 | + * @param integer $x Horizontal position. |
|
| 305 | + * @param integer $y Vertical position. |
|
| 306 | + * @access public |
|
| 307 | + */ |
|
| 308 | + function setCoordinates($x = false, $y = false) { |
|
| 309 | + if (($x === false) || ($y === false)) { |
|
| 310 | + $this->_x = $this->_y = false; |
|
| 311 | + return; |
|
| 312 | + } |
|
| 313 | + $this->_x = (integer)$x; |
|
| 314 | + $this->_y = (integer)$y; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Accessor for horizontal image coordinate. |
|
| 319 | + * @return integer X value. |
|
| 320 | + * @access public |
|
| 321 | + */ |
|
| 322 | + function getX() { |
|
| 323 | + return $this->_x; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Accessor for vertical image coordinate. |
|
| 328 | + * @return integer Y value. |
|
| 329 | + * @access public |
|
| 330 | + */ |
|
| 331 | + function getY() { |
|
| 332 | + return $this->_y; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Accessor for current request parameters |
|
| 337 | + * in URL string form. Will return teh original request |
|
| 338 | + * if at all possible even if it doesn't make much |
|
| 339 | + * sense. |
|
| 340 | + * @return string Form is string "?a=1&b=2", etc. |
|
| 341 | + * @access public |
|
| 342 | + */ |
|
| 343 | + function getEncodedRequest() { |
|
| 344 | + if ($this->_raw) { |
|
| 345 | + $encoded = $this->_raw; |
|
| 346 | + } else { |
|
| 347 | + $encoded = $this->_request->asUrlRequest(); |
|
| 348 | + } |
|
| 349 | + if ($encoded) { |
|
| 350 | + return '?' . preg_replace('/^\?/', '', $encoded); |
|
| 351 | + } |
|
| 352 | + return ''; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Adds an additional parameter to the request. |
|
| 357 | + * @param string $key Name of parameter. |
|
| 358 | + * @param string $value Value as string. |
|
| 359 | + * @access public |
|
| 360 | + */ |
|
| 361 | + function addRequestParameter($key, $value) { |
|
| 362 | + $this->_raw = false; |
|
| 363 | + $this->_request->add($key, $value); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Adds additional parameters to the request. |
|
| 368 | + * @param hash/SimpleFormEncoding $parameters Additional |
|
| 369 | + * parameters. |
|
| 370 | + * @access public |
|
| 371 | + */ |
|
| 372 | + function addRequestParameters($parameters) { |
|
| 373 | + $this->_raw = false; |
|
| 374 | + $this->_request->merge($parameters); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Clears down all parameters. |
|
| 379 | + * @access public |
|
| 380 | + */ |
|
| 381 | + function clearRequest() { |
|
| 382 | + $this->_raw = false; |
|
| 383 | + $this->_request = new SimpleGetEncoding(); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Gets the frame target if present. Although |
|
| 388 | + * not strictly part of the URL specification it |
|
| 389 | + * acts as similarily to the browser. |
|
| 390 | + * @return boolean/string Frame name or false if none. |
|
| 391 | + * @access public |
|
| 392 | + */ |
|
| 393 | + function getTarget() { |
|
| 394 | + return $this->_target; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Attaches a frame target. |
|
| 399 | + * @param string $frame Name of frame. |
|
| 400 | + * @access public |
|
| 401 | + */ |
|
| 402 | + function setTarget($frame) { |
|
| 403 | + $this->_raw = false; |
|
| 404 | + $this->_target = $frame; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Renders the URL back into a string. |
|
| 409 | + * @return string URL in canonical form. |
|
| 410 | + * @access public |
|
| 411 | + */ |
|
| 412 | + function asString() { |
|
| 413 | + $scheme = $identity = $host = $path = $encoded = $fragment = ''; |
|
| 414 | + if ($this->_username && $this->_password) { |
|
| 415 | + $identity = $this->_username . ':' . $this->_password . '@'; |
|
| 416 | + } |
|
| 417 | + if ($this->getHost()) { |
|
| 418 | + $scheme = $this->getScheme() ? $this->getScheme() : 'http'; |
|
| 419 | + $host = $this->getHost(); |
|
| 420 | + } |
|
| 421 | + if (substr($this->_path, 0, 1) == '/') { |
|
| 422 | + $path = $this->normalisePath($this->_path); |
|
| 423 | + } |
|
| 424 | + $encoded = $this->getEncodedRequest(); |
|
| 425 | + $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; |
|
| 426 | + $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); |
|
| 427 | + return "$scheme://$identity$host$path$encoded$fragment$coords"; |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Replaces unknown sections to turn a relative |
|
| 432 | + * URL into an absolute one. The base URL can |
|
| 433 | + * be either a string or a SimpleUrl object. |
|
| 434 | + * @param string/SimpleUrl $base Base URL. |
|
| 435 | + * @access public |
|
| 436 | + */ |
|
| 437 | + function makeAbsolute($base) { |
|
| 438 | + if (! is_object($base)) { |
|
| 439 | + $base = new SimpleUrl($base); |
|
| 440 | + } |
|
| 441 | + $scheme = $this->getScheme() ? $this->getScheme() : $base->getScheme(); |
|
| 442 | + if ($this->getHost()) { |
|
| 443 | + $host = $this->getHost(); |
|
| 444 | + $port = $this->getPort() ? ':' . $this->getPort() : ''; |
|
| 445 | + $identity = $this->getIdentity() ? $this->getIdentity() . '@' : ''; |
|
| 446 | + if (! $identity) { |
|
| 447 | + $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; |
|
| 448 | + } |
|
| 449 | + } else { |
|
| 450 | + $host = $base->getHost(); |
|
| 451 | + $port = $base->getPort() ? ':' . $base->getPort() : ''; |
|
| 452 | + $identity = $base->getIdentity() ? $base->getIdentity() . '@' : ''; |
|
| 453 | + } |
|
| 454 | + $path = $this->normalisePath($this->_extractAbsolutePath($base)); |
|
| 455 | + $encoded = $this->getEncodedRequest(); |
|
| 456 | + $fragment = $this->getFragment() ? '#'. $this->getFragment() : ''; |
|
| 457 | + $coords = $this->getX() === false ? '' : '?' . $this->getX() . ',' . $this->getY(); |
|
| 458 | + return new SimpleUrl("$scheme://$identity$host$port$path$encoded$fragment$coords"); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + /** |
|
| 462 | + * Replaces unknown sections of the path with base parts |
|
| 463 | + * to return a complete absolute one. |
|
| 464 | + * @param string/SimpleUrl $base Base URL. |
|
| 465 | + * @param string Absolute path. |
|
| 466 | + * @access private |
|
| 467 | + */ |
|
| 468 | + function _extractAbsolutePath($base) { |
|
| 469 | + if ($this->getHost()) { |
|
| 470 | + return $this->_path; |
|
| 471 | + } |
|
| 472 | + if (! $this->_isRelativePath($this->_path)) { |
|
| 473 | + return $this->_path; |
|
| 474 | + } |
|
| 475 | + if ($this->_path) { |
|
| 476 | + return $base->getBasePath() . $this->_path; |
|
| 477 | + } |
|
| 478 | + return $base->getPath(); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + /** |
|
| 482 | + * Simple test to see if a path part is relative. |
|
| 483 | + * @param string $path Path to test. |
|
| 484 | + * @return boolean True if starts with a "/". |
|
| 485 | + * @access private |
|
| 486 | + */ |
|
| 487 | + function _isRelativePath($path) { |
|
| 488 | + return (substr($path, 0, 1) != '/'); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Extracts the username and password for use in rendering |
|
| 493 | + * a URL. |
|
| 494 | + * @return string/boolean Form of username:password or false. |
|
| 495 | + * @access public |
|
| 496 | + */ |
|
| 497 | + function getIdentity() { |
|
| 498 | + if ($this->_username && $this->_password) { |
|
| 499 | + return $this->_username . ':' . $this->_password; |
|
| 500 | + } |
|
| 501 | + return false; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Replaces . and .. sections of the path. |
|
| 506 | + * @param string $path Unoptimised path. |
|
| 507 | + * @return string Path with dots removed if possible. |
|
| 508 | + * @access public |
|
| 509 | + */ |
|
| 510 | + function normalisePath($path) { |
|
| 511 | + $path = preg_replace('|/[^/]+/\.\./|', '/', $path); |
|
| 512 | + return preg_replace('|/\./|', '/', $path); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * A pipe seperated list of all TLDs that result in two part |
|
| 517 | + * domain names. |
|
| 518 | + * @return string Pipe separated list. |
|
| 519 | + * @access public |
|
| 520 | + * @static |
|
| 521 | + */ |
|
| 522 | + static function getAllTopLevelDomains() { |
|
| 523 | + return 'com|edu|net|org|gov|mil|int|biz|info|name|pro|aero|coop|museum'; |
|
| 524 | + } |
|
| 525 | + } |
|
| 526 | 526 | \ No newline at end of file |
@@ -1,1454 +1,1454 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * Base include file for SimpleTest. |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage WebTester |
|
| 6 | - * @version $Id: web_tester.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | - */ |
|
| 8 | - |
|
| 9 | - /**#@+ |
|
| 2 | + /** |
|
| 3 | + * Base include file for SimpleTest. |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage WebTester |
|
| 6 | + * @version $Id: web_tester.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | + */ |
|
| 8 | + |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include other SimpleTest class files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/test_case.php'); |
|
| 13 | - require_once(dirname(__FILE__) . '/browser.php'); |
|
| 14 | - require_once(dirname(__FILE__) . '/page.php'); |
|
| 15 | - require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 16 | - /**#@-*/ |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Test for an HTML widget value match. |
|
| 12 | + require_once(dirname(__FILE__) . '/test_case.php'); |
|
| 13 | + require_once(dirname(__FILE__) . '/browser.php'); |
|
| 14 | + require_once(dirname(__FILE__) . '/page.php'); |
|
| 15 | + require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 16 | + /**#@-*/ |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Test for an HTML widget value match. |
|
| 20 | 20 | * @package SimpleTest |
| 21 | 21 | * @subpackage WebTester |
| 22 | - */ |
|
| 23 | - class FieldExpectation extends SimpleExpectation { |
|
| 24 | - protected $_value; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Sets the field value to compare against. |
|
| 28 | - * @param mixed $value Test value to match. Can be an |
|
| 29 | - * expectation for say pattern matching. |
|
| 30 | - * @param string $message Optiona message override. Can use %s as |
|
| 31 | - * a placeholder for the original message. |
|
| 32 | - * @access public |
|
| 33 | - */ |
|
| 34 | - function FieldExpectation($value, $message = '%s') { |
|
| 35 | - $this->SimpleExpectation($message); |
|
| 36 | - if (is_array($value)) { |
|
| 37 | - sort($value); |
|
| 38 | - } |
|
| 39 | - $this->_value = $value; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Tests the expectation. True if it matches |
|
| 44 | - * a string value or an array value in any order. |
|
| 45 | - * @param mixed $compare Comparison value. False for |
|
| 46 | - * an unset field. |
|
| 47 | - * @return boolean True if correct. |
|
| 48 | - * @access public |
|
| 49 | - */ |
|
| 50 | - function test($compare) { |
|
| 51 | - if ($this->_value === false) { |
|
| 52 | - return ($compare === false); |
|
| 53 | - } |
|
| 54 | - if ($this->_isSingle($this->_value)) { |
|
| 55 | - return $this->_testSingle($compare); |
|
| 56 | - } |
|
| 57 | - if (is_array($this->_value)) { |
|
| 58 | - return $this->_testMultiple($compare); |
|
| 59 | - } |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Tests for valid field comparisons with a single option. |
|
| 65 | - * @param mixed $value Value to type check. |
|
| 66 | - * @return boolean True if integer, string or float. |
|
| 67 | - * @access private |
|
| 68 | - */ |
|
| 69 | - function _isSingle($value) { |
|
| 70 | - return is_string($value) || is_integer($value) || is_float($value); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * String comparison for simple field with a single option. |
|
| 75 | - * @param mixed $compare String to test against. |
|
| 76 | - * @returns boolean True if matching. |
|
| 77 | - * @access private |
|
| 78 | - */ |
|
| 79 | - function _testSingle($compare) { |
|
| 80 | - if (is_array($compare) && count($compare) == 1) { |
|
| 81 | - $compare = $compare[0]; |
|
| 82 | - } |
|
| 83 | - if (! $this->_isSingle($compare)) { |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 86 | - return ($this->_value == $compare); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * List comparison for multivalue field. |
|
| 91 | - * @param mixed $compare List in any order to test against. |
|
| 92 | - * @returns boolean True if matching. |
|
| 93 | - * @access private |
|
| 94 | - */ |
|
| 95 | - function _testMultiple($compare) { |
|
| 96 | - if (is_string($compare)) { |
|
| 97 | - $compare = array($compare); |
|
| 98 | - } |
|
| 99 | - if (! is_array($compare)) { |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - sort($compare); |
|
| 103 | - return ($this->_value === $compare); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Returns a human readable test message. |
|
| 108 | - * @param mixed $compare Comparison value. |
|
| 109 | - * @return string Description of success |
|
| 110 | - * or failure. |
|
| 111 | - * @access public |
|
| 112 | - */ |
|
| 113 | - function testMessage($compare) { |
|
| 114 | - $dumper = $this->_getDumper(); |
|
| 115 | - if (is_array($compare)) { |
|
| 116 | - sort($compare); |
|
| 117 | - } |
|
| 118 | - if ($this->test($compare)) { |
|
| 119 | - return "Field expectation [" . $dumper->describeValue($this->_value) . "]"; |
|
| 120 | - } else { |
|
| 121 | - return "Field expectation [" . $dumper->describeValue($this->_value) . |
|
| 122 | - "] fails with [" . |
|
| 123 | - $this->_dumper->describeValue($compare) . "] " . |
|
| 124 | - $this->_dumper->describeDifference($this->_value, $compare); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Test for a specific HTTP header within a header block. |
|
| 22 | + */ |
|
| 23 | + class FieldExpectation extends SimpleExpectation { |
|
| 24 | + protected $_value; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Sets the field value to compare against. |
|
| 28 | + * @param mixed $value Test value to match. Can be an |
|
| 29 | + * expectation for say pattern matching. |
|
| 30 | + * @param string $message Optiona message override. Can use %s as |
|
| 31 | + * a placeholder for the original message. |
|
| 32 | + * @access public |
|
| 33 | + */ |
|
| 34 | + function FieldExpectation($value, $message = '%s') { |
|
| 35 | + $this->SimpleExpectation($message); |
|
| 36 | + if (is_array($value)) { |
|
| 37 | + sort($value); |
|
| 38 | + } |
|
| 39 | + $this->_value = $value; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Tests the expectation. True if it matches |
|
| 44 | + * a string value or an array value in any order. |
|
| 45 | + * @param mixed $compare Comparison value. False for |
|
| 46 | + * an unset field. |
|
| 47 | + * @return boolean True if correct. |
|
| 48 | + * @access public |
|
| 49 | + */ |
|
| 50 | + function test($compare) { |
|
| 51 | + if ($this->_value === false) { |
|
| 52 | + return ($compare === false); |
|
| 53 | + } |
|
| 54 | + if ($this->_isSingle($this->_value)) { |
|
| 55 | + return $this->_testSingle($compare); |
|
| 56 | + } |
|
| 57 | + if (is_array($this->_value)) { |
|
| 58 | + return $this->_testMultiple($compare); |
|
| 59 | + } |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Tests for valid field comparisons with a single option. |
|
| 65 | + * @param mixed $value Value to type check. |
|
| 66 | + * @return boolean True if integer, string or float. |
|
| 67 | + * @access private |
|
| 68 | + */ |
|
| 69 | + function _isSingle($value) { |
|
| 70 | + return is_string($value) || is_integer($value) || is_float($value); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * String comparison for simple field with a single option. |
|
| 75 | + * @param mixed $compare String to test against. |
|
| 76 | + * @returns boolean True if matching. |
|
| 77 | + * @access private |
|
| 78 | + */ |
|
| 79 | + function _testSingle($compare) { |
|
| 80 | + if (is_array($compare) && count($compare) == 1) { |
|
| 81 | + $compare = $compare[0]; |
|
| 82 | + } |
|
| 83 | + if (! $this->_isSingle($compare)) { |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | + return ($this->_value == $compare); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * List comparison for multivalue field. |
|
| 91 | + * @param mixed $compare List in any order to test against. |
|
| 92 | + * @returns boolean True if matching. |
|
| 93 | + * @access private |
|
| 94 | + */ |
|
| 95 | + function _testMultiple($compare) { |
|
| 96 | + if (is_string($compare)) { |
|
| 97 | + $compare = array($compare); |
|
| 98 | + } |
|
| 99 | + if (! is_array($compare)) { |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + sort($compare); |
|
| 103 | + return ($this->_value === $compare); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Returns a human readable test message. |
|
| 108 | + * @param mixed $compare Comparison value. |
|
| 109 | + * @return string Description of success |
|
| 110 | + * or failure. |
|
| 111 | + * @access public |
|
| 112 | + */ |
|
| 113 | + function testMessage($compare) { |
|
| 114 | + $dumper = $this->_getDumper(); |
|
| 115 | + if (is_array($compare)) { |
|
| 116 | + sort($compare); |
|
| 117 | + } |
|
| 118 | + if ($this->test($compare)) { |
|
| 119 | + return "Field expectation [" . $dumper->describeValue($this->_value) . "]"; |
|
| 120 | + } else { |
|
| 121 | + return "Field expectation [" . $dumper->describeValue($this->_value) . |
|
| 122 | + "] fails with [" . |
|
| 123 | + $this->_dumper->describeValue($compare) . "] " . |
|
| 124 | + $this->_dumper->describeDifference($this->_value, $compare); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Test for a specific HTTP header within a header block. |
|
| 131 | 131 | * @package SimpleTest |
| 132 | 132 | * @subpackage WebTester |
| 133 | - */ |
|
| 134 | - class HttpHeaderExpectation extends SimpleExpectation { |
|
| 135 | - protected $_expected_header; |
|
| 136 | - protected $_expected_value; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Sets the field and value to compare against. |
|
| 140 | - * @param string $header Case insenstive trimmed header name. |
|
| 141 | - * @param mixed $value Optional value to compare. If not |
|
| 142 | - * given then any value will match. If |
|
| 143 | - * an expectation object then that will |
|
| 144 | - * be used instead. |
|
| 145 | - * @param string $message Optiona message override. Can use %s as |
|
| 146 | - * a placeholder for the original message. |
|
| 147 | - */ |
|
| 148 | - function HttpHeaderExpectation($header, $value = false, $message = '%s') { |
|
| 149 | - $this->SimpleExpectation($message); |
|
| 150 | - $this->_expected_header = $this->_normaliseHeader($header); |
|
| 151 | - $this->_expected_value = $value; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Accessor for aggregated object. |
|
| 156 | - * @return mixed Expectation set in constructor. |
|
| 157 | - * @access protected |
|
| 158 | - */ |
|
| 159 | - function _getExpectation() { |
|
| 160 | - return $this->_expected_value; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Removes whitespace at ends and case variations. |
|
| 165 | - * @param string $header Name of header. |
|
| 166 | - * @param string Trimmed and lowecased header |
|
| 167 | - * name. |
|
| 168 | - * @access private |
|
| 169 | - */ |
|
| 170 | - function _normaliseHeader($header) { |
|
| 171 | - return strtolower(trim($header)); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Tests the expectation. True if it matches |
|
| 176 | - * a string value or an array value in any order. |
|
| 177 | - * @param mixed $compare Raw header block to search. |
|
| 178 | - * @return boolean True if header present. |
|
| 179 | - * @access public |
|
| 180 | - */ |
|
| 181 | - function test($compare) { |
|
| 182 | - return is_string($this->_findHeader($compare)); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Searches the incoming result. Will extract the matching |
|
| 187 | - * line as text. |
|
| 188 | - * @param mixed $compare Raw header block to search. |
|
| 189 | - * @return string Matching header line. |
|
| 190 | - * @access protected |
|
| 191 | - */ |
|
| 192 | - function _findHeader($compare) { |
|
| 193 | - $lines = split("\r\n", $compare); |
|
| 194 | - foreach ($lines as $line) { |
|
| 195 | - if ($this->_testHeaderLine($line)) { |
|
| 196 | - return $line; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - return false; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Compares a single header line against the expectation. |
|
| 204 | - * @param string $line A single line to compare. |
|
| 205 | - * @return boolean True if matched. |
|
| 206 | - * @access private |
|
| 207 | - */ |
|
| 208 | - function _testHeaderLine($line) { |
|
| 209 | - if (count($parsed = split(':', $line, 2)) < 2) { |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - list($header, $value) = $parsed; |
|
| 213 | - if ($this->_normaliseHeader($header) != $this->_expected_header) { |
|
| 214 | - return false; |
|
| 215 | - } |
|
| 216 | - return $this->_testHeaderValue($value, $this->_expected_value); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Tests the value part of the header. |
|
| 221 | - * @param string $value Value to test. |
|
| 222 | - * @param mixed $expected Value to test against. |
|
| 223 | - * @return boolean True if matched. |
|
| 224 | - * @access protected |
|
| 225 | - */ |
|
| 226 | - function _testHeaderValue($value, $expected) { |
|
| 227 | - if ($expected === false) { |
|
| 228 | - return true; |
|
| 229 | - } |
|
| 230 | - if (SimpleExpectation::isExpectation($expected)) { |
|
| 231 | - return $expected->test(trim($value)); |
|
| 232 | - } |
|
| 233 | - return (trim($value) == trim($expected)); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Returns a human readable test message. |
|
| 238 | - * @param mixed $compare Raw header block to search. |
|
| 239 | - * @return string Description of success |
|
| 240 | - * or failure. |
|
| 241 | - * @access public |
|
| 242 | - */ |
|
| 243 | - function testMessage($compare) { |
|
| 244 | - if (SimpleExpectation::isExpectation($this->_expected_value)) { |
|
| 245 | - $message = $this->_expected_value->testMessage($compare); |
|
| 246 | - } else { |
|
| 247 | - $message = $this->_expected_header . |
|
| 248 | - ($this->_expected_value ? ': ' . $this->_expected_value : ''); |
|
| 249 | - } |
|
| 250 | - if (is_string($line = $this->_findHeader($compare))) { |
|
| 251 | - return "Searching for header [$message] found [$line]"; |
|
| 252 | - } else { |
|
| 253 | - return "Failed to find header [$message]"; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Test for a specific HTTP header within a header block that |
|
| 260 | - * should not be found. |
|
| 133 | + */ |
|
| 134 | + class HttpHeaderExpectation extends SimpleExpectation { |
|
| 135 | + protected $_expected_header; |
|
| 136 | + protected $_expected_value; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Sets the field and value to compare against. |
|
| 140 | + * @param string $header Case insenstive trimmed header name. |
|
| 141 | + * @param mixed $value Optional value to compare. If not |
|
| 142 | + * given then any value will match. If |
|
| 143 | + * an expectation object then that will |
|
| 144 | + * be used instead. |
|
| 145 | + * @param string $message Optiona message override. Can use %s as |
|
| 146 | + * a placeholder for the original message. |
|
| 147 | + */ |
|
| 148 | + function HttpHeaderExpectation($header, $value = false, $message = '%s') { |
|
| 149 | + $this->SimpleExpectation($message); |
|
| 150 | + $this->_expected_header = $this->_normaliseHeader($header); |
|
| 151 | + $this->_expected_value = $value; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Accessor for aggregated object. |
|
| 156 | + * @return mixed Expectation set in constructor. |
|
| 157 | + * @access protected |
|
| 158 | + */ |
|
| 159 | + function _getExpectation() { |
|
| 160 | + return $this->_expected_value; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Removes whitespace at ends and case variations. |
|
| 165 | + * @param string $header Name of header. |
|
| 166 | + * @param string Trimmed and lowecased header |
|
| 167 | + * name. |
|
| 168 | + * @access private |
|
| 169 | + */ |
|
| 170 | + function _normaliseHeader($header) { |
|
| 171 | + return strtolower(trim($header)); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Tests the expectation. True if it matches |
|
| 176 | + * a string value or an array value in any order. |
|
| 177 | + * @param mixed $compare Raw header block to search. |
|
| 178 | + * @return boolean True if header present. |
|
| 179 | + * @access public |
|
| 180 | + */ |
|
| 181 | + function test($compare) { |
|
| 182 | + return is_string($this->_findHeader($compare)); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Searches the incoming result. Will extract the matching |
|
| 187 | + * line as text. |
|
| 188 | + * @param mixed $compare Raw header block to search. |
|
| 189 | + * @return string Matching header line. |
|
| 190 | + * @access protected |
|
| 191 | + */ |
|
| 192 | + function _findHeader($compare) { |
|
| 193 | + $lines = split("\r\n", $compare); |
|
| 194 | + foreach ($lines as $line) { |
|
| 195 | + if ($this->_testHeaderLine($line)) { |
|
| 196 | + return $line; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + return false; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Compares a single header line against the expectation. |
|
| 204 | + * @param string $line A single line to compare. |
|
| 205 | + * @return boolean True if matched. |
|
| 206 | + * @access private |
|
| 207 | + */ |
|
| 208 | + function _testHeaderLine($line) { |
|
| 209 | + if (count($parsed = split(':', $line, 2)) < 2) { |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + list($header, $value) = $parsed; |
|
| 213 | + if ($this->_normaliseHeader($header) != $this->_expected_header) { |
|
| 214 | + return false; |
|
| 215 | + } |
|
| 216 | + return $this->_testHeaderValue($value, $this->_expected_value); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Tests the value part of the header. |
|
| 221 | + * @param string $value Value to test. |
|
| 222 | + * @param mixed $expected Value to test against. |
|
| 223 | + * @return boolean True if matched. |
|
| 224 | + * @access protected |
|
| 225 | + */ |
|
| 226 | + function _testHeaderValue($value, $expected) { |
|
| 227 | + if ($expected === false) { |
|
| 228 | + return true; |
|
| 229 | + } |
|
| 230 | + if (SimpleExpectation::isExpectation($expected)) { |
|
| 231 | + return $expected->test(trim($value)); |
|
| 232 | + } |
|
| 233 | + return (trim($value) == trim($expected)); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Returns a human readable test message. |
|
| 238 | + * @param mixed $compare Raw header block to search. |
|
| 239 | + * @return string Description of success |
|
| 240 | + * or failure. |
|
| 241 | + * @access public |
|
| 242 | + */ |
|
| 243 | + function testMessage($compare) { |
|
| 244 | + if (SimpleExpectation::isExpectation($this->_expected_value)) { |
|
| 245 | + $message = $this->_expected_value->testMessage($compare); |
|
| 246 | + } else { |
|
| 247 | + $message = $this->_expected_header . |
|
| 248 | + ($this->_expected_value ? ': ' . $this->_expected_value : ''); |
|
| 249 | + } |
|
| 250 | + if (is_string($line = $this->_findHeader($compare))) { |
|
| 251 | + return "Searching for header [$message] found [$line]"; |
|
| 252 | + } else { |
|
| 253 | + return "Failed to find header [$message]"; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Test for a specific HTTP header within a header block that |
|
| 260 | + * should not be found. |
|
| 261 | 261 | * @package SimpleTest |
| 262 | 262 | * @subpackage WebTester |
| 263 | - */ |
|
| 264 | - class NoHttpHeaderExpectation extends HttpHeaderExpectation { |
|
| 265 | - protected $_expected_header; |
|
| 266 | - protected $_expected_value; |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Sets the field and value to compare against. |
|
| 270 | - * @param string $unwanted Case insenstive trimmed header name. |
|
| 271 | - * @param string $message Optiona message override. Can use %s as |
|
| 272 | - * a placeholder for the original message. |
|
| 273 | - */ |
|
| 274 | - function NoHttpHeaderExpectation($unwanted, $message = '%s') { |
|
| 275 | - $this->HttpHeaderExpectation($unwanted, false, $message); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Tests that the unwanted header is not found. |
|
| 280 | - * @param mixed $compare Raw header block to search. |
|
| 281 | - * @return boolean True if header present. |
|
| 282 | - * @access public |
|
| 283 | - */ |
|
| 284 | - function test($compare) { |
|
| 285 | - return ($this->_findHeader($compare) === false); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns a human readable test message. |
|
| 290 | - * @param mixed $compare Raw header block to search. |
|
| 291 | - * @return string Description of success |
|
| 292 | - * or failure. |
|
| 293 | - * @access public |
|
| 294 | - */ |
|
| 295 | - function testMessage($compare) { |
|
| 296 | - $expectation = $this->_getExpectation(); |
|
| 297 | - if (is_string($line = $this->_findHeader($compare))) { |
|
| 298 | - return "Found unwanted header [$expectation] with [$line]"; |
|
| 299 | - } else { |
|
| 300 | - return "Did not find unwanted header [$expectation]"; |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Test for a text substring. |
|
| 263 | + */ |
|
| 264 | + class NoHttpHeaderExpectation extends HttpHeaderExpectation { |
|
| 265 | + protected $_expected_header; |
|
| 266 | + protected $_expected_value; |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Sets the field and value to compare against. |
|
| 270 | + * @param string $unwanted Case insenstive trimmed header name. |
|
| 271 | + * @param string $message Optiona message override. Can use %s as |
|
| 272 | + * a placeholder for the original message. |
|
| 273 | + */ |
|
| 274 | + function NoHttpHeaderExpectation($unwanted, $message = '%s') { |
|
| 275 | + $this->HttpHeaderExpectation($unwanted, false, $message); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Tests that the unwanted header is not found. |
|
| 280 | + * @param mixed $compare Raw header block to search. |
|
| 281 | + * @return boolean True if header present. |
|
| 282 | + * @access public |
|
| 283 | + */ |
|
| 284 | + function test($compare) { |
|
| 285 | + return ($this->_findHeader($compare) === false); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns a human readable test message. |
|
| 290 | + * @param mixed $compare Raw header block to search. |
|
| 291 | + * @return string Description of success |
|
| 292 | + * or failure. |
|
| 293 | + * @access public |
|
| 294 | + */ |
|
| 295 | + function testMessage($compare) { |
|
| 296 | + $expectation = $this->_getExpectation(); |
|
| 297 | + if (is_string($line = $this->_findHeader($compare))) { |
|
| 298 | + return "Found unwanted header [$expectation] with [$line]"; |
|
| 299 | + } else { |
|
| 300 | + return "Did not find unwanted header [$expectation]"; |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Test for a text substring. |
|
| 307 | 307 | * @package SimpleTest |
| 308 | 308 | * @subpackage UnitTester |
| 309 | - */ |
|
| 310 | - class TextExpectation extends SimpleExpectation { |
|
| 311 | - protected $_substring; |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Sets the value to compare against. |
|
| 315 | - * @param string $substring Text to search for. |
|
| 316 | - * @param string $message Customised message on failure. |
|
| 317 | - * @access public |
|
| 318 | - */ |
|
| 319 | - function TextExpectation($substring, $message = '%s') { |
|
| 320 | - $this->SimpleExpectation($message); |
|
| 321 | - $this->_substring = $substring; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Accessor for the substring. |
|
| 326 | - * @return string Text to match. |
|
| 327 | - * @access protected |
|
| 328 | - */ |
|
| 329 | - function _getSubstring() { |
|
| 330 | - return $this->_substring; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Tests the expectation. True if the text contains the |
|
| 335 | - * substring. |
|
| 336 | - * @param string $compare Comparison value. |
|
| 337 | - * @return boolean True if correct. |
|
| 338 | - * @access public |
|
| 339 | - */ |
|
| 340 | - function test($compare) { |
|
| 341 | - return (strpos($compare, $this->_substring) !== false); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Returns a human readable test message. |
|
| 346 | - * @param mixed $compare Comparison value. |
|
| 347 | - * @return string Description of success |
|
| 348 | - * or failure. |
|
| 349 | - * @access public |
|
| 350 | - */ |
|
| 351 | - function testMessage($compare) { |
|
| 352 | - if ($this->test($compare)) { |
|
| 353 | - return $this->_describeTextMatch($this->_getSubstring(), $compare); |
|
| 354 | - } else { |
|
| 355 | - $dumper = $this->_getDumper(); |
|
| 356 | - return "Text [" . $this->_getSubstring() . |
|
| 357 | - "] not detected in [" . |
|
| 358 | - $dumper->describeValue($compare) . "]"; |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Describes a pattern match including the string |
|
| 364 | - * found and it's position. |
|
| 365 | - * @param string $substring Text to search for. |
|
| 366 | - * @param string $subject Subject to search. |
|
| 367 | - * @access protected |
|
| 368 | - */ |
|
| 369 | - function _describeTextMatch($substring, $subject) { |
|
| 370 | - $position = strpos($subject, $substring); |
|
| 371 | - $dumper = $this->_getDumper(); |
|
| 372 | - return "Text [$substring] detected at character [$position] in [" . |
|
| 373 | - $dumper->describeValue($subject) . "] in region [" . |
|
| 374 | - $dumper->clipString($subject, 100, $position) . "]"; |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * Fail if a substring is detected within the |
|
| 380 | - * comparison text. |
|
| 309 | + */ |
|
| 310 | + class TextExpectation extends SimpleExpectation { |
|
| 311 | + protected $_substring; |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Sets the value to compare against. |
|
| 315 | + * @param string $substring Text to search for. |
|
| 316 | + * @param string $message Customised message on failure. |
|
| 317 | + * @access public |
|
| 318 | + */ |
|
| 319 | + function TextExpectation($substring, $message = '%s') { |
|
| 320 | + $this->SimpleExpectation($message); |
|
| 321 | + $this->_substring = $substring; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Accessor for the substring. |
|
| 326 | + * @return string Text to match. |
|
| 327 | + * @access protected |
|
| 328 | + */ |
|
| 329 | + function _getSubstring() { |
|
| 330 | + return $this->_substring; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Tests the expectation. True if the text contains the |
|
| 335 | + * substring. |
|
| 336 | + * @param string $compare Comparison value. |
|
| 337 | + * @return boolean True if correct. |
|
| 338 | + * @access public |
|
| 339 | + */ |
|
| 340 | + function test($compare) { |
|
| 341 | + return (strpos($compare, $this->_substring) !== false); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Returns a human readable test message. |
|
| 346 | + * @param mixed $compare Comparison value. |
|
| 347 | + * @return string Description of success |
|
| 348 | + * or failure. |
|
| 349 | + * @access public |
|
| 350 | + */ |
|
| 351 | + function testMessage($compare) { |
|
| 352 | + if ($this->test($compare)) { |
|
| 353 | + return $this->_describeTextMatch($this->_getSubstring(), $compare); |
|
| 354 | + } else { |
|
| 355 | + $dumper = $this->_getDumper(); |
|
| 356 | + return "Text [" . $this->_getSubstring() . |
|
| 357 | + "] not detected in [" . |
|
| 358 | + $dumper->describeValue($compare) . "]"; |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Describes a pattern match including the string |
|
| 364 | + * found and it's position. |
|
| 365 | + * @param string $substring Text to search for. |
|
| 366 | + * @param string $subject Subject to search. |
|
| 367 | + * @access protected |
|
| 368 | + */ |
|
| 369 | + function _describeTextMatch($substring, $subject) { |
|
| 370 | + $position = strpos($subject, $substring); |
|
| 371 | + $dumper = $this->_getDumper(); |
|
| 372 | + return "Text [$substring] detected at character [$position] in [" . |
|
| 373 | + $dumper->describeValue($subject) . "] in region [" . |
|
| 374 | + $dumper->clipString($subject, 100, $position) . "]"; |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * Fail if a substring is detected within the |
|
| 380 | + * comparison text. |
|
| 381 | 381 | * @package SimpleTest |
| 382 | 382 | * @subpackage UnitTester |
| 383 | - */ |
|
| 384 | - class NoTextExpectation extends TextExpectation { |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Sets the reject pattern |
|
| 388 | - * @param string $substring Text to search for. |
|
| 389 | - * @param string $message Customised message on failure. |
|
| 390 | - * @access public |
|
| 391 | - */ |
|
| 392 | - function NoTextExpectation($substring, $message = '%s') { |
|
| 393 | - $this->TextExpectation($substring, $message); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Tests the expectation. False if the substring appears |
|
| 398 | - * in the text. |
|
| 399 | - * @param string $compare Comparison value. |
|
| 400 | - * @return boolean True if correct. |
|
| 401 | - * @access public |
|
| 402 | - */ |
|
| 403 | - function test($compare) { |
|
| 404 | - return ! parent::test($compare); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Returns a human readable test message. |
|
| 409 | - * @param string $compare Comparison value. |
|
| 410 | - * @return string Description of success |
|
| 411 | - * or failure. |
|
| 412 | - * @access public |
|
| 413 | - */ |
|
| 414 | - function testMessage($compare) { |
|
| 415 | - if ($this->test($compare)) { |
|
| 416 | - $dumper = $this->_getDumper(); |
|
| 417 | - return "Text [" . $this->_getSubstring() . |
|
| 418 | - "] not detected in [" . |
|
| 419 | - $dumper->describeValue($compare) . "]"; |
|
| 420 | - } else { |
|
| 421 | - return $this->_describeTextMatch($this->_getSubstring(), $compare); |
|
| 422 | - } |
|
| 423 | - } |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Test case for testing of web pages. Allows |
|
| 428 | - * fetching of pages, parsing of HTML and |
|
| 429 | - * submitting forms. |
|
| 383 | + */ |
|
| 384 | + class NoTextExpectation extends TextExpectation { |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Sets the reject pattern |
|
| 388 | + * @param string $substring Text to search for. |
|
| 389 | + * @param string $message Customised message on failure. |
|
| 390 | + * @access public |
|
| 391 | + */ |
|
| 392 | + function NoTextExpectation($substring, $message = '%s') { |
|
| 393 | + $this->TextExpectation($substring, $message); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Tests the expectation. False if the substring appears |
|
| 398 | + * in the text. |
|
| 399 | + * @param string $compare Comparison value. |
|
| 400 | + * @return boolean True if correct. |
|
| 401 | + * @access public |
|
| 402 | + */ |
|
| 403 | + function test($compare) { |
|
| 404 | + return ! parent::test($compare); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Returns a human readable test message. |
|
| 409 | + * @param string $compare Comparison value. |
|
| 410 | + * @return string Description of success |
|
| 411 | + * or failure. |
|
| 412 | + * @access public |
|
| 413 | + */ |
|
| 414 | + function testMessage($compare) { |
|
| 415 | + if ($this->test($compare)) { |
|
| 416 | + $dumper = $this->_getDumper(); |
|
| 417 | + return "Text [" . $this->_getSubstring() . |
|
| 418 | + "] not detected in [" . |
|
| 419 | + $dumper->describeValue($compare) . "]"; |
|
| 420 | + } else { |
|
| 421 | + return $this->_describeTextMatch($this->_getSubstring(), $compare); |
|
| 422 | + } |
|
| 423 | + } |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Test case for testing of web pages. Allows |
|
| 428 | + * fetching of pages, parsing of HTML and |
|
| 429 | + * submitting forms. |
|
| 430 | 430 | * @package SimpleTest |
| 431 | 431 | * @subpackage WebTester |
| 432 | - */ |
|
| 433 | - class WebTestCase extends SimpleTestCase { |
|
| 434 | - protected $_browser; |
|
| 435 | - protected $_ignore_errors = false; |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * Creates an empty test case. Should be subclassed |
|
| 439 | - * with test methods for a functional test case. |
|
| 440 | - * @param string $label Name of test case. Will use |
|
| 441 | - * the class name if none specified. |
|
| 442 | - * @access public |
|
| 443 | - */ |
|
| 444 | - function WebTestCase($label = false) { |
|
| 445 | - $this->SimpleTestCase($label); |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Announces the start of the test. |
|
| 450 | - * @param string $method Test method just started. |
|
| 451 | - * @access public |
|
| 452 | - */ |
|
| 453 | - function before($method) { |
|
| 454 | - parent::before($method); |
|
| 455 | - $this->setBrowser($this->createBrowser()); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Announces the end of the test. Includes private clean up. |
|
| 460 | - * @param string $method Test method just finished. |
|
| 461 | - * @access public |
|
| 462 | - */ |
|
| 463 | - function after($method) { |
|
| 464 | - $this->unsetBrowser(); |
|
| 465 | - parent::after($method); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Gets a current browser reference for setting |
|
| 470 | - * special expectations or for detailed |
|
| 471 | - * examination of page fetches. |
|
| 472 | - * @return SimpleBrowser Current test browser object. |
|
| 473 | - * @access public |
|
| 474 | - */ |
|
| 475 | - function &getBrowser() { |
|
| 476 | - return $this->_browser; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Gets a current browser reference for setting |
|
| 481 | - * special expectations or for detailed |
|
| 482 | - * examination of page fetches. |
|
| 483 | - * @param SimpleBrowser $browser New test browser object. |
|
| 484 | - * @access public |
|
| 485 | - */ |
|
| 486 | - function setBrowser($browser) { |
|
| 487 | - return $this->_browser = $browser; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Clears the current browser reference to help the |
|
| 492 | - * PHP garbage collector. |
|
| 493 | - * @access public |
|
| 494 | - */ |
|
| 495 | - function unsetBrowser() { |
|
| 496 | - unset($this->_browser); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - /** |
|
| 500 | - * Creates a new default web browser object. |
|
| 501 | - * Will be cleared at the end of the test method. |
|
| 502 | - * @return TestBrowser New browser. |
|
| 503 | - * @access public |
|
| 504 | - */ |
|
| 505 | - function &createBrowser() { |
|
| 506 | - $browser = new SimpleBrowser(); |
|
| 507 | - return $browser; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Gets the last response error. |
|
| 512 | - * @return string Last low level HTTP error. |
|
| 513 | - * @access public |
|
| 514 | - */ |
|
| 515 | - function getTransportError() { |
|
| 516 | - return $this->_browser->getTransportError(); |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Accessor for the currently selected URL. |
|
| 521 | - * @return string Current location or false if |
|
| 522 | - * no page yet fetched. |
|
| 523 | - * @access public |
|
| 524 | - */ |
|
| 525 | - function getUrl() { |
|
| 526 | - return $this->_browser->getUrl(); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Dumps the current request for debugging. |
|
| 531 | - * @access public |
|
| 532 | - */ |
|
| 533 | - function showRequest() { |
|
| 534 | - $this->dump($this->_browser->getRequest()); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * Dumps the current HTTP headers for debugging. |
|
| 539 | - * @access public |
|
| 540 | - */ |
|
| 541 | - function showHeaders() { |
|
| 542 | - $this->dump($this->_browser->getHeaders()); |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Dumps the current HTML source for debugging. |
|
| 547 | - * @access public |
|
| 548 | - */ |
|
| 549 | - function showSource() { |
|
| 550 | - $this->dump($this->_browser->getContent()); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * Dumps the visible text only for debugging. |
|
| 555 | - * @access public |
|
| 556 | - */ |
|
| 557 | - function showText() { |
|
| 558 | - $this->dump(wordwrap($this->_browser->getContentAsText(), 80)); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * Simulates the closing and reopening of the browser. |
|
| 563 | - * Temporary cookies will be discarded and timed |
|
| 564 | - * cookies will be expired if later than the |
|
| 565 | - * specified time. |
|
| 566 | - * @param string/integer $date Time when session restarted. |
|
| 567 | - * If ommitted then all persistent |
|
| 568 | - * cookies are kept. Time is either |
|
| 569 | - * Cookie format string or timestamp. |
|
| 570 | - * @access public |
|
| 571 | - */ |
|
| 572 | - function restart($date = false) { |
|
| 573 | - if ($date === false) { |
|
| 574 | - $date = time(); |
|
| 575 | - } |
|
| 576 | - $this->_browser->restart($date); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Moves cookie expiry times back into the past. |
|
| 581 | - * Useful for testing timeouts and expiries. |
|
| 582 | - * @param integer $interval Amount to age in seconds. |
|
| 583 | - * @access public |
|
| 584 | - */ |
|
| 585 | - function ageCookies($interval) { |
|
| 586 | - $this->_browser->ageCookies($interval); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * Disables frames support. Frames will not be fetched |
|
| 591 | - * and the frameset page will be used instead. |
|
| 592 | - * @access public |
|
| 593 | - */ |
|
| 594 | - function ignoreFrames() { |
|
| 595 | - $this->_browser->ignoreFrames(); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Switches off cookie sending and recieving. |
|
| 600 | - * @access public |
|
| 601 | - */ |
|
| 602 | - function ignoreCookies() { |
|
| 603 | - $this->_browser->ignoreCookies(); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Skips errors for the next request only. You might |
|
| 608 | - * want to confirm that a page is unreachable for |
|
| 609 | - * example. |
|
| 610 | - * @access public |
|
| 611 | - */ |
|
| 612 | - function ignoreErrors() { |
|
| 613 | - $this->_ignore_errors = true; |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Issues a fail if there is a transport error anywhere |
|
| 618 | - * in the current frameset. Only one such error is |
|
| 619 | - * reported. |
|
| 620 | - * @param string/boolean $result HTML or failure. |
|
| 621 | - * @return string/boolean $result Passes through result. |
|
| 622 | - * @access private |
|
| 623 | - */ |
|
| 624 | - function _failOnError($result) { |
|
| 625 | - if (! $this->_ignore_errors) { |
|
| 626 | - if ($error = $this->_browser->getTransportError()) { |
|
| 627 | - $this->fail($error); |
|
| 628 | - } |
|
| 629 | - } |
|
| 630 | - $this->_ignore_errors = false; |
|
| 631 | - return $result; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Adds a header to every fetch. |
|
| 636 | - * @param string $header Header line to add to every |
|
| 637 | - * request until cleared. |
|
| 638 | - * @access public |
|
| 639 | - */ |
|
| 640 | - function addHeader($header) { |
|
| 641 | - $this->_browser->addHeader($header); |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Sets the maximum number of redirects before |
|
| 646 | - * the web page is loaded regardless. |
|
| 647 | - * @param integer $max Maximum hops. |
|
| 648 | - * @access public |
|
| 649 | - */ |
|
| 650 | - function setMaximumRedirects($max) { |
|
| 651 | - if (! $this->_browser) { |
|
| 652 | - trigger_error( |
|
| 653 | - 'Can only set maximum redirects in a test method, setUp() or tearDown()'); |
|
| 654 | - } |
|
| 655 | - $this->_browser->setMaximumRedirects($max); |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - /** |
|
| 659 | - * Sets the socket timeout for opening a connection and |
|
| 660 | - * receiving at least one byte of information. |
|
| 661 | - * @param integer $timeout Maximum time in seconds. |
|
| 662 | - * @access public |
|
| 663 | - */ |
|
| 664 | - function setConnectionTimeout($timeout) { |
|
| 665 | - $this->_browser->setConnectionTimeout($timeout); |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Sets proxy to use on all requests for when |
|
| 670 | - * testing from behind a firewall. Set URL |
|
| 671 | - * to false to disable. |
|
| 672 | - * @param string $proxy Proxy URL. |
|
| 673 | - * @param string $username Proxy username for authentication. |
|
| 674 | - * @param string $password Proxy password for authentication. |
|
| 675 | - * @access public |
|
| 676 | - */ |
|
| 677 | - function useProxy($proxy, $username = false, $password = false) { |
|
| 678 | - $this->_browser->useProxy($proxy, $username, $password); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Fetches a page into the page buffer. If |
|
| 683 | - * there is no base for the URL then the |
|
| 684 | - * current base URL is used. After the fetch |
|
| 685 | - * the base URL reflects the new location. |
|
| 686 | - * @param string $url URL to fetch. |
|
| 687 | - * @param hash $parameters Optional additional GET data. |
|
| 688 | - * @return boolean/string Raw page on success. |
|
| 689 | - * @access public |
|
| 690 | - */ |
|
| 691 | - function get($url, $parameters = false) { |
|
| 692 | - return $this->_failOnError($this->_browser->get($url, $parameters)); |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - /** |
|
| 696 | - * Fetches a page by POST into the page buffer. |
|
| 697 | - * If there is no base for the URL then the |
|
| 698 | - * current base URL is used. After the fetch |
|
| 699 | - * the base URL reflects the new location. |
|
| 700 | - * @param string $url URL to fetch. |
|
| 701 | - * @param hash $parameters Optional additional GET data. |
|
| 702 | - * @return boolean/string Raw page on success. |
|
| 703 | - * @access public |
|
| 704 | - */ |
|
| 705 | - function post($url, $parameters = false) { |
|
| 706 | - return $this->_failOnError($this->_browser->post($url, $parameters)); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - /** |
|
| 710 | - * Does a HTTP HEAD fetch, fetching only the page |
|
| 711 | - * headers. The current base URL is unchanged by this. |
|
| 712 | - * @param string $url URL to fetch. |
|
| 713 | - * @param hash $parameters Optional additional GET data. |
|
| 714 | - * @return boolean True on success. |
|
| 715 | - * @access public |
|
| 716 | - */ |
|
| 717 | - function head($url, $parameters = false) { |
|
| 718 | - return $this->_failOnError($this->_browser->head($url, $parameters)); |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - /** |
|
| 722 | - * Equivalent to hitting the retry button on the |
|
| 723 | - * browser. Will attempt to repeat the page fetch. |
|
| 724 | - * @return boolean True if fetch succeeded. |
|
| 725 | - * @access public |
|
| 726 | - */ |
|
| 727 | - function retry() { |
|
| 728 | - return $this->_failOnError($this->_browser->retry()); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * Equivalent to hitting the back button on the |
|
| 733 | - * browser. |
|
| 734 | - * @return boolean True if history entry and |
|
| 735 | - * fetch succeeded. |
|
| 736 | - * @access public |
|
| 737 | - */ |
|
| 738 | - function back() { |
|
| 739 | - return $this->_failOnError($this->_browser->back()); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - /** |
|
| 743 | - * Equivalent to hitting the forward button on the |
|
| 744 | - * browser. |
|
| 745 | - * @return boolean True if history entry and |
|
| 746 | - * fetch succeeded. |
|
| 747 | - * @access public |
|
| 748 | - */ |
|
| 749 | - function forward() { |
|
| 750 | - return $this->_failOnError($this->_browser->forward()); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Retries a request after setting the authentication |
|
| 755 | - * for the current realm. |
|
| 756 | - * @param string $username Username for realm. |
|
| 757 | - * @param string $password Password for realm. |
|
| 758 | - * @return boolean/string HTML on successful fetch. Note |
|
| 759 | - * that authentication may still have |
|
| 760 | - * failed. |
|
| 761 | - * @access public |
|
| 762 | - */ |
|
| 763 | - function authenticate($username, $password) { |
|
| 764 | - return $this->_failOnError( |
|
| 765 | - $this->_browser->authenticate($username, $password)); |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - /** |
|
| 769 | - * Gets the cookie value for the current browser context. |
|
| 770 | - * @param string $name Name of cookie. |
|
| 771 | - * @return string Value of cookie or false if unset. |
|
| 772 | - * @access public |
|
| 773 | - */ |
|
| 774 | - function getCookie($name) { |
|
| 775 | - return $this->_browser->getCurrentCookieValue($name); |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - /** |
|
| 779 | - * Sets a cookie in the current browser. |
|
| 780 | - * @param string $name Name of cookie. |
|
| 781 | - * @param string $value Cookie value. |
|
| 782 | - * @param string $host Host upon which the cookie is valid. |
|
| 783 | - * @param string $path Cookie path if not host wide. |
|
| 784 | - * @param string $expiry Expiry date. |
|
| 785 | - * @access public |
|
| 786 | - */ |
|
| 787 | - function setCookie($name, $value, $host = false, $path = "/", $expiry = false) { |
|
| 788 | - $this->_browser->setCookie($name, $value, $host, $path, $expiry); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Accessor for current frame focus. Will be |
|
| 793 | - * false if no frame has focus. |
|
| 794 | - * @return integer/string/boolean Label if any, otherwise |
|
| 795 | - * the position in the frameset |
|
| 796 | - * or false if none. |
|
| 797 | - * @access public |
|
| 798 | - */ |
|
| 799 | - function getFrameFocus() { |
|
| 800 | - return $this->_browser->getFrameFocus(); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * Sets the focus by index. The integer index starts from 1. |
|
| 805 | - * @param integer $choice Chosen frame. |
|
| 806 | - * @return boolean True if frame exists. |
|
| 807 | - * @access public |
|
| 808 | - */ |
|
| 809 | - function setFrameFocusByIndex($choice) { |
|
| 810 | - return $this->_browser->setFrameFocusByIndex($choice); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - /** |
|
| 814 | - * Sets the focus by name. |
|
| 815 | - * @param string $name Chosen frame. |
|
| 816 | - * @return boolean True if frame exists. |
|
| 817 | - * @access public |
|
| 818 | - */ |
|
| 819 | - function setFrameFocus($name) { |
|
| 820 | - return $this->_browser->setFrameFocus($name); |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Clears the frame focus. All frames will be searched |
|
| 825 | - * for content. |
|
| 826 | - * @access public |
|
| 827 | - */ |
|
| 828 | - function clearFrameFocus() { |
|
| 829 | - return $this->_browser->clearFrameFocus(); |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Clicks a visible text item. Will first try buttons, |
|
| 834 | - * then links and then images. |
|
| 835 | - * @param string $label Visible text or alt text. |
|
| 836 | - * @return string/boolean Raw page or false. |
|
| 837 | - * @access public |
|
| 838 | - */ |
|
| 839 | - function click($label) { |
|
| 840 | - return $this->_failOnError($this->_browser->click($label)); |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - /** |
|
| 844 | - * Clicks the submit button by label. The owning |
|
| 845 | - * form will be submitted by this. |
|
| 846 | - * @param string $label Button label. An unlabeled |
|
| 847 | - * button can be triggered by 'Submit'. |
|
| 848 | - * @param hash $additional Additional form values. |
|
| 849 | - * @return boolean/string Page on success, else false. |
|
| 850 | - * @access public |
|
| 851 | - */ |
|
| 852 | - function clickSubmit($label = 'Submit', $additional = false) { |
|
| 853 | - return $this->_failOnError( |
|
| 854 | - $this->_browser->clickSubmit($label, $additional)); |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Clicks the submit button by name attribute. The owning |
|
| 859 | - * form will be submitted by this. |
|
| 860 | - * @param string $name Name attribute of button. |
|
| 861 | - * @param hash $additional Additional form values. |
|
| 862 | - * @return boolean/string Page on success. |
|
| 863 | - * @access public |
|
| 864 | - */ |
|
| 865 | - function clickSubmitByName($name, $additional = false) { |
|
| 866 | - return $this->_failOnError( |
|
| 867 | - $this->_browser->clickSubmitByName($name, $additional)); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * Clicks the submit button by ID attribute. The owning |
|
| 872 | - * form will be submitted by this. |
|
| 873 | - * @param string $id ID attribute of button. |
|
| 874 | - * @param hash $additional Additional form values. |
|
| 875 | - * @return boolean/string Page on success. |
|
| 876 | - * @access public |
|
| 877 | - */ |
|
| 878 | - function clickSubmitById($id, $additional = false) { |
|
| 879 | - return $this->_failOnError( |
|
| 880 | - $this->_browser->clickSubmitById($id, $additional)); |
|
| 881 | - } |
|
| 882 | - |
|
| 883 | - /** |
|
| 884 | - * Clicks the submit image by some kind of label. Usually |
|
| 885 | - * the alt tag or the nearest equivalent. The owning |
|
| 886 | - * form will be submitted by this. Clicking outside of |
|
| 887 | - * the boundary of the coordinates will result in |
|
| 888 | - * a failure. |
|
| 889 | - * @param string $label Alt attribute of button. |
|
| 890 | - * @param integer $x X-coordinate of imaginary click. |
|
| 891 | - * @param integer $y Y-coordinate of imaginary click. |
|
| 892 | - * @param hash $additional Additional form values. |
|
| 893 | - * @return boolean/string Page on success. |
|
| 894 | - * @access public |
|
| 895 | - */ |
|
| 896 | - function clickImage($label, $x = 1, $y = 1, $additional = false) { |
|
| 897 | - return $this->_failOnError( |
|
| 898 | - $this->_browser->clickImage($label, $x, $y, $additional)); |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - /** |
|
| 902 | - * Clicks the submit image by the name. Usually |
|
| 903 | - * the alt tag or the nearest equivalent. The owning |
|
| 904 | - * form will be submitted by this. Clicking outside of |
|
| 905 | - * the boundary of the coordinates will result in |
|
| 906 | - * a failure. |
|
| 907 | - * @param string $name Name attribute of button. |
|
| 908 | - * @param integer $x X-coordinate of imaginary click. |
|
| 909 | - * @param integer $y Y-coordinate of imaginary click. |
|
| 910 | - * @param hash $additional Additional form values. |
|
| 911 | - * @return boolean/string Page on success. |
|
| 912 | - * @access public |
|
| 913 | - */ |
|
| 914 | - function clickImageByName($name, $x = 1, $y = 1, $additional = false) { |
|
| 915 | - return $this->_failOnError( |
|
| 916 | - $this->_browser->clickImageByName($name, $x, $y, $additional)); |
|
| 917 | - } |
|
| 918 | - |
|
| 919 | - /** |
|
| 920 | - * Clicks the submit image by ID attribute. The owning |
|
| 921 | - * form will be submitted by this. Clicking outside of |
|
| 922 | - * the boundary of the coordinates will result in |
|
| 923 | - * a failure. |
|
| 924 | - * @param integer/string $id ID attribute of button. |
|
| 925 | - * @param integer $x X-coordinate of imaginary click. |
|
| 926 | - * @param integer $y Y-coordinate of imaginary click. |
|
| 927 | - * @param hash $additional Additional form values. |
|
| 928 | - * @return boolean/string Page on success. |
|
| 929 | - * @access public |
|
| 930 | - */ |
|
| 931 | - function clickImageById($id, $x = 1, $y = 1, $additional = false) { |
|
| 932 | - return $this->_failOnError( |
|
| 933 | - $this->_browser->clickImageById($id, $x, $y, $additional)); |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - /** |
|
| 937 | - * Submits a form by the ID. |
|
| 938 | - * @param string $id Form ID. No button information |
|
| 939 | - * is submitted this way. |
|
| 940 | - * @return boolean/string Page on success. |
|
| 941 | - * @access public |
|
| 942 | - */ |
|
| 943 | - function submitFormById($id) { |
|
| 944 | - return $this->_failOnError($this->_browser->submitFormById($id)); |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * Follows a link by name. Will click the first link |
|
| 949 | - * found with this link text by default, or a later |
|
| 950 | - * one if an index is given. Match is case insensitive |
|
| 951 | - * with normalised space. |
|
| 952 | - * @param string $label Text between the anchor tags. |
|
| 953 | - * @param integer $index Link position counting from zero. |
|
| 954 | - * @return boolean/string Page on success. |
|
| 955 | - * @access public |
|
| 956 | - */ |
|
| 957 | - function clickLink($label, $index = 0) { |
|
| 958 | - return $this->_failOnError($this->_browser->clickLink($label, $index)); |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - /** |
|
| 962 | - * Follows a link by id attribute. |
|
| 963 | - * @param string $id ID attribute value. |
|
| 964 | - * @return boolean/string Page on success. |
|
| 965 | - * @access public |
|
| 966 | - */ |
|
| 967 | - function clickLinkById($id) { |
|
| 968 | - return $this->_failOnError($this->_browser->clickLinkById($id)); |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - /** |
|
| 972 | - * Will trigger a pass if the two parameters have |
|
| 973 | - * the same value only. Otherwise a fail. This |
|
| 974 | - * is for testing hand extracted text, etc. |
|
| 975 | - * @param mixed $first Value to compare. |
|
| 976 | - * @param mixed $second Value to compare. |
|
| 977 | - * @param string $message Message to display. |
|
| 978 | - * @return boolean True on pass |
|
| 979 | - * @access public |
|
| 980 | - */ |
|
| 981 | - function assertEqual($first, $second, $message = "%s") { |
|
| 982 | - return $this->assert( |
|
| 983 | - new EqualExpectation($first), |
|
| 984 | - $second, |
|
| 985 | - $message); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - /** |
|
| 989 | - * Will trigger a pass if the two parameters have |
|
| 990 | - * a different value. Otherwise a fail. This |
|
| 991 | - * is for testing hand extracted text, etc. |
|
| 992 | - * @param mixed $first Value to compare. |
|
| 993 | - * @param mixed $second Value to compare. |
|
| 994 | - * @param string $message Message to display. |
|
| 995 | - * @return boolean True on pass |
|
| 996 | - * @access public |
|
| 997 | - */ |
|
| 998 | - function assertNotEqual($first, $second, $message = "%s") { |
|
| 999 | - return $this->assert( |
|
| 1000 | - new NotEqualExpectation($first), |
|
| 1001 | - $second, |
|
| 1002 | - $message); |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - /** |
|
| 1006 | - * Tests for the presence of a link label. Match is |
|
| 1007 | - * case insensitive with normalised space. |
|
| 1008 | - * @param string $label Text between the anchor tags. |
|
| 1009 | - * @param string $message Message to display. Default |
|
| 1010 | - * can be embedded with %s. |
|
| 1011 | - * @return boolean True if link present. |
|
| 1012 | - * @access public |
|
| 1013 | - */ |
|
| 1014 | - function assertLink($label, $message = "%s") { |
|
| 1015 | - return $this->assertTrue( |
|
| 1016 | - $this->_browser->isLink($label), |
|
| 1017 | - sprintf($message, "Link [$label] should exist")); |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - /** |
|
| 1021 | - * Tests for the non-presence of a link label. Match is |
|
| 1022 | - * case insensitive with normalised space. |
|
| 1023 | - * @param string/integer $label Text between the anchor tags |
|
| 1024 | - * or ID attribute. |
|
| 1025 | - * @param string $message Message to display. Default |
|
| 1026 | - * can be embedded with %s. |
|
| 1027 | - * @return boolean True if link missing. |
|
| 1028 | - * @access public |
|
| 1029 | - */ |
|
| 1030 | - function assertNoLink($label, $message = "%s") { |
|
| 1031 | - return $this->assertFalse( |
|
| 1032 | - $this->_browser->isLink($label), |
|
| 1033 | - sprintf($message, "Link [$label] should not exist")); |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - /** |
|
| 1037 | - * Tests for the presence of a link id attribute. |
|
| 1038 | - * @param string $id Id attribute value. |
|
| 1039 | - * @param string $message Message to display. Default |
|
| 1040 | - * can be embedded with %s. |
|
| 1041 | - * @return boolean True if link present. |
|
| 1042 | - * @access public |
|
| 1043 | - */ |
|
| 1044 | - function assertLinkById($id, $message = "%s") { |
|
| 1045 | - return $this->assertTrue( |
|
| 1046 | - $this->_browser->isLinkById($id), |
|
| 1047 | - sprintf($message, "Link ID [$id] should exist")); |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - /** |
|
| 1051 | - * Tests for the non-presence of a link label. Match is |
|
| 1052 | - * case insensitive with normalised space. |
|
| 1053 | - * @param string $id Id attribute value. |
|
| 1054 | - * @param string $message Message to display. Default |
|
| 1055 | - * can be embedded with %s. |
|
| 1056 | - * @return boolean True if link missing. |
|
| 1057 | - * @access public |
|
| 1058 | - */ |
|
| 1059 | - function assertNoLinkById($id, $message = "%s") { |
|
| 1060 | - return $this->assertFalse( |
|
| 1061 | - $this->_browser->isLinkById($id), |
|
| 1062 | - sprintf($message, "Link ID [$id] should not exist")); |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - /** |
|
| 1066 | - * Sets all form fields with that label, or name if there |
|
| 1067 | - * is no label attached. |
|
| 1068 | - * @param string $name Name of field in forms. |
|
| 1069 | - * @param string $value New value of field. |
|
| 1070 | - * @return boolean True if field exists, otherwise false. |
|
| 1071 | - * @access public |
|
| 1072 | - */ |
|
| 1073 | - function setField($label, $value) { |
|
| 1074 | - return $this->_browser->setField($label, $value); |
|
| 1075 | - } |
|
| 1076 | - |
|
| 1077 | - /** |
|
| 1078 | - * Sets all form fields with that name. |
|
| 1079 | - * @param string $name Name of field in forms. |
|
| 1080 | - * @param string $value New value of field. |
|
| 1081 | - * @return boolean True if field exists, otherwise false. |
|
| 1082 | - * @access public |
|
| 1083 | - */ |
|
| 1084 | - function setFieldByName($name, $value) { |
|
| 1085 | - return $this->_browser->setFieldByName($name, $value); |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - /** |
|
| 1089 | - * Sets all form fields with that name. |
|
| 1090 | - * @param string/integer $id Id of field in forms. |
|
| 1091 | - * @param string $value New value of field. |
|
| 1092 | - * @return boolean True if field exists, otherwise false. |
|
| 1093 | - * @access public |
|
| 1094 | - */ |
|
| 1095 | - function setFieldById($id, $value) { |
|
| 1096 | - return $this->_browser->setFieldById($id, $value); |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - /** |
|
| 1100 | - * Confirms that the form element is currently set |
|
| 1101 | - * to the expected value. A missing form will always |
|
| 1102 | - * fail. If no value is given then only the existence |
|
| 1103 | - * of the field is checked. |
|
| 1104 | - * @param string $name Name of field in forms. |
|
| 1105 | - * @param mixed $expected Expected string/array value or |
|
| 1106 | - * false for unset fields. |
|
| 1107 | - * @param string $message Message to display. Default |
|
| 1108 | - * can be embedded with %s. |
|
| 1109 | - * @return boolean True if pass. |
|
| 1110 | - * @access public |
|
| 1111 | - */ |
|
| 1112 | - function assertField($label, $expected = true, $message = '%s') { |
|
| 1113 | - $value = $this->_browser->getField($label); |
|
| 1114 | - return $this->_assertFieldValue($label, $value, $expected, $message); |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - /** |
|
| 1118 | - * Confirms that the form element is currently set |
|
| 1119 | - * to the expected value. A missing form element will always |
|
| 1120 | - * fail. If no value is given then only the existence |
|
| 1121 | - * of the field is checked. |
|
| 1122 | - * @param string $name Name of field in forms. |
|
| 1123 | - * @param mixed $expected Expected string/array value or |
|
| 1124 | - * false for unset fields. |
|
| 1125 | - * @param string $message Message to display. Default |
|
| 1126 | - * can be embedded with %s. |
|
| 1127 | - * @return boolean True if pass. |
|
| 1128 | - * @access public |
|
| 1129 | - */ |
|
| 1130 | - function assertFieldByName($name, $expected = true, $message = '%s') { |
|
| 1131 | - $value = $this->_browser->getFieldByName($name); |
|
| 1132 | - return $this->_assertFieldValue($name, $value, $expected, $message); |
|
| 1133 | - } |
|
| 1134 | - |
|
| 1135 | - /** |
|
| 1136 | - * Confirms that the form element is currently set |
|
| 1137 | - * to the expected value. A missing form will always |
|
| 1138 | - * fail. If no ID is given then only the existence |
|
| 1139 | - * of the field is checked. |
|
| 1140 | - * @param string/integer $id Name of field in forms. |
|
| 1141 | - * @param mixed $expected Expected string/array value or |
|
| 1142 | - * false for unset fields. |
|
| 1143 | - * @param string $message Message to display. Default |
|
| 1144 | - * can be embedded with %s. |
|
| 1145 | - * @return boolean True if pass. |
|
| 1146 | - * @access public |
|
| 1147 | - */ |
|
| 1148 | - function assertFieldById($id, $expected = true, $message = '%s') { |
|
| 1149 | - $value = $this->_browser->getFieldById($id); |
|
| 1150 | - return $this->_assertFieldValue($id, $value, $expected, $message); |
|
| 1151 | - } |
|
| 1152 | - |
|
| 1153 | - /** |
|
| 1154 | - * Tests the field value against the expectation. |
|
| 1155 | - * @param string $identifier Name, ID or label. |
|
| 1156 | - * @param mixed $value Current field value. |
|
| 1157 | - * @param mixed $expected Expected value to match. |
|
| 1158 | - * @param string $message Failure message. |
|
| 1159 | - * @return boolean True if pass |
|
| 1160 | - * @access protected |
|
| 1161 | - */ |
|
| 1162 | - function _assertFieldValue($identifier, $value, $expected, $message) { |
|
| 1163 | - if ($expected === true) { |
|
| 1164 | - return $this->assertTrue( |
|
| 1165 | - isset($value), |
|
| 1166 | - sprintf($message, "Field [$identifier] should exist")); |
|
| 1167 | - } |
|
| 1168 | - if (! SimpleExpectation::isExpectation($expected)) { |
|
| 1169 | - $identifier = str_replace('%', '%%', $identifier); |
|
| 1170 | - $expected = new FieldExpectation( |
|
| 1171 | - $expected, |
|
| 1172 | - "Field [$identifier] should match with [%s]"); |
|
| 1173 | - } |
|
| 1174 | - return $this->assert($expected, $value, $message); |
|
| 1175 | - } |
|
| 1176 | - |
|
| 1177 | - /** |
|
| 1178 | - * Checks the response code against a list |
|
| 1179 | - * of possible values. |
|
| 1180 | - * @param array $responses Possible responses for a pass. |
|
| 1181 | - * @param string $message Message to display. Default |
|
| 1182 | - * can be embedded with %s. |
|
| 1183 | - * @return boolean True if pass. |
|
| 1184 | - * @access public |
|
| 1185 | - */ |
|
| 1186 | - function assertResponse($responses, $message = '%s') { |
|
| 1187 | - $responses = (is_array($responses) ? $responses : array($responses)); |
|
| 1188 | - $code = $this->_browser->getResponseCode(); |
|
| 1189 | - $message = sprintf($message, "Expecting response in [" . |
|
| 1190 | - implode(", ", $responses) . "] got [$code]"); |
|
| 1191 | - return $this->assertTrue(in_array($code, $responses), $message); |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - /** |
|
| 1195 | - * Checks the mime type against a list |
|
| 1196 | - * of possible values. |
|
| 1197 | - * @param array $types Possible mime types for a pass. |
|
| 1198 | - * @param string $message Message to display. |
|
| 1199 | - * @return boolean True if pass. |
|
| 1200 | - * @access public |
|
| 1201 | - */ |
|
| 1202 | - function assertMime($types, $message = '%s') { |
|
| 1203 | - $types = (is_array($types) ? $types : array($types)); |
|
| 1204 | - $type = $this->_browser->getMimeType(); |
|
| 1205 | - $message = sprintf($message, "Expecting mime type in [" . |
|
| 1206 | - implode(", ", $types) . "] got [$type]"); |
|
| 1207 | - return $this->assertTrue(in_array($type, $types), $message); |
|
| 1208 | - } |
|
| 1209 | - |
|
| 1210 | - /** |
|
| 1211 | - * Attempt to match the authentication type within |
|
| 1212 | - * the security realm we are currently matching. |
|
| 1213 | - * @param string $authentication Usually basic. |
|
| 1214 | - * @param string $message Message to display. |
|
| 1215 | - * @return boolean True if pass. |
|
| 1216 | - * @access public |
|
| 1217 | - */ |
|
| 1218 | - function assertAuthentication($authentication = false, $message = '%s') { |
|
| 1219 | - if (! $authentication) { |
|
| 1220 | - $message = sprintf($message, "Expected any authentication type, got [" . |
|
| 1221 | - $this->_browser->getAuthentication() . "]"); |
|
| 1222 | - return $this->assertTrue( |
|
| 1223 | - $this->_browser->getAuthentication(), |
|
| 1224 | - $message); |
|
| 1225 | - } else { |
|
| 1226 | - $message = sprintf($message, "Expected authentication [$authentication] got [" . |
|
| 1227 | - $this->_browser->getAuthentication() . "]"); |
|
| 1228 | - return $this->assertTrue( |
|
| 1229 | - strtolower($this->_browser->getAuthentication()) == strtolower($authentication), |
|
| 1230 | - $message); |
|
| 1231 | - } |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - /** |
|
| 1235 | - * Checks that no authentication is necessary to view |
|
| 1236 | - * the desired page. |
|
| 1237 | - * @param string $message Message to display. |
|
| 1238 | - * @return boolean True if pass. |
|
| 1239 | - * @access public |
|
| 1240 | - */ |
|
| 1241 | - function assertNoAuthentication($message = '%s') { |
|
| 1242 | - $message = sprintf($message, "Expected no authentication type, got [" . |
|
| 1243 | - $this->_browser->getAuthentication() . "]"); |
|
| 1244 | - return $this->assertFalse($this->_browser->getAuthentication(), $message); |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - /** |
|
| 1248 | - * Attempts to match the current security realm. |
|
| 1249 | - * @param string $realm Name of security realm. |
|
| 1250 | - * @param string $message Message to display. |
|
| 1251 | - * @return boolean True if pass. |
|
| 1252 | - * @access public |
|
| 1253 | - */ |
|
| 1254 | - function assertRealm($realm, $message = '%s') { |
|
| 1255 | - if (! SimpleExpectation::isExpectation($realm)) { |
|
| 1256 | - $realm = new EqualExpectation($realm); |
|
| 1257 | - } |
|
| 1258 | - return $this->assert( |
|
| 1259 | - $realm, |
|
| 1260 | - $this->_browser->getRealm(), |
|
| 1261 | - "Expected realm -> $message"); |
|
| 1262 | - } |
|
| 1263 | - |
|
| 1264 | - /** |
|
| 1265 | - * Checks each header line for the required value. If no |
|
| 1266 | - * value is given then only an existence check is made. |
|
| 1267 | - * @param string $header Case insensitive header name. |
|
| 1268 | - * @param mixed $value Case sensitive trimmed string to |
|
| 1269 | - * match against. An expectation object |
|
| 1270 | - * can be used for pattern matching. |
|
| 1271 | - * @return boolean True if pass. |
|
| 1272 | - * @access public |
|
| 1273 | - */ |
|
| 1274 | - function assertHeader($header, $value = false, $message = '%s') { |
|
| 1275 | - return $this->assert( |
|
| 1276 | - new HttpHeaderExpectation($header, $value), |
|
| 1277 | - $this->_browser->getHeaders(), |
|
| 1278 | - $message); |
|
| 1279 | - } |
|
| 1280 | - |
|
| 1281 | - /** |
|
| 1282 | - * @deprecated |
|
| 1283 | - */ |
|
| 1284 | - function assertHeaderPattern($header, $pattern, $message = '%s') { |
|
| 1285 | - return $this->assert( |
|
| 1286 | - new HttpHeaderExpectation($header, new PatternExpectation($pattern)), |
|
| 1287 | - $this->_browser->getHeaders(), |
|
| 1288 | - $message); |
|
| 1289 | - } |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * Confirms that the header type has not been received. |
|
| 1293 | - * Only the landing page is checked. If you want to check |
|
| 1294 | - * redirect pages, then you should limit redirects so |
|
| 1295 | - * as to capture the page you want. |
|
| 1296 | - * @param string $header Case insensitive header name. |
|
| 1297 | - * @return boolean True if pass. |
|
| 1298 | - * @access public |
|
| 1299 | - */ |
|
| 1300 | - function assertNoHeader($header, $message = '%s') { |
|
| 1301 | - return $this->assert( |
|
| 1302 | - new NoHttpHeaderExpectation($header), |
|
| 1303 | - $this->_browser->getHeaders(), |
|
| 1304 | - $message); |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * @deprecated |
|
| 1309 | - */ |
|
| 1310 | - function assertNoUnwantedHeader($header, $message = '%s') { |
|
| 1311 | - return $this->assertNoHeader($header, $message); |
|
| 1312 | - } |
|
| 1313 | - |
|
| 1314 | - /** |
|
| 1315 | - * Tests the text between the title tags. |
|
| 1316 | - * @param string $title Expected title. |
|
| 1317 | - * @param string $message Message to display. |
|
| 1318 | - * @return boolean True if pass. |
|
| 1319 | - * @access public |
|
| 1320 | - */ |
|
| 1321 | - function assertTitle($title = false, $message = '%s') { |
|
| 1322 | - if (! SimpleExpectation::isExpectation($title)) { |
|
| 1323 | - $title = new EqualExpectation($title); |
|
| 1324 | - } |
|
| 1325 | - return $this->assert($title, $this->_browser->getTitle(), $message); |
|
| 1326 | - } |
|
| 1327 | - |
|
| 1328 | - /** |
|
| 1329 | - * Will trigger a pass if the text is found in the plain |
|
| 1330 | - * text form of the page. |
|
| 1331 | - * @param string $text Text to look for. |
|
| 1332 | - * @param string $message Message to display. |
|
| 1333 | - * @return boolean True if pass. |
|
| 1334 | - * @access public |
|
| 1335 | - */ |
|
| 1336 | - function assertText($text, $message = '%s') { |
|
| 1337 | - return $this->assert( |
|
| 1338 | - new TextExpectation($text), |
|
| 1339 | - $this->_browser->getContentAsText(), |
|
| 1340 | - $message); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - /** |
|
| 1344 | - * @deprecated |
|
| 1345 | - */ |
|
| 1346 | - function assertWantedText($text, $message = '%s') { |
|
| 1347 | - return $this->assertText($text, $message); |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * Will trigger a pass if the text is not found in the plain |
|
| 1352 | - * text form of the page. |
|
| 1353 | - * @param string $text Text to look for. |
|
| 1354 | - * @param string $message Message to display. |
|
| 1355 | - * @return boolean True if pass. |
|
| 1356 | - * @access public |
|
| 1357 | - */ |
|
| 1358 | - function assertNoText($text, $message = '%s') { |
|
| 1359 | - return $this->assert( |
|
| 1360 | - new NoTextExpectation($text), |
|
| 1361 | - $this->_browser->getContentAsText(), |
|
| 1362 | - $message); |
|
| 1363 | - } |
|
| 1364 | - |
|
| 1365 | - /** |
|
| 1366 | - * @deprecated |
|
| 1367 | - */ |
|
| 1368 | - function assertNoUnwantedText($text, $message = '%s') { |
|
| 1369 | - return $this->assertNoText($text, $message); |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * Will trigger a pass if the Perl regex pattern |
|
| 1374 | - * is found in the raw content. |
|
| 1375 | - * @param string $pattern Perl regex to look for including |
|
| 1376 | - * the regex delimiters. |
|
| 1377 | - * @param string $message Message to display. |
|
| 1378 | - * @return boolean True if pass. |
|
| 1379 | - * @access public |
|
| 1380 | - */ |
|
| 1381 | - function assertPattern($pattern, $message = '%s') { |
|
| 1382 | - return $this->assert( |
|
| 1383 | - new PatternExpectation($pattern), |
|
| 1384 | - $this->_browser->getContent(), |
|
| 1385 | - $message); |
|
| 1386 | - } |
|
| 1387 | - |
|
| 1388 | - /** |
|
| 1389 | - * @deprecated |
|
| 1390 | - */ |
|
| 1391 | - function assertWantedPattern($pattern, $message = '%s') { |
|
| 1392 | - return $this->assertPattern($pattern, $message); |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - /** |
|
| 1396 | - * Will trigger a pass if the perl regex pattern |
|
| 1397 | - * is not present in raw content. |
|
| 1398 | - * @param string $pattern Perl regex to look for including |
|
| 1399 | - * the regex delimiters. |
|
| 1400 | - * @param string $message Message to display. |
|
| 1401 | - * @return boolean True if pass. |
|
| 1402 | - * @access public |
|
| 1403 | - */ |
|
| 1404 | - function assertNoPattern($pattern, $message = '%s') { |
|
| 1405 | - return $this->assert( |
|
| 1406 | - new NoPatternExpectation($pattern), |
|
| 1407 | - $this->_browser->getContent(), |
|
| 1408 | - $message); |
|
| 1409 | - } |
|
| 1410 | - |
|
| 1411 | - /** |
|
| 1412 | - * @deprecated |
|
| 1413 | - */ |
|
| 1414 | - function assertNoUnwantedPattern($pattern, $message = '%s') { |
|
| 1415 | - return $this->assertNoPattern($pattern, $message); |
|
| 1416 | - } |
|
| 1417 | - |
|
| 1418 | - /** |
|
| 1419 | - * Checks that a cookie is set for the current page |
|
| 1420 | - * and optionally checks the value. |
|
| 1421 | - * @param string $name Name of cookie to test. |
|
| 1422 | - * @param string $expected Expected value as a string or |
|
| 1423 | - * false if any value will do. |
|
| 1424 | - * @param string $message Message to display. |
|
| 1425 | - * @return boolean True if pass. |
|
| 1426 | - * @access public |
|
| 1427 | - */ |
|
| 1428 | - function assertCookie($name, $expected = false, $message = '%s') { |
|
| 1429 | - $value = $this->getCookie($name); |
|
| 1430 | - if (! $expected) { |
|
| 1431 | - return $this->assertTrue( |
|
| 1432 | - $value, |
|
| 1433 | - sprintf($message, "Expecting cookie [$name]")); |
|
| 1434 | - } |
|
| 1435 | - if (! SimpleExpectation::isExpectation($expected)) { |
|
| 1436 | - $expected = new EqualExpectation($expected); |
|
| 1437 | - } |
|
| 1438 | - return $this->assert($expected, $value, "Expecting cookie [$name] -> $message"); |
|
| 1439 | - } |
|
| 1440 | - |
|
| 1441 | - /** |
|
| 1442 | - * Checks that no cookie is present or that it has |
|
| 1443 | - * been successfully cleared. |
|
| 1444 | - * @param string $name Name of cookie to test. |
|
| 1445 | - * @param string $message Message to display. |
|
| 1446 | - * @return boolean True if pass. |
|
| 1447 | - * @access public |
|
| 1448 | - */ |
|
| 1449 | - function assertNoCookie($name, $message = '%s') { |
|
| 1450 | - return $this->assertTrue( |
|
| 1451 | - $this->getCookie($name) === false, |
|
| 1452 | - sprintf($message, "Not expecting cookie [$name]")); |
|
| 1453 | - } |
|
| 1454 | - } |
|
| 1455 | 432 | \ No newline at end of file |
| 433 | + */ |
|
| 434 | + class WebTestCase extends SimpleTestCase { |
|
| 435 | + protected $_browser; |
|
| 436 | + protected $_ignore_errors = false; |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Creates an empty test case. Should be subclassed |
|
| 440 | + * with test methods for a functional test case. |
|
| 441 | + * @param string $label Name of test case. Will use |
|
| 442 | + * the class name if none specified. |
|
| 443 | + * @access public |
|
| 444 | + */ |
|
| 445 | + function WebTestCase($label = false) { |
|
| 446 | + $this->SimpleTestCase($label); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Announces the start of the test. |
|
| 451 | + * @param string $method Test method just started. |
|
| 452 | + * @access public |
|
| 453 | + */ |
|
| 454 | + function before($method) { |
|
| 455 | + parent::before($method); |
|
| 456 | + $this->setBrowser($this->createBrowser()); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * Announces the end of the test. Includes private clean up. |
|
| 461 | + * @param string $method Test method just finished. |
|
| 462 | + * @access public |
|
| 463 | + */ |
|
| 464 | + function after($method) { |
|
| 465 | + $this->unsetBrowser(); |
|
| 466 | + parent::after($method); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Gets a current browser reference for setting |
|
| 471 | + * special expectations or for detailed |
|
| 472 | + * examination of page fetches. |
|
| 473 | + * @return SimpleBrowser Current test browser object. |
|
| 474 | + * @access public |
|
| 475 | + */ |
|
| 476 | + function &getBrowser() { |
|
| 477 | + return $this->_browser; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Gets a current browser reference for setting |
|
| 482 | + * special expectations or for detailed |
|
| 483 | + * examination of page fetches. |
|
| 484 | + * @param SimpleBrowser $browser New test browser object. |
|
| 485 | + * @access public |
|
| 486 | + */ |
|
| 487 | + function setBrowser($browser) { |
|
| 488 | + return $this->_browser = $browser; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Clears the current browser reference to help the |
|
| 493 | + * PHP garbage collector. |
|
| 494 | + * @access public |
|
| 495 | + */ |
|
| 496 | + function unsetBrowser() { |
|
| 497 | + unset($this->_browser); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * Creates a new default web browser object. |
|
| 502 | + * Will be cleared at the end of the test method. |
|
| 503 | + * @return TestBrowser New browser. |
|
| 504 | + * @access public |
|
| 505 | + */ |
|
| 506 | + function &createBrowser() { |
|
| 507 | + $browser = new SimpleBrowser(); |
|
| 508 | + return $browser; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Gets the last response error. |
|
| 513 | + * @return string Last low level HTTP error. |
|
| 514 | + * @access public |
|
| 515 | + */ |
|
| 516 | + function getTransportError() { |
|
| 517 | + return $this->_browser->getTransportError(); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Accessor for the currently selected URL. |
|
| 522 | + * @return string Current location or false if |
|
| 523 | + * no page yet fetched. |
|
| 524 | + * @access public |
|
| 525 | + */ |
|
| 526 | + function getUrl() { |
|
| 527 | + return $this->_browser->getUrl(); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Dumps the current request for debugging. |
|
| 532 | + * @access public |
|
| 533 | + */ |
|
| 534 | + function showRequest() { |
|
| 535 | + $this->dump($this->_browser->getRequest()); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Dumps the current HTTP headers for debugging. |
|
| 540 | + * @access public |
|
| 541 | + */ |
|
| 542 | + function showHeaders() { |
|
| 543 | + $this->dump($this->_browser->getHeaders()); |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Dumps the current HTML source for debugging. |
|
| 548 | + * @access public |
|
| 549 | + */ |
|
| 550 | + function showSource() { |
|
| 551 | + $this->dump($this->_browser->getContent()); |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Dumps the visible text only for debugging. |
|
| 556 | + * @access public |
|
| 557 | + */ |
|
| 558 | + function showText() { |
|
| 559 | + $this->dump(wordwrap($this->_browser->getContentAsText(), 80)); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * Simulates the closing and reopening of the browser. |
|
| 564 | + * Temporary cookies will be discarded and timed |
|
| 565 | + * cookies will be expired if later than the |
|
| 566 | + * specified time. |
|
| 567 | + * @param string/integer $date Time when session restarted. |
|
| 568 | + * If ommitted then all persistent |
|
| 569 | + * cookies are kept. Time is either |
|
| 570 | + * Cookie format string or timestamp. |
|
| 571 | + * @access public |
|
| 572 | + */ |
|
| 573 | + function restart($date = false) { |
|
| 574 | + if ($date === false) { |
|
| 575 | + $date = time(); |
|
| 576 | + } |
|
| 577 | + $this->_browser->restart($date); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Moves cookie expiry times back into the past. |
|
| 582 | + * Useful for testing timeouts and expiries. |
|
| 583 | + * @param integer $interval Amount to age in seconds. |
|
| 584 | + * @access public |
|
| 585 | + */ |
|
| 586 | + function ageCookies($interval) { |
|
| 587 | + $this->_browser->ageCookies($interval); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Disables frames support. Frames will not be fetched |
|
| 592 | + * and the frameset page will be used instead. |
|
| 593 | + * @access public |
|
| 594 | + */ |
|
| 595 | + function ignoreFrames() { |
|
| 596 | + $this->_browser->ignoreFrames(); |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Switches off cookie sending and recieving. |
|
| 601 | + * @access public |
|
| 602 | + */ |
|
| 603 | + function ignoreCookies() { |
|
| 604 | + $this->_browser->ignoreCookies(); |
|
| 605 | + } |
|
| 606 | + |
|
| 607 | + /** |
|
| 608 | + * Skips errors for the next request only. You might |
|
| 609 | + * want to confirm that a page is unreachable for |
|
| 610 | + * example. |
|
| 611 | + * @access public |
|
| 612 | + */ |
|
| 613 | + function ignoreErrors() { |
|
| 614 | + $this->_ignore_errors = true; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Issues a fail if there is a transport error anywhere |
|
| 619 | + * in the current frameset. Only one such error is |
|
| 620 | + * reported. |
|
| 621 | + * @param string/boolean $result HTML or failure. |
|
| 622 | + * @return string/boolean $result Passes through result. |
|
| 623 | + * @access private |
|
| 624 | + */ |
|
| 625 | + function _failOnError($result) { |
|
| 626 | + if (! $this->_ignore_errors) { |
|
| 627 | + if ($error = $this->_browser->getTransportError()) { |
|
| 628 | + $this->fail($error); |
|
| 629 | + } |
|
| 630 | + } |
|
| 631 | + $this->_ignore_errors = false; |
|
| 632 | + return $result; |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * Adds a header to every fetch. |
|
| 637 | + * @param string $header Header line to add to every |
|
| 638 | + * request until cleared. |
|
| 639 | + * @access public |
|
| 640 | + */ |
|
| 641 | + function addHeader($header) { |
|
| 642 | + $this->_browser->addHeader($header); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * Sets the maximum number of redirects before |
|
| 647 | + * the web page is loaded regardless. |
|
| 648 | + * @param integer $max Maximum hops. |
|
| 649 | + * @access public |
|
| 650 | + */ |
|
| 651 | + function setMaximumRedirects($max) { |
|
| 652 | + if (! $this->_browser) { |
|
| 653 | + trigger_error( |
|
| 654 | + 'Can only set maximum redirects in a test method, setUp() or tearDown()'); |
|
| 655 | + } |
|
| 656 | + $this->_browser->setMaximumRedirects($max); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Sets the socket timeout for opening a connection and |
|
| 661 | + * receiving at least one byte of information. |
|
| 662 | + * @param integer $timeout Maximum time in seconds. |
|
| 663 | + * @access public |
|
| 664 | + */ |
|
| 665 | + function setConnectionTimeout($timeout) { |
|
| 666 | + $this->_browser->setConnectionTimeout($timeout); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Sets proxy to use on all requests for when |
|
| 671 | + * testing from behind a firewall. Set URL |
|
| 672 | + * to false to disable. |
|
| 673 | + * @param string $proxy Proxy URL. |
|
| 674 | + * @param string $username Proxy username for authentication. |
|
| 675 | + * @param string $password Proxy password for authentication. |
|
| 676 | + * @access public |
|
| 677 | + */ |
|
| 678 | + function useProxy($proxy, $username = false, $password = false) { |
|
| 679 | + $this->_browser->useProxy($proxy, $username, $password); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Fetches a page into the page buffer. If |
|
| 684 | + * there is no base for the URL then the |
|
| 685 | + * current base URL is used. After the fetch |
|
| 686 | + * the base URL reflects the new location. |
|
| 687 | + * @param string $url URL to fetch. |
|
| 688 | + * @param hash $parameters Optional additional GET data. |
|
| 689 | + * @return boolean/string Raw page on success. |
|
| 690 | + * @access public |
|
| 691 | + */ |
|
| 692 | + function get($url, $parameters = false) { |
|
| 693 | + return $this->_failOnError($this->_browser->get($url, $parameters)); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Fetches a page by POST into the page buffer. |
|
| 698 | + * If there is no base for the URL then the |
|
| 699 | + * current base URL is used. After the fetch |
|
| 700 | + * the base URL reflects the new location. |
|
| 701 | + * @param string $url URL to fetch. |
|
| 702 | + * @param hash $parameters Optional additional GET data. |
|
| 703 | + * @return boolean/string Raw page on success. |
|
| 704 | + * @access public |
|
| 705 | + */ |
|
| 706 | + function post($url, $parameters = false) { |
|
| 707 | + return $this->_failOnError($this->_browser->post($url, $parameters)); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * Does a HTTP HEAD fetch, fetching only the page |
|
| 712 | + * headers. The current base URL is unchanged by this. |
|
| 713 | + * @param string $url URL to fetch. |
|
| 714 | + * @param hash $parameters Optional additional GET data. |
|
| 715 | + * @return boolean True on success. |
|
| 716 | + * @access public |
|
| 717 | + */ |
|
| 718 | + function head($url, $parameters = false) { |
|
| 719 | + return $this->_failOnError($this->_browser->head($url, $parameters)); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * Equivalent to hitting the retry button on the |
|
| 724 | + * browser. Will attempt to repeat the page fetch. |
|
| 725 | + * @return boolean True if fetch succeeded. |
|
| 726 | + * @access public |
|
| 727 | + */ |
|
| 728 | + function retry() { |
|
| 729 | + return $this->_failOnError($this->_browser->retry()); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * Equivalent to hitting the back button on the |
|
| 734 | + * browser. |
|
| 735 | + * @return boolean True if history entry and |
|
| 736 | + * fetch succeeded. |
|
| 737 | + * @access public |
|
| 738 | + */ |
|
| 739 | + function back() { |
|
| 740 | + return $this->_failOnError($this->_browser->back()); |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Equivalent to hitting the forward button on the |
|
| 745 | + * browser. |
|
| 746 | + * @return boolean True if history entry and |
|
| 747 | + * fetch succeeded. |
|
| 748 | + * @access public |
|
| 749 | + */ |
|
| 750 | + function forward() { |
|
| 751 | + return $this->_failOnError($this->_browser->forward()); |
|
| 752 | + } |
|
| 753 | + |
|
| 754 | + /** |
|
| 755 | + * Retries a request after setting the authentication |
|
| 756 | + * for the current realm. |
|
| 757 | + * @param string $username Username for realm. |
|
| 758 | + * @param string $password Password for realm. |
|
| 759 | + * @return boolean/string HTML on successful fetch. Note |
|
| 760 | + * that authentication may still have |
|
| 761 | + * failed. |
|
| 762 | + * @access public |
|
| 763 | + */ |
|
| 764 | + function authenticate($username, $password) { |
|
| 765 | + return $this->_failOnError( |
|
| 766 | + $this->_browser->authenticate($username, $password)); |
|
| 767 | + } |
|
| 768 | + |
|
| 769 | + /** |
|
| 770 | + * Gets the cookie value for the current browser context. |
|
| 771 | + * @param string $name Name of cookie. |
|
| 772 | + * @return string Value of cookie or false if unset. |
|
| 773 | + * @access public |
|
| 774 | + */ |
|
| 775 | + function getCookie($name) { |
|
| 776 | + return $this->_browser->getCurrentCookieValue($name); |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + /** |
|
| 780 | + * Sets a cookie in the current browser. |
|
| 781 | + * @param string $name Name of cookie. |
|
| 782 | + * @param string $value Cookie value. |
|
| 783 | + * @param string $host Host upon which the cookie is valid. |
|
| 784 | + * @param string $path Cookie path if not host wide. |
|
| 785 | + * @param string $expiry Expiry date. |
|
| 786 | + * @access public |
|
| 787 | + */ |
|
| 788 | + function setCookie($name, $value, $host = false, $path = "/", $expiry = false) { |
|
| 789 | + $this->_browser->setCookie($name, $value, $host, $path, $expiry); |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + /** |
|
| 793 | + * Accessor for current frame focus. Will be |
|
| 794 | + * false if no frame has focus. |
|
| 795 | + * @return integer/string/boolean Label if any, otherwise |
|
| 796 | + * the position in the frameset |
|
| 797 | + * or false if none. |
|
| 798 | + * @access public |
|
| 799 | + */ |
|
| 800 | + function getFrameFocus() { |
|
| 801 | + return $this->_browser->getFrameFocus(); |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + /** |
|
| 805 | + * Sets the focus by index. The integer index starts from 1. |
|
| 806 | + * @param integer $choice Chosen frame. |
|
| 807 | + * @return boolean True if frame exists. |
|
| 808 | + * @access public |
|
| 809 | + */ |
|
| 810 | + function setFrameFocusByIndex($choice) { |
|
| 811 | + return $this->_browser->setFrameFocusByIndex($choice); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + /** |
|
| 815 | + * Sets the focus by name. |
|
| 816 | + * @param string $name Chosen frame. |
|
| 817 | + * @return boolean True if frame exists. |
|
| 818 | + * @access public |
|
| 819 | + */ |
|
| 820 | + function setFrameFocus($name) { |
|
| 821 | + return $this->_browser->setFrameFocus($name); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Clears the frame focus. All frames will be searched |
|
| 826 | + * for content. |
|
| 827 | + * @access public |
|
| 828 | + */ |
|
| 829 | + function clearFrameFocus() { |
|
| 830 | + return $this->_browser->clearFrameFocus(); |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + /** |
|
| 834 | + * Clicks a visible text item. Will first try buttons, |
|
| 835 | + * then links and then images. |
|
| 836 | + * @param string $label Visible text or alt text. |
|
| 837 | + * @return string/boolean Raw page or false. |
|
| 838 | + * @access public |
|
| 839 | + */ |
|
| 840 | + function click($label) { |
|
| 841 | + return $this->_failOnError($this->_browser->click($label)); |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + /** |
|
| 845 | + * Clicks the submit button by label. The owning |
|
| 846 | + * form will be submitted by this. |
|
| 847 | + * @param string $label Button label. An unlabeled |
|
| 848 | + * button can be triggered by 'Submit'. |
|
| 849 | + * @param hash $additional Additional form values. |
|
| 850 | + * @return boolean/string Page on success, else false. |
|
| 851 | + * @access public |
|
| 852 | + */ |
|
| 853 | + function clickSubmit($label = 'Submit', $additional = false) { |
|
| 854 | + return $this->_failOnError( |
|
| 855 | + $this->_browser->clickSubmit($label, $additional)); |
|
| 856 | + } |
|
| 857 | + |
|
| 858 | + /** |
|
| 859 | + * Clicks the submit button by name attribute. The owning |
|
| 860 | + * form will be submitted by this. |
|
| 861 | + * @param string $name Name attribute of button. |
|
| 862 | + * @param hash $additional Additional form values. |
|
| 863 | + * @return boolean/string Page on success. |
|
| 864 | + * @access public |
|
| 865 | + */ |
|
| 866 | + function clickSubmitByName($name, $additional = false) { |
|
| 867 | + return $this->_failOnError( |
|
| 868 | + $this->_browser->clickSubmitByName($name, $additional)); |
|
| 869 | + } |
|
| 870 | + |
|
| 871 | + /** |
|
| 872 | + * Clicks the submit button by ID attribute. The owning |
|
| 873 | + * form will be submitted by this. |
|
| 874 | + * @param string $id ID attribute of button. |
|
| 875 | + * @param hash $additional Additional form values. |
|
| 876 | + * @return boolean/string Page on success. |
|
| 877 | + * @access public |
|
| 878 | + */ |
|
| 879 | + function clickSubmitById($id, $additional = false) { |
|
| 880 | + return $this->_failOnError( |
|
| 881 | + $this->_browser->clickSubmitById($id, $additional)); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + /** |
|
| 885 | + * Clicks the submit image by some kind of label. Usually |
|
| 886 | + * the alt tag or the nearest equivalent. The owning |
|
| 887 | + * form will be submitted by this. Clicking outside of |
|
| 888 | + * the boundary of the coordinates will result in |
|
| 889 | + * a failure. |
|
| 890 | + * @param string $label Alt attribute of button. |
|
| 891 | + * @param integer $x X-coordinate of imaginary click. |
|
| 892 | + * @param integer $y Y-coordinate of imaginary click. |
|
| 893 | + * @param hash $additional Additional form values. |
|
| 894 | + * @return boolean/string Page on success. |
|
| 895 | + * @access public |
|
| 896 | + */ |
|
| 897 | + function clickImage($label, $x = 1, $y = 1, $additional = false) { |
|
| 898 | + return $this->_failOnError( |
|
| 899 | + $this->_browser->clickImage($label, $x, $y, $additional)); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + /** |
|
| 903 | + * Clicks the submit image by the name. Usually |
|
| 904 | + * the alt tag or the nearest equivalent. The owning |
|
| 905 | + * form will be submitted by this. Clicking outside of |
|
| 906 | + * the boundary of the coordinates will result in |
|
| 907 | + * a failure. |
|
| 908 | + * @param string $name Name attribute of button. |
|
| 909 | + * @param integer $x X-coordinate of imaginary click. |
|
| 910 | + * @param integer $y Y-coordinate of imaginary click. |
|
| 911 | + * @param hash $additional Additional form values. |
|
| 912 | + * @return boolean/string Page on success. |
|
| 913 | + * @access public |
|
| 914 | + */ |
|
| 915 | + function clickImageByName($name, $x = 1, $y = 1, $additional = false) { |
|
| 916 | + return $this->_failOnError( |
|
| 917 | + $this->_browser->clickImageByName($name, $x, $y, $additional)); |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * Clicks the submit image by ID attribute. The owning |
|
| 922 | + * form will be submitted by this. Clicking outside of |
|
| 923 | + * the boundary of the coordinates will result in |
|
| 924 | + * a failure. |
|
| 925 | + * @param integer/string $id ID attribute of button. |
|
| 926 | + * @param integer $x X-coordinate of imaginary click. |
|
| 927 | + * @param integer $y Y-coordinate of imaginary click. |
|
| 928 | + * @param hash $additional Additional form values. |
|
| 929 | + * @return boolean/string Page on success. |
|
| 930 | + * @access public |
|
| 931 | + */ |
|
| 932 | + function clickImageById($id, $x = 1, $y = 1, $additional = false) { |
|
| 933 | + return $this->_failOnError( |
|
| 934 | + $this->_browser->clickImageById($id, $x, $y, $additional)); |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + /** |
|
| 938 | + * Submits a form by the ID. |
|
| 939 | + * @param string $id Form ID. No button information |
|
| 940 | + * is submitted this way. |
|
| 941 | + * @return boolean/string Page on success. |
|
| 942 | + * @access public |
|
| 943 | + */ |
|
| 944 | + function submitFormById($id) { |
|
| 945 | + return $this->_failOnError($this->_browser->submitFormById($id)); |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + /** |
|
| 949 | + * Follows a link by name. Will click the first link |
|
| 950 | + * found with this link text by default, or a later |
|
| 951 | + * one if an index is given. Match is case insensitive |
|
| 952 | + * with normalised space. |
|
| 953 | + * @param string $label Text between the anchor tags. |
|
| 954 | + * @param integer $index Link position counting from zero. |
|
| 955 | + * @return boolean/string Page on success. |
|
| 956 | + * @access public |
|
| 957 | + */ |
|
| 958 | + function clickLink($label, $index = 0) { |
|
| 959 | + return $this->_failOnError($this->_browser->clickLink($label, $index)); |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + /** |
|
| 963 | + * Follows a link by id attribute. |
|
| 964 | + * @param string $id ID attribute value. |
|
| 965 | + * @return boolean/string Page on success. |
|
| 966 | + * @access public |
|
| 967 | + */ |
|
| 968 | + function clickLinkById($id) { |
|
| 969 | + return $this->_failOnError($this->_browser->clickLinkById($id)); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * Will trigger a pass if the two parameters have |
|
| 974 | + * the same value only. Otherwise a fail. This |
|
| 975 | + * is for testing hand extracted text, etc. |
|
| 976 | + * @param mixed $first Value to compare. |
|
| 977 | + * @param mixed $second Value to compare. |
|
| 978 | + * @param string $message Message to display. |
|
| 979 | + * @return boolean True on pass |
|
| 980 | + * @access public |
|
| 981 | + */ |
|
| 982 | + function assertEqual($first, $second, $message = "%s") { |
|
| 983 | + return $this->assert( |
|
| 984 | + new EqualExpectation($first), |
|
| 985 | + $second, |
|
| 986 | + $message); |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Will trigger a pass if the two parameters have |
|
| 991 | + * a different value. Otherwise a fail. This |
|
| 992 | + * is for testing hand extracted text, etc. |
|
| 993 | + * @param mixed $first Value to compare. |
|
| 994 | + * @param mixed $second Value to compare. |
|
| 995 | + * @param string $message Message to display. |
|
| 996 | + * @return boolean True on pass |
|
| 997 | + * @access public |
|
| 998 | + */ |
|
| 999 | + function assertNotEqual($first, $second, $message = "%s") { |
|
| 1000 | + return $this->assert( |
|
| 1001 | + new NotEqualExpectation($first), |
|
| 1002 | + $second, |
|
| 1003 | + $message); |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + /** |
|
| 1007 | + * Tests for the presence of a link label. Match is |
|
| 1008 | + * case insensitive with normalised space. |
|
| 1009 | + * @param string $label Text between the anchor tags. |
|
| 1010 | + * @param string $message Message to display. Default |
|
| 1011 | + * can be embedded with %s. |
|
| 1012 | + * @return boolean True if link present. |
|
| 1013 | + * @access public |
|
| 1014 | + */ |
|
| 1015 | + function assertLink($label, $message = "%s") { |
|
| 1016 | + return $this->assertTrue( |
|
| 1017 | + $this->_browser->isLink($label), |
|
| 1018 | + sprintf($message, "Link [$label] should exist")); |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + /** |
|
| 1022 | + * Tests for the non-presence of a link label. Match is |
|
| 1023 | + * case insensitive with normalised space. |
|
| 1024 | + * @param string/integer $label Text between the anchor tags |
|
| 1025 | + * or ID attribute. |
|
| 1026 | + * @param string $message Message to display. Default |
|
| 1027 | + * can be embedded with %s. |
|
| 1028 | + * @return boolean True if link missing. |
|
| 1029 | + * @access public |
|
| 1030 | + */ |
|
| 1031 | + function assertNoLink($label, $message = "%s") { |
|
| 1032 | + return $this->assertFalse( |
|
| 1033 | + $this->_browser->isLink($label), |
|
| 1034 | + sprintf($message, "Link [$label] should not exist")); |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + /** |
|
| 1038 | + * Tests for the presence of a link id attribute. |
|
| 1039 | + * @param string $id Id attribute value. |
|
| 1040 | + * @param string $message Message to display. Default |
|
| 1041 | + * can be embedded with %s. |
|
| 1042 | + * @return boolean True if link present. |
|
| 1043 | + * @access public |
|
| 1044 | + */ |
|
| 1045 | + function assertLinkById($id, $message = "%s") { |
|
| 1046 | + return $this->assertTrue( |
|
| 1047 | + $this->_browser->isLinkById($id), |
|
| 1048 | + sprintf($message, "Link ID [$id] should exist")); |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * Tests for the non-presence of a link label. Match is |
|
| 1053 | + * case insensitive with normalised space. |
|
| 1054 | + * @param string $id Id attribute value. |
|
| 1055 | + * @param string $message Message to display. Default |
|
| 1056 | + * can be embedded with %s. |
|
| 1057 | + * @return boolean True if link missing. |
|
| 1058 | + * @access public |
|
| 1059 | + */ |
|
| 1060 | + function assertNoLinkById($id, $message = "%s") { |
|
| 1061 | + return $this->assertFalse( |
|
| 1062 | + $this->_browser->isLinkById($id), |
|
| 1063 | + sprintf($message, "Link ID [$id] should not exist")); |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * Sets all form fields with that label, or name if there |
|
| 1068 | + * is no label attached. |
|
| 1069 | + * @param string $name Name of field in forms. |
|
| 1070 | + * @param string $value New value of field. |
|
| 1071 | + * @return boolean True if field exists, otherwise false. |
|
| 1072 | + * @access public |
|
| 1073 | + */ |
|
| 1074 | + function setField($label, $value) { |
|
| 1075 | + return $this->_browser->setField($label, $value); |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * Sets all form fields with that name. |
|
| 1080 | + * @param string $name Name of field in forms. |
|
| 1081 | + * @param string $value New value of field. |
|
| 1082 | + * @return boolean True if field exists, otherwise false. |
|
| 1083 | + * @access public |
|
| 1084 | + */ |
|
| 1085 | + function setFieldByName($name, $value) { |
|
| 1086 | + return $this->_browser->setFieldByName($name, $value); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + /** |
|
| 1090 | + * Sets all form fields with that name. |
|
| 1091 | + * @param string/integer $id Id of field in forms. |
|
| 1092 | + * @param string $value New value of field. |
|
| 1093 | + * @return boolean True if field exists, otherwise false. |
|
| 1094 | + * @access public |
|
| 1095 | + */ |
|
| 1096 | + function setFieldById($id, $value) { |
|
| 1097 | + return $this->_browser->setFieldById($id, $value); |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + /** |
|
| 1101 | + * Confirms that the form element is currently set |
|
| 1102 | + * to the expected value. A missing form will always |
|
| 1103 | + * fail. If no value is given then only the existence |
|
| 1104 | + * of the field is checked. |
|
| 1105 | + * @param string $name Name of field in forms. |
|
| 1106 | + * @param mixed $expected Expected string/array value or |
|
| 1107 | + * false for unset fields. |
|
| 1108 | + * @param string $message Message to display. Default |
|
| 1109 | + * can be embedded with %s. |
|
| 1110 | + * @return boolean True if pass. |
|
| 1111 | + * @access public |
|
| 1112 | + */ |
|
| 1113 | + function assertField($label, $expected = true, $message = '%s') { |
|
| 1114 | + $value = $this->_browser->getField($label); |
|
| 1115 | + return $this->_assertFieldValue($label, $value, $expected, $message); |
|
| 1116 | + } |
|
| 1117 | + |
|
| 1118 | + /** |
|
| 1119 | + * Confirms that the form element is currently set |
|
| 1120 | + * to the expected value. A missing form element will always |
|
| 1121 | + * fail. If no value is given then only the existence |
|
| 1122 | + * of the field is checked. |
|
| 1123 | + * @param string $name Name of field in forms. |
|
| 1124 | + * @param mixed $expected Expected string/array value or |
|
| 1125 | + * false for unset fields. |
|
| 1126 | + * @param string $message Message to display. Default |
|
| 1127 | + * can be embedded with %s. |
|
| 1128 | + * @return boolean True if pass. |
|
| 1129 | + * @access public |
|
| 1130 | + */ |
|
| 1131 | + function assertFieldByName($name, $expected = true, $message = '%s') { |
|
| 1132 | + $value = $this->_browser->getFieldByName($name); |
|
| 1133 | + return $this->_assertFieldValue($name, $value, $expected, $message); |
|
| 1134 | + } |
|
| 1135 | + |
|
| 1136 | + /** |
|
| 1137 | + * Confirms that the form element is currently set |
|
| 1138 | + * to the expected value. A missing form will always |
|
| 1139 | + * fail. If no ID is given then only the existence |
|
| 1140 | + * of the field is checked. |
|
| 1141 | + * @param string/integer $id Name of field in forms. |
|
| 1142 | + * @param mixed $expected Expected string/array value or |
|
| 1143 | + * false for unset fields. |
|
| 1144 | + * @param string $message Message to display. Default |
|
| 1145 | + * can be embedded with %s. |
|
| 1146 | + * @return boolean True if pass. |
|
| 1147 | + * @access public |
|
| 1148 | + */ |
|
| 1149 | + function assertFieldById($id, $expected = true, $message = '%s') { |
|
| 1150 | + $value = $this->_browser->getFieldById($id); |
|
| 1151 | + return $this->_assertFieldValue($id, $value, $expected, $message); |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + /** |
|
| 1155 | + * Tests the field value against the expectation. |
|
| 1156 | + * @param string $identifier Name, ID or label. |
|
| 1157 | + * @param mixed $value Current field value. |
|
| 1158 | + * @param mixed $expected Expected value to match. |
|
| 1159 | + * @param string $message Failure message. |
|
| 1160 | + * @return boolean True if pass |
|
| 1161 | + * @access protected |
|
| 1162 | + */ |
|
| 1163 | + function _assertFieldValue($identifier, $value, $expected, $message) { |
|
| 1164 | + if ($expected === true) { |
|
| 1165 | + return $this->assertTrue( |
|
| 1166 | + isset($value), |
|
| 1167 | + sprintf($message, "Field [$identifier] should exist")); |
|
| 1168 | + } |
|
| 1169 | + if (! SimpleExpectation::isExpectation($expected)) { |
|
| 1170 | + $identifier = str_replace('%', '%%', $identifier); |
|
| 1171 | + $expected = new FieldExpectation( |
|
| 1172 | + $expected, |
|
| 1173 | + "Field [$identifier] should match with [%s]"); |
|
| 1174 | + } |
|
| 1175 | + return $this->assert($expected, $value, $message); |
|
| 1176 | + } |
|
| 1177 | + |
|
| 1178 | + /** |
|
| 1179 | + * Checks the response code against a list |
|
| 1180 | + * of possible values. |
|
| 1181 | + * @param array $responses Possible responses for a pass. |
|
| 1182 | + * @param string $message Message to display. Default |
|
| 1183 | + * can be embedded with %s. |
|
| 1184 | + * @return boolean True if pass. |
|
| 1185 | + * @access public |
|
| 1186 | + */ |
|
| 1187 | + function assertResponse($responses, $message = '%s') { |
|
| 1188 | + $responses = (is_array($responses) ? $responses : array($responses)); |
|
| 1189 | + $code = $this->_browser->getResponseCode(); |
|
| 1190 | + $message = sprintf($message, "Expecting response in [" . |
|
| 1191 | + implode(", ", $responses) . "] got [$code]"); |
|
| 1192 | + return $this->assertTrue(in_array($code, $responses), $message); |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + /** |
|
| 1196 | + * Checks the mime type against a list |
|
| 1197 | + * of possible values. |
|
| 1198 | + * @param array $types Possible mime types for a pass. |
|
| 1199 | + * @param string $message Message to display. |
|
| 1200 | + * @return boolean True if pass. |
|
| 1201 | + * @access public |
|
| 1202 | + */ |
|
| 1203 | + function assertMime($types, $message = '%s') { |
|
| 1204 | + $types = (is_array($types) ? $types : array($types)); |
|
| 1205 | + $type = $this->_browser->getMimeType(); |
|
| 1206 | + $message = sprintf($message, "Expecting mime type in [" . |
|
| 1207 | + implode(", ", $types) . "] got [$type]"); |
|
| 1208 | + return $this->assertTrue(in_array($type, $types), $message); |
|
| 1209 | + } |
|
| 1210 | + |
|
| 1211 | + /** |
|
| 1212 | + * Attempt to match the authentication type within |
|
| 1213 | + * the security realm we are currently matching. |
|
| 1214 | + * @param string $authentication Usually basic. |
|
| 1215 | + * @param string $message Message to display. |
|
| 1216 | + * @return boolean True if pass. |
|
| 1217 | + * @access public |
|
| 1218 | + */ |
|
| 1219 | + function assertAuthentication($authentication = false, $message = '%s') { |
|
| 1220 | + if (! $authentication) { |
|
| 1221 | + $message = sprintf($message, "Expected any authentication type, got [" . |
|
| 1222 | + $this->_browser->getAuthentication() . "]"); |
|
| 1223 | + return $this->assertTrue( |
|
| 1224 | + $this->_browser->getAuthentication(), |
|
| 1225 | + $message); |
|
| 1226 | + } else { |
|
| 1227 | + $message = sprintf($message, "Expected authentication [$authentication] got [" . |
|
| 1228 | + $this->_browser->getAuthentication() . "]"); |
|
| 1229 | + return $this->assertTrue( |
|
| 1230 | + strtolower($this->_browser->getAuthentication()) == strtolower($authentication), |
|
| 1231 | + $message); |
|
| 1232 | + } |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + /** |
|
| 1236 | + * Checks that no authentication is necessary to view |
|
| 1237 | + * the desired page. |
|
| 1238 | + * @param string $message Message to display. |
|
| 1239 | + * @return boolean True if pass. |
|
| 1240 | + * @access public |
|
| 1241 | + */ |
|
| 1242 | + function assertNoAuthentication($message = '%s') { |
|
| 1243 | + $message = sprintf($message, "Expected no authentication type, got [" . |
|
| 1244 | + $this->_browser->getAuthentication() . "]"); |
|
| 1245 | + return $this->assertFalse($this->_browser->getAuthentication(), $message); |
|
| 1246 | + } |
|
| 1247 | + |
|
| 1248 | + /** |
|
| 1249 | + * Attempts to match the current security realm. |
|
| 1250 | + * @param string $realm Name of security realm. |
|
| 1251 | + * @param string $message Message to display. |
|
| 1252 | + * @return boolean True if pass. |
|
| 1253 | + * @access public |
|
| 1254 | + */ |
|
| 1255 | + function assertRealm($realm, $message = '%s') { |
|
| 1256 | + if (! SimpleExpectation::isExpectation($realm)) { |
|
| 1257 | + $realm = new EqualExpectation($realm); |
|
| 1258 | + } |
|
| 1259 | + return $this->assert( |
|
| 1260 | + $realm, |
|
| 1261 | + $this->_browser->getRealm(), |
|
| 1262 | + "Expected realm -> $message"); |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + /** |
|
| 1266 | + * Checks each header line for the required value. If no |
|
| 1267 | + * value is given then only an existence check is made. |
|
| 1268 | + * @param string $header Case insensitive header name. |
|
| 1269 | + * @param mixed $value Case sensitive trimmed string to |
|
| 1270 | + * match against. An expectation object |
|
| 1271 | + * can be used for pattern matching. |
|
| 1272 | + * @return boolean True if pass. |
|
| 1273 | + * @access public |
|
| 1274 | + */ |
|
| 1275 | + function assertHeader($header, $value = false, $message = '%s') { |
|
| 1276 | + return $this->assert( |
|
| 1277 | + new HttpHeaderExpectation($header, $value), |
|
| 1278 | + $this->_browser->getHeaders(), |
|
| 1279 | + $message); |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + /** |
|
| 1283 | + * @deprecated |
|
| 1284 | + */ |
|
| 1285 | + function assertHeaderPattern($header, $pattern, $message = '%s') { |
|
| 1286 | + return $this->assert( |
|
| 1287 | + new HttpHeaderExpectation($header, new PatternExpectation($pattern)), |
|
| 1288 | + $this->_browser->getHeaders(), |
|
| 1289 | + $message); |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * Confirms that the header type has not been received. |
|
| 1294 | + * Only the landing page is checked. If you want to check |
|
| 1295 | + * redirect pages, then you should limit redirects so |
|
| 1296 | + * as to capture the page you want. |
|
| 1297 | + * @param string $header Case insensitive header name. |
|
| 1298 | + * @return boolean True if pass. |
|
| 1299 | + * @access public |
|
| 1300 | + */ |
|
| 1301 | + function assertNoHeader($header, $message = '%s') { |
|
| 1302 | + return $this->assert( |
|
| 1303 | + new NoHttpHeaderExpectation($header), |
|
| 1304 | + $this->_browser->getHeaders(), |
|
| 1305 | + $message); |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + /** |
|
| 1309 | + * @deprecated |
|
| 1310 | + */ |
|
| 1311 | + function assertNoUnwantedHeader($header, $message = '%s') { |
|
| 1312 | + return $this->assertNoHeader($header, $message); |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + /** |
|
| 1316 | + * Tests the text between the title tags. |
|
| 1317 | + * @param string $title Expected title. |
|
| 1318 | + * @param string $message Message to display. |
|
| 1319 | + * @return boolean True if pass. |
|
| 1320 | + * @access public |
|
| 1321 | + */ |
|
| 1322 | + function assertTitle($title = false, $message = '%s') { |
|
| 1323 | + if (! SimpleExpectation::isExpectation($title)) { |
|
| 1324 | + $title = new EqualExpectation($title); |
|
| 1325 | + } |
|
| 1326 | + return $this->assert($title, $this->_browser->getTitle(), $message); |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + /** |
|
| 1330 | + * Will trigger a pass if the text is found in the plain |
|
| 1331 | + * text form of the page. |
|
| 1332 | + * @param string $text Text to look for. |
|
| 1333 | + * @param string $message Message to display. |
|
| 1334 | + * @return boolean True if pass. |
|
| 1335 | + * @access public |
|
| 1336 | + */ |
|
| 1337 | + function assertText($text, $message = '%s') { |
|
| 1338 | + return $this->assert( |
|
| 1339 | + new TextExpectation($text), |
|
| 1340 | + $this->_browser->getContentAsText(), |
|
| 1341 | + $message); |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + /** |
|
| 1345 | + * @deprecated |
|
| 1346 | + */ |
|
| 1347 | + function assertWantedText($text, $message = '%s') { |
|
| 1348 | + return $this->assertText($text, $message); |
|
| 1349 | + } |
|
| 1350 | + |
|
| 1351 | + /** |
|
| 1352 | + * Will trigger a pass if the text is not found in the plain |
|
| 1353 | + * text form of the page. |
|
| 1354 | + * @param string $text Text to look for. |
|
| 1355 | + * @param string $message Message to display. |
|
| 1356 | + * @return boolean True if pass. |
|
| 1357 | + * @access public |
|
| 1358 | + */ |
|
| 1359 | + function assertNoText($text, $message = '%s') { |
|
| 1360 | + return $this->assert( |
|
| 1361 | + new NoTextExpectation($text), |
|
| 1362 | + $this->_browser->getContentAsText(), |
|
| 1363 | + $message); |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + /** |
|
| 1367 | + * @deprecated |
|
| 1368 | + */ |
|
| 1369 | + function assertNoUnwantedText($text, $message = '%s') { |
|
| 1370 | + return $this->assertNoText($text, $message); |
|
| 1371 | + } |
|
| 1372 | + |
|
| 1373 | + /** |
|
| 1374 | + * Will trigger a pass if the Perl regex pattern |
|
| 1375 | + * is found in the raw content. |
|
| 1376 | + * @param string $pattern Perl regex to look for including |
|
| 1377 | + * the regex delimiters. |
|
| 1378 | + * @param string $message Message to display. |
|
| 1379 | + * @return boolean True if pass. |
|
| 1380 | + * @access public |
|
| 1381 | + */ |
|
| 1382 | + function assertPattern($pattern, $message = '%s') { |
|
| 1383 | + return $this->assert( |
|
| 1384 | + new PatternExpectation($pattern), |
|
| 1385 | + $this->_browser->getContent(), |
|
| 1386 | + $message); |
|
| 1387 | + } |
|
| 1388 | + |
|
| 1389 | + /** |
|
| 1390 | + * @deprecated |
|
| 1391 | + */ |
|
| 1392 | + function assertWantedPattern($pattern, $message = '%s') { |
|
| 1393 | + return $this->assertPattern($pattern, $message); |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + /** |
|
| 1397 | + * Will trigger a pass if the perl regex pattern |
|
| 1398 | + * is not present in raw content. |
|
| 1399 | + * @param string $pattern Perl regex to look for including |
|
| 1400 | + * the regex delimiters. |
|
| 1401 | + * @param string $message Message to display. |
|
| 1402 | + * @return boolean True if pass. |
|
| 1403 | + * @access public |
|
| 1404 | + */ |
|
| 1405 | + function assertNoPattern($pattern, $message = '%s') { |
|
| 1406 | + return $this->assert( |
|
| 1407 | + new NoPatternExpectation($pattern), |
|
| 1408 | + $this->_browser->getContent(), |
|
| 1409 | + $message); |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * @deprecated |
|
| 1414 | + */ |
|
| 1415 | + function assertNoUnwantedPattern($pattern, $message = '%s') { |
|
| 1416 | + return $this->assertNoPattern($pattern, $message); |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + /** |
|
| 1420 | + * Checks that a cookie is set for the current page |
|
| 1421 | + * and optionally checks the value. |
|
| 1422 | + * @param string $name Name of cookie to test. |
|
| 1423 | + * @param string $expected Expected value as a string or |
|
| 1424 | + * false if any value will do. |
|
| 1425 | + * @param string $message Message to display. |
|
| 1426 | + * @return boolean True if pass. |
|
| 1427 | + * @access public |
|
| 1428 | + */ |
|
| 1429 | + function assertCookie($name, $expected = false, $message = '%s') { |
|
| 1430 | + $value = $this->getCookie($name); |
|
| 1431 | + if (! $expected) { |
|
| 1432 | + return $this->assertTrue( |
|
| 1433 | + $value, |
|
| 1434 | + sprintf($message, "Expecting cookie [$name]")); |
|
| 1435 | + } |
|
| 1436 | + if (! SimpleExpectation::isExpectation($expected)) { |
|
| 1437 | + $expected = new EqualExpectation($expected); |
|
| 1438 | + } |
|
| 1439 | + return $this->assert($expected, $value, "Expecting cookie [$name] -> $message"); |
|
| 1440 | + } |
|
| 1441 | + |
|
| 1442 | + /** |
|
| 1443 | + * Checks that no cookie is present or that it has |
|
| 1444 | + * been successfully cleared. |
|
| 1445 | + * @param string $name Name of cookie to test. |
|
| 1446 | + * @param string $message Message to display. |
|
| 1447 | + * @return boolean True if pass. |
|
| 1448 | + * @access public |
|
| 1449 | + */ |
|
| 1450 | + function assertNoCookie($name, $message = '%s') { |
|
| 1451 | + return $this->assertTrue( |
|
| 1452 | + $this->getCookie($name) === false, |
|
| 1453 | + sprintf($message, "Not expecting cookie [$name]")); |
|
| 1454 | + } |
|
| 1455 | + } |
|
| 1456 | 1456 | \ No newline at end of file |
@@ -1,401 +1,401 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage UnitTester |
|
| 6 | - * @version $Id: dumper.php 1532 2006-12-01 12:28:55Z xue $ |
|
| 7 | - */ |
|
| 8 | - /** |
|
| 9 | - * does type matter |
|
| 10 | - */ |
|
| 11 | - if (! defined('TYPE_MATTERS')) { |
|
| 12 | - define('TYPE_MATTERS', true); |
|
| 13 | - } |
|
| 2 | + /** |
|
| 3 | + * base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage UnitTester |
|
| 6 | + * @version $Id: dumper.php 1532 2006-12-01 12:28:55Z xue $ |
|
| 7 | + */ |
|
| 8 | + /** |
|
| 9 | + * does type matter |
|
| 10 | + */ |
|
| 11 | + if (! defined('TYPE_MATTERS')) { |
|
| 12 | + define('TYPE_MATTERS', true); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Displays variables as text and does diffs. |
|
| 15 | + /** |
|
| 16 | + * Displays variables as text and does diffs. |
|
| 17 | 17 | * @package SimpleTest |
| 18 | 18 | * @subpackage UnitTester |
| 19 | - */ |
|
| 20 | - class SimpleDumper { |
|
| 19 | + */ |
|
| 20 | + class SimpleDumper { |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Renders a variable in a shorter form than print_r(). |
|
| 24 | - * @param mixed $value Variable to render as a string. |
|
| 25 | - * @return string Human readable string form. |
|
| 26 | - * @access public |
|
| 27 | - */ |
|
| 28 | - function describeValue($value) { |
|
| 29 | - $type = $this->getType($value); |
|
| 30 | - switch($type) { |
|
| 31 | - case "Null": |
|
| 32 | - return "NULL"; |
|
| 33 | - case "Boolean": |
|
| 34 | - return "Boolean: " . ($value ? "true" : "false"); |
|
| 35 | - case "Array": |
|
| 36 | - return "Array: " . count($value) . " items"; |
|
| 37 | - case "Object": |
|
| 38 | - return "Object: of " . get_class($value); |
|
| 39 | - case "String": |
|
| 40 | - return "String: " . $this->clipString($value, 200); |
|
| 41 | - default: |
|
| 42 | - return "$type: $value"; |
|
| 43 | - } |
|
| 44 | - return "Unknown"; |
|
| 45 | - } |
|
| 22 | + /** |
|
| 23 | + * Renders a variable in a shorter form than print_r(). |
|
| 24 | + * @param mixed $value Variable to render as a string. |
|
| 25 | + * @return string Human readable string form. |
|
| 26 | + * @access public |
|
| 27 | + */ |
|
| 28 | + function describeValue($value) { |
|
| 29 | + $type = $this->getType($value); |
|
| 30 | + switch($type) { |
|
| 31 | + case "Null": |
|
| 32 | + return "NULL"; |
|
| 33 | + case "Boolean": |
|
| 34 | + return "Boolean: " . ($value ? "true" : "false"); |
|
| 35 | + case "Array": |
|
| 36 | + return "Array: " . count($value) . " items"; |
|
| 37 | + case "Object": |
|
| 38 | + return "Object: of " . get_class($value); |
|
| 39 | + case "String": |
|
| 40 | + return "String: " . $this->clipString($value, 200); |
|
| 41 | + default: |
|
| 42 | + return "$type: $value"; |
|
| 43 | + } |
|
| 44 | + return "Unknown"; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Gets the string representation of a type. |
|
| 49 | - * @param mixed $value Variable to check against. |
|
| 50 | - * @return string Type. |
|
| 51 | - * @access public |
|
| 52 | - */ |
|
| 53 | - function getType($value) { |
|
| 54 | - if (! isset($value)) { |
|
| 55 | - return "Null"; |
|
| 56 | - } elseif (is_bool($value)) { |
|
| 57 | - return "Boolean"; |
|
| 58 | - } elseif (is_string($value)) { |
|
| 59 | - return "String"; |
|
| 60 | - } elseif (is_integer($value)) { |
|
| 61 | - return "Integer"; |
|
| 62 | - } elseif (is_float($value)) { |
|
| 63 | - return "Float"; |
|
| 64 | - } elseif (is_array($value)) { |
|
| 65 | - return "Array"; |
|
| 66 | - } elseif (is_resource($value)) { |
|
| 67 | - return "Resource"; |
|
| 68 | - } elseif (is_object($value)) { |
|
| 69 | - return "Object"; |
|
| 70 | - } |
|
| 71 | - return "Unknown"; |
|
| 72 | - } |
|
| 47 | + /** |
|
| 48 | + * Gets the string representation of a type. |
|
| 49 | + * @param mixed $value Variable to check against. |
|
| 50 | + * @return string Type. |
|
| 51 | + * @access public |
|
| 52 | + */ |
|
| 53 | + function getType($value) { |
|
| 54 | + if (! isset($value)) { |
|
| 55 | + return "Null"; |
|
| 56 | + } elseif (is_bool($value)) { |
|
| 57 | + return "Boolean"; |
|
| 58 | + } elseif (is_string($value)) { |
|
| 59 | + return "String"; |
|
| 60 | + } elseif (is_integer($value)) { |
|
| 61 | + return "Integer"; |
|
| 62 | + } elseif (is_float($value)) { |
|
| 63 | + return "Float"; |
|
| 64 | + } elseif (is_array($value)) { |
|
| 65 | + return "Array"; |
|
| 66 | + } elseif (is_resource($value)) { |
|
| 67 | + return "Resource"; |
|
| 68 | + } elseif (is_object($value)) { |
|
| 69 | + return "Object"; |
|
| 70 | + } |
|
| 71 | + return "Unknown"; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Creates a human readable description of the |
|
| 76 | - * difference between two variables. Uses a |
|
| 77 | - * dynamic call. |
|
| 78 | - * @param mixed $first First variable. |
|
| 79 | - * @param mixed $second Value to compare with. |
|
| 80 | - * @param boolean $identical If true then type anomolies count. |
|
| 81 | - * @return string Description of difference. |
|
| 82 | - * @access public |
|
| 83 | - */ |
|
| 84 | - function describeDifference($first, $second, $identical = false) { |
|
| 85 | - if ($identical) { |
|
| 86 | - if (! $this->_isTypeMatch($first, $second)) { |
|
| 87 | - return "with type mismatch as [" . $this->describeValue($first) . |
|
| 88 | - "] does not match [" . $this->describeValue($second) . "]"; |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - $type = $this->getType($first); |
|
| 92 | - if ($type == "Unknown") { |
|
| 93 | - return "with unknown type"; |
|
| 94 | - } |
|
| 95 | - $method = '_describe' . $type . 'Difference'; |
|
| 96 | - return $this->$method($first, $second, $identical); |
|
| 97 | - } |
|
| 74 | + /** |
|
| 75 | + * Creates a human readable description of the |
|
| 76 | + * difference between two variables. Uses a |
|
| 77 | + * dynamic call. |
|
| 78 | + * @param mixed $first First variable. |
|
| 79 | + * @param mixed $second Value to compare with. |
|
| 80 | + * @param boolean $identical If true then type anomolies count. |
|
| 81 | + * @return string Description of difference. |
|
| 82 | + * @access public |
|
| 83 | + */ |
|
| 84 | + function describeDifference($first, $second, $identical = false) { |
|
| 85 | + if ($identical) { |
|
| 86 | + if (! $this->_isTypeMatch($first, $second)) { |
|
| 87 | + return "with type mismatch as [" . $this->describeValue($first) . |
|
| 88 | + "] does not match [" . $this->describeValue($second) . "]"; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + $type = $this->getType($first); |
|
| 92 | + if ($type == "Unknown") { |
|
| 93 | + return "with unknown type"; |
|
| 94 | + } |
|
| 95 | + $method = '_describe' . $type . 'Difference'; |
|
| 96 | + return $this->$method($first, $second, $identical); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Tests to see if types match. |
|
| 101 | - * @param mixed $first First variable. |
|
| 102 | - * @param mixed $second Value to compare with. |
|
| 103 | - * @return boolean True if matches. |
|
| 104 | - * @access private |
|
| 105 | - */ |
|
| 106 | - function _isTypeMatch($first, $second) { |
|
| 107 | - return ($this->getType($first) == $this->getType($second)); |
|
| 108 | - } |
|
| 99 | + /** |
|
| 100 | + * Tests to see if types match. |
|
| 101 | + * @param mixed $first First variable. |
|
| 102 | + * @param mixed $second Value to compare with. |
|
| 103 | + * @return boolean True if matches. |
|
| 104 | + * @access private |
|
| 105 | + */ |
|
| 106 | + function _isTypeMatch($first, $second) { |
|
| 107 | + return ($this->getType($first) == $this->getType($second)); |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Clips a string to a maximum length. |
|
| 112 | - * @param string $value String to truncate. |
|
| 113 | - * @param integer $size Minimum string size to show. |
|
| 114 | - * @param integer $position Centre of string section. |
|
| 115 | - * @return string Shortened version. |
|
| 116 | - * @access public |
|
| 117 | - */ |
|
| 118 | - function clipString($value, $size, $position = 0) { |
|
| 119 | - $length = strlen($value); |
|
| 120 | - if ($length <= $size) { |
|
| 121 | - return $value; |
|
| 122 | - } |
|
| 123 | - $position = min($position, $length); |
|
| 124 | - $start = ($size/2 > $position ? 0 : $position - $size/2); |
|
| 125 | - if ($start + $size > $length) { |
|
| 126 | - $start = $length - $size; |
|
| 127 | - } |
|
| 128 | - $value = substr($value, $start, $size); |
|
| 129 | - return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : ""); |
|
| 130 | - } |
|
| 110 | + /** |
|
| 111 | + * Clips a string to a maximum length. |
|
| 112 | + * @param string $value String to truncate. |
|
| 113 | + * @param integer $size Minimum string size to show. |
|
| 114 | + * @param integer $position Centre of string section. |
|
| 115 | + * @return string Shortened version. |
|
| 116 | + * @access public |
|
| 117 | + */ |
|
| 118 | + function clipString($value, $size, $position = 0) { |
|
| 119 | + $length = strlen($value); |
|
| 120 | + if ($length <= $size) { |
|
| 121 | + return $value; |
|
| 122 | + } |
|
| 123 | + $position = min($position, $length); |
|
| 124 | + $start = ($size/2 > $position ? 0 : $position - $size/2); |
|
| 125 | + if ($start + $size > $length) { |
|
| 126 | + $start = $length - $size; |
|
| 127 | + } |
|
| 128 | + $value = substr($value, $start, $size); |
|
| 129 | + return ($start > 0 ? "..." : "") . $value . ($start + $size < $length ? "..." : ""); |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * Creates a human readable description of the |
|
| 134 | - * difference between two variables. The minimal |
|
| 135 | - * version. |
|
| 136 | - * @param null $first First value. |
|
| 137 | - * @param mixed $second Value to compare with. |
|
| 138 | - * @return string Human readable description. |
|
| 139 | - * @access private |
|
| 140 | - */ |
|
| 141 | - function _describeGenericDifference($first, $second) { |
|
| 142 | - return "as [" . $this->describeValue($first) . |
|
| 143 | - "] does not match [" . |
|
| 144 | - $this->describeValue($second) . "]"; |
|
| 145 | - } |
|
| 132 | + /** |
|
| 133 | + * Creates a human readable description of the |
|
| 134 | + * difference between two variables. The minimal |
|
| 135 | + * version. |
|
| 136 | + * @param null $first First value. |
|
| 137 | + * @param mixed $second Value to compare with. |
|
| 138 | + * @return string Human readable description. |
|
| 139 | + * @access private |
|
| 140 | + */ |
|
| 141 | + function _describeGenericDifference($first, $second) { |
|
| 142 | + return "as [" . $this->describeValue($first) . |
|
| 143 | + "] does not match [" . |
|
| 144 | + $this->describeValue($second) . "]"; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Creates a human readable description of the |
|
| 149 | - * difference between a null and another variable. |
|
| 150 | - * @param null $first First null. |
|
| 151 | - * @param mixed $second Null to compare with. |
|
| 152 | - * @param boolean $identical If true then type anomolies count. |
|
| 153 | - * @return string Human readable description. |
|
| 154 | - * @access private |
|
| 155 | - */ |
|
| 156 | - function _describeNullDifference($first, $second, $identical) { |
|
| 157 | - return $this->_describeGenericDifference($first, $second); |
|
| 158 | - } |
|
| 147 | + /** |
|
| 148 | + * Creates a human readable description of the |
|
| 149 | + * difference between a null and another variable. |
|
| 150 | + * @param null $first First null. |
|
| 151 | + * @param mixed $second Null to compare with. |
|
| 152 | + * @param boolean $identical If true then type anomolies count. |
|
| 153 | + * @return string Human readable description. |
|
| 154 | + * @access private |
|
| 155 | + */ |
|
| 156 | + function _describeNullDifference($first, $second, $identical) { |
|
| 157 | + return $this->_describeGenericDifference($first, $second); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Creates a human readable description of the |
|
| 162 | - * difference between a boolean and another variable. |
|
| 163 | - * @param boolean $first First boolean. |
|
| 164 | - * @param mixed $second Boolean to compare with. |
|
| 165 | - * @param boolean $identical If true then type anomolies count. |
|
| 166 | - * @return string Human readable description. |
|
| 167 | - * @access private |
|
| 168 | - */ |
|
| 169 | - function _describeBooleanDifference($first, $second, $identical) { |
|
| 170 | - return $this->_describeGenericDifference($first, $second); |
|
| 171 | - } |
|
| 160 | + /** |
|
| 161 | + * Creates a human readable description of the |
|
| 162 | + * difference between a boolean and another variable. |
|
| 163 | + * @param boolean $first First boolean. |
|
| 164 | + * @param mixed $second Boolean to compare with. |
|
| 165 | + * @param boolean $identical If true then type anomolies count. |
|
| 166 | + * @return string Human readable description. |
|
| 167 | + * @access private |
|
| 168 | + */ |
|
| 169 | + function _describeBooleanDifference($first, $second, $identical) { |
|
| 170 | + return $this->_describeGenericDifference($first, $second); |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Creates a human readable description of the |
|
| 175 | - * difference between a string and another variable. |
|
| 176 | - * @param string $first First string. |
|
| 177 | - * @param mixed $second String to compare with. |
|
| 178 | - * @param boolean $identical If true then type anomolies count. |
|
| 179 | - * @return string Human readable description. |
|
| 180 | - * @access private |
|
| 181 | - */ |
|
| 182 | - function _describeStringDifference($first, $second, $identical) { |
|
| 183 | - if (is_object($second) || is_array($second)) { |
|
| 184 | - return $this->_describeGenericDifference($first, $second); |
|
| 185 | - } |
|
| 186 | - $position = $this->_stringDiffersAt($first, $second); |
|
| 187 | - $message = "at character $position"; |
|
| 188 | - $message .= " with [" . |
|
| 189 | - $this->clipString($first, 200, $position) . "] and [" . |
|
| 190 | - $this->clipString($second, 200, $position) . "]"; |
|
| 191 | - return $message; |
|
| 192 | - } |
|
| 173 | + /** |
|
| 174 | + * Creates a human readable description of the |
|
| 175 | + * difference between a string and another variable. |
|
| 176 | + * @param string $first First string. |
|
| 177 | + * @param mixed $second String to compare with. |
|
| 178 | + * @param boolean $identical If true then type anomolies count. |
|
| 179 | + * @return string Human readable description. |
|
| 180 | + * @access private |
|
| 181 | + */ |
|
| 182 | + function _describeStringDifference($first, $second, $identical) { |
|
| 183 | + if (is_object($second) || is_array($second)) { |
|
| 184 | + return $this->_describeGenericDifference($first, $second); |
|
| 185 | + } |
|
| 186 | + $position = $this->_stringDiffersAt($first, $second); |
|
| 187 | + $message = "at character $position"; |
|
| 188 | + $message .= " with [" . |
|
| 189 | + $this->clipString($first, 200, $position) . "] and [" . |
|
| 190 | + $this->clipString($second, 200, $position) . "]"; |
|
| 191 | + return $message; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Creates a human readable description of the |
|
| 196 | - * difference between an integer and another variable. |
|
| 197 | - * @param integer $first First number. |
|
| 198 | - * @param mixed $second Number to compare with. |
|
| 199 | - * @param boolean $identical If true then type anomolies count. |
|
| 200 | - * @return string Human readable description. |
|
| 201 | - * @access private |
|
| 202 | - */ |
|
| 203 | - function _describeIntegerDifference($first, $second, $identical) { |
|
| 204 | - if (is_object($second) || is_array($second)) { |
|
| 205 | - return $this->_describeGenericDifference($first, $second); |
|
| 206 | - } |
|
| 207 | - return "because [" . $this->describeValue($first) . |
|
| 208 | - "] differs from [" . |
|
| 209 | - $this->describeValue($second) . "] by " . |
|
| 210 | - abs($first - $second); |
|
| 211 | - } |
|
| 194 | + /** |
|
| 195 | + * Creates a human readable description of the |
|
| 196 | + * difference between an integer and another variable. |
|
| 197 | + * @param integer $first First number. |
|
| 198 | + * @param mixed $second Number to compare with. |
|
| 199 | + * @param boolean $identical If true then type anomolies count. |
|
| 200 | + * @return string Human readable description. |
|
| 201 | + * @access private |
|
| 202 | + */ |
|
| 203 | + function _describeIntegerDifference($first, $second, $identical) { |
|
| 204 | + if (is_object($second) || is_array($second)) { |
|
| 205 | + return $this->_describeGenericDifference($first, $second); |
|
| 206 | + } |
|
| 207 | + return "because [" . $this->describeValue($first) . |
|
| 208 | + "] differs from [" . |
|
| 209 | + $this->describeValue($second) . "] by " . |
|
| 210 | + abs($first - $second); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * Creates a human readable description of the |
|
| 215 | - * difference between two floating point numbers. |
|
| 216 | - * @param float $first First float. |
|
| 217 | - * @param mixed $second Float to compare with. |
|
| 218 | - * @param boolean $identical If true then type anomolies count. |
|
| 219 | - * @return string Human readable description. |
|
| 220 | - * @access private |
|
| 221 | - */ |
|
| 222 | - function _describeFloatDifference($first, $second, $identical) { |
|
| 223 | - if (is_object($second) || is_array($second)) { |
|
| 224 | - return $this->_describeGenericDifference($first, $second); |
|
| 225 | - } |
|
| 226 | - return "because [" . $this->describeValue($first) . |
|
| 227 | - "] differs from [" . |
|
| 228 | - $this->describeValue($second) . "] by " . |
|
| 229 | - abs($first - $second); |
|
| 230 | - } |
|
| 213 | + /** |
|
| 214 | + * Creates a human readable description of the |
|
| 215 | + * difference between two floating point numbers. |
|
| 216 | + * @param float $first First float. |
|
| 217 | + * @param mixed $second Float to compare with. |
|
| 218 | + * @param boolean $identical If true then type anomolies count. |
|
| 219 | + * @return string Human readable description. |
|
| 220 | + * @access private |
|
| 221 | + */ |
|
| 222 | + function _describeFloatDifference($first, $second, $identical) { |
|
| 223 | + if (is_object($second) || is_array($second)) { |
|
| 224 | + return $this->_describeGenericDifference($first, $second); |
|
| 225 | + } |
|
| 226 | + return "because [" . $this->describeValue($first) . |
|
| 227 | + "] differs from [" . |
|
| 228 | + $this->describeValue($second) . "] by " . |
|
| 229 | + abs($first - $second); |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Creates a human readable description of the |
|
| 234 | - * difference between two arrays. |
|
| 235 | - * @param array $first First array. |
|
| 236 | - * @param mixed $second Array to compare with. |
|
| 237 | - * @param boolean $identical If true then type anomolies count. |
|
| 238 | - * @return string Human readable description. |
|
| 239 | - * @access private |
|
| 240 | - */ |
|
| 241 | - function _describeArrayDifference($first, $second, $identical) { |
|
| 242 | - if (! is_array($second)) { |
|
| 243 | - return $this->_describeGenericDifference($first, $second); |
|
| 244 | - } |
|
| 245 | - if (! $this->_isMatchingKeys($first, $second, $identical)) { |
|
| 246 | - return "as key list [" . |
|
| 247 | - implode(", ", array_keys($first)) . "] does not match key list [" . |
|
| 248 | - implode(", ", array_keys($second)) . "]"; |
|
| 249 | - } |
|
| 250 | - foreach (array_keys($first) as $key) { |
|
| 251 | - if ($identical && ($first[$key] === $second[$key])) { |
|
| 252 | - continue; |
|
| 253 | - } |
|
| 254 | - if (! $identical && ($first[$key] == $second[$key])) { |
|
| 255 | - continue; |
|
| 256 | - } |
|
| 257 | - return "with member [$key] " . $this->describeDifference( |
|
| 258 | - $first[$key], |
|
| 259 | - $second[$key], |
|
| 260 | - $identical); |
|
| 261 | - } |
|
| 262 | - return ""; |
|
| 263 | - } |
|
| 232 | + /** |
|
| 233 | + * Creates a human readable description of the |
|
| 234 | + * difference between two arrays. |
|
| 235 | + * @param array $first First array. |
|
| 236 | + * @param mixed $second Array to compare with. |
|
| 237 | + * @param boolean $identical If true then type anomolies count. |
|
| 238 | + * @return string Human readable description. |
|
| 239 | + * @access private |
|
| 240 | + */ |
|
| 241 | + function _describeArrayDifference($first, $second, $identical) { |
|
| 242 | + if (! is_array($second)) { |
|
| 243 | + return $this->_describeGenericDifference($first, $second); |
|
| 244 | + } |
|
| 245 | + if (! $this->_isMatchingKeys($first, $second, $identical)) { |
|
| 246 | + return "as key list [" . |
|
| 247 | + implode(", ", array_keys($first)) . "] does not match key list [" . |
|
| 248 | + implode(", ", array_keys($second)) . "]"; |
|
| 249 | + } |
|
| 250 | + foreach (array_keys($first) as $key) { |
|
| 251 | + if ($identical && ($first[$key] === $second[$key])) { |
|
| 252 | + continue; |
|
| 253 | + } |
|
| 254 | + if (! $identical && ($first[$key] == $second[$key])) { |
|
| 255 | + continue; |
|
| 256 | + } |
|
| 257 | + return "with member [$key] " . $this->describeDifference( |
|
| 258 | + $first[$key], |
|
| 259 | + $second[$key], |
|
| 260 | + $identical); |
|
| 261 | + } |
|
| 262 | + return ""; |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - /** |
|
| 266 | - * Compares two arrays to see if their key lists match. |
|
| 267 | - * For an identical match, the ordering and types of the keys |
|
| 268 | - * is significant. |
|
| 269 | - * @param array $first First array. |
|
| 270 | - * @param array $second Array to compare with. |
|
| 271 | - * @param boolean $identical If true then type anomolies count. |
|
| 272 | - * @return boolean True if matching. |
|
| 273 | - * @access private |
|
| 274 | - */ |
|
| 275 | - function _isMatchingKeys($first, $second, $identical) { |
|
| 276 | - $first_keys = array_keys($first); |
|
| 277 | - $second_keys = array_keys($second); |
|
| 278 | - if ($identical) { |
|
| 279 | - return ($first_keys === $second_keys); |
|
| 280 | - } |
|
| 281 | - sort($first_keys); |
|
| 282 | - sort($second_keys); |
|
| 283 | - return ($first_keys == $second_keys); |
|
| 284 | - } |
|
| 265 | + /** |
|
| 266 | + * Compares two arrays to see if their key lists match. |
|
| 267 | + * For an identical match, the ordering and types of the keys |
|
| 268 | + * is significant. |
|
| 269 | + * @param array $first First array. |
|
| 270 | + * @param array $second Array to compare with. |
|
| 271 | + * @param boolean $identical If true then type anomolies count. |
|
| 272 | + * @return boolean True if matching. |
|
| 273 | + * @access private |
|
| 274 | + */ |
|
| 275 | + function _isMatchingKeys($first, $second, $identical) { |
|
| 276 | + $first_keys = array_keys($first); |
|
| 277 | + $second_keys = array_keys($second); |
|
| 278 | + if ($identical) { |
|
| 279 | + return ($first_keys === $second_keys); |
|
| 280 | + } |
|
| 281 | + sort($first_keys); |
|
| 282 | + sort($second_keys); |
|
| 283 | + return ($first_keys == $second_keys); |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Creates a human readable description of the |
|
| 288 | - * difference between a resource and another variable. |
|
| 289 | - * @param resource $first First resource. |
|
| 290 | - * @param mixed $second Resource to compare with. |
|
| 291 | - * @param boolean $identical If true then type anomolies count. |
|
| 292 | - * @return string Human readable description. |
|
| 293 | - * @access private |
|
| 294 | - */ |
|
| 295 | - function _describeResourceDifference($first, $second, $identical) { |
|
| 296 | - return $this->_describeGenericDifference($first, $second); |
|
| 297 | - } |
|
| 286 | + /** |
|
| 287 | + * Creates a human readable description of the |
|
| 288 | + * difference between a resource and another variable. |
|
| 289 | + * @param resource $first First resource. |
|
| 290 | + * @param mixed $second Resource to compare with. |
|
| 291 | + * @param boolean $identical If true then type anomolies count. |
|
| 292 | + * @return string Human readable description. |
|
| 293 | + * @access private |
|
| 294 | + */ |
|
| 295 | + function _describeResourceDifference($first, $second, $identical) { |
|
| 296 | + return $this->_describeGenericDifference($first, $second); |
|
| 297 | + } |
|
| 298 | 298 | |
| 299 | - /** |
|
| 300 | - * Creates a human readable description of the |
|
| 301 | - * difference between two objects. |
|
| 302 | - * @param object $first First object. |
|
| 303 | - * @param mixed $second Object to compare with. |
|
| 304 | - * @param boolean $identical If true then type anomolies count. |
|
| 305 | - * @return string Human readable description. |
|
| 306 | - * @access private |
|
| 307 | - */ |
|
| 308 | - function _describeObjectDifference($first, $second, $identical) { |
|
| 309 | - if (! is_object($second)) { |
|
| 310 | - return $this->_describeGenericDifference($first, $second); |
|
| 311 | - } |
|
| 312 | - return $this->_describeArrayDifference( |
|
| 313 | - get_object_vars($first), |
|
| 314 | - get_object_vars($second), |
|
| 315 | - $identical); |
|
| 316 | - } |
|
| 299 | + /** |
|
| 300 | + * Creates a human readable description of the |
|
| 301 | + * difference between two objects. |
|
| 302 | + * @param object $first First object. |
|
| 303 | + * @param mixed $second Object to compare with. |
|
| 304 | + * @param boolean $identical If true then type anomolies count. |
|
| 305 | + * @return string Human readable description. |
|
| 306 | + * @access private |
|
| 307 | + */ |
|
| 308 | + function _describeObjectDifference($first, $second, $identical) { |
|
| 309 | + if (! is_object($second)) { |
|
| 310 | + return $this->_describeGenericDifference($first, $second); |
|
| 311 | + } |
|
| 312 | + return $this->_describeArrayDifference( |
|
| 313 | + get_object_vars($first), |
|
| 314 | + get_object_vars($second), |
|
| 315 | + $identical); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * Find the first character position that differs |
|
| 320 | - * in two strings by binary chop. |
|
| 321 | - * @param string $first First string. |
|
| 322 | - * @param string $second String to compare with. |
|
| 323 | - * @return integer Position of first differing |
|
| 324 | - * character. |
|
| 325 | - * @access private |
|
| 326 | - */ |
|
| 327 | - function _stringDiffersAt($first, $second) { |
|
| 328 | - if (! $first || ! $second) { |
|
| 329 | - return 0; |
|
| 330 | - } |
|
| 331 | - if (strlen($first) < strlen($second)) { |
|
| 332 | - list($first, $second) = array($second, $first); |
|
| 333 | - } |
|
| 334 | - $position = 0; |
|
| 335 | - $step = strlen($first); |
|
| 336 | - while ($step > 1) { |
|
| 337 | - $step = (integer)(($step + 1) / 2); |
|
| 338 | - if (strncmp($first, $second, $position + $step) == 0) { |
|
| 339 | - $position += $step; |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - return $position; |
|
| 343 | - } |
|
| 318 | + /** |
|
| 319 | + * Find the first character position that differs |
|
| 320 | + * in two strings by binary chop. |
|
| 321 | + * @param string $first First string. |
|
| 322 | + * @param string $second String to compare with. |
|
| 323 | + * @return integer Position of first differing |
|
| 324 | + * character. |
|
| 325 | + * @access private |
|
| 326 | + */ |
|
| 327 | + function _stringDiffersAt($first, $second) { |
|
| 328 | + if (! $first || ! $second) { |
|
| 329 | + return 0; |
|
| 330 | + } |
|
| 331 | + if (strlen($first) < strlen($second)) { |
|
| 332 | + list($first, $second) = array($second, $first); |
|
| 333 | + } |
|
| 334 | + $position = 0; |
|
| 335 | + $step = strlen($first); |
|
| 336 | + while ($step > 1) { |
|
| 337 | + $step = (integer)(($step + 1) / 2); |
|
| 338 | + if (strncmp($first, $second, $position + $step) == 0) { |
|
| 339 | + $position += $step; |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + return $position; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * Sends a formatted dump of a variable to a string. |
|
| 347 | - * @param mixed $variable Variable to display. |
|
| 348 | - * @return string Output from print_r(). |
|
| 349 | - * @access public |
|
| 350 | - * @static |
|
| 351 | - */ |
|
| 352 | - static function dump($variable) { |
|
| 353 | - ob_start(); |
|
| 354 | - print_r($variable); |
|
| 355 | - $formatted = ob_get_contents(); |
|
| 356 | - ob_end_clean(); |
|
| 357 | - return $formatted; |
|
| 358 | - } |
|
| 345 | + /** |
|
| 346 | + * Sends a formatted dump of a variable to a string. |
|
| 347 | + * @param mixed $variable Variable to display. |
|
| 348 | + * @return string Output from print_r(). |
|
| 349 | + * @access public |
|
| 350 | + * @static |
|
| 351 | + */ |
|
| 352 | + static function dump($variable) { |
|
| 353 | + ob_start(); |
|
| 354 | + print_r($variable); |
|
| 355 | + $formatted = ob_get_contents(); |
|
| 356 | + ob_end_clean(); |
|
| 357 | + return $formatted; |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | - /** |
|
| 361 | - * Extracts the last assertion that was not within |
|
| 362 | - * Simpletest itself. The name must start with "assert". |
|
| 363 | - * @param array $stack List of stack frames. |
|
| 364 | - * @access public |
|
| 365 | - * @static |
|
| 366 | - */ |
|
| 367 | - static function getFormattedAssertionLine($stack) { |
|
| 368 | - foreach ($stack as $frame) { |
|
| 369 | - if (isset($frame['file'])) { |
|
| 370 | - if (strpos($frame['file'], SIMPLE_TEST) !== false) { |
|
| 371 | - if (dirname($frame['file']) . '/' == SIMPLE_TEST) { |
|
| 372 | - continue; |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - if (SimpleDumper::_stackFrameIsAnAssertion($frame)) { |
|
| 377 | - return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']'; |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - return ''; |
|
| 381 | - } |
|
| 360 | + /** |
|
| 361 | + * Extracts the last assertion that was not within |
|
| 362 | + * Simpletest itself. The name must start with "assert". |
|
| 363 | + * @param array $stack List of stack frames. |
|
| 364 | + * @access public |
|
| 365 | + * @static |
|
| 366 | + */ |
|
| 367 | + static function getFormattedAssertionLine($stack) { |
|
| 368 | + foreach ($stack as $frame) { |
|
| 369 | + if (isset($frame['file'])) { |
|
| 370 | + if (strpos($frame['file'], SIMPLE_TEST) !== false) { |
|
| 371 | + if (dirname($frame['file']) . '/' == SIMPLE_TEST) { |
|
| 372 | + continue; |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + if (SimpleDumper::_stackFrameIsAnAssertion($frame)) { |
|
| 377 | + return ' at [' . $frame['file'] . ' line ' . $frame['line'] . ']'; |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + return ''; |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - /** |
|
| 384 | - * Tries to determine if the method call is an assertion. |
|
| 385 | - * @param array $frame PHP stack frame. |
|
| 386 | - * @access private |
|
| 387 | - * @static |
|
| 388 | - */ |
|
| 389 | - static function _stackFrameIsAnAssertion($frame) { |
|
| 390 | - if (($frame['function'] == 'fail') || ($frame['function'] == 'pass')) { |
|
| 391 | - return true; |
|
| 392 | - } |
|
| 393 | - if (strncmp($frame['function'], 'assert', 6) == 0) { |
|
| 394 | - return true; |
|
| 395 | - } |
|
| 396 | - if (strncmp($frame['function'], 'expect', 6) == 0) { |
|
| 397 | - return true; |
|
| 398 | - } |
|
| 399 | - return false; |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | 383 | \ No newline at end of file |
| 384 | + /** |
|
| 385 | + * Tries to determine if the method call is an assertion. |
|
| 386 | + * @param array $frame PHP stack frame. |
|
| 387 | + * @access private |
|
| 388 | + * @static |
|
| 389 | + */ |
|
| 390 | + static function _stackFrameIsAnAssertion($frame) { |
|
| 391 | + if (($frame['function'] == 'fail') || ($frame['function'] == 'pass')) { |
|
| 392 | + return true; |
|
| 393 | + } |
|
| 394 | + if (strncmp($frame['function'], 'assert', 6) == 0) { |
|
| 395 | + return true; |
|
| 396 | + } |
|
| 397 | + if (strncmp($frame['function'], 'expect', 6) == 0) { |
|
| 398 | + return true; |
|
| 399 | + } |
|
| 400 | + return false; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | 403 | \ No newline at end of file |
@@ -1,299 +1,299 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * Base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage UnitTester |
|
| 6 | - * @version $Id: runner.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | - */ |
|
| 2 | + /** |
|
| 3 | + * Base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage UnitTester |
|
| 6 | + * @version $Id: runner.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | - /**#@+ |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * Includes SimpleTest files and defined the root constant |
| 11 | 11 | * for dependent libraries. |
| 12 | 12 | */ |
| 13 | - require_once(dirname(__FILE__) . '/errors.php'); |
|
| 14 | - require_once(dirname(__FILE__) . '/options.php'); |
|
| 15 | - require_once(dirname(__FILE__) . '/scorer.php'); |
|
| 16 | - require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 17 | - require_once(dirname(__FILE__) . '/dumper.php'); |
|
| 18 | - if (! defined('SIMPLE_TEST')) { |
|
| 19 | - define('SIMPLE_TEST', dirname(__FILE__) . '/'); |
|
| 20 | - } |
|
| 21 | - /**#@-*/ |
|
| 13 | + require_once(dirname(__FILE__) . '/errors.php'); |
|
| 14 | + require_once(dirname(__FILE__) . '/options.php'); |
|
| 15 | + require_once(dirname(__FILE__) . '/scorer.php'); |
|
| 16 | + require_once(dirname(__FILE__) . '/expectation.php'); |
|
| 17 | + require_once(dirname(__FILE__) . '/dumper.php'); |
|
| 18 | + if (! defined('SIMPLE_TEST')) { |
|
| 19 | + define('SIMPLE_TEST', dirname(__FILE__) . '/'); |
|
| 20 | + } |
|
| 21 | + /**#@-*/ |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * This is called by the class runner to run a |
|
| 25 | - * single test method. Will also run the setUp() |
|
| 26 | - * and tearDown() methods. |
|
| 23 | + /** |
|
| 24 | + * This is called by the class runner to run a |
|
| 25 | + * single test method. Will also run the setUp() |
|
| 26 | + * and tearDown() methods. |
|
| 27 | 27 | * @package SimpleTest |
| 28 | 28 | * @subpackage UnitTester |
| 29 | - */ |
|
| 30 | - class SimpleInvoker { |
|
| 31 | - protected $_test_case; |
|
| 29 | + */ |
|
| 30 | + class SimpleInvoker { |
|
| 31 | + protected $_test_case; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Stashes the test case for later. |
|
| 35 | - * @param SimpleTestCase $test_case Test case to run. |
|
| 36 | - */ |
|
| 37 | - function SimpleInvoker($test_case) { |
|
| 38 | - $this->_test_case = $test_case; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * Stashes the test case for later. |
|
| 35 | + * @param SimpleTestCase $test_case Test case to run. |
|
| 36 | + */ |
|
| 37 | + function SimpleInvoker($test_case) { |
|
| 38 | + $this->_test_case = $test_case; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Accessor for test case being run. |
|
| 43 | - * @return SimpleTestCase Test case. |
|
| 44 | - * @access public |
|
| 45 | - */ |
|
| 46 | - function getTestCase() { |
|
| 47 | - return $this->_test_case; |
|
| 48 | - } |
|
| 41 | + /** |
|
| 42 | + * Accessor for test case being run. |
|
| 43 | + * @return SimpleTestCase Test case. |
|
| 44 | + * @access public |
|
| 45 | + */ |
|
| 46 | + function getTestCase() { |
|
| 47 | + return $this->_test_case; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Invokes a test method and buffered with setUp() |
|
| 52 | - * and tearDown() calls. |
|
| 53 | - * @param string $method Test method to call. |
|
| 54 | - * @access public |
|
| 55 | - */ |
|
| 56 | - function invoke($method) { |
|
| 57 | - $this->_test_case->setUp(); |
|
| 58 | - $this->_test_case->$method(); |
|
| 59 | - $this->_test_case->tearDown(); |
|
| 60 | - } |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * Invokes a test method and buffered with setUp() |
|
| 52 | + * and tearDown() calls. |
|
| 53 | + * @param string $method Test method to call. |
|
| 54 | + * @access public |
|
| 55 | + */ |
|
| 56 | + function invoke($method) { |
|
| 57 | + $this->_test_case->setUp(); |
|
| 58 | + $this->_test_case->$method(); |
|
| 59 | + $this->_test_case->tearDown(); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Do nothing decorator. Just passes the invocation |
|
| 65 | - * straight through. |
|
| 63 | + /** |
|
| 64 | + * Do nothing decorator. Just passes the invocation |
|
| 65 | + * straight through. |
|
| 66 | 66 | * @package SimpleTest |
| 67 | 67 | * @subpackage UnitTester |
| 68 | - */ |
|
| 69 | - class SimpleInvokerDecorator { |
|
| 70 | - protected $_invoker; |
|
| 68 | + */ |
|
| 69 | + class SimpleInvokerDecorator { |
|
| 70 | + protected $_invoker; |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Stores the invoker to wrap. |
|
| 74 | - * @param SimpleInvoker $invoker Test method runner. |
|
| 75 | - */ |
|
| 76 | - function SimpleInvokerDecorator($invoker) { |
|
| 77 | - $this->_invoker = $invoker; |
|
| 78 | - } |
|
| 72 | + /** |
|
| 73 | + * Stores the invoker to wrap. |
|
| 74 | + * @param SimpleInvoker $invoker Test method runner. |
|
| 75 | + */ |
|
| 76 | + function SimpleInvokerDecorator($invoker) { |
|
| 77 | + $this->_invoker = $invoker; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Accessor for test case being run. |
|
| 82 | - * @return SimpleTestCase Test case. |
|
| 83 | - * @access public |
|
| 84 | - */ |
|
| 85 | - function getTestCase() { |
|
| 86 | - return $this->_invoker->getTestCase(); |
|
| 87 | - } |
|
| 80 | + /** |
|
| 81 | + * Accessor for test case being run. |
|
| 82 | + * @return SimpleTestCase Test case. |
|
| 83 | + * @access public |
|
| 84 | + */ |
|
| 85 | + function getTestCase() { |
|
| 86 | + return $this->_invoker->getTestCase(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Invokes a test method and buffered with setUp() |
|
| 91 | - * and tearDown() calls. |
|
| 92 | - * @param string $method Test method to call. |
|
| 93 | - * @access public |
|
| 94 | - */ |
|
| 95 | - function invoke($method) { |
|
| 96 | - $this->_invoker->invoke($method); |
|
| 97 | - } |
|
| 98 | - } |
|
| 89 | + /** |
|
| 90 | + * Invokes a test method and buffered with setUp() |
|
| 91 | + * and tearDown() calls. |
|
| 92 | + * @param string $method Test method to call. |
|
| 93 | + * @access public |
|
| 94 | + */ |
|
| 95 | + function invoke($method) { |
|
| 96 | + $this->_invoker->invoke($method); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * Extension that traps errors into an error queue. |
|
| 100 | + /** |
|
| 101 | + * Extension that traps errors into an error queue. |
|
| 102 | 102 | * @package SimpleTest |
| 103 | 103 | * @subpackage UnitTester |
| 104 | - */ |
|
| 105 | - class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator { |
|
| 104 | + */ |
|
| 105 | + class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator { |
|
| 106 | 106 | |
| 107 | + /** |
|
| 107 | 108 | /** |
| 108 | - /** |
|
| 109 | - * Stores the invoker to wrap. |
|
| 110 | - * @param SimpleInvoker $invoker Test method runner. |
|
| 111 | - */ |
|
| 112 | - function SimpleErrorTrappingInvoker($invoker) { |
|
| 113 | - $this->SimpleInvokerDecorator($invoker); |
|
| 114 | - } |
|
| 109 | + * Stores the invoker to wrap. |
|
| 110 | + * @param SimpleInvoker $invoker Test method runner. |
|
| 111 | + */ |
|
| 112 | + function SimpleErrorTrappingInvoker($invoker) { |
|
| 113 | + $this->SimpleInvokerDecorator($invoker); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * Invokes a test method and dispatches any |
|
| 118 | - * untrapped errors. Called back from |
|
| 119 | - * the visiting runner. |
|
| 120 | - * @param string $method Test method to call. |
|
| 121 | - * @access public |
|
| 122 | - */ |
|
| 123 | - function invoke($method) { |
|
| 124 | - set_error_handler('simpleTestErrorHandler'); |
|
| 125 | - parent::invoke($method); |
|
| 126 | - $queue = SimpleErrorQueue::instance(); |
|
| 127 | - while (list($severity, $message, $file, $line, $globals) = $queue->extract()) { |
|
| 128 | - $test_case = $this->getTestCase(); |
|
| 129 | - $test_case->error($severity, $message, $file, $line, $globals); |
|
| 130 | - } |
|
| 131 | - restore_error_handler(); |
|
| 132 | - } |
|
| 133 | - } |
|
| 116 | + /** |
|
| 117 | + * Invokes a test method and dispatches any |
|
| 118 | + * untrapped errors. Called back from |
|
| 119 | + * the visiting runner. |
|
| 120 | + * @param string $method Test method to call. |
|
| 121 | + * @access public |
|
| 122 | + */ |
|
| 123 | + function invoke($method) { |
|
| 124 | + set_error_handler('simpleTestErrorHandler'); |
|
| 125 | + parent::invoke($method); |
|
| 126 | + $queue = SimpleErrorQueue::instance(); |
|
| 127 | + while (list($severity, $message, $file, $line, $globals) = $queue->extract()) { |
|
| 128 | + $test_case = $this->getTestCase(); |
|
| 129 | + $test_case->error($severity, $message, $file, $line, $globals); |
|
| 130 | + } |
|
| 131 | + restore_error_handler(); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * The standard runner. Will run every method starting |
|
| 137 | - * with test Basically the |
|
| 138 | - * Mediator pattern. |
|
| 135 | + /** |
|
| 136 | + * The standard runner. Will run every method starting |
|
| 137 | + * with test Basically the |
|
| 138 | + * Mediator pattern. |
|
| 139 | 139 | * @package SimpleTest |
| 140 | 140 | * @subpackage UnitTester |
| 141 | - */ |
|
| 142 | - class SimpleRunner { |
|
| 143 | - protected $_test_case; |
|
| 144 | - protected $_scorer; |
|
| 141 | + */ |
|
| 142 | + class SimpleRunner { |
|
| 143 | + protected $_test_case; |
|
| 144 | + protected $_scorer; |
|
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Takes in the test case and reporter to mediate between. |
|
| 148 | - * @param SimpleTestCase $test_case Test case to run. |
|
| 149 | - * @param SimpleScorer $scorer Reporter to receive events. |
|
| 150 | - */ |
|
| 151 | - function SimpleRunner($test_case, $scorer) { |
|
| 152 | - $this->_test_case = $test_case; |
|
| 153 | - $this->_scorer = $scorer; |
|
| 154 | - } |
|
| 146 | + /** |
|
| 147 | + * Takes in the test case and reporter to mediate between. |
|
| 148 | + * @param SimpleTestCase $test_case Test case to run. |
|
| 149 | + * @param SimpleScorer $scorer Reporter to receive events. |
|
| 150 | + */ |
|
| 151 | + function SimpleRunner($test_case, $scorer) { |
|
| 152 | + $this->_test_case = $test_case; |
|
| 153 | + $this->_scorer = $scorer; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Accessor for test case being run. |
|
| 158 | - * @return SimpleTestCase Test case. |
|
| 159 | - * @access public |
|
| 160 | - */ |
|
| 161 | - function getTestCase() { |
|
| 162 | - return $this->_test_case; |
|
| 163 | - } |
|
| 156 | + /** |
|
| 157 | + * Accessor for test case being run. |
|
| 158 | + * @return SimpleTestCase Test case. |
|
| 159 | + * @access public |
|
| 160 | + */ |
|
| 161 | + function getTestCase() { |
|
| 162 | + return $this->_test_case; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * Runs the test methods in the test case. |
|
| 167 | - * @param SimpleTest $test_case Test case to run test on. |
|
| 168 | - * @param string $method Name of test method. |
|
| 169 | - * @access public |
|
| 170 | - */ |
|
| 171 | - function run() { |
|
| 172 | - $methods = get_class_methods(get_class($this->_test_case)); |
|
| 173 | - $invoker = $this->_test_case->createInvoker(); |
|
| 174 | - foreach ($methods as $method) { |
|
| 175 | - if (! $this->_isTest($method)) { |
|
| 176 | - continue; |
|
| 177 | - } |
|
| 178 | - if ($this->_isConstructor($method)) { |
|
| 179 | - continue; |
|
| 180 | - } |
|
| 181 | - $this->_scorer->paintMethodStart($method); |
|
| 182 | - if ($this->_scorer->shouldInvoke($this->_test_case->getLabel(), $method)) { |
|
| 183 | - $invoker->invoke($method); |
|
| 184 | - } |
|
| 185 | - $this->_scorer->paintMethodEnd($method); |
|
| 186 | - } |
|
| 187 | - } |
|
| 165 | + /** |
|
| 166 | + * Runs the test methods in the test case. |
|
| 167 | + * @param SimpleTest $test_case Test case to run test on. |
|
| 168 | + * @param string $method Name of test method. |
|
| 169 | + * @access public |
|
| 170 | + */ |
|
| 171 | + function run() { |
|
| 172 | + $methods = get_class_methods(get_class($this->_test_case)); |
|
| 173 | + $invoker = $this->_test_case->createInvoker(); |
|
| 174 | + foreach ($methods as $method) { |
|
| 175 | + if (! $this->_isTest($method)) { |
|
| 176 | + continue; |
|
| 177 | + } |
|
| 178 | + if ($this->_isConstructor($method)) { |
|
| 179 | + continue; |
|
| 180 | + } |
|
| 181 | + $this->_scorer->paintMethodStart($method); |
|
| 182 | + if ($this->_scorer->shouldInvoke($this->_test_case->getLabel(), $method)) { |
|
| 183 | + $invoker->invoke($method); |
|
| 184 | + } |
|
| 185 | + $this->_scorer->paintMethodEnd($method); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Tests to see if the method is the constructor and |
|
| 191 | - * so should be ignored. |
|
| 192 | - * @param string $method Method name to try. |
|
| 193 | - * @return boolean True if constructor. |
|
| 194 | - * @access protected |
|
| 195 | - */ |
|
| 196 | - function _isConstructor($method) { |
|
| 197 | - return SimpleTestCompatibility::isA( |
|
| 198 | - $this->_test_case, |
|
| 199 | - strtolower($method)); |
|
| 200 | - } |
|
| 189 | + /** |
|
| 190 | + * Tests to see if the method is the constructor and |
|
| 191 | + * so should be ignored. |
|
| 192 | + * @param string $method Method name to try. |
|
| 193 | + * @return boolean True if constructor. |
|
| 194 | + * @access protected |
|
| 195 | + */ |
|
| 196 | + function _isConstructor($method) { |
|
| 197 | + return SimpleTestCompatibility::isA( |
|
| 198 | + $this->_test_case, |
|
| 199 | + strtolower($method)); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Tests to see if the method is a test that should |
|
| 204 | - * be run. Currently any method that starts with 'test' |
|
| 205 | - * is a candidate. |
|
| 206 | - * @param string $method Method name to try. |
|
| 207 | - * @return boolean True if test method. |
|
| 208 | - * @access protected |
|
| 209 | - */ |
|
| 210 | - function _isTest($method) { |
|
| 211 | - return strtolower(substr($method, 0, 4)) == 'test'; |
|
| 212 | - } |
|
| 202 | + /** |
|
| 203 | + * Tests to see if the method is a test that should |
|
| 204 | + * be run. Currently any method that starts with 'test' |
|
| 205 | + * is a candidate. |
|
| 206 | + * @param string $method Method name to try. |
|
| 207 | + * @return boolean True if test method. |
|
| 208 | + * @access protected |
|
| 209 | + */ |
|
| 210 | + function _isTest($method) { |
|
| 211 | + return strtolower(substr($method, 0, 4)) == 'test'; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Paints the start of a test method. |
|
| 216 | - * @param string $test_name Name of test or other label. |
|
| 217 | - * @access public |
|
| 218 | - */ |
|
| 219 | - function paintMethodStart($test_name) { |
|
| 220 | - $this->_scorer->paintMethodStart($test_name); |
|
| 221 | - } |
|
| 214 | + /** |
|
| 215 | + * Paints the start of a test method. |
|
| 216 | + * @param string $test_name Name of test or other label. |
|
| 217 | + * @access public |
|
| 218 | + */ |
|
| 219 | + function paintMethodStart($test_name) { |
|
| 220 | + $this->_scorer->paintMethodStart($test_name); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * Paints the end of a test method. |
|
| 225 | - * @param string $test_name Name of test or other label. |
|
| 226 | - * @access public |
|
| 227 | - */ |
|
| 228 | - function paintMethodEnd($test_name) { |
|
| 229 | - $this->_scorer->paintMethodEnd($test_name); |
|
| 230 | - } |
|
| 223 | + /** |
|
| 224 | + * Paints the end of a test method. |
|
| 225 | + * @param string $test_name Name of test or other label. |
|
| 226 | + * @access public |
|
| 227 | + */ |
|
| 228 | + function paintMethodEnd($test_name) { |
|
| 229 | + $this->_scorer->paintMethodEnd($test_name); |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Chains to the wrapped reporter. |
|
| 234 | - * @param string $message Message is ignored. |
|
| 235 | - * @access public |
|
| 236 | - */ |
|
| 237 | - function paintPass($message) { |
|
| 238 | - $this->_scorer->paintPass($message); |
|
| 239 | - } |
|
| 232 | + /** |
|
| 233 | + * Chains to the wrapped reporter. |
|
| 234 | + * @param string $message Message is ignored. |
|
| 235 | + * @access public |
|
| 236 | + */ |
|
| 237 | + function paintPass($message) { |
|
| 238 | + $this->_scorer->paintPass($message); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * Chains to the wrapped reporter. |
|
| 243 | - * @param string $message Message is ignored. |
|
| 244 | - * @access public |
|
| 245 | - */ |
|
| 246 | - function paintFail($message) { |
|
| 247 | - $this->_scorer->paintFail($message); |
|
| 248 | - } |
|
| 241 | + /** |
|
| 242 | + * Chains to the wrapped reporter. |
|
| 243 | + * @param string $message Message is ignored. |
|
| 244 | + * @access public |
|
| 245 | + */ |
|
| 246 | + function paintFail($message) { |
|
| 247 | + $this->_scorer->paintFail($message); |
|
| 248 | + } |
|
| 249 | 249 | |
| 250 | - /** |
|
| 251 | - * Chains to the wrapped reporter. |
|
| 252 | - * @param string $message Text of error formatted by |
|
| 253 | - * the test case. |
|
| 254 | - * @access public |
|
| 255 | - */ |
|
| 256 | - function paintError($message) { |
|
| 257 | - $this->_scorer->paintError($message); |
|
| 258 | - } |
|
| 250 | + /** |
|
| 251 | + * Chains to the wrapped reporter. |
|
| 252 | + * @param string $message Text of error formatted by |
|
| 253 | + * the test case. |
|
| 254 | + * @access public |
|
| 255 | + */ |
|
| 256 | + function paintError($message) { |
|
| 257 | + $this->_scorer->paintError($message); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Chains to the wrapped reporter. |
|
| 262 | - * @param Exception $exception Object thrown. |
|
| 263 | - * @access public |
|
| 264 | - */ |
|
| 265 | - function paintException($exception) { |
|
| 266 | - $this->_scorer->paintException($exception); |
|
| 267 | - } |
|
| 260 | + /** |
|
| 261 | + * Chains to the wrapped reporter. |
|
| 262 | + * @param Exception $exception Object thrown. |
|
| 263 | + * @access public |
|
| 264 | + */ |
|
| 265 | + function paintException($exception) { |
|
| 266 | + $this->_scorer->paintException($exception); |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | - /** |
|
| 270 | - * Chains to the wrapped reporter. |
|
| 271 | - * @param string $message Text to display. |
|
| 272 | - * @access public |
|
| 273 | - */ |
|
| 274 | - function paintMessage($message) { |
|
| 275 | - $this->_scorer->paintMessage($message); |
|
| 276 | - } |
|
| 269 | + /** |
|
| 270 | + * Chains to the wrapped reporter. |
|
| 271 | + * @param string $message Text to display. |
|
| 272 | + * @access public |
|
| 273 | + */ |
|
| 274 | + function paintMessage($message) { |
|
| 275 | + $this->_scorer->paintMessage($message); |
|
| 276 | + } |
|
| 277 | 277 | |
| 278 | - /** |
|
| 279 | - * Chains to the wrapped reporter. |
|
| 280 | - * @param string $message Text to display. |
|
| 281 | - * @access public |
|
| 282 | - */ |
|
| 283 | - function paintFormattedMessage($message) { |
|
| 284 | - $this->_scorer->paintFormattedMessage($message); |
|
| 285 | - } |
|
| 278 | + /** |
|
| 279 | + * Chains to the wrapped reporter. |
|
| 280 | + * @param string $message Text to display. |
|
| 281 | + * @access public |
|
| 282 | + */ |
|
| 283 | + function paintFormattedMessage($message) { |
|
| 284 | + $this->_scorer->paintFormattedMessage($message); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * Chains to the wrapped reporter. |
|
| 289 | - * @param string $type Event type as text. |
|
| 290 | - * @param mixed $payload Message or object. |
|
| 291 | - * @return boolean Should return false if this |
|
| 292 | - * type of signal should fail the |
|
| 293 | - * test suite. |
|
| 294 | - * @access public |
|
| 295 | - */ |
|
| 296 | - function paintSignal($type, $payload) { |
|
| 297 | - $this->_scorer->paintSignal($type, $payload); |
|
| 298 | - } |
|
| 299 | - } |
|
| 287 | + /** |
|
| 288 | + * Chains to the wrapped reporter. |
|
| 289 | + * @param string $type Event type as text. |
|
| 290 | + * @param mixed $payload Message or object. |
|
| 291 | + * @return boolean Should return false if this |
|
| 292 | + * type of signal should fail the |
|
| 293 | + * test suite. |
|
| 294 | + * @access public |
|
| 295 | + */ |
|
| 296 | + function paintSignal($type, $payload) { |
|
| 297 | + $this->_scorer->paintSignal($type, $payload); |
|
| 298 | + } |
|
| 299 | + } |
|
@@ -1,623 +1,623 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage WebTester |
|
| 6 | - * @version $Id: http.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | - */ |
|
| 8 | - |
|
| 9 | - /**#@+ |
|
| 2 | + /** |
|
| 3 | + * base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage WebTester |
|
| 6 | + * @version $Id: http.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | + */ |
|
| 8 | + |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include other SimpleTest class files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/socket.php'); |
|
| 13 | - require_once(dirname(__FILE__) . '/cookies.php'); |
|
| 14 | - require_once(dirname(__FILE__) . '/url.php'); |
|
| 15 | - /**#@-*/ |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * Creates HTTP headers for the end point of |
|
| 19 | - * a HTTP request. |
|
| 12 | + require_once(dirname(__FILE__) . '/socket.php'); |
|
| 13 | + require_once(dirname(__FILE__) . '/cookies.php'); |
|
| 14 | + require_once(dirname(__FILE__) . '/url.php'); |
|
| 15 | + /**#@-*/ |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * Creates HTTP headers for the end point of |
|
| 19 | + * a HTTP request. |
|
| 20 | 20 | * @package SimpleTest |
| 21 | 21 | * @subpackage WebTester |
| 22 | - */ |
|
| 23 | - class SimpleRoute { |
|
| 24 | - protected $_url; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Sets the target URL. |
|
| 28 | - * @param SimpleUrl $url URL as object. |
|
| 29 | - * @access public |
|
| 30 | - */ |
|
| 31 | - function SimpleRoute($url) { |
|
| 32 | - $this->_url = $url; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Resource name. |
|
| 37 | - * @return SimpleUrl Current url. |
|
| 38 | - * @access protected |
|
| 39 | - */ |
|
| 40 | - function getUrl() { |
|
| 41 | - return $this->_url; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Creates the first line which is the actual request. |
|
| 46 | - * @param string $method HTTP request method, usually GET. |
|
| 47 | - * @return string Request line content. |
|
| 48 | - * @access protected |
|
| 49 | - */ |
|
| 50 | - function _getRequestLine($method) { |
|
| 51 | - return $method . ' ' . $this->_url->getPath() . |
|
| 52 | - $this->_url->getEncodedRequest() . ' HTTP/1.0'; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Creates the host part of the request. |
|
| 57 | - * @return string Host line content. |
|
| 58 | - * @access protected |
|
| 59 | - */ |
|
| 60 | - function _getHostLine() { |
|
| 61 | - $line = 'Host: ' . $this->_url->getHost(); |
|
| 62 | - if ($this->_url->getPort()) { |
|
| 63 | - $line .= ':' . $this->_url->getPort(); |
|
| 64 | - } |
|
| 65 | - return $line; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Opens a socket to the route. |
|
| 70 | - * @param string $method HTTP request method, usually GET. |
|
| 71 | - * @param integer $timeout Connection timeout. |
|
| 72 | - * @return SimpleSocket New socket. |
|
| 73 | - * @access public |
|
| 74 | - */ |
|
| 75 | - function &createConnection($method, $timeout) { |
|
| 76 | - $default_port = ('https' == $this->_url->getScheme()) ? 443 : 80; |
|
| 77 | - $socket = $this->_createSocket( |
|
| 78 | - $this->_url->getScheme() ? $this->_url->getScheme() : 'http', |
|
| 79 | - $this->_url->getHost(), |
|
| 80 | - $this->_url->getPort() ? $this->_url->getPort() : $default_port, |
|
| 81 | - $timeout); |
|
| 82 | - if (! $socket->isError()) { |
|
| 83 | - $socket->write($this->_getRequestLine($method) . "\r\n"); |
|
| 84 | - $socket->write($this->_getHostLine() . "\r\n"); |
|
| 85 | - $socket->write("Connection: close\r\n"); |
|
| 86 | - } |
|
| 87 | - return $socket; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Factory for socket. |
|
| 92 | - * @param string $scheme Protocol to use. |
|
| 93 | - * @param string $host Hostname to connect to. |
|
| 94 | - * @param integer $port Remote port. |
|
| 95 | - * @param integer $timeout Connection timeout. |
|
| 96 | - * @return SimpleSocket/SimpleSecureSocket New socket. |
|
| 97 | - * @access protected |
|
| 98 | - */ |
|
| 99 | - function &_createSocket($scheme, $host, $port, $timeout) { |
|
| 100 | - if (in_array($scheme, array('https'))) { |
|
| 101 | - $socket = new SimpleSecureSocket($host, $port, $timeout); |
|
| 102 | - } else { |
|
| 103 | - $socket = new SimpleSocket($host, $port, $timeout); |
|
| 104 | - } |
|
| 105 | - return $socket; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Creates HTTP headers for the end point of |
|
| 111 | - * a HTTP request via a proxy server. |
|
| 22 | + */ |
|
| 23 | + class SimpleRoute { |
|
| 24 | + protected $_url; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Sets the target URL. |
|
| 28 | + * @param SimpleUrl $url URL as object. |
|
| 29 | + * @access public |
|
| 30 | + */ |
|
| 31 | + function SimpleRoute($url) { |
|
| 32 | + $this->_url = $url; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Resource name. |
|
| 37 | + * @return SimpleUrl Current url. |
|
| 38 | + * @access protected |
|
| 39 | + */ |
|
| 40 | + function getUrl() { |
|
| 41 | + return $this->_url; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Creates the first line which is the actual request. |
|
| 46 | + * @param string $method HTTP request method, usually GET. |
|
| 47 | + * @return string Request line content. |
|
| 48 | + * @access protected |
|
| 49 | + */ |
|
| 50 | + function _getRequestLine($method) { |
|
| 51 | + return $method . ' ' . $this->_url->getPath() . |
|
| 52 | + $this->_url->getEncodedRequest() . ' HTTP/1.0'; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Creates the host part of the request. |
|
| 57 | + * @return string Host line content. |
|
| 58 | + * @access protected |
|
| 59 | + */ |
|
| 60 | + function _getHostLine() { |
|
| 61 | + $line = 'Host: ' . $this->_url->getHost(); |
|
| 62 | + if ($this->_url->getPort()) { |
|
| 63 | + $line .= ':' . $this->_url->getPort(); |
|
| 64 | + } |
|
| 65 | + return $line; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Opens a socket to the route. |
|
| 70 | + * @param string $method HTTP request method, usually GET. |
|
| 71 | + * @param integer $timeout Connection timeout. |
|
| 72 | + * @return SimpleSocket New socket. |
|
| 73 | + * @access public |
|
| 74 | + */ |
|
| 75 | + function &createConnection($method, $timeout) { |
|
| 76 | + $default_port = ('https' == $this->_url->getScheme()) ? 443 : 80; |
|
| 77 | + $socket = $this->_createSocket( |
|
| 78 | + $this->_url->getScheme() ? $this->_url->getScheme() : 'http', |
|
| 79 | + $this->_url->getHost(), |
|
| 80 | + $this->_url->getPort() ? $this->_url->getPort() : $default_port, |
|
| 81 | + $timeout); |
|
| 82 | + if (! $socket->isError()) { |
|
| 83 | + $socket->write($this->_getRequestLine($method) . "\r\n"); |
|
| 84 | + $socket->write($this->_getHostLine() . "\r\n"); |
|
| 85 | + $socket->write("Connection: close\r\n"); |
|
| 86 | + } |
|
| 87 | + return $socket; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Factory for socket. |
|
| 92 | + * @param string $scheme Protocol to use. |
|
| 93 | + * @param string $host Hostname to connect to. |
|
| 94 | + * @param integer $port Remote port. |
|
| 95 | + * @param integer $timeout Connection timeout. |
|
| 96 | + * @return SimpleSocket/SimpleSecureSocket New socket. |
|
| 97 | + * @access protected |
|
| 98 | + */ |
|
| 99 | + function &_createSocket($scheme, $host, $port, $timeout) { |
|
| 100 | + if (in_array($scheme, array('https'))) { |
|
| 101 | + $socket = new SimpleSecureSocket($host, $port, $timeout); |
|
| 102 | + } else { |
|
| 103 | + $socket = new SimpleSocket($host, $port, $timeout); |
|
| 104 | + } |
|
| 105 | + return $socket; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Creates HTTP headers for the end point of |
|
| 111 | + * a HTTP request via a proxy server. |
|
| 112 | 112 | * @package SimpleTest |
| 113 | 113 | * @subpackage WebTester |
| 114 | - */ |
|
| 115 | - class SimpleProxyRoute extends SimpleRoute { |
|
| 116 | - protected $_proxy; |
|
| 117 | - protected $_username; |
|
| 118 | - protected $_password; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Stashes the proxy address. |
|
| 122 | - * @param SimpleUrl $url URL as object. |
|
| 123 | - * @param string $proxy Proxy URL. |
|
| 124 | - * @param string $username Username for autentication. |
|
| 125 | - * @param string $password Password for autentication. |
|
| 126 | - * @access public |
|
| 127 | - */ |
|
| 128 | - function SimpleProxyRoute($url, $proxy, $username = false, $password = false) { |
|
| 129 | - $this->SimpleRoute($url); |
|
| 130 | - $this->_proxy = $proxy; |
|
| 131 | - $this->_username = $username; |
|
| 132 | - $this->_password = $password; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Creates the first line which is the actual request. |
|
| 137 | - * @param string $method HTTP request method, usually GET. |
|
| 138 | - * @param SimpleUrl $url URL as object. |
|
| 139 | - * @return string Request line content. |
|
| 140 | - * @access protected |
|
| 141 | - */ |
|
| 142 | - function _getRequestLine($method) { |
|
| 143 | - $url = $this->getUrl(); |
|
| 144 | - $scheme = $url->getScheme() ? $url->getScheme() : 'http'; |
|
| 145 | - $port = $url->getPort() ? ':' . $url->getPort() : ''; |
|
| 146 | - return $method . ' ' . $scheme . '://' . $url->getHost() . $port . |
|
| 147 | - $url->getPath() . $url->getEncodedRequest() . ' HTTP/1.0'; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Creates the host part of the request. |
|
| 152 | - * @param SimpleUrl $url URL as object. |
|
| 153 | - * @return string Host line content. |
|
| 154 | - * @access protected |
|
| 155 | - */ |
|
| 156 | - function _getHostLine() { |
|
| 157 | - $host = 'Host: ' . $this->_proxy->getHost(); |
|
| 158 | - $port = $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080; |
|
| 159 | - return "$host:$port"; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Opens a socket to the route. |
|
| 164 | - * @param string $method HTTP request method, usually GET. |
|
| 165 | - * @param integer $timeout Connection timeout. |
|
| 166 | - * @return SimpleSocket New socket. |
|
| 167 | - * @access public |
|
| 168 | - */ |
|
| 169 | - function &createConnection($method, $timeout) { |
|
| 170 | - $socket = $this->_createSocket( |
|
| 171 | - $this->_proxy->getScheme() ? $this->_proxy->getScheme() : 'http', |
|
| 172 | - $this->_proxy->getHost(), |
|
| 173 | - $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080, |
|
| 174 | - $timeout); |
|
| 175 | - if ($socket->isError()) { |
|
| 176 | - return $socket; |
|
| 177 | - } |
|
| 178 | - $socket->write($this->_getRequestLine($method) . "\r\n"); |
|
| 179 | - $socket->write($this->_getHostLine() . "\r\n"); |
|
| 180 | - if ($this->_username && $this->_password) { |
|
| 181 | - $socket->write('Proxy-Authorization: Basic ' . |
|
| 182 | - base64_encode($this->_username . ':' . $this->_password) . |
|
| 183 | - "\r\n"); |
|
| 184 | - } |
|
| 185 | - $socket->write("Connection: close\r\n"); |
|
| 186 | - return $socket; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * HTTP request for a web page. Factory for |
|
| 192 | - * HttpResponse object. |
|
| 114 | + */ |
|
| 115 | + class SimpleProxyRoute extends SimpleRoute { |
|
| 116 | + protected $_proxy; |
|
| 117 | + protected $_username; |
|
| 118 | + protected $_password; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Stashes the proxy address. |
|
| 122 | + * @param SimpleUrl $url URL as object. |
|
| 123 | + * @param string $proxy Proxy URL. |
|
| 124 | + * @param string $username Username for autentication. |
|
| 125 | + * @param string $password Password for autentication. |
|
| 126 | + * @access public |
|
| 127 | + */ |
|
| 128 | + function SimpleProxyRoute($url, $proxy, $username = false, $password = false) { |
|
| 129 | + $this->SimpleRoute($url); |
|
| 130 | + $this->_proxy = $proxy; |
|
| 131 | + $this->_username = $username; |
|
| 132 | + $this->_password = $password; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Creates the first line which is the actual request. |
|
| 137 | + * @param string $method HTTP request method, usually GET. |
|
| 138 | + * @param SimpleUrl $url URL as object. |
|
| 139 | + * @return string Request line content. |
|
| 140 | + * @access protected |
|
| 141 | + */ |
|
| 142 | + function _getRequestLine($method) { |
|
| 143 | + $url = $this->getUrl(); |
|
| 144 | + $scheme = $url->getScheme() ? $url->getScheme() : 'http'; |
|
| 145 | + $port = $url->getPort() ? ':' . $url->getPort() : ''; |
|
| 146 | + return $method . ' ' . $scheme . '://' . $url->getHost() . $port . |
|
| 147 | + $url->getPath() . $url->getEncodedRequest() . ' HTTP/1.0'; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Creates the host part of the request. |
|
| 152 | + * @param SimpleUrl $url URL as object. |
|
| 153 | + * @return string Host line content. |
|
| 154 | + * @access protected |
|
| 155 | + */ |
|
| 156 | + function _getHostLine() { |
|
| 157 | + $host = 'Host: ' . $this->_proxy->getHost(); |
|
| 158 | + $port = $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080; |
|
| 159 | + return "$host:$port"; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Opens a socket to the route. |
|
| 164 | + * @param string $method HTTP request method, usually GET. |
|
| 165 | + * @param integer $timeout Connection timeout. |
|
| 166 | + * @return SimpleSocket New socket. |
|
| 167 | + * @access public |
|
| 168 | + */ |
|
| 169 | + function &createConnection($method, $timeout) { |
|
| 170 | + $socket = $this->_createSocket( |
|
| 171 | + $this->_proxy->getScheme() ? $this->_proxy->getScheme() : 'http', |
|
| 172 | + $this->_proxy->getHost(), |
|
| 173 | + $this->_proxy->getPort() ? $this->_proxy->getPort() : 8080, |
|
| 174 | + $timeout); |
|
| 175 | + if ($socket->isError()) { |
|
| 176 | + return $socket; |
|
| 177 | + } |
|
| 178 | + $socket->write($this->_getRequestLine($method) . "\r\n"); |
|
| 179 | + $socket->write($this->_getHostLine() . "\r\n"); |
|
| 180 | + if ($this->_username && $this->_password) { |
|
| 181 | + $socket->write('Proxy-Authorization: Basic ' . |
|
| 182 | + base64_encode($this->_username . ':' . $this->_password) . |
|
| 183 | + "\r\n"); |
|
| 184 | + } |
|
| 185 | + $socket->write("Connection: close\r\n"); |
|
| 186 | + return $socket; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * HTTP request for a web page. Factory for |
|
| 192 | + * HttpResponse object. |
|
| 193 | 193 | * @package SimpleTest |
| 194 | 194 | * @subpackage WebTester |
| 195 | - */ |
|
| 196 | - class SimpleHttpRequest { |
|
| 197 | - protected $_route; |
|
| 198 | - protected $_encoding; |
|
| 199 | - protected $_headers; |
|
| 200 | - protected $_cookies; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Builds the socket request from the different pieces. |
|
| 204 | - * These include proxy information, URL, cookies, headers, |
|
| 205 | - * request method and choice of encoding. |
|
| 206 | - * @param SimpleRoute $route Request route. |
|
| 207 | - * @param SimpleFormEncoding $encoding Content to send with |
|
| 208 | - * request. |
|
| 209 | - * @access public |
|
| 210 | - */ |
|
| 211 | - function SimpleHttpRequest($route, $encoding) { |
|
| 212 | - $this->_route = $route; |
|
| 213 | - $this->_encoding = $encoding; |
|
| 214 | - $this->_headers = array(); |
|
| 215 | - $this->_cookies = array(); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Dispatches the content to the route's socket. |
|
| 220 | - * @param integer $timeout Connection timeout. |
|
| 221 | - * @return SimpleHttpResponse A response which may only have |
|
| 222 | - * an error, but hopefully has a |
|
| 223 | - * complete web page. |
|
| 224 | - * @access public |
|
| 225 | - */ |
|
| 226 | - function &fetch($timeout) { |
|
| 227 | - $socket = $this->_route->createConnection($this->_encoding->getMethod(), $timeout); |
|
| 228 | - if (! $socket->isError()) { |
|
| 229 | - $this->_dispatchRequest($socket, $this->_encoding); |
|
| 230 | - } |
|
| 231 | - $response = $this->_createResponse($socket); |
|
| 232 | - return $response; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Sends the headers. |
|
| 237 | - * @param SimpleSocket $socket Open socket. |
|
| 238 | - * @param string $method HTTP request method, |
|
| 239 | - * usually GET. |
|
| 240 | - * @param SimpleFormEncoding $encoding Content to send with request. |
|
| 241 | - * @access private |
|
| 242 | - */ |
|
| 243 | - function _dispatchRequest($socket, $encoding) { |
|
| 244 | - foreach ($this->_headers as $header_line) { |
|
| 245 | - $socket->write($header_line . "\r\n"); |
|
| 246 | - } |
|
| 247 | - if (count($this->_cookies) > 0) { |
|
| 248 | - $socket->write("Cookie: " . implode(";", $this->_cookies) . "\r\n"); |
|
| 249 | - } |
|
| 250 | - $encoding->writeHeadersTo($socket); |
|
| 251 | - $socket->write("\r\n"); |
|
| 252 | - $encoding->writeTo($socket); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Adds a header line to the request. |
|
| 257 | - * @param string $header_line Text of full header line. |
|
| 258 | - * @access public |
|
| 259 | - */ |
|
| 260 | - function addHeaderLine($header_line) { |
|
| 261 | - $this->_headers[] = $header_line; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Reads all the relevant cookies from the |
|
| 266 | - * cookie jar. |
|
| 267 | - * @param SimpleCookieJar $jar Jar to read |
|
| 268 | - * @param SimpleUrl $url Url to use for scope. |
|
| 269 | - * @access public |
|
| 270 | - */ |
|
| 271 | - function readCookiesFromJar($jar, $url) { |
|
| 272 | - $this->_cookies = $jar->selectAsPairs($url); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Wraps the socket in a response parser. |
|
| 277 | - * @param SimpleSocket $socket Responding socket. |
|
| 278 | - * @return SimpleHttpResponse Parsed response object. |
|
| 279 | - * @access protected |
|
| 280 | - */ |
|
| 281 | - function &_createResponse($socket) { |
|
| 282 | - $response = new SimpleHttpResponse( |
|
| 283 | - $socket, |
|
| 284 | - $this->_route->getUrl(), |
|
| 285 | - $this->_encoding); |
|
| 286 | - return $response; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Collection of header lines in the response. |
|
| 195 | + */ |
|
| 196 | + class SimpleHttpRequest { |
|
| 197 | + protected $_route; |
|
| 198 | + protected $_encoding; |
|
| 199 | + protected $_headers; |
|
| 200 | + protected $_cookies; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Builds the socket request from the different pieces. |
|
| 204 | + * These include proxy information, URL, cookies, headers, |
|
| 205 | + * request method and choice of encoding. |
|
| 206 | + * @param SimpleRoute $route Request route. |
|
| 207 | + * @param SimpleFormEncoding $encoding Content to send with |
|
| 208 | + * request. |
|
| 209 | + * @access public |
|
| 210 | + */ |
|
| 211 | + function SimpleHttpRequest($route, $encoding) { |
|
| 212 | + $this->_route = $route; |
|
| 213 | + $this->_encoding = $encoding; |
|
| 214 | + $this->_headers = array(); |
|
| 215 | + $this->_cookies = array(); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Dispatches the content to the route's socket. |
|
| 220 | + * @param integer $timeout Connection timeout. |
|
| 221 | + * @return SimpleHttpResponse A response which may only have |
|
| 222 | + * an error, but hopefully has a |
|
| 223 | + * complete web page. |
|
| 224 | + * @access public |
|
| 225 | + */ |
|
| 226 | + function &fetch($timeout) { |
|
| 227 | + $socket = $this->_route->createConnection($this->_encoding->getMethod(), $timeout); |
|
| 228 | + if (! $socket->isError()) { |
|
| 229 | + $this->_dispatchRequest($socket, $this->_encoding); |
|
| 230 | + } |
|
| 231 | + $response = $this->_createResponse($socket); |
|
| 232 | + return $response; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Sends the headers. |
|
| 237 | + * @param SimpleSocket $socket Open socket. |
|
| 238 | + * @param string $method HTTP request method, |
|
| 239 | + * usually GET. |
|
| 240 | + * @param SimpleFormEncoding $encoding Content to send with request. |
|
| 241 | + * @access private |
|
| 242 | + */ |
|
| 243 | + function _dispatchRequest($socket, $encoding) { |
|
| 244 | + foreach ($this->_headers as $header_line) { |
|
| 245 | + $socket->write($header_line . "\r\n"); |
|
| 246 | + } |
|
| 247 | + if (count($this->_cookies) > 0) { |
|
| 248 | + $socket->write("Cookie: " . implode(";", $this->_cookies) . "\r\n"); |
|
| 249 | + } |
|
| 250 | + $encoding->writeHeadersTo($socket); |
|
| 251 | + $socket->write("\r\n"); |
|
| 252 | + $encoding->writeTo($socket); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Adds a header line to the request. |
|
| 257 | + * @param string $header_line Text of full header line. |
|
| 258 | + * @access public |
|
| 259 | + */ |
|
| 260 | + function addHeaderLine($header_line) { |
|
| 261 | + $this->_headers[] = $header_line; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Reads all the relevant cookies from the |
|
| 266 | + * cookie jar. |
|
| 267 | + * @param SimpleCookieJar $jar Jar to read |
|
| 268 | + * @param SimpleUrl $url Url to use for scope. |
|
| 269 | + * @access public |
|
| 270 | + */ |
|
| 271 | + function readCookiesFromJar($jar, $url) { |
|
| 272 | + $this->_cookies = $jar->selectAsPairs($url); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Wraps the socket in a response parser. |
|
| 277 | + * @param SimpleSocket $socket Responding socket. |
|
| 278 | + * @return SimpleHttpResponse Parsed response object. |
|
| 279 | + * @access protected |
|
| 280 | + */ |
|
| 281 | + function &_createResponse($socket) { |
|
| 282 | + $response = new SimpleHttpResponse( |
|
| 283 | + $socket, |
|
| 284 | + $this->_route->getUrl(), |
|
| 285 | + $this->_encoding); |
|
| 286 | + return $response; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Collection of header lines in the response. |
|
| 292 | 292 | * @package SimpleTest |
| 293 | 293 | * @subpackage WebTester |
| 294 | - */ |
|
| 295 | - class SimpleHttpHeaders { |
|
| 296 | - protected $_raw_headers; |
|
| 297 | - protected $_response_code; |
|
| 298 | - protected $_http_version; |
|
| 299 | - protected $_mime_type; |
|
| 300 | - protected $_location; |
|
| 301 | - protected $_cookies; |
|
| 302 | - protected $_authentication; |
|
| 303 | - protected $_realm; |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Parses the incoming header block. |
|
| 307 | - * @param string $headers Header block. |
|
| 308 | - * @access public |
|
| 309 | - */ |
|
| 310 | - function SimpleHttpHeaders($headers) { |
|
| 311 | - $this->_raw_headers = $headers; |
|
| 312 | - $this->_response_code = false; |
|
| 313 | - $this->_http_version = false; |
|
| 314 | - $this->_mime_type = ''; |
|
| 315 | - $this->_location = false; |
|
| 316 | - $this->_cookies = array(); |
|
| 317 | - $this->_authentication = false; |
|
| 318 | - $this->_realm = false; |
|
| 319 | - foreach (split("\r\n", $headers) as $header_line) { |
|
| 320 | - $this->_parseHeaderLine($header_line); |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Accessor for parsed HTTP protocol version. |
|
| 326 | - * @return integer HTTP error code. |
|
| 327 | - * @access public |
|
| 328 | - */ |
|
| 329 | - function getHttpVersion() { |
|
| 330 | - return $this->_http_version; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Accessor for raw header block. |
|
| 335 | - * @return string All headers as raw string. |
|
| 336 | - * @access public |
|
| 337 | - */ |
|
| 338 | - function getRaw() { |
|
| 339 | - return $this->_raw_headers; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Accessor for parsed HTTP error code. |
|
| 344 | - * @return integer HTTP error code. |
|
| 345 | - * @access public |
|
| 346 | - */ |
|
| 347 | - function getResponseCode() { |
|
| 348 | - return (integer)$this->_response_code; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Returns the redirected URL or false if |
|
| 353 | - * no redirection. |
|
| 354 | - * @return string URL or false for none. |
|
| 355 | - * @access public |
|
| 356 | - */ |
|
| 357 | - function getLocation() { |
|
| 358 | - return $this->_location; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Test to see if the response is a valid redirect. |
|
| 363 | - * @return boolean True if valid redirect. |
|
| 364 | - * @access public |
|
| 365 | - */ |
|
| 366 | - function isRedirect() { |
|
| 367 | - return in_array($this->_response_code, array(301, 302, 303, 307)) && |
|
| 368 | - (boolean)$this->getLocation(); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Test to see if the response is an authentication |
|
| 373 | - * challenge. |
|
| 374 | - * @return boolean True if challenge. |
|
| 375 | - * @access public |
|
| 376 | - */ |
|
| 377 | - function isChallenge() { |
|
| 378 | - return ($this->_response_code == 401) && |
|
| 379 | - (boolean)$this->_authentication && |
|
| 380 | - (boolean)$this->_realm; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Accessor for MIME type header information. |
|
| 385 | - * @return string MIME type. |
|
| 386 | - * @access public |
|
| 387 | - */ |
|
| 388 | - function getMimeType() { |
|
| 389 | - return $this->_mime_type; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Accessor for authentication type. |
|
| 394 | - * @return string Type. |
|
| 395 | - * @access public |
|
| 396 | - */ |
|
| 397 | - function getAuthentication() { |
|
| 398 | - return $this->_authentication; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - /** |
|
| 402 | - * Accessor for security realm. |
|
| 403 | - * @return string Realm. |
|
| 404 | - * @access public |
|
| 405 | - */ |
|
| 406 | - function getRealm() { |
|
| 407 | - return $this->_realm; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Writes new cookies to the cookie jar. |
|
| 412 | - * @param SimpleCookieJar $jar Jar to write to. |
|
| 413 | - * @param SimpleUrl $url Host and path to write under. |
|
| 414 | - * @access public |
|
| 415 | - */ |
|
| 416 | - function writeCookiesToJar($jar, $url) { |
|
| 417 | - foreach ($this->_cookies as $cookie) { |
|
| 418 | - $jar->setCookie( |
|
| 419 | - $cookie->getName(), |
|
| 420 | - $cookie->getValue(), |
|
| 421 | - $url->getHost(), |
|
| 422 | - $cookie->getPath(), |
|
| 423 | - $cookie->getExpiry()); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Called on each header line to accumulate the held |
|
| 429 | - * data within the class. |
|
| 430 | - * @param string $header_line One line of header. |
|
| 431 | - * @access protected |
|
| 432 | - */ |
|
| 433 | - function _parseHeaderLine($header_line) { |
|
| 434 | - if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) { |
|
| 435 | - $this->_http_version = $matches[1]; |
|
| 436 | - $this->_response_code = $matches[2]; |
|
| 437 | - } |
|
| 438 | - if (preg_match('/Content-type:\s*(.*)/i', $header_line, $matches)) { |
|
| 439 | - $this->_mime_type = trim($matches[1]); |
|
| 440 | - } |
|
| 441 | - if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) { |
|
| 442 | - $this->_location = trim($matches[1]); |
|
| 443 | - } |
|
| 444 | - if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) { |
|
| 445 | - $this->_cookies[] = $this->_parseCookie($matches[1]); |
|
| 446 | - } |
|
| 447 | - if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) { |
|
| 448 | - $this->_authentication = $matches[1]; |
|
| 449 | - $this->_realm = trim($matches[2]); |
|
| 450 | - } |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Parse the Set-cookie content. |
|
| 455 | - * @param string $cookie_line Text after "Set-cookie:" |
|
| 456 | - * @return SimpleCookie New cookie object. |
|
| 457 | - * @access private |
|
| 458 | - */ |
|
| 459 | - function _parseCookie($cookie_line) { |
|
| 460 | - $parts = split(";", $cookie_line); |
|
| 461 | - $cookie = array(); |
|
| 462 | - preg_match('/\s*(.*?)\s*=(.*)/', array_shift($parts), $cookie); |
|
| 463 | - foreach ($parts as $part) { |
|
| 464 | - if (preg_match('/\s*(.*?)\s*=(.*)/', $part, $matches)) { |
|
| 465 | - $cookie[$matches[1]] = trim($matches[2]); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - return new SimpleCookie( |
|
| 469 | - $cookie[1], |
|
| 470 | - trim($cookie[2]), |
|
| 471 | - isset($cookie["path"]) ? $cookie["path"] : "", |
|
| 472 | - isset($cookie["expires"]) ? $cookie["expires"] : false); |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Basic HTTP response. |
|
| 294 | + */ |
|
| 295 | + class SimpleHttpHeaders { |
|
| 296 | + protected $_raw_headers; |
|
| 297 | + protected $_response_code; |
|
| 298 | + protected $_http_version; |
|
| 299 | + protected $_mime_type; |
|
| 300 | + protected $_location; |
|
| 301 | + protected $_cookies; |
|
| 302 | + protected $_authentication; |
|
| 303 | + protected $_realm; |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Parses the incoming header block. |
|
| 307 | + * @param string $headers Header block. |
|
| 308 | + * @access public |
|
| 309 | + */ |
|
| 310 | + function SimpleHttpHeaders($headers) { |
|
| 311 | + $this->_raw_headers = $headers; |
|
| 312 | + $this->_response_code = false; |
|
| 313 | + $this->_http_version = false; |
|
| 314 | + $this->_mime_type = ''; |
|
| 315 | + $this->_location = false; |
|
| 316 | + $this->_cookies = array(); |
|
| 317 | + $this->_authentication = false; |
|
| 318 | + $this->_realm = false; |
|
| 319 | + foreach (split("\r\n", $headers) as $header_line) { |
|
| 320 | + $this->_parseHeaderLine($header_line); |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Accessor for parsed HTTP protocol version. |
|
| 326 | + * @return integer HTTP error code. |
|
| 327 | + * @access public |
|
| 328 | + */ |
|
| 329 | + function getHttpVersion() { |
|
| 330 | + return $this->_http_version; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Accessor for raw header block. |
|
| 335 | + * @return string All headers as raw string. |
|
| 336 | + * @access public |
|
| 337 | + */ |
|
| 338 | + function getRaw() { |
|
| 339 | + return $this->_raw_headers; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Accessor for parsed HTTP error code. |
|
| 344 | + * @return integer HTTP error code. |
|
| 345 | + * @access public |
|
| 346 | + */ |
|
| 347 | + function getResponseCode() { |
|
| 348 | + return (integer)$this->_response_code; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Returns the redirected URL or false if |
|
| 353 | + * no redirection. |
|
| 354 | + * @return string URL or false for none. |
|
| 355 | + * @access public |
|
| 356 | + */ |
|
| 357 | + function getLocation() { |
|
| 358 | + return $this->_location; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Test to see if the response is a valid redirect. |
|
| 363 | + * @return boolean True if valid redirect. |
|
| 364 | + * @access public |
|
| 365 | + */ |
|
| 366 | + function isRedirect() { |
|
| 367 | + return in_array($this->_response_code, array(301, 302, 303, 307)) && |
|
| 368 | + (boolean)$this->getLocation(); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Test to see if the response is an authentication |
|
| 373 | + * challenge. |
|
| 374 | + * @return boolean True if challenge. |
|
| 375 | + * @access public |
|
| 376 | + */ |
|
| 377 | + function isChallenge() { |
|
| 378 | + return ($this->_response_code == 401) && |
|
| 379 | + (boolean)$this->_authentication && |
|
| 380 | + (boolean)$this->_realm; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Accessor for MIME type header information. |
|
| 385 | + * @return string MIME type. |
|
| 386 | + * @access public |
|
| 387 | + */ |
|
| 388 | + function getMimeType() { |
|
| 389 | + return $this->_mime_type; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Accessor for authentication type. |
|
| 394 | + * @return string Type. |
|
| 395 | + * @access public |
|
| 396 | + */ |
|
| 397 | + function getAuthentication() { |
|
| 398 | + return $this->_authentication; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + /** |
|
| 402 | + * Accessor for security realm. |
|
| 403 | + * @return string Realm. |
|
| 404 | + * @access public |
|
| 405 | + */ |
|
| 406 | + function getRealm() { |
|
| 407 | + return $this->_realm; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Writes new cookies to the cookie jar. |
|
| 412 | + * @param SimpleCookieJar $jar Jar to write to. |
|
| 413 | + * @param SimpleUrl $url Host and path to write under. |
|
| 414 | + * @access public |
|
| 415 | + */ |
|
| 416 | + function writeCookiesToJar($jar, $url) { |
|
| 417 | + foreach ($this->_cookies as $cookie) { |
|
| 418 | + $jar->setCookie( |
|
| 419 | + $cookie->getName(), |
|
| 420 | + $cookie->getValue(), |
|
| 421 | + $url->getHost(), |
|
| 422 | + $cookie->getPath(), |
|
| 423 | + $cookie->getExpiry()); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Called on each header line to accumulate the held |
|
| 429 | + * data within the class. |
|
| 430 | + * @param string $header_line One line of header. |
|
| 431 | + * @access protected |
|
| 432 | + */ |
|
| 433 | + function _parseHeaderLine($header_line) { |
|
| 434 | + if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) { |
|
| 435 | + $this->_http_version = $matches[1]; |
|
| 436 | + $this->_response_code = $matches[2]; |
|
| 437 | + } |
|
| 438 | + if (preg_match('/Content-type:\s*(.*)/i', $header_line, $matches)) { |
|
| 439 | + $this->_mime_type = trim($matches[1]); |
|
| 440 | + } |
|
| 441 | + if (preg_match('/Location:\s*(.*)/i', $header_line, $matches)) { |
|
| 442 | + $this->_location = trim($matches[1]); |
|
| 443 | + } |
|
| 444 | + if (preg_match('/Set-cookie:(.*)/i', $header_line, $matches)) { |
|
| 445 | + $this->_cookies[] = $this->_parseCookie($matches[1]); |
|
| 446 | + } |
|
| 447 | + if (preg_match('/WWW-Authenticate:\s+(\S+)\s+realm=\"(.*?)\"/i', $header_line, $matches)) { |
|
| 448 | + $this->_authentication = $matches[1]; |
|
| 449 | + $this->_realm = trim($matches[2]); |
|
| 450 | + } |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Parse the Set-cookie content. |
|
| 455 | + * @param string $cookie_line Text after "Set-cookie:" |
|
| 456 | + * @return SimpleCookie New cookie object. |
|
| 457 | + * @access private |
|
| 458 | + */ |
|
| 459 | + function _parseCookie($cookie_line) { |
|
| 460 | + $parts = split(";", $cookie_line); |
|
| 461 | + $cookie = array(); |
|
| 462 | + preg_match('/\s*(.*?)\s*=(.*)/', array_shift($parts), $cookie); |
|
| 463 | + foreach ($parts as $part) { |
|
| 464 | + if (preg_match('/\s*(.*?)\s*=(.*)/', $part, $matches)) { |
|
| 465 | + $cookie[$matches[1]] = trim($matches[2]); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + return new SimpleCookie( |
|
| 469 | + $cookie[1], |
|
| 470 | + trim($cookie[2]), |
|
| 471 | + isset($cookie["path"]) ? $cookie["path"] : "", |
|
| 472 | + isset($cookie["expires"]) ? $cookie["expires"] : false); |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Basic HTTP response. |
|
| 478 | 478 | * @package SimpleTest |
| 479 | 479 | * @subpackage WebTester |
| 480 | - */ |
|
| 481 | - class SimpleHttpResponse extends SimpleStickyError { |
|
| 482 | - protected $_url; |
|
| 483 | - protected $_encoding; |
|
| 484 | - protected $_sent; |
|
| 485 | - protected $_content; |
|
| 486 | - protected $_headers; |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Constructor. Reads and parses the incoming |
|
| 490 | - * content and headers. |
|
| 491 | - * @param SimpleSocket $socket Network connection to fetch |
|
| 492 | - * response text from. |
|
| 493 | - * @param SimpleUrl $url Resource name. |
|
| 494 | - * @param mixed $encoding Record of content sent. |
|
| 495 | - * @access public |
|
| 496 | - */ |
|
| 497 | - function SimpleHttpResponse($socket, $url, $encoding) { |
|
| 498 | - $this->SimpleStickyError(); |
|
| 499 | - $this->_url = $url; |
|
| 500 | - $this->_encoding = $encoding; |
|
| 501 | - $this->_sent = $socket->getSent(); |
|
| 502 | - $this->_content = false; |
|
| 503 | - $raw = $this->_readAll($socket); |
|
| 504 | - if ($socket->isError()) { |
|
| 505 | - $this->_setError('Error reading socket [' . $socket->getError() . ']'); |
|
| 506 | - return; |
|
| 507 | - } |
|
| 508 | - $this->_parse($raw); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Splits up the headers and the rest of the content. |
|
| 513 | - * @param string $raw Content to parse. |
|
| 514 | - * @access private |
|
| 515 | - */ |
|
| 516 | - function _parse($raw) { |
|
| 517 | - if (! $raw) { |
|
| 518 | - $this->_setError('Nothing fetched'); |
|
| 519 | - $this->_headers = new SimpleHttpHeaders(''); |
|
| 520 | - } elseif (! strstr($raw, "\r\n\r\n")) { |
|
| 521 | - $this->_setError('Could not split headers from content'); |
|
| 522 | - $this->_headers = new SimpleHttpHeaders($raw); |
|
| 523 | - } else { |
|
| 524 | - list($headers, $this->_content) = split("\r\n\r\n", $raw, 2); |
|
| 525 | - $this->_headers = new SimpleHttpHeaders($headers); |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Original request method. |
|
| 531 | - * @return string GET, POST or HEAD. |
|
| 532 | - * @access public |
|
| 533 | - */ |
|
| 534 | - function getMethod() { |
|
| 535 | - return $this->_encoding->getMethod(); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Resource name. |
|
| 540 | - * @return SimpleUrl Current url. |
|
| 541 | - * @access public |
|
| 542 | - */ |
|
| 543 | - function getUrl() { |
|
| 544 | - return $this->_url; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Original request data. |
|
| 549 | - * @return mixed Sent content. |
|
| 550 | - * @access public |
|
| 551 | - */ |
|
| 552 | - function getRequestData() { |
|
| 553 | - return $this->_encoding; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Raw request that was sent down the wire. |
|
| 558 | - * @return string Bytes actually sent. |
|
| 559 | - * @access public |
|
| 560 | - */ |
|
| 561 | - function getSent() { |
|
| 562 | - return $this->_sent; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Accessor for the content after the last |
|
| 567 | - * header line. |
|
| 568 | - * @return string All content. |
|
| 569 | - * @access public |
|
| 570 | - */ |
|
| 571 | - function getContent() { |
|
| 572 | - return $this->_content; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Accessor for header block. The response is the |
|
| 577 | - * combination of this and the content. |
|
| 578 | - * @return SimpleHeaders Wrapped header block. |
|
| 579 | - * @access public |
|
| 580 | - */ |
|
| 581 | - function getHeaders() { |
|
| 582 | - return $this->_headers; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Accessor for any new cookies. |
|
| 587 | - * @return array List of new cookies. |
|
| 588 | - * @access public |
|
| 589 | - */ |
|
| 590 | - function getNewCookies() { |
|
| 591 | - return $this->_headers->getNewCookies(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Reads the whole of the socket output into a |
|
| 596 | - * single string. |
|
| 597 | - * @param SimpleSocket $socket Unread socket. |
|
| 598 | - * @return string Raw output if successful |
|
| 599 | - * else false. |
|
| 600 | - * @access private |
|
| 601 | - */ |
|
| 602 | - function _readAll($socket) { |
|
| 603 | - $all = ''; |
|
| 604 | - while (! $this->_isLastPacket($next = $socket->read())) { |
|
| 605 | - $all .= $next; |
|
| 606 | - } |
|
| 607 | - return $all; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Test to see if the packet from the socket is the |
|
| 612 | - * last one. |
|
| 613 | - * @param string $packet Chunk to interpret. |
|
| 614 | - * @return boolean True if empty or EOF. |
|
| 615 | - * @access private |
|
| 616 | - */ |
|
| 617 | - function _isLastPacket($packet) { |
|
| 618 | - if (is_string($packet)) { |
|
| 619 | - return $packet === ''; |
|
| 620 | - } |
|
| 621 | - return ! $packet; |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | 480 | \ No newline at end of file |
| 481 | + */ |
|
| 482 | + class SimpleHttpResponse extends SimpleStickyError { |
|
| 483 | + protected $_url; |
|
| 484 | + protected $_encoding; |
|
| 485 | + protected $_sent; |
|
| 486 | + protected $_content; |
|
| 487 | + protected $_headers; |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Constructor. Reads and parses the incoming |
|
| 491 | + * content and headers. |
|
| 492 | + * @param SimpleSocket $socket Network connection to fetch |
|
| 493 | + * response text from. |
|
| 494 | + * @param SimpleUrl $url Resource name. |
|
| 495 | + * @param mixed $encoding Record of content sent. |
|
| 496 | + * @access public |
|
| 497 | + */ |
|
| 498 | + function SimpleHttpResponse($socket, $url, $encoding) { |
|
| 499 | + $this->SimpleStickyError(); |
|
| 500 | + $this->_url = $url; |
|
| 501 | + $this->_encoding = $encoding; |
|
| 502 | + $this->_sent = $socket->getSent(); |
|
| 503 | + $this->_content = false; |
|
| 504 | + $raw = $this->_readAll($socket); |
|
| 505 | + if ($socket->isError()) { |
|
| 506 | + $this->_setError('Error reading socket [' . $socket->getError() . ']'); |
|
| 507 | + return; |
|
| 508 | + } |
|
| 509 | + $this->_parse($raw); |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Splits up the headers and the rest of the content. |
|
| 514 | + * @param string $raw Content to parse. |
|
| 515 | + * @access private |
|
| 516 | + */ |
|
| 517 | + function _parse($raw) { |
|
| 518 | + if (! $raw) { |
|
| 519 | + $this->_setError('Nothing fetched'); |
|
| 520 | + $this->_headers = new SimpleHttpHeaders(''); |
|
| 521 | + } elseif (! strstr($raw, "\r\n\r\n")) { |
|
| 522 | + $this->_setError('Could not split headers from content'); |
|
| 523 | + $this->_headers = new SimpleHttpHeaders($raw); |
|
| 524 | + } else { |
|
| 525 | + list($headers, $this->_content) = split("\r\n\r\n", $raw, 2); |
|
| 526 | + $this->_headers = new SimpleHttpHeaders($headers); |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Original request method. |
|
| 532 | + * @return string GET, POST or HEAD. |
|
| 533 | + * @access public |
|
| 534 | + */ |
|
| 535 | + function getMethod() { |
|
| 536 | + return $this->_encoding->getMethod(); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Resource name. |
|
| 541 | + * @return SimpleUrl Current url. |
|
| 542 | + * @access public |
|
| 543 | + */ |
|
| 544 | + function getUrl() { |
|
| 545 | + return $this->_url; |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Original request data. |
|
| 550 | + * @return mixed Sent content. |
|
| 551 | + * @access public |
|
| 552 | + */ |
|
| 553 | + function getRequestData() { |
|
| 554 | + return $this->_encoding; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Raw request that was sent down the wire. |
|
| 559 | + * @return string Bytes actually sent. |
|
| 560 | + * @access public |
|
| 561 | + */ |
|
| 562 | + function getSent() { |
|
| 563 | + return $this->_sent; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Accessor for the content after the last |
|
| 568 | + * header line. |
|
| 569 | + * @return string All content. |
|
| 570 | + * @access public |
|
| 571 | + */ |
|
| 572 | + function getContent() { |
|
| 573 | + return $this->_content; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * Accessor for header block. The response is the |
|
| 578 | + * combination of this and the content. |
|
| 579 | + * @return SimpleHeaders Wrapped header block. |
|
| 580 | + * @access public |
|
| 581 | + */ |
|
| 582 | + function getHeaders() { |
|
| 583 | + return $this->_headers; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Accessor for any new cookies. |
|
| 588 | + * @return array List of new cookies. |
|
| 589 | + * @access public |
|
| 590 | + */ |
|
| 591 | + function getNewCookies() { |
|
| 592 | + return $this->_headers->getNewCookies(); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Reads the whole of the socket output into a |
|
| 597 | + * single string. |
|
| 598 | + * @param SimpleSocket $socket Unread socket. |
|
| 599 | + * @return string Raw output if successful |
|
| 600 | + * else false. |
|
| 601 | + * @access private |
|
| 602 | + */ |
|
| 603 | + function _readAll($socket) { |
|
| 604 | + $all = ''; |
|
| 605 | + while (! $this->_isLastPacket($next = $socket->read())) { |
|
| 606 | + $all .= $next; |
|
| 607 | + } |
|
| 608 | + return $all; |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Test to see if the packet from the socket is the |
|
| 613 | + * last one. |
|
| 614 | + * @param string $packet Chunk to interpret. |
|
| 615 | + * @return boolean True if empty or EOF. |
|
| 616 | + * @access private |
|
| 617 | + */ |
|
| 618 | + function _isLastPacket($packet) { |
|
| 619 | + if (is_string($packet)) { |
|
| 620 | + return $packet === ''; |
|
| 621 | + } |
|
| 622 | + return ! $packet; |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | 625 | \ No newline at end of file |
@@ -1,332 +1,332 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * Base include file for SimpleTest |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage WebTester |
|
| 6 | - * @version $Id: user_agent.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | - */ |
|
| 2 | + /** |
|
| 3 | + * Base include file for SimpleTest |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage WebTester |
|
| 6 | + * @version $Id: user_agent.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | + */ |
|
| 8 | 8 | |
| 9 | - /**#@+ |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include other SimpleTest class files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/cookies.php'); |
|
| 13 | - require_once(dirname(__FILE__) . '/http.php'); |
|
| 14 | - require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 15 | - require_once(dirname(__FILE__) . '/authentication.php'); |
|
| 16 | - /**#@-*/ |
|
| 12 | + require_once(dirname(__FILE__) . '/cookies.php'); |
|
| 13 | + require_once(dirname(__FILE__) . '/http.php'); |
|
| 14 | + require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 15 | + require_once(dirname(__FILE__) . '/authentication.php'); |
|
| 16 | + /**#@-*/ |
|
| 17 | 17 | |
| 18 | - if (! defined('DEFAULT_MAX_REDIRECTS')) { |
|
| 19 | - define('DEFAULT_MAX_REDIRECTS', 3); |
|
| 20 | - } |
|
| 18 | + if (! defined('DEFAULT_MAX_REDIRECTS')) { |
|
| 19 | + define('DEFAULT_MAX_REDIRECTS', 3); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - if (! defined('DEFAULT_CONNECTION_TIMEOUT')) { |
|
| 23 | - define('DEFAULT_CONNECTION_TIMEOUT', 15); |
|
| 24 | - } |
|
| 22 | + if (! defined('DEFAULT_CONNECTION_TIMEOUT')) { |
|
| 23 | + define('DEFAULT_CONNECTION_TIMEOUT', 15); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Fetches web pages whilst keeping track of |
|
| 28 | - * cookies and authentication. |
|
| 26 | + /** |
|
| 27 | + * Fetches web pages whilst keeping track of |
|
| 28 | + * cookies and authentication. |
|
| 29 | 29 | * @package SimpleTest |
| 30 | 30 | * @subpackage WebTester |
| 31 | - */ |
|
| 32 | - class SimpleUserAgent { |
|
| 33 | - protected $_cookie_jar; |
|
| 34 | - protected $_cookies_enabled = true; |
|
| 35 | - protected $_authenticator; |
|
| 36 | - protected $_max_redirects = DEFAULT_MAX_REDIRECTS; |
|
| 37 | - protected $_proxy = false; |
|
| 38 | - protected $_proxy_username = false; |
|
| 39 | - protected $_proxy_password = false; |
|
| 40 | - protected $_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; |
|
| 41 | - protected $_additional_headers = array(); |
|
| 31 | + */ |
|
| 32 | + class SimpleUserAgent { |
|
| 33 | + protected $_cookie_jar; |
|
| 34 | + protected $_cookies_enabled = true; |
|
| 35 | + protected $_authenticator; |
|
| 36 | + protected $_max_redirects = DEFAULT_MAX_REDIRECTS; |
|
| 37 | + protected $_proxy = false; |
|
| 38 | + protected $_proxy_username = false; |
|
| 39 | + protected $_proxy_password = false; |
|
| 40 | + protected $_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; |
|
| 41 | + protected $_additional_headers = array(); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Starts with no cookies, realms or proxies. |
|
| 45 | - * @access public |
|
| 46 | - */ |
|
| 47 | - function SimpleUserAgent() { |
|
| 48 | - $this->_cookie_jar = new SimpleCookieJar(); |
|
| 49 | - $this->_authenticator = new SimpleAuthenticator(); |
|
| 50 | - } |
|
| 43 | + /** |
|
| 44 | + * Starts with no cookies, realms or proxies. |
|
| 45 | + * @access public |
|
| 46 | + */ |
|
| 47 | + function SimpleUserAgent() { |
|
| 48 | + $this->_cookie_jar = new SimpleCookieJar(); |
|
| 49 | + $this->_authenticator = new SimpleAuthenticator(); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Removes expired and temporary cookies as if |
|
| 54 | - * the browser was closed and re-opened. Authorisation |
|
| 55 | - * has to be obtained again as well. |
|
| 56 | - * @param string/integer $date Time when session restarted. |
|
| 57 | - * If omitted then all persistent |
|
| 58 | - * cookies are kept. |
|
| 59 | - * @access public |
|
| 60 | - */ |
|
| 61 | - function restart($date = false) { |
|
| 62 | - $this->_cookie_jar->restartSession($date); |
|
| 63 | - $this->_authenticator->restartSession(); |
|
| 64 | - } |
|
| 52 | + /** |
|
| 53 | + * Removes expired and temporary cookies as if |
|
| 54 | + * the browser was closed and re-opened. Authorisation |
|
| 55 | + * has to be obtained again as well. |
|
| 56 | + * @param string/integer $date Time when session restarted. |
|
| 57 | + * If omitted then all persistent |
|
| 58 | + * cookies are kept. |
|
| 59 | + * @access public |
|
| 60 | + */ |
|
| 61 | + function restart($date = false) { |
|
| 62 | + $this->_cookie_jar->restartSession($date); |
|
| 63 | + $this->_authenticator->restartSession(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Adds a header to every fetch. |
|
| 68 | - * @param string $header Header line to add to every |
|
| 69 | - * request until cleared. |
|
| 70 | - * @access public |
|
| 71 | - */ |
|
| 72 | - function addHeader($header) { |
|
| 73 | - $this->_additional_headers[] = $header; |
|
| 74 | - } |
|
| 66 | + /** |
|
| 67 | + * Adds a header to every fetch. |
|
| 68 | + * @param string $header Header line to add to every |
|
| 69 | + * request until cleared. |
|
| 70 | + * @access public |
|
| 71 | + */ |
|
| 72 | + function addHeader($header) { |
|
| 73 | + $this->_additional_headers[] = $header; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Ages the cookies by the specified time. |
|
| 78 | - * @param integer $interval Amount in seconds. |
|
| 79 | - * @access public |
|
| 80 | - */ |
|
| 81 | - function ageCookies($interval) { |
|
| 82 | - $this->_cookie_jar->agePrematurely($interval); |
|
| 83 | - } |
|
| 76 | + /** |
|
| 77 | + * Ages the cookies by the specified time. |
|
| 78 | + * @param integer $interval Amount in seconds. |
|
| 79 | + * @access public |
|
| 80 | + */ |
|
| 81 | + function ageCookies($interval) { |
|
| 82 | + $this->_cookie_jar->agePrematurely($interval); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Sets an additional cookie. If a cookie has |
|
| 87 | - * the same name and path it is replaced. |
|
| 88 | - * @param string $name Cookie key. |
|
| 89 | - * @param string $value Value of cookie. |
|
| 90 | - * @param string $host Host upon which the cookie is valid. |
|
| 91 | - * @param string $path Cookie path if not host wide. |
|
| 92 | - * @param string $expiry Expiry date. |
|
| 93 | - * @access public |
|
| 94 | - */ |
|
| 95 | - function setCookie($name, $value, $host = false, $path = '/', $expiry = false) { |
|
| 96 | - $this->_cookie_jar->setCookie($name, $value, $host, $path, $expiry); |
|
| 97 | - } |
|
| 85 | + /** |
|
| 86 | + * Sets an additional cookie. If a cookie has |
|
| 87 | + * the same name and path it is replaced. |
|
| 88 | + * @param string $name Cookie key. |
|
| 89 | + * @param string $value Value of cookie. |
|
| 90 | + * @param string $host Host upon which the cookie is valid. |
|
| 91 | + * @param string $path Cookie path if not host wide. |
|
| 92 | + * @param string $expiry Expiry date. |
|
| 93 | + * @access public |
|
| 94 | + */ |
|
| 95 | + function setCookie($name, $value, $host = false, $path = '/', $expiry = false) { |
|
| 96 | + $this->_cookie_jar->setCookie($name, $value, $host, $path, $expiry); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Reads the most specific cookie value from the |
|
| 101 | - * browser cookies. |
|
| 102 | - * @param string $host Host to search. |
|
| 103 | - * @param string $path Applicable path. |
|
| 104 | - * @param string $name Name of cookie to read. |
|
| 105 | - * @return string False if not present, else the |
|
| 106 | - * value as a string. |
|
| 107 | - * @access public |
|
| 108 | - */ |
|
| 109 | - function getCookieValue($host, $path, $name) { |
|
| 110 | - return $this->_cookie_jar->getCookieValue($host, $path, $name); |
|
| 111 | - } |
|
| 99 | + /** |
|
| 100 | + * Reads the most specific cookie value from the |
|
| 101 | + * browser cookies. |
|
| 102 | + * @param string $host Host to search. |
|
| 103 | + * @param string $path Applicable path. |
|
| 104 | + * @param string $name Name of cookie to read. |
|
| 105 | + * @return string False if not present, else the |
|
| 106 | + * value as a string. |
|
| 107 | + * @access public |
|
| 108 | + */ |
|
| 109 | + function getCookieValue($host, $path, $name) { |
|
| 110 | + return $this->_cookie_jar->getCookieValue($host, $path, $name); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Reads the current cookies within the base URL. |
|
| 115 | - * @param string $name Key of cookie to find. |
|
| 116 | - * @param SimpleUrl $base Base URL to search from. |
|
| 117 | - * @return string/boolean Null if there is no base URL, false |
|
| 118 | - * if the cookie is not set. |
|
| 119 | - * @access public |
|
| 120 | - */ |
|
| 121 | - function getBaseCookieValue($name, $base) { |
|
| 122 | - if (! $base) { |
|
| 123 | - return null; |
|
| 124 | - } |
|
| 125 | - return $this->getCookieValue($base->getHost(), $base->getPath(), $name); |
|
| 126 | - } |
|
| 113 | + /** |
|
| 114 | + * Reads the current cookies within the base URL. |
|
| 115 | + * @param string $name Key of cookie to find. |
|
| 116 | + * @param SimpleUrl $base Base URL to search from. |
|
| 117 | + * @return string/boolean Null if there is no base URL, false |
|
| 118 | + * if the cookie is not set. |
|
| 119 | + * @access public |
|
| 120 | + */ |
|
| 121 | + function getBaseCookieValue($name, $base) { |
|
| 122 | + if (! $base) { |
|
| 123 | + return null; |
|
| 124 | + } |
|
| 125 | + return $this->getCookieValue($base->getHost(), $base->getPath(), $name); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Switches off cookie sending and recieving. |
|
| 130 | - * @access public |
|
| 131 | - */ |
|
| 132 | - function ignoreCookies() { |
|
| 133 | - $this->_cookies_enabled = false; |
|
| 134 | - } |
|
| 128 | + /** |
|
| 129 | + * Switches off cookie sending and recieving. |
|
| 130 | + * @access public |
|
| 131 | + */ |
|
| 132 | + function ignoreCookies() { |
|
| 133 | + $this->_cookies_enabled = false; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Switches back on the cookie sending and recieving. |
|
| 138 | - * @access public |
|
| 139 | - */ |
|
| 140 | - function useCookies() { |
|
| 141 | - $this->_cookies_enabled = true; |
|
| 142 | - } |
|
| 136 | + /** |
|
| 137 | + * Switches back on the cookie sending and recieving. |
|
| 138 | + * @access public |
|
| 139 | + */ |
|
| 140 | + function useCookies() { |
|
| 141 | + $this->_cookies_enabled = true; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Sets the socket timeout for opening a connection. |
|
| 146 | - * @param integer $timeout Maximum time in seconds. |
|
| 147 | - * @access public |
|
| 148 | - */ |
|
| 149 | - function setConnectionTimeout($timeout) { |
|
| 150 | - $this->_connection_timeout = $timeout; |
|
| 151 | - } |
|
| 144 | + /** |
|
| 145 | + * Sets the socket timeout for opening a connection. |
|
| 146 | + * @param integer $timeout Maximum time in seconds. |
|
| 147 | + * @access public |
|
| 148 | + */ |
|
| 149 | + function setConnectionTimeout($timeout) { |
|
| 150 | + $this->_connection_timeout = $timeout; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Sets the maximum number of redirects before |
|
| 155 | - * a page will be loaded anyway. |
|
| 156 | - * @param integer $max Most hops allowed. |
|
| 157 | - * @access public |
|
| 158 | - */ |
|
| 159 | - function setMaximumRedirects($max) { |
|
| 160 | - $this->_max_redirects = $max; |
|
| 161 | - } |
|
| 153 | + /** |
|
| 154 | + * Sets the maximum number of redirects before |
|
| 155 | + * a page will be loaded anyway. |
|
| 156 | + * @param integer $max Most hops allowed. |
|
| 157 | + * @access public |
|
| 158 | + */ |
|
| 159 | + function setMaximumRedirects($max) { |
|
| 160 | + $this->_max_redirects = $max; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - /** |
|
| 164 | - * Sets proxy to use on all requests for when |
|
| 165 | - * testing from behind a firewall. Set URL |
|
| 166 | - * to false to disable. |
|
| 167 | - * @param string $proxy Proxy URL. |
|
| 168 | - * @param string $username Proxy username for authentication. |
|
| 169 | - * @param string $password Proxy password for authentication. |
|
| 170 | - * @access public |
|
| 171 | - */ |
|
| 172 | - function useProxy($proxy, $username, $password) { |
|
| 173 | - if (! $proxy) { |
|
| 174 | - $this->_proxy = false; |
|
| 175 | - return; |
|
| 176 | - } |
|
| 177 | - if ((strncmp($proxy, 'http://', 7) != 0) && (strncmp($proxy, 'https://', 8) != 0)) { |
|
| 178 | - $proxy = 'http://'. $proxy; |
|
| 179 | - } |
|
| 180 | - $this->_proxy = new SimpleUrl($proxy); |
|
| 181 | - $this->_proxy_username = $username; |
|
| 182 | - $this->_proxy_password = $password; |
|
| 183 | - } |
|
| 163 | + /** |
|
| 164 | + * Sets proxy to use on all requests for when |
|
| 165 | + * testing from behind a firewall. Set URL |
|
| 166 | + * to false to disable. |
|
| 167 | + * @param string $proxy Proxy URL. |
|
| 168 | + * @param string $username Proxy username for authentication. |
|
| 169 | + * @param string $password Proxy password for authentication. |
|
| 170 | + * @access public |
|
| 171 | + */ |
|
| 172 | + function useProxy($proxy, $username, $password) { |
|
| 173 | + if (! $proxy) { |
|
| 174 | + $this->_proxy = false; |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | + if ((strncmp($proxy, 'http://', 7) != 0) && (strncmp($proxy, 'https://', 8) != 0)) { |
|
| 178 | + $proxy = 'http://'. $proxy; |
|
| 179 | + } |
|
| 180 | + $this->_proxy = new SimpleUrl($proxy); |
|
| 181 | + $this->_proxy_username = $username; |
|
| 182 | + $this->_proxy_password = $password; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Test to see if the redirect limit is passed. |
|
| 187 | - * @param integer $redirects Count so far. |
|
| 188 | - * @return boolean True if over. |
|
| 189 | - * @access private |
|
| 190 | - */ |
|
| 191 | - function _isTooManyRedirects($redirects) { |
|
| 192 | - return ($redirects > $this->_max_redirects); |
|
| 193 | - } |
|
| 185 | + /** |
|
| 186 | + * Test to see if the redirect limit is passed. |
|
| 187 | + * @param integer $redirects Count so far. |
|
| 188 | + * @return boolean True if over. |
|
| 189 | + * @access private |
|
| 190 | + */ |
|
| 191 | + function _isTooManyRedirects($redirects) { |
|
| 192 | + return ($redirects > $this->_max_redirects); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * Sets the identity for the current realm. |
|
| 197 | - * @param string $host Host to which realm applies. |
|
| 198 | - * @param string $realm Full name of realm. |
|
| 199 | - * @param string $username Username for realm. |
|
| 200 | - * @param string $password Password for realm. |
|
| 201 | - * @access public |
|
| 202 | - */ |
|
| 203 | - function setIdentity($host, $realm, $username, $password) { |
|
| 204 | - $this->_authenticator->setIdentityForRealm($host, $realm, $username, $password); |
|
| 205 | - } |
|
| 195 | + /** |
|
| 196 | + * Sets the identity for the current realm. |
|
| 197 | + * @param string $host Host to which realm applies. |
|
| 198 | + * @param string $realm Full name of realm. |
|
| 199 | + * @param string $username Username for realm. |
|
| 200 | + * @param string $password Password for realm. |
|
| 201 | + * @access public |
|
| 202 | + */ |
|
| 203 | + function setIdentity($host, $realm, $username, $password) { |
|
| 204 | + $this->_authenticator->setIdentityForRealm($host, $realm, $username, $password); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * Fetches a URL as a response object. Will keep trying if redirected. |
|
| 209 | - * It will also collect authentication realm information. |
|
| 210 | - * @param string/SimpleUrl $url Target to fetch. |
|
| 211 | - * @param SimpleEncoding $encoding Additional parameters for request. |
|
| 212 | - * @return SimpleHttpResponse Hopefully the target page. |
|
| 213 | - * @access public |
|
| 214 | - */ |
|
| 215 | - function &fetchResponse($url, $encoding) { |
|
| 216 | - if ($encoding->getMethod() != 'POST') { |
|
| 217 | - $url->addRequestParameters($encoding); |
|
| 218 | - $encoding->clear(); |
|
| 219 | - } |
|
| 220 | - $response = $this->_fetchWhileRedirected($url, $encoding); |
|
| 221 | - if ($headers = $response->getHeaders()) { |
|
| 222 | - if ($headers->isChallenge()) { |
|
| 223 | - $this->_authenticator->addRealm( |
|
| 224 | - $url, |
|
| 225 | - $headers->getAuthentication(), |
|
| 226 | - $headers->getRealm()); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - return $response; |
|
| 230 | - } |
|
| 207 | + /** |
|
| 208 | + * Fetches a URL as a response object. Will keep trying if redirected. |
|
| 209 | + * It will also collect authentication realm information. |
|
| 210 | + * @param string/SimpleUrl $url Target to fetch. |
|
| 211 | + * @param SimpleEncoding $encoding Additional parameters for request. |
|
| 212 | + * @return SimpleHttpResponse Hopefully the target page. |
|
| 213 | + * @access public |
|
| 214 | + */ |
|
| 215 | + function &fetchResponse($url, $encoding) { |
|
| 216 | + if ($encoding->getMethod() != 'POST') { |
|
| 217 | + $url->addRequestParameters($encoding); |
|
| 218 | + $encoding->clear(); |
|
| 219 | + } |
|
| 220 | + $response = $this->_fetchWhileRedirected($url, $encoding); |
|
| 221 | + if ($headers = $response->getHeaders()) { |
|
| 222 | + if ($headers->isChallenge()) { |
|
| 223 | + $this->_authenticator->addRealm( |
|
| 224 | + $url, |
|
| 225 | + $headers->getAuthentication(), |
|
| 226 | + $headers->getRealm()); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + return $response; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Fetches the page until no longer redirected or |
|
| 234 | - * until the redirect limit runs out. |
|
| 235 | - * @param SimpleUrl $url Target to fetch. |
|
| 236 | - * @param SimpelFormEncoding $encoding Additional parameters for request. |
|
| 237 | - * @return SimpleHttpResponse Hopefully the target page. |
|
| 238 | - * @access private |
|
| 239 | - */ |
|
| 240 | - function &_fetchWhileRedirected($url, $encoding) { |
|
| 241 | - $redirects = 0; |
|
| 242 | - do { |
|
| 243 | - $response = $this->_fetch($url, $encoding); |
|
| 244 | - if ($response->isError()) { |
|
| 245 | - return $response; |
|
| 246 | - } |
|
| 247 | - $headers = $response->getHeaders(); |
|
| 248 | - $location = new SimpleUrl($headers->getLocation()); |
|
| 249 | - $url = $location->makeAbsolute($url); |
|
| 250 | - if ($this->_cookies_enabled) { |
|
| 251 | - $headers->writeCookiesToJar($this->_cookie_jar, $url); |
|
| 252 | - } |
|
| 253 | - if (! $headers->isRedirect()) { |
|
| 254 | - break; |
|
| 255 | - } |
|
| 256 | - $encoding = new SimpleGetEncoding(); |
|
| 257 | - } while (! $this->_isTooManyRedirects(++$redirects)); |
|
| 258 | - return $response; |
|
| 259 | - } |
|
| 232 | + /** |
|
| 233 | + * Fetches the page until no longer redirected or |
|
| 234 | + * until the redirect limit runs out. |
|
| 235 | + * @param SimpleUrl $url Target to fetch. |
|
| 236 | + * @param SimpelFormEncoding $encoding Additional parameters for request. |
|
| 237 | + * @return SimpleHttpResponse Hopefully the target page. |
|
| 238 | + * @access private |
|
| 239 | + */ |
|
| 240 | + function &_fetchWhileRedirected($url, $encoding) { |
|
| 241 | + $redirects = 0; |
|
| 242 | + do { |
|
| 243 | + $response = $this->_fetch($url, $encoding); |
|
| 244 | + if ($response->isError()) { |
|
| 245 | + return $response; |
|
| 246 | + } |
|
| 247 | + $headers = $response->getHeaders(); |
|
| 248 | + $location = new SimpleUrl($headers->getLocation()); |
|
| 249 | + $url = $location->makeAbsolute($url); |
|
| 250 | + if ($this->_cookies_enabled) { |
|
| 251 | + $headers->writeCookiesToJar($this->_cookie_jar, $url); |
|
| 252 | + } |
|
| 253 | + if (! $headers->isRedirect()) { |
|
| 254 | + break; |
|
| 255 | + } |
|
| 256 | + $encoding = new SimpleGetEncoding(); |
|
| 257 | + } while (! $this->_isTooManyRedirects(++$redirects)); |
|
| 258 | + return $response; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * Actually make the web request. |
|
| 263 | - * @param SimpleUrl $url Target to fetch. |
|
| 264 | - * @param SimpleFormEncoding $encoding Additional parameters for request. |
|
| 265 | - * @return SimpleHttpResponse Headers and hopefully content. |
|
| 266 | - * @access protected |
|
| 267 | - */ |
|
| 268 | - function &_fetch($url, $encoding) { |
|
| 269 | - $request = $this->_createRequest($url, $encoding); |
|
| 270 | - $response = $request->fetch($this->_connection_timeout); |
|
| 271 | - return $response; |
|
| 272 | - } |
|
| 261 | + /** |
|
| 262 | + * Actually make the web request. |
|
| 263 | + * @param SimpleUrl $url Target to fetch. |
|
| 264 | + * @param SimpleFormEncoding $encoding Additional parameters for request. |
|
| 265 | + * @return SimpleHttpResponse Headers and hopefully content. |
|
| 266 | + * @access protected |
|
| 267 | + */ |
|
| 268 | + function &_fetch($url, $encoding) { |
|
| 269 | + $request = $this->_createRequest($url, $encoding); |
|
| 270 | + $response = $request->fetch($this->_connection_timeout); |
|
| 271 | + return $response; |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Creates a full page request. |
|
| 276 | - * @param SimpleUrl $url Target to fetch as url object. |
|
| 277 | - * @param SimpleFormEncoding $encoding POST/GET parameters. |
|
| 278 | - * @return SimpleHttpRequest New request. |
|
| 279 | - * @access private |
|
| 280 | - */ |
|
| 281 | - function &_createRequest($url, $encoding) { |
|
| 282 | - $request = $this->_createHttpRequest($url, $encoding); |
|
| 283 | - $this->_addAdditionalHeaders($request); |
|
| 284 | - if ($this->_cookies_enabled) { |
|
| 285 | - $request->readCookiesFromJar($this->_cookie_jar, $url); |
|
| 286 | - } |
|
| 287 | - $this->_authenticator->addHeaders($request, $url); |
|
| 288 | - return $request; |
|
| 289 | - } |
|
| 274 | + /** |
|
| 275 | + * Creates a full page request. |
|
| 276 | + * @param SimpleUrl $url Target to fetch as url object. |
|
| 277 | + * @param SimpleFormEncoding $encoding POST/GET parameters. |
|
| 278 | + * @return SimpleHttpRequest New request. |
|
| 279 | + * @access private |
|
| 280 | + */ |
|
| 281 | + function &_createRequest($url, $encoding) { |
|
| 282 | + $request = $this->_createHttpRequest($url, $encoding); |
|
| 283 | + $this->_addAdditionalHeaders($request); |
|
| 284 | + if ($this->_cookies_enabled) { |
|
| 285 | + $request->readCookiesFromJar($this->_cookie_jar, $url); |
|
| 286 | + } |
|
| 287 | + $this->_authenticator->addHeaders($request, $url); |
|
| 288 | + return $request; |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * Builds the appropriate HTTP request object. |
|
| 293 | - * @param SimpleUrl $url Target to fetch as url object. |
|
| 294 | - * @param SimpleFormEncoding $parameters POST/GET parameters. |
|
| 295 | - * @return SimpleHttpRequest New request object. |
|
| 296 | - * @access protected |
|
| 297 | - */ |
|
| 298 | - function &_createHttpRequest($url, $encoding) { |
|
| 299 | - $request = new SimpleHttpRequest($this->_createRoute($url), $encoding); |
|
| 300 | - return $request; |
|
| 301 | - } |
|
| 291 | + /** |
|
| 292 | + * Builds the appropriate HTTP request object. |
|
| 293 | + * @param SimpleUrl $url Target to fetch as url object. |
|
| 294 | + * @param SimpleFormEncoding $parameters POST/GET parameters. |
|
| 295 | + * @return SimpleHttpRequest New request object. |
|
| 296 | + * @access protected |
|
| 297 | + */ |
|
| 298 | + function &_createHttpRequest($url, $encoding) { |
|
| 299 | + $request = new SimpleHttpRequest($this->_createRoute($url), $encoding); |
|
| 300 | + return $request; |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * Sets up either a direct route or via a proxy. |
|
| 305 | - * @param SimpleUrl $url Target to fetch as url object. |
|
| 306 | - * @return SimpleRoute Route to take to fetch URL. |
|
| 307 | - * @access protected |
|
| 308 | - */ |
|
| 309 | - function &_createRoute($url) { |
|
| 310 | - if ($this->_proxy) { |
|
| 311 | - $route = new SimpleProxyRoute( |
|
| 312 | - $url, |
|
| 313 | - $this->_proxy, |
|
| 314 | - $this->_proxy_username, |
|
| 315 | - $this->_proxy_password); |
|
| 316 | - } else { |
|
| 317 | - $route = new SimpleRoute($url); |
|
| 318 | - } |
|
| 319 | - return $route; |
|
| 320 | - } |
|
| 303 | + /** |
|
| 304 | + * Sets up either a direct route or via a proxy. |
|
| 305 | + * @param SimpleUrl $url Target to fetch as url object. |
|
| 306 | + * @return SimpleRoute Route to take to fetch URL. |
|
| 307 | + * @access protected |
|
| 308 | + */ |
|
| 309 | + function &_createRoute($url) { |
|
| 310 | + if ($this->_proxy) { |
|
| 311 | + $route = new SimpleProxyRoute( |
|
| 312 | + $url, |
|
| 313 | + $this->_proxy, |
|
| 314 | + $this->_proxy_username, |
|
| 315 | + $this->_proxy_password); |
|
| 316 | + } else { |
|
| 317 | + $route = new SimpleRoute($url); |
|
| 318 | + } |
|
| 319 | + return $route; |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - /** |
|
| 323 | - * Adds additional manual headers. |
|
| 324 | - * @param SimpleHttpRequest $request Outgoing request. |
|
| 325 | - * @access private |
|
| 326 | - */ |
|
| 327 | - function _addAdditionalHeaders($request) { |
|
| 328 | - foreach ($this->_additional_headers as $header) { |
|
| 329 | - $request->addHeaderLine($header); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - } |
|
| 333 | 322 | \ No newline at end of file |
| 323 | + /** |
|
| 324 | + * Adds additional manual headers. |
|
| 325 | + * @param SimpleHttpRequest $request Outgoing request. |
|
| 326 | + * @access private |
|
| 327 | + */ |
|
| 328 | + function _addAdditionalHeaders($request) { |
|
| 329 | + foreach ($this->_additional_headers as $header) { |
|
| 330 | + $request->addHeaderLine($header); |
|
| 331 | + } |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | 334 | \ No newline at end of file |
@@ -1,1391 +1,1391 @@ |
||
| 1 | 1 | <?php |
| 2 | - /** |
|
| 3 | - * Base include file for SimpleTest. |
|
| 4 | - * @package SimpleTest |
|
| 5 | - * @subpackage WebTester |
|
| 6 | - * @version $Id: tag.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | - */ |
|
| 8 | - |
|
| 9 | - /**#@+ |
|
| 2 | + /** |
|
| 3 | + * Base include file for SimpleTest. |
|
| 4 | + * @package SimpleTest |
|
| 5 | + * @subpackage WebTester |
|
| 6 | + * @version $Id: tag.php 1398 2006-09-08 19:31:03Z xue $ |
|
| 7 | + */ |
|
| 8 | + |
|
| 9 | + /**#@+ |
|
| 10 | 10 | * include SimpleTest files |
| 11 | 11 | */ |
| 12 | - require_once(dirname(__FILE__) . '/parser.php'); |
|
| 13 | - require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 14 | - /**#@-*/ |
|
| 12 | + require_once(dirname(__FILE__) . '/parser.php'); |
|
| 13 | + require_once(dirname(__FILE__) . '/encoding.php'); |
|
| 14 | + /**#@-*/ |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * HTML or XML tag. |
|
| 16 | + /** |
|
| 17 | + * HTML or XML tag. |
|
| 18 | 18 | * @package SimpleTest |
| 19 | 19 | * @subpackage WebTester |
| 20 | - */ |
|
| 21 | - class SimpleTag { |
|
| 22 | - protected $_name; |
|
| 23 | - protected $_attributes; |
|
| 24 | - protected $_content; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Starts with a named tag with attributes only. |
|
| 28 | - * @param string $name Tag name. |
|
| 29 | - * @param hash $attributes Attribute names and |
|
| 30 | - * string values. Note that |
|
| 31 | - * the keys must have been |
|
| 32 | - * converted to lower case. |
|
| 33 | - */ |
|
| 34 | - function SimpleTag($name, $attributes) { |
|
| 35 | - $this->_name = strtolower(trim($name)); |
|
| 36 | - $this->_attributes = $attributes; |
|
| 37 | - $this->_content = ''; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Check to see if the tag can have both start and |
|
| 42 | - * end tags with content in between. |
|
| 43 | - * @return boolean True if content allowed. |
|
| 44 | - * @access public |
|
| 45 | - */ |
|
| 46 | - function expectEndTag() { |
|
| 47 | - return true; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * The current tag should not swallow all content for |
|
| 52 | - * itself as it's searchable page content. Private |
|
| 53 | - * content tags are usually widgets that contain default |
|
| 54 | - * values. |
|
| 55 | - * @return boolean False as content is available |
|
| 56 | - * to other tags by default. |
|
| 57 | - * @access public |
|
| 58 | - */ |
|
| 59 | - function isPrivateContent() { |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Appends string content to the current content. |
|
| 65 | - * @param string $content Additional text. |
|
| 66 | - * @access public |
|
| 67 | - */ |
|
| 68 | - function addContent($content) { |
|
| 69 | - $this->_content .= (string)$content; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Adds an enclosed tag to the content. |
|
| 74 | - * @param SimpleTag $tag New tag. |
|
| 75 | - * @access public |
|
| 76 | - */ |
|
| 77 | - function addTag($tag) { |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Accessor for tag name. |
|
| 82 | - * @return string Name of tag. |
|
| 83 | - * @access public |
|
| 84 | - */ |
|
| 85 | - function getTagName() { |
|
| 86 | - return $this->_name; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * List of legal child elements. |
|
| 91 | - * @return array List of element names. |
|
| 92 | - * @access public |
|
| 93 | - */ |
|
| 94 | - function getChildElements() { |
|
| 95 | - return array(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Accessor for an attribute. |
|
| 100 | - * @param string $label Attribute name. |
|
| 101 | - * @return string Attribute value. |
|
| 102 | - * @access public |
|
| 103 | - */ |
|
| 104 | - function getAttribute($label) { |
|
| 105 | - $label = strtolower($label); |
|
| 106 | - if (! isset($this->_attributes[$label])) { |
|
| 107 | - return false; |
|
| 108 | - } |
|
| 109 | - return (string)$this->_attributes[$label]; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Sets an attribute. |
|
| 114 | - * @param string $label Attribute name. |
|
| 115 | - * @return string $value New attribute value. |
|
| 116 | - * @access protected |
|
| 117 | - */ |
|
| 118 | - function _setAttribute($label, $value) { |
|
| 119 | - $this->_attributes[strtolower($label)] = $value; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Accessor for the whole content so far. |
|
| 124 | - * @return string Content as big raw string. |
|
| 125 | - * @access public |
|
| 126 | - */ |
|
| 127 | - function getContent() { |
|
| 128 | - return $this->_content; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Accessor for content reduced to visible text. Acts |
|
| 133 | - * like a text mode browser, normalising space and |
|
| 134 | - * reducing images to their alt text. |
|
| 135 | - * @return string Content as plain text. |
|
| 136 | - * @access public |
|
| 137 | - */ |
|
| 138 | - function getText() { |
|
| 139 | - return SimpleHtmlSaxParser::normalise($this->_content); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Test to see if id attribute matches. |
|
| 144 | - * @param string $id ID to test against. |
|
| 145 | - * @return boolean True on match. |
|
| 146 | - * @access public |
|
| 147 | - */ |
|
| 148 | - function isId($id) { |
|
| 149 | - return ($this->getAttribute('id') == $id); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Page title. |
|
| 20 | + */ |
|
| 21 | + class SimpleTag { |
|
| 22 | + protected $_name; |
|
| 23 | + protected $_attributes; |
|
| 24 | + protected $_content; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Starts with a named tag with attributes only. |
|
| 28 | + * @param string $name Tag name. |
|
| 29 | + * @param hash $attributes Attribute names and |
|
| 30 | + * string values. Note that |
|
| 31 | + * the keys must have been |
|
| 32 | + * converted to lower case. |
|
| 33 | + */ |
|
| 34 | + function SimpleTag($name, $attributes) { |
|
| 35 | + $this->_name = strtolower(trim($name)); |
|
| 36 | + $this->_attributes = $attributes; |
|
| 37 | + $this->_content = ''; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Check to see if the tag can have both start and |
|
| 42 | + * end tags with content in between. |
|
| 43 | + * @return boolean True if content allowed. |
|
| 44 | + * @access public |
|
| 45 | + */ |
|
| 46 | + function expectEndTag() { |
|
| 47 | + return true; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * The current tag should not swallow all content for |
|
| 52 | + * itself as it's searchable page content. Private |
|
| 53 | + * content tags are usually widgets that contain default |
|
| 54 | + * values. |
|
| 55 | + * @return boolean False as content is available |
|
| 56 | + * to other tags by default. |
|
| 57 | + * @access public |
|
| 58 | + */ |
|
| 59 | + function isPrivateContent() { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Appends string content to the current content. |
|
| 65 | + * @param string $content Additional text. |
|
| 66 | + * @access public |
|
| 67 | + */ |
|
| 68 | + function addContent($content) { |
|
| 69 | + $this->_content .= (string)$content; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Adds an enclosed tag to the content. |
|
| 74 | + * @param SimpleTag $tag New tag. |
|
| 75 | + * @access public |
|
| 76 | + */ |
|
| 77 | + function addTag($tag) { |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Accessor for tag name. |
|
| 82 | + * @return string Name of tag. |
|
| 83 | + * @access public |
|
| 84 | + */ |
|
| 85 | + function getTagName() { |
|
| 86 | + return $this->_name; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * List of legal child elements. |
|
| 91 | + * @return array List of element names. |
|
| 92 | + * @access public |
|
| 93 | + */ |
|
| 94 | + function getChildElements() { |
|
| 95 | + return array(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Accessor for an attribute. |
|
| 100 | + * @param string $label Attribute name. |
|
| 101 | + * @return string Attribute value. |
|
| 102 | + * @access public |
|
| 103 | + */ |
|
| 104 | + function getAttribute($label) { |
|
| 105 | + $label = strtolower($label); |
|
| 106 | + if (! isset($this->_attributes[$label])) { |
|
| 107 | + return false; |
|
| 108 | + } |
|
| 109 | + return (string)$this->_attributes[$label]; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Sets an attribute. |
|
| 114 | + * @param string $label Attribute name. |
|
| 115 | + * @return string $value New attribute value. |
|
| 116 | + * @access protected |
|
| 117 | + */ |
|
| 118 | + function _setAttribute($label, $value) { |
|
| 119 | + $this->_attributes[strtolower($label)] = $value; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Accessor for the whole content so far. |
|
| 124 | + * @return string Content as big raw string. |
|
| 125 | + * @access public |
|
| 126 | + */ |
|
| 127 | + function getContent() { |
|
| 128 | + return $this->_content; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Accessor for content reduced to visible text. Acts |
|
| 133 | + * like a text mode browser, normalising space and |
|
| 134 | + * reducing images to their alt text. |
|
| 135 | + * @return string Content as plain text. |
|
| 136 | + * @access public |
|
| 137 | + */ |
|
| 138 | + function getText() { |
|
| 139 | + return SimpleHtmlSaxParser::normalise($this->_content); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Test to see if id attribute matches. |
|
| 144 | + * @param string $id ID to test against. |
|
| 145 | + * @return boolean True on match. |
|
| 146 | + * @access public |
|
| 147 | + */ |
|
| 148 | + function isId($id) { |
|
| 149 | + return ($this->getAttribute('id') == $id); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Page title. |
|
| 155 | 155 | * @package SimpleTest |
| 156 | 156 | * @subpackage WebTester |
| 157 | - */ |
|
| 158 | - class SimpleTitleTag extends SimpleTag { |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Starts with a named tag with attributes only. |
|
| 162 | - * @param hash $attributes Attribute names and |
|
| 163 | - * string values. |
|
| 164 | - */ |
|
| 165 | - function SimpleTitleTag($attributes) { |
|
| 166 | - $this->SimpleTag('title', $attributes); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Link. |
|
| 157 | + */ |
|
| 158 | + class SimpleTitleTag extends SimpleTag { |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Starts with a named tag with attributes only. |
|
| 162 | + * @param hash $attributes Attribute names and |
|
| 163 | + * string values. |
|
| 164 | + */ |
|
| 165 | + function SimpleTitleTag($attributes) { |
|
| 166 | + $this->SimpleTag('title', $attributes); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Link. |
|
| 172 | 172 | * @package SimpleTest |
| 173 | 173 | * @subpackage WebTester |
| 174 | - */ |
|
| 175 | - class SimpleAnchorTag extends SimpleTag { |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Starts with a named tag with attributes only. |
|
| 179 | - * @param hash $attributes Attribute names and |
|
| 180 | - * string values. |
|
| 181 | - */ |
|
| 182 | - function SimpleAnchorTag($attributes) { |
|
| 183 | - $this->SimpleTag('a', $attributes); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Accessor for URL as string. |
|
| 188 | - * @return string Coerced as string. |
|
| 189 | - * @access public |
|
| 190 | - */ |
|
| 191 | - function getHref() { |
|
| 192 | - $url = $this->getAttribute('href'); |
|
| 193 | - if (is_bool($url)) { |
|
| 194 | - $url = ''; |
|
| 195 | - } |
|
| 196 | - return $url; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Form element. |
|
| 174 | + */ |
|
| 175 | + class SimpleAnchorTag extends SimpleTag { |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Starts with a named tag with attributes only. |
|
| 179 | + * @param hash $attributes Attribute names and |
|
| 180 | + * string values. |
|
| 181 | + */ |
|
| 182 | + function SimpleAnchorTag($attributes) { |
|
| 183 | + $this->SimpleTag('a', $attributes); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Accessor for URL as string. |
|
| 188 | + * @return string Coerced as string. |
|
| 189 | + * @access public |
|
| 190 | + */ |
|
| 191 | + function getHref() { |
|
| 192 | + $url = $this->getAttribute('href'); |
|
| 193 | + if (is_bool($url)) { |
|
| 194 | + $url = ''; |
|
| 195 | + } |
|
| 196 | + return $url; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Form element. |
|
| 202 | 202 | * @package SimpleTest |
| 203 | 203 | * @subpackage WebTester |
| 204 | - */ |
|
| 205 | - class SimpleWidget extends SimpleTag { |
|
| 206 | - protected $_value; |
|
| 207 | - protected $_label; |
|
| 208 | - protected $_is_set; |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Starts with a named tag with attributes only. |
|
| 212 | - * @param string $name Tag name. |
|
| 213 | - * @param hash $attributes Attribute names and |
|
| 214 | - * string values. |
|
| 215 | - */ |
|
| 216 | - function SimpleWidget($name, $attributes) { |
|
| 217 | - $this->SimpleTag($name, $attributes); |
|
| 218 | - $this->_value = false; |
|
| 219 | - $this->_label = false; |
|
| 220 | - $this->_is_set = false; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Accessor for name submitted as the key in |
|
| 225 | - * GET/POST variables hash. |
|
| 226 | - * @return string Parsed value. |
|
| 227 | - * @access public |
|
| 228 | - */ |
|
| 229 | - function getName() { |
|
| 230 | - return $this->getAttribute('name'); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Accessor for default value parsed with the tag. |
|
| 235 | - * @return string Parsed value. |
|
| 236 | - * @access public |
|
| 237 | - */ |
|
| 238 | - function getDefault() { |
|
| 239 | - return $this->getAttribute('value'); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Accessor for currently set value or default if |
|
| 244 | - * none. |
|
| 245 | - * @return string Value set by form or default |
|
| 246 | - * if none. |
|
| 247 | - * @access public |
|
| 248 | - */ |
|
| 249 | - function getValue() { |
|
| 250 | - if (! $this->_is_set) { |
|
| 251 | - return $this->getDefault(); |
|
| 252 | - } |
|
| 253 | - return $this->_value; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Sets the current form element value. |
|
| 258 | - * @param string $value New value. |
|
| 259 | - * @return boolean True if allowed. |
|
| 260 | - * @access public |
|
| 261 | - */ |
|
| 262 | - function setValue($value) { |
|
| 263 | - $this->_value = $value; |
|
| 264 | - $this->_is_set = true; |
|
| 265 | - return true; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Resets the form element value back to the |
|
| 270 | - * default. |
|
| 271 | - * @access public |
|
| 272 | - */ |
|
| 273 | - function resetValue() { |
|
| 274 | - $this->_is_set = false; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Allows setting of a label externally, say by a |
|
| 279 | - * label tag. |
|
| 280 | - * @param string $label Label to attach. |
|
| 281 | - * @access public |
|
| 282 | - */ |
|
| 283 | - function setLabel($label) { |
|
| 284 | - $this->_label = trim($label); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Reads external or internal label. |
|
| 289 | - * @param string $label Label to test. |
|
| 290 | - * @return boolean True is match. |
|
| 291 | - * @access public |
|
| 292 | - */ |
|
| 293 | - function isLabel($label) { |
|
| 294 | - return $this->_label == trim($label); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Dispatches the value into the form encoded packet. |
|
| 299 | - * @param SimpleEncoding $encoding Form packet. |
|
| 300 | - * @access public |
|
| 301 | - */ |
|
| 302 | - function write($encoding) { |
|
| 303 | - if ($this->getName()) { |
|
| 304 | - $encoding->add($this->getName(), $this->getValue()); |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Text, password and hidden field. |
|
| 204 | + */ |
|
| 205 | + class SimpleWidget extends SimpleTag { |
|
| 206 | + protected $_value; |
|
| 207 | + protected $_label; |
|
| 208 | + protected $_is_set; |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Starts with a named tag with attributes only. |
|
| 212 | + * @param string $name Tag name. |
|
| 213 | + * @param hash $attributes Attribute names and |
|
| 214 | + * string values. |
|
| 215 | + */ |
|
| 216 | + function SimpleWidget($name, $attributes) { |
|
| 217 | + $this->SimpleTag($name, $attributes); |
|
| 218 | + $this->_value = false; |
|
| 219 | + $this->_label = false; |
|
| 220 | + $this->_is_set = false; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Accessor for name submitted as the key in |
|
| 225 | + * GET/POST variables hash. |
|
| 226 | + * @return string Parsed value. |
|
| 227 | + * @access public |
|
| 228 | + */ |
|
| 229 | + function getName() { |
|
| 230 | + return $this->getAttribute('name'); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Accessor for default value parsed with the tag. |
|
| 235 | + * @return string Parsed value. |
|
| 236 | + * @access public |
|
| 237 | + */ |
|
| 238 | + function getDefault() { |
|
| 239 | + return $this->getAttribute('value'); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Accessor for currently set value or default if |
|
| 244 | + * none. |
|
| 245 | + * @return string Value set by form or default |
|
| 246 | + * if none. |
|
| 247 | + * @access public |
|
| 248 | + */ |
|
| 249 | + function getValue() { |
|
| 250 | + if (! $this->_is_set) { |
|
| 251 | + return $this->getDefault(); |
|
| 252 | + } |
|
| 253 | + return $this->_value; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Sets the current form element value. |
|
| 258 | + * @param string $value New value. |
|
| 259 | + * @return boolean True if allowed. |
|
| 260 | + * @access public |
|
| 261 | + */ |
|
| 262 | + function setValue($value) { |
|
| 263 | + $this->_value = $value; |
|
| 264 | + $this->_is_set = true; |
|
| 265 | + return true; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Resets the form element value back to the |
|
| 270 | + * default. |
|
| 271 | + * @access public |
|
| 272 | + */ |
|
| 273 | + function resetValue() { |
|
| 274 | + $this->_is_set = false; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Allows setting of a label externally, say by a |
|
| 279 | + * label tag. |
|
| 280 | + * @param string $label Label to attach. |
|
| 281 | + * @access public |
|
| 282 | + */ |
|
| 283 | + function setLabel($label) { |
|
| 284 | + $this->_label = trim($label); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Reads external or internal label. |
|
| 289 | + * @param string $label Label to test. |
|
| 290 | + * @return boolean True is match. |
|
| 291 | + * @access public |
|
| 292 | + */ |
|
| 293 | + function isLabel($label) { |
|
| 294 | + return $this->_label == trim($label); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Dispatches the value into the form encoded packet. |
|
| 299 | + * @param SimpleEncoding $encoding Form packet. |
|
| 300 | + * @access public |
|
| 301 | + */ |
|
| 302 | + function write($encoding) { |
|
| 303 | + if ($this->getName()) { |
|
| 304 | + $encoding->add($this->getName(), $this->getValue()); |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Text, password and hidden field. |
|
| 311 | 311 | * @package SimpleTest |
| 312 | 312 | * @subpackage WebTester |
| 313 | - */ |
|
| 314 | - class SimpleTextTag extends SimpleWidget { |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Starts with a named tag with attributes only. |
|
| 318 | - * @param hash $attributes Attribute names and |
|
| 319 | - * string values. |
|
| 320 | - */ |
|
| 321 | - function SimpleTextTag($attributes) { |
|
| 322 | - $this->SimpleWidget('input', $attributes); |
|
| 323 | - if ($this->getAttribute('value') === false) { |
|
| 324 | - $this->_setAttribute('value', ''); |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Tag contains no content. |
|
| 330 | - * @return boolean False. |
|
| 331 | - * @access public |
|
| 332 | - */ |
|
| 333 | - function expectEndTag() { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Sets the current form element value. Cannot |
|
| 339 | - * change the value of a hidden field. |
|
| 340 | - * @param string $value New value. |
|
| 341 | - * @return boolean True if allowed. |
|
| 342 | - * @access public |
|
| 343 | - */ |
|
| 344 | - function setValue($value) { |
|
| 345 | - if ($this->getAttribute('type') == 'hidden') { |
|
| 346 | - return false; |
|
| 347 | - } |
|
| 348 | - return parent::setValue($value); |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Submit button as input tag. |
|
| 313 | + */ |
|
| 314 | + class SimpleTextTag extends SimpleWidget { |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Starts with a named tag with attributes only. |
|
| 318 | + * @param hash $attributes Attribute names and |
|
| 319 | + * string values. |
|
| 320 | + */ |
|
| 321 | + function SimpleTextTag($attributes) { |
|
| 322 | + $this->SimpleWidget('input', $attributes); |
|
| 323 | + if ($this->getAttribute('value') === false) { |
|
| 324 | + $this->_setAttribute('value', ''); |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Tag contains no content. |
|
| 330 | + * @return boolean False. |
|
| 331 | + * @access public |
|
| 332 | + */ |
|
| 333 | + function expectEndTag() { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Sets the current form element value. Cannot |
|
| 339 | + * change the value of a hidden field. |
|
| 340 | + * @param string $value New value. |
|
| 341 | + * @return boolean True if allowed. |
|
| 342 | + * @access public |
|
| 343 | + */ |
|
| 344 | + function setValue($value) { |
|
| 345 | + if ($this->getAttribute('type') == 'hidden') { |
|
| 346 | + return false; |
|
| 347 | + } |
|
| 348 | + return parent::setValue($value); |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Submit button as input tag. |
|
| 354 | 354 | * @package SimpleTest |
| 355 | 355 | * @subpackage WebTester |
| 356 | - */ |
|
| 357 | - class SimpleSubmitTag extends SimpleWidget { |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Starts with a named tag with attributes only. |
|
| 361 | - * @param hash $attributes Attribute names and |
|
| 362 | - * string values. |
|
| 363 | - */ |
|
| 364 | - function SimpleSubmitTag($attributes) { |
|
| 365 | - $this->SimpleWidget('input', $attributes); |
|
| 366 | - if ($this->getAttribute('value') === false) { |
|
| 367 | - $this->_setAttribute('value', 'Submit'); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Tag contains no end element. |
|
| 373 | - * @return boolean False. |
|
| 374 | - * @access public |
|
| 375 | - */ |
|
| 376 | - function expectEndTag() { |
|
| 377 | - return false; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Disables the setting of the button value. |
|
| 382 | - * @param string $value Ignored. |
|
| 383 | - * @return boolean True if allowed. |
|
| 384 | - * @access public |
|
| 385 | - */ |
|
| 386 | - function setValue($value) { |
|
| 387 | - return false; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Value of browser visible text. |
|
| 392 | - * @return string Visible label. |
|
| 393 | - * @access public |
|
| 394 | - */ |
|
| 395 | - function getLabel() { |
|
| 396 | - return $this->getValue(); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Test for a label match when searching. |
|
| 401 | - * @param string $label Label to test. |
|
| 402 | - * @return boolean True on match. |
|
| 403 | - * @access public |
|
| 404 | - */ |
|
| 405 | - function isLabel($label) { |
|
| 406 | - return trim($label) == trim($this->getLabel()); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Image button as input tag. |
|
| 356 | + */ |
|
| 357 | + class SimpleSubmitTag extends SimpleWidget { |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Starts with a named tag with attributes only. |
|
| 361 | + * @param hash $attributes Attribute names and |
|
| 362 | + * string values. |
|
| 363 | + */ |
|
| 364 | + function SimpleSubmitTag($attributes) { |
|
| 365 | + $this->SimpleWidget('input', $attributes); |
|
| 366 | + if ($this->getAttribute('value') === false) { |
|
| 367 | + $this->_setAttribute('value', 'Submit'); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Tag contains no end element. |
|
| 373 | + * @return boolean False. |
|
| 374 | + * @access public |
|
| 375 | + */ |
|
| 376 | + function expectEndTag() { |
|
| 377 | + return false; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Disables the setting of the button value. |
|
| 382 | + * @param string $value Ignored. |
|
| 383 | + * @return boolean True if allowed. |
|
| 384 | + * @access public |
|
| 385 | + */ |
|
| 386 | + function setValue($value) { |
|
| 387 | + return false; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Value of browser visible text. |
|
| 392 | + * @return string Visible label. |
|
| 393 | + * @access public |
|
| 394 | + */ |
|
| 395 | + function getLabel() { |
|
| 396 | + return $this->getValue(); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Test for a label match when searching. |
|
| 401 | + * @param string $label Label to test. |
|
| 402 | + * @return boolean True on match. |
|
| 403 | + * @access public |
|
| 404 | + */ |
|
| 405 | + function isLabel($label) { |
|
| 406 | + return trim($label) == trim($this->getLabel()); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Image button as input tag. |
|
| 412 | 412 | * @package SimpleTest |
| 413 | 413 | * @subpackage WebTester |
| 414 | - */ |
|
| 415 | - class SimpleImageSubmitTag extends SimpleWidget { |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Starts with a named tag with attributes only. |
|
| 419 | - * @param hash $attributes Attribute names and |
|
| 420 | - * string values. |
|
| 421 | - */ |
|
| 422 | - function SimpleImageSubmitTag($attributes) { |
|
| 423 | - $this->SimpleWidget('input', $attributes); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Tag contains no end element. |
|
| 428 | - * @return boolean False. |
|
| 429 | - * @access public |
|
| 430 | - */ |
|
| 431 | - function expectEndTag() { |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * Disables the setting of the button value. |
|
| 437 | - * @param string $value Ignored. |
|
| 438 | - * @return boolean True if allowed. |
|
| 439 | - * @access public |
|
| 440 | - */ |
|
| 441 | - function setValue($value) { |
|
| 442 | - return false; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Value of browser visible text. |
|
| 447 | - * @return string Visible label. |
|
| 448 | - * @access public |
|
| 449 | - */ |
|
| 450 | - function getLabel() { |
|
| 451 | - if ($this->getAttribute('title')) { |
|
| 452 | - return $this->getAttribute('title'); |
|
| 453 | - } |
|
| 454 | - return $this->getAttribute('alt'); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Test for a label match when searching. |
|
| 459 | - * @param string $label Label to test. |
|
| 460 | - * @return boolean True on match. |
|
| 461 | - * @access public |
|
| 462 | - */ |
|
| 463 | - function isLabel($label) { |
|
| 464 | - return trim($label) == trim($this->getLabel()); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Dispatches the value into the form encoded packet. |
|
| 469 | - * @param SimpleEncoding $encoding Form packet. |
|
| 470 | - * @param integer $x X coordinate of click. |
|
| 471 | - * @param integer $y Y coordinate of click. |
|
| 472 | - * @access public |
|
| 473 | - */ |
|
| 474 | - function write($encoding){//, $x, $y) { |
|
| 475 | - if ($this->getName()) { |
|
| 476 | - $encoding->add($this->getName() . '.x', $x); |
|
| 477 | - $encoding->add($this->getName() . '.y', $y); |
|
| 478 | - } else { |
|
| 479 | - $encoding->add('x', $x); |
|
| 480 | - $encoding->add('y', $y); |
|
| 481 | - } |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * Submit button as button tag. |
|
| 414 | + */ |
|
| 415 | + class SimpleImageSubmitTag extends SimpleWidget { |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Starts with a named tag with attributes only. |
|
| 419 | + * @param hash $attributes Attribute names and |
|
| 420 | + * string values. |
|
| 421 | + */ |
|
| 422 | + function SimpleImageSubmitTag($attributes) { |
|
| 423 | + $this->SimpleWidget('input', $attributes); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Tag contains no end element. |
|
| 428 | + * @return boolean False. |
|
| 429 | + * @access public |
|
| 430 | + */ |
|
| 431 | + function expectEndTag() { |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * Disables the setting of the button value. |
|
| 437 | + * @param string $value Ignored. |
|
| 438 | + * @return boolean True if allowed. |
|
| 439 | + * @access public |
|
| 440 | + */ |
|
| 441 | + function setValue($value) { |
|
| 442 | + return false; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Value of browser visible text. |
|
| 447 | + * @return string Visible label. |
|
| 448 | + * @access public |
|
| 449 | + */ |
|
| 450 | + function getLabel() { |
|
| 451 | + if ($this->getAttribute('title')) { |
|
| 452 | + return $this->getAttribute('title'); |
|
| 453 | + } |
|
| 454 | + return $this->getAttribute('alt'); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Test for a label match when searching. |
|
| 459 | + * @param string $label Label to test. |
|
| 460 | + * @return boolean True on match. |
|
| 461 | + * @access public |
|
| 462 | + */ |
|
| 463 | + function isLabel($label) { |
|
| 464 | + return trim($label) == trim($this->getLabel()); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Dispatches the value into the form encoded packet. |
|
| 469 | + * @param SimpleEncoding $encoding Form packet. |
|
| 470 | + * @param integer $x X coordinate of click. |
|
| 471 | + * @param integer $y Y coordinate of click. |
|
| 472 | + * @access public |
|
| 473 | + */ |
|
| 474 | + function write($encoding){//, $x, $y) { |
|
| 475 | + if ($this->getName()) { |
|
| 476 | + $encoding->add($this->getName() . '.x', $x); |
|
| 477 | + $encoding->add($this->getName() . '.y', $y); |
|
| 478 | + } else { |
|
| 479 | + $encoding->add('x', $x); |
|
| 480 | + $encoding->add('y', $y); |
|
| 481 | + } |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * Submit button as button tag. |
|
| 487 | 487 | * @package SimpleTest |
| 488 | 488 | * @subpackage WebTester |
| 489 | - */ |
|
| 490 | - class SimpleButtonTag extends SimpleWidget { |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Starts with a named tag with attributes only. |
|
| 494 | - * Defaults are very browser dependent. |
|
| 495 | - * @param hash $attributes Attribute names and |
|
| 496 | - * string values. |
|
| 497 | - */ |
|
| 498 | - function SimpleButtonTag($attributes) { |
|
| 499 | - $this->SimpleWidget('button', $attributes); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Check to see if the tag can have both start and |
|
| 504 | - * end tags with content in between. |
|
| 505 | - * @return boolean True if content allowed. |
|
| 506 | - * @access public |
|
| 507 | - */ |
|
| 508 | - function expectEndTag() { |
|
| 509 | - return true; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * Disables the setting of the button value. |
|
| 514 | - * @param string $value Ignored. |
|
| 515 | - * @return boolean True if allowed. |
|
| 516 | - * @access public |
|
| 517 | - */ |
|
| 518 | - function setValue($value) { |
|
| 519 | - return false; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Value of browser visible text. |
|
| 524 | - * @return string Visible label. |
|
| 525 | - * @access public |
|
| 526 | - */ |
|
| 527 | - function getLabel() { |
|
| 528 | - return $this->getContent(); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * Test for a label match when searching. |
|
| 533 | - * @param string $label Label to test. |
|
| 534 | - * @return boolean True on match. |
|
| 535 | - * @access public |
|
| 536 | - */ |
|
| 537 | - function isLabel($label) { |
|
| 538 | - return trim($label) == trim($this->getLabel()); |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Content tag for text area. |
|
| 489 | + */ |
|
| 490 | + class SimpleButtonTag extends SimpleWidget { |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Starts with a named tag with attributes only. |
|
| 494 | + * Defaults are very browser dependent. |
|
| 495 | + * @param hash $attributes Attribute names and |
|
| 496 | + * string values. |
|
| 497 | + */ |
|
| 498 | + function SimpleButtonTag($attributes) { |
|
| 499 | + $this->SimpleWidget('button', $attributes); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Check to see if the tag can have both start and |
|
| 504 | + * end tags with content in between. |
|
| 505 | + * @return boolean True if content allowed. |
|
| 506 | + * @access public |
|
| 507 | + */ |
|
| 508 | + function expectEndTag() { |
|
| 509 | + return true; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Disables the setting of the button value. |
|
| 514 | + * @param string $value Ignored. |
|
| 515 | + * @return boolean True if allowed. |
|
| 516 | + * @access public |
|
| 517 | + */ |
|
| 518 | + function setValue($value) { |
|
| 519 | + return false; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Value of browser visible text. |
|
| 524 | + * @return string Visible label. |
|
| 525 | + * @access public |
|
| 526 | + */ |
|
| 527 | + function getLabel() { |
|
| 528 | + return $this->getContent(); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * Test for a label match when searching. |
|
| 533 | + * @param string $label Label to test. |
|
| 534 | + * @return boolean True on match. |
|
| 535 | + * @access public |
|
| 536 | + */ |
|
| 537 | + function isLabel($label) { |
|
| 538 | + return trim($label) == trim($this->getLabel()); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Content tag for text area. |
|
| 544 | 544 | * @package SimpleTest |
| 545 | 545 | * @subpackage WebTester |
| 546 | - */ |
|
| 547 | - class SimpleTextAreaTag extends SimpleWidget { |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Starts with a named tag with attributes only. |
|
| 551 | - * @param hash $attributes Attribute names and |
|
| 552 | - * string values. |
|
| 553 | - */ |
|
| 554 | - function SimpleTextAreaTag($attributes) { |
|
| 555 | - $this->SimpleWidget('textarea', $attributes); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * Accessor for starting value. |
|
| 560 | - * @return string Parsed value. |
|
| 561 | - * @access public |
|
| 562 | - */ |
|
| 563 | - function getDefault() { |
|
| 564 | - return $this->_wrap(SimpleHtmlSaxParser::decodeHtml($this->getContent())); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /** |
|
| 568 | - * Applies word wrapping if needed. |
|
| 569 | - * @param string $value New value. |
|
| 570 | - * @return boolean True if allowed. |
|
| 571 | - * @access public |
|
| 572 | - */ |
|
| 573 | - function setValue($value) { |
|
| 574 | - return parent::setValue($this->_wrap($value)); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Test to see if text should be wrapped. |
|
| 579 | - * @return boolean True if wrapping on. |
|
| 580 | - * @access private |
|
| 581 | - */ |
|
| 582 | - function _wrapIsEnabled() { |
|
| 583 | - if ($this->getAttribute('cols')) { |
|
| 584 | - $wrap = $this->getAttribute('wrap'); |
|
| 585 | - if (($wrap == 'physical') || ($wrap == 'hard')) { |
|
| 586 | - return true; |
|
| 587 | - } |
|
| 588 | - } |
|
| 589 | - return false; |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * Performs the formatting that is peculiar to |
|
| 594 | - * this tag. There is strange behaviour in this |
|
| 595 | - * one, including stripping a leading new line. |
|
| 596 | - * Go figure. I am using Firefox as a guide. |
|
| 597 | - * @param string $text Text to wrap. |
|
| 598 | - * @return string Text wrapped with carriage |
|
| 599 | - * returns and line feeds |
|
| 600 | - * @access private |
|
| 601 | - */ |
|
| 602 | - function _wrap($text) { |
|
| 603 | - $text = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $text)); |
|
| 604 | - $text = str_replace("\r\n\n", "\r\n", str_replace("\r", "\r\n", $text)); |
|
| 605 | - if (strncmp($text, "\r\n", strlen("\r\n")) == 0) { |
|
| 606 | - $text = substr($text, strlen("\r\n")); |
|
| 607 | - } |
|
| 608 | - if ($this->_wrapIsEnabled()) { |
|
| 609 | - return wordwrap( |
|
| 610 | - $text, |
|
| 611 | - (integer)$this->getAttribute('cols'), |
|
| 612 | - "\r\n"); |
|
| 613 | - } |
|
| 614 | - return $text; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * The content of textarea is not part of the page. |
|
| 619 | - * @return boolean True. |
|
| 620 | - * @access public |
|
| 621 | - */ |
|
| 622 | - function isPrivateContent() { |
|
| 623 | - return true; |
|
| 624 | - } |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * File upload widget. |
|
| 546 | + */ |
|
| 547 | + class SimpleTextAreaTag extends SimpleWidget { |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Starts with a named tag with attributes only. |
|
| 551 | + * @param hash $attributes Attribute names and |
|
| 552 | + * string values. |
|
| 553 | + */ |
|
| 554 | + function SimpleTextAreaTag($attributes) { |
|
| 555 | + $this->SimpleWidget('textarea', $attributes); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * Accessor for starting value. |
|
| 560 | + * @return string Parsed value. |
|
| 561 | + * @access public |
|
| 562 | + */ |
|
| 563 | + function getDefault() { |
|
| 564 | + return $this->_wrap(SimpleHtmlSaxParser::decodeHtml($this->getContent())); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /** |
|
| 568 | + * Applies word wrapping if needed. |
|
| 569 | + * @param string $value New value. |
|
| 570 | + * @return boolean True if allowed. |
|
| 571 | + * @access public |
|
| 572 | + */ |
|
| 573 | + function setValue($value) { |
|
| 574 | + return parent::setValue($this->_wrap($value)); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Test to see if text should be wrapped. |
|
| 579 | + * @return boolean True if wrapping on. |
|
| 580 | + * @access private |
|
| 581 | + */ |
|
| 582 | + function _wrapIsEnabled() { |
|
| 583 | + if ($this->getAttribute('cols')) { |
|
| 584 | + $wrap = $this->getAttribute('wrap'); |
|
| 585 | + if (($wrap == 'physical') || ($wrap == 'hard')) { |
|
| 586 | + return true; |
|
| 587 | + } |
|
| 588 | + } |
|
| 589 | + return false; |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * Performs the formatting that is peculiar to |
|
| 594 | + * this tag. There is strange behaviour in this |
|
| 595 | + * one, including stripping a leading new line. |
|
| 596 | + * Go figure. I am using Firefox as a guide. |
|
| 597 | + * @param string $text Text to wrap. |
|
| 598 | + * @return string Text wrapped with carriage |
|
| 599 | + * returns and line feeds |
|
| 600 | + * @access private |
|
| 601 | + */ |
|
| 602 | + function _wrap($text) { |
|
| 603 | + $text = str_replace("\r\r\n", "\r\n", str_replace("\n", "\r\n", $text)); |
|
| 604 | + $text = str_replace("\r\n\n", "\r\n", str_replace("\r", "\r\n", $text)); |
|
| 605 | + if (strncmp($text, "\r\n", strlen("\r\n")) == 0) { |
|
| 606 | + $text = substr($text, strlen("\r\n")); |
|
| 607 | + } |
|
| 608 | + if ($this->_wrapIsEnabled()) { |
|
| 609 | + return wordwrap( |
|
| 610 | + $text, |
|
| 611 | + (integer)$this->getAttribute('cols'), |
|
| 612 | + "\r\n"); |
|
| 613 | + } |
|
| 614 | + return $text; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * The content of textarea is not part of the page. |
|
| 619 | + * @return boolean True. |
|
| 620 | + * @access public |
|
| 621 | + */ |
|
| 622 | + function isPrivateContent() { |
|
| 623 | + return true; |
|
| 624 | + } |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * File upload widget. |
|
| 629 | 629 | * @package SimpleTest |
| 630 | 630 | * @subpackage WebTester |
| 631 | - */ |
|
| 632 | - class SimpleUploadTag extends SimpleWidget { |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * Starts with attributes only. |
|
| 636 | - * @param hash $attributes Attribute names and |
|
| 637 | - * string values. |
|
| 638 | - */ |
|
| 639 | - function SimpleUploadTag($attributes) { |
|
| 640 | - $this->SimpleWidget('input', $attributes); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * Tag contains no content. |
|
| 645 | - * @return boolean False. |
|
| 646 | - * @access public |
|
| 647 | - */ |
|
| 648 | - function expectEndTag() { |
|
| 649 | - return false; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Dispatches the value into the form encoded packet. |
|
| 654 | - * @param SimpleEncoding $encoding Form packet. |
|
| 655 | - * @access public |
|
| 656 | - */ |
|
| 657 | - function write($encoding) { |
|
| 658 | - if (! file_exists($this->getValue())) { |
|
| 659 | - return; |
|
| 660 | - } |
|
| 661 | - $encoding->attach( |
|
| 662 | - $this->getName(), |
|
| 663 | - implode('', file($this->getValue())), |
|
| 664 | - basename($this->getValue())); |
|
| 665 | - } |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * Drop down widget. |
|
| 631 | + */ |
|
| 632 | + class SimpleUploadTag extends SimpleWidget { |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * Starts with attributes only. |
|
| 636 | + * @param hash $attributes Attribute names and |
|
| 637 | + * string values. |
|
| 638 | + */ |
|
| 639 | + function SimpleUploadTag($attributes) { |
|
| 640 | + $this->SimpleWidget('input', $attributes); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * Tag contains no content. |
|
| 645 | + * @return boolean False. |
|
| 646 | + * @access public |
|
| 647 | + */ |
|
| 648 | + function expectEndTag() { |
|
| 649 | + return false; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Dispatches the value into the form encoded packet. |
|
| 654 | + * @param SimpleEncoding $encoding Form packet. |
|
| 655 | + * @access public |
|
| 656 | + */ |
|
| 657 | + function write($encoding) { |
|
| 658 | + if (! file_exists($this->getValue())) { |
|
| 659 | + return; |
|
| 660 | + } |
|
| 661 | + $encoding->attach( |
|
| 662 | + $this->getName(), |
|
| 663 | + implode('', file($this->getValue())), |
|
| 664 | + basename($this->getValue())); |
|
| 665 | + } |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * Drop down widget. |
|
| 670 | 670 | * @package SimpleTest |
| 671 | 671 | * @subpackage WebTester |
| 672 | - */ |
|
| 673 | - class SimpleSelectionTag extends SimpleWidget { |
|
| 674 | - protected $_options; |
|
| 675 | - protected $_choice; |
|
| 676 | - |
|
| 677 | - /** |
|
| 678 | - * Starts with attributes only. |
|
| 679 | - * @param hash $attributes Attribute names and |
|
| 680 | - * string values. |
|
| 681 | - */ |
|
| 682 | - function SimpleSelectionTag($attributes) { |
|
| 683 | - $this->SimpleWidget('select', $attributes); |
|
| 684 | - $this->_options = array(); |
|
| 685 | - $this->_choice = false; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * Adds an option tag to a selection field. |
|
| 690 | - * @param SimpleOptionTag $tag New option. |
|
| 691 | - * @access public |
|
| 692 | - */ |
|
| 693 | - function addTag($tag) { |
|
| 694 | - if ($tag->getTagName() == 'option') { |
|
| 695 | - $this->_options[] = $tag; |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - |
|
| 699 | - /** |
|
| 700 | - * Text within the selection element is ignored. |
|
| 701 | - * @param string $content Ignored. |
|
| 702 | - * @access public |
|
| 703 | - */ |
|
| 704 | - function addContent($content) { |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * Scans options for defaults. If none, then |
|
| 709 | - * the first option is selected. |
|
| 710 | - * @return string Selected field. |
|
| 711 | - * @access public |
|
| 712 | - */ |
|
| 713 | - function getDefault() { |
|
| 714 | - for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 715 | - if ($this->_options[$i]->getAttribute('selected') !== false) { |
|
| 716 | - return $this->_options[$i]->getDefault(); |
|
| 717 | - } |
|
| 718 | - } |
|
| 719 | - if ($count > 0) { |
|
| 720 | - return $this->_options[0]->getDefault(); |
|
| 721 | - } |
|
| 722 | - return ''; |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - /** |
|
| 726 | - * Can only set allowed values. |
|
| 727 | - * @param string $value New choice. |
|
| 728 | - * @return boolean True if allowed. |
|
| 729 | - * @access public |
|
| 730 | - */ |
|
| 731 | - function setValue($value) { |
|
| 732 | - for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 733 | - if ($this->_options[$i]->isValue($value)) { |
|
| 734 | - $this->_choice = $i; |
|
| 735 | - return true; |
|
| 736 | - } |
|
| 737 | - } |
|
| 738 | - return false; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Accessor for current selection value. |
|
| 743 | - * @return string Value attribute or |
|
| 744 | - * content of opton. |
|
| 745 | - * @access public |
|
| 746 | - */ |
|
| 747 | - function getValue() { |
|
| 748 | - if ($this->_choice === false) { |
|
| 749 | - return $this->getDefault(); |
|
| 750 | - } |
|
| 751 | - return $this->_options[$this->_choice]->getValue(); |
|
| 752 | - } |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - /** |
|
| 756 | - * Drop down widget. |
|
| 672 | + */ |
|
| 673 | + class SimpleSelectionTag extends SimpleWidget { |
|
| 674 | + protected $_options; |
|
| 675 | + protected $_choice; |
|
| 676 | + |
|
| 677 | + /** |
|
| 678 | + * Starts with attributes only. |
|
| 679 | + * @param hash $attributes Attribute names and |
|
| 680 | + * string values. |
|
| 681 | + */ |
|
| 682 | + function SimpleSelectionTag($attributes) { |
|
| 683 | + $this->SimpleWidget('select', $attributes); |
|
| 684 | + $this->_options = array(); |
|
| 685 | + $this->_choice = false; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * Adds an option tag to a selection field. |
|
| 690 | + * @param SimpleOptionTag $tag New option. |
|
| 691 | + * @access public |
|
| 692 | + */ |
|
| 693 | + function addTag($tag) { |
|
| 694 | + if ($tag->getTagName() == 'option') { |
|
| 695 | + $this->_options[] = $tag; |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + |
|
| 699 | + /** |
|
| 700 | + * Text within the selection element is ignored. |
|
| 701 | + * @param string $content Ignored. |
|
| 702 | + * @access public |
|
| 703 | + */ |
|
| 704 | + function addContent($content) { |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * Scans options for defaults. If none, then |
|
| 709 | + * the first option is selected. |
|
| 710 | + * @return string Selected field. |
|
| 711 | + * @access public |
|
| 712 | + */ |
|
| 713 | + function getDefault() { |
|
| 714 | + for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 715 | + if ($this->_options[$i]->getAttribute('selected') !== false) { |
|
| 716 | + return $this->_options[$i]->getDefault(); |
|
| 717 | + } |
|
| 718 | + } |
|
| 719 | + if ($count > 0) { |
|
| 720 | + return $this->_options[0]->getDefault(); |
|
| 721 | + } |
|
| 722 | + return ''; |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + /** |
|
| 726 | + * Can only set allowed values. |
|
| 727 | + * @param string $value New choice. |
|
| 728 | + * @return boolean True if allowed. |
|
| 729 | + * @access public |
|
| 730 | + */ |
|
| 731 | + function setValue($value) { |
|
| 732 | + for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 733 | + if ($this->_options[$i]->isValue($value)) { |
|
| 734 | + $this->_choice = $i; |
|
| 735 | + return true; |
|
| 736 | + } |
|
| 737 | + } |
|
| 738 | + return false; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Accessor for current selection value. |
|
| 743 | + * @return string Value attribute or |
|
| 744 | + * content of opton. |
|
| 745 | + * @access public |
|
| 746 | + */ |
|
| 747 | + function getValue() { |
|
| 748 | + if ($this->_choice === false) { |
|
| 749 | + return $this->getDefault(); |
|
| 750 | + } |
|
| 751 | + return $this->_options[$this->_choice]->getValue(); |
|
| 752 | + } |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * Drop down widget. |
|
| 757 | 757 | * @package SimpleTest |
| 758 | 758 | * @subpackage WebTester |
| 759 | - */ |
|
| 760 | - class MultipleSelectionTag extends SimpleWidget { |
|
| 761 | - protected $_options; |
|
| 762 | - protected $_values; |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * Starts with attributes only. |
|
| 766 | - * @param hash $attributes Attribute names and |
|
| 767 | - * string values. |
|
| 768 | - */ |
|
| 769 | - function MultipleSelectionTag($attributes) { |
|
| 770 | - $this->SimpleWidget('select', $attributes); |
|
| 771 | - $this->_options = array(); |
|
| 772 | - $this->_values = false; |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /** |
|
| 776 | - * Adds an option tag to a selection field. |
|
| 777 | - * @param SimpleOptionTag $tag New option. |
|
| 778 | - * @access public |
|
| 779 | - */ |
|
| 780 | - function addTag($tag) { |
|
| 781 | - if ($tag->getTagName() == 'option') { |
|
| 782 | - $this->_options[] = $tag; |
|
| 783 | - } |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Text within the selection element is ignored. |
|
| 788 | - * @param string $content Ignored. |
|
| 789 | - * @access public |
|
| 790 | - */ |
|
| 791 | - function addContent($content) { |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Scans options for defaults to populate the |
|
| 796 | - * value array(). |
|
| 797 | - * @return array Selected fields. |
|
| 798 | - * @access public |
|
| 799 | - */ |
|
| 800 | - function getDefault() { |
|
| 801 | - $default = array(); |
|
| 802 | - for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 803 | - if ($this->_options[$i]->getAttribute('selected') !== false) { |
|
| 804 | - $default[] = $this->_options[$i]->getDefault(); |
|
| 805 | - } |
|
| 806 | - } |
|
| 807 | - return $default; |
|
| 808 | - } |
|
| 809 | - |
|
| 810 | - /** |
|
| 811 | - * Can only set allowed values. Any illegal value |
|
| 812 | - * will result in a failure, but all correct values |
|
| 813 | - * will be set. |
|
| 814 | - * @param array $desired New choices. |
|
| 815 | - * @return boolean True if all allowed. |
|
| 816 | - * @access public |
|
| 817 | - */ |
|
| 818 | - function setValue($desired) { |
|
| 819 | - $achieved = array(); |
|
| 820 | - foreach ($desired as $value) { |
|
| 821 | - $success = false; |
|
| 822 | - for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 823 | - if ($this->_options[$i]->isValue($value)) { |
|
| 824 | - $achieved[] = $this->_options[$i]->getValue(); |
|
| 825 | - $success = true; |
|
| 826 | - break; |
|
| 827 | - } |
|
| 828 | - } |
|
| 829 | - if (! $success) { |
|
| 830 | - return false; |
|
| 831 | - } |
|
| 832 | - } |
|
| 833 | - $this->_values = $achieved; |
|
| 834 | - return true; |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - /** |
|
| 838 | - * Accessor for current selection value. |
|
| 839 | - * @return array List of currently set options. |
|
| 840 | - * @access public |
|
| 841 | - */ |
|
| 842 | - function getValue() { |
|
| 843 | - if ($this->_values === false) { |
|
| 844 | - return $this->getDefault(); |
|
| 845 | - } |
|
| 846 | - return $this->_values; |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - /** |
|
| 851 | - * Option for selection field. |
|
| 759 | + */ |
|
| 760 | + class MultipleSelectionTag extends SimpleWidget { |
|
| 761 | + protected $_options; |
|
| 762 | + protected $_values; |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * Starts with attributes only. |
|
| 766 | + * @param hash $attributes Attribute names and |
|
| 767 | + * string values. |
|
| 768 | + */ |
|
| 769 | + function MultipleSelectionTag($attributes) { |
|
| 770 | + $this->SimpleWidget('select', $attributes); |
|
| 771 | + $this->_options = array(); |
|
| 772 | + $this->_values = false; |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /** |
|
| 776 | + * Adds an option tag to a selection field. |
|
| 777 | + * @param SimpleOptionTag $tag New option. |
|
| 778 | + * @access public |
|
| 779 | + */ |
|
| 780 | + function addTag($tag) { |
|
| 781 | + if ($tag->getTagName() == 'option') { |
|
| 782 | + $this->_options[] = $tag; |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Text within the selection element is ignored. |
|
| 788 | + * @param string $content Ignored. |
|
| 789 | + * @access public |
|
| 790 | + */ |
|
| 791 | + function addContent($content) { |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Scans options for defaults to populate the |
|
| 796 | + * value array(). |
|
| 797 | + * @return array Selected fields. |
|
| 798 | + * @access public |
|
| 799 | + */ |
|
| 800 | + function getDefault() { |
|
| 801 | + $default = array(); |
|
| 802 | + for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 803 | + if ($this->_options[$i]->getAttribute('selected') !== false) { |
|
| 804 | + $default[] = $this->_options[$i]->getDefault(); |
|
| 805 | + } |
|
| 806 | + } |
|
| 807 | + return $default; |
|
| 808 | + } |
|
| 809 | + |
|
| 810 | + /** |
|
| 811 | + * Can only set allowed values. Any illegal value |
|
| 812 | + * will result in a failure, but all correct values |
|
| 813 | + * will be set. |
|
| 814 | + * @param array $desired New choices. |
|
| 815 | + * @return boolean True if all allowed. |
|
| 816 | + * @access public |
|
| 817 | + */ |
|
| 818 | + function setValue($desired) { |
|
| 819 | + $achieved = array(); |
|
| 820 | + foreach ($desired as $value) { |
|
| 821 | + $success = false; |
|
| 822 | + for ($i = 0, $count = count($this->_options); $i < $count; $i++) { |
|
| 823 | + if ($this->_options[$i]->isValue($value)) { |
|
| 824 | + $achieved[] = $this->_options[$i]->getValue(); |
|
| 825 | + $success = true; |
|
| 826 | + break; |
|
| 827 | + } |
|
| 828 | + } |
|
| 829 | + if (! $success) { |
|
| 830 | + return false; |
|
| 831 | + } |
|
| 832 | + } |
|
| 833 | + $this->_values = $achieved; |
|
| 834 | + return true; |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + /** |
|
| 838 | + * Accessor for current selection value. |
|
| 839 | + * @return array List of currently set options. |
|
| 840 | + * @access public |
|
| 841 | + */ |
|
| 842 | + function getValue() { |
|
| 843 | + if ($this->_values === false) { |
|
| 844 | + return $this->getDefault(); |
|
| 845 | + } |
|
| 846 | + return $this->_values; |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + /** |
|
| 851 | + * Option for selection field. |
|
| 852 | 852 | * @package SimpleTest |
| 853 | 853 | * @subpackage WebTester |
| 854 | - */ |
|
| 855 | - class SimpleOptionTag extends SimpleWidget { |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Stashes the attributes. |
|
| 859 | - */ |
|
| 860 | - function SimpleOptionTag($attributes) { |
|
| 861 | - $this->SimpleWidget('option', $attributes); |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - /** |
|
| 865 | - * Does nothing. |
|
| 866 | - * @param string $value Ignored. |
|
| 867 | - * @return boolean Not allowed. |
|
| 868 | - * @access public |
|
| 869 | - */ |
|
| 870 | - function setValue($value) { |
|
| 871 | - return false; |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Test to see if a value matches the option. |
|
| 876 | - * @param string $compare Value to compare with. |
|
| 877 | - * @return boolean True if possible match. |
|
| 878 | - * @access public |
|
| 879 | - */ |
|
| 880 | - function isValue($compare) { |
|
| 881 | - $compare = trim($compare); |
|
| 882 | - if (trim($this->getValue()) == $compare) { |
|
| 883 | - return true; |
|
| 884 | - } |
|
| 885 | - return trim($this->getContent()) == $compare; |
|
| 886 | - } |
|
| 887 | - |
|
| 888 | - /** |
|
| 889 | - * Accessor for starting value. Will be set to |
|
| 890 | - * the option label if no value exists. |
|
| 891 | - * @return string Parsed value. |
|
| 892 | - * @access public |
|
| 893 | - */ |
|
| 894 | - function getDefault() { |
|
| 895 | - if ($this->getAttribute('value') === false) { |
|
| 896 | - return $this->getContent(); |
|
| 897 | - } |
|
| 898 | - return $this->getAttribute('value'); |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - /** |
|
| 902 | - * The content of options is not part of the page. |
|
| 903 | - * @return boolean True. |
|
| 904 | - * @access public |
|
| 905 | - */ |
|
| 906 | - function isPrivateContent() { |
|
| 907 | - return true; |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - /** |
|
| 912 | - * Radio button. |
|
| 854 | + */ |
|
| 855 | + class SimpleOptionTag extends SimpleWidget { |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * Stashes the attributes. |
|
| 859 | + */ |
|
| 860 | + function SimpleOptionTag($attributes) { |
|
| 861 | + $this->SimpleWidget('option', $attributes); |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + /** |
|
| 865 | + * Does nothing. |
|
| 866 | + * @param string $value Ignored. |
|
| 867 | + * @return boolean Not allowed. |
|
| 868 | + * @access public |
|
| 869 | + */ |
|
| 870 | + function setValue($value) { |
|
| 871 | + return false; |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Test to see if a value matches the option. |
|
| 876 | + * @param string $compare Value to compare with. |
|
| 877 | + * @return boolean True if possible match. |
|
| 878 | + * @access public |
|
| 879 | + */ |
|
| 880 | + function isValue($compare) { |
|
| 881 | + $compare = trim($compare); |
|
| 882 | + if (trim($this->getValue()) == $compare) { |
|
| 883 | + return true; |
|
| 884 | + } |
|
| 885 | + return trim($this->getContent()) == $compare; |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + /** |
|
| 889 | + * Accessor for starting value. Will be set to |
|
| 890 | + * the option label if no value exists. |
|
| 891 | + * @return string Parsed value. |
|
| 892 | + * @access public |
|
| 893 | + */ |
|
| 894 | + function getDefault() { |
|
| 895 | + if ($this->getAttribute('value') === false) { |
|
| 896 | + return $this->getContent(); |
|
| 897 | + } |
|
| 898 | + return $this->getAttribute('value'); |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + /** |
|
| 902 | + * The content of options is not part of the page. |
|
| 903 | + * @return boolean True. |
|
| 904 | + * @access public |
|
| 905 | + */ |
|
| 906 | + function isPrivateContent() { |
|
| 907 | + return true; |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + /** |
|
| 912 | + * Radio button. |
|
| 913 | 913 | * @package SimpleTest |
| 914 | 914 | * @subpackage WebTester |
| 915 | - */ |
|
| 916 | - class SimpleRadioButtonTag extends SimpleWidget { |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * Stashes the attributes. |
|
| 920 | - * @param array $attributes Hash of attributes. |
|
| 921 | - */ |
|
| 922 | - function SimpleRadioButtonTag($attributes) { |
|
| 923 | - $this->SimpleWidget('input', $attributes); |
|
| 924 | - if ($this->getAttribute('value') === false) { |
|
| 925 | - $this->_setAttribute('value', 'on'); |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Tag contains no content. |
|
| 931 | - * @return boolean False. |
|
| 932 | - * @access public |
|
| 933 | - */ |
|
| 934 | - function expectEndTag() { |
|
| 935 | - return false; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * The only allowed value sn the one in the |
|
| 940 | - * "value" attribute. |
|
| 941 | - * @param string $value New value. |
|
| 942 | - * @return boolean True if allowed. |
|
| 943 | - * @access public |
|
| 944 | - */ |
|
| 945 | - function setValue($value) { |
|
| 946 | - if ($value === false) { |
|
| 947 | - return parent::setValue($value); |
|
| 948 | - } |
|
| 949 | - if ($value !== $this->getAttribute('value')) { |
|
| 950 | - return false; |
|
| 951 | - } |
|
| 952 | - return parent::setValue($value); |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * Accessor for starting value. |
|
| 957 | - * @return string Parsed value. |
|
| 958 | - * @access public |
|
| 959 | - */ |
|
| 960 | - function getDefault() { |
|
| 961 | - if ($this->getAttribute('checked') !== false) { |
|
| 962 | - return $this->getAttribute('value'); |
|
| 963 | - } |
|
| 964 | - return false; |
|
| 965 | - } |
|
| 966 | - } |
|
| 967 | - |
|
| 968 | - /** |
|
| 969 | - * Checkbox widget. |
|
| 915 | + */ |
|
| 916 | + class SimpleRadioButtonTag extends SimpleWidget { |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * Stashes the attributes. |
|
| 920 | + * @param array $attributes Hash of attributes. |
|
| 921 | + */ |
|
| 922 | + function SimpleRadioButtonTag($attributes) { |
|
| 923 | + $this->SimpleWidget('input', $attributes); |
|
| 924 | + if ($this->getAttribute('value') === false) { |
|
| 925 | + $this->_setAttribute('value', 'on'); |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Tag contains no content. |
|
| 931 | + * @return boolean False. |
|
| 932 | + * @access public |
|
| 933 | + */ |
|
| 934 | + function expectEndTag() { |
|
| 935 | + return false; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * The only allowed value sn the one in the |
|
| 940 | + * "value" attribute. |
|
| 941 | + * @param string $value New value. |
|
| 942 | + * @return boolean True if allowed. |
|
| 943 | + * @access public |
|
| 944 | + */ |
|
| 945 | + function setValue($value) { |
|
| 946 | + if ($value === false) { |
|
| 947 | + return parent::setValue($value); |
|
| 948 | + } |
|
| 949 | + if ($value !== $this->getAttribute('value')) { |
|
| 950 | + return false; |
|
| 951 | + } |
|
| 952 | + return parent::setValue($value); |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * Accessor for starting value. |
|
| 957 | + * @return string Parsed value. |
|
| 958 | + * @access public |
|
| 959 | + */ |
|
| 960 | + function getDefault() { |
|
| 961 | + if ($this->getAttribute('checked') !== false) { |
|
| 962 | + return $this->getAttribute('value'); |
|
| 963 | + } |
|
| 964 | + return false; |
|
| 965 | + } |
|
| 966 | + } |
|
| 967 | + |
|
| 968 | + /** |
|
| 969 | + * Checkbox widget. |
|
| 970 | 970 | * @package SimpleTest |
| 971 | 971 | * @subpackage WebTester |
| 972 | - */ |
|
| 973 | - class SimpleCheckboxTag extends SimpleWidget { |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * Starts with attributes only. |
|
| 977 | - * @param hash $attributes Attribute names and |
|
| 978 | - * string values. |
|
| 979 | - */ |
|
| 980 | - function SimpleCheckboxTag($attributes) { |
|
| 981 | - $this->SimpleWidget('input', $attributes); |
|
| 982 | - if ($this->getAttribute('value') === false) { |
|
| 983 | - $this->_setAttribute('value', 'on'); |
|
| 984 | - } |
|
| 985 | - } |
|
| 986 | - |
|
| 987 | - /** |
|
| 988 | - * Tag contains no content. |
|
| 989 | - * @return boolean False. |
|
| 990 | - * @access public |
|
| 991 | - */ |
|
| 992 | - function expectEndTag() { |
|
| 993 | - return false; |
|
| 994 | - } |
|
| 995 | - |
|
| 996 | - /** |
|
| 997 | - * The only allowed value in the one in the |
|
| 998 | - * "value" attribute. The default for this |
|
| 999 | - * attribute is "on". If this widget is set to |
|
| 1000 | - * true, then the usual value will be taken. |
|
| 1001 | - * @param string $value New value. |
|
| 1002 | - * @return boolean True if allowed. |
|
| 1003 | - * @access public |
|
| 1004 | - */ |
|
| 1005 | - function setValue($value) { |
|
| 1006 | - if ($value === false) { |
|
| 1007 | - return parent::setValue($value); |
|
| 1008 | - } |
|
| 1009 | - if ($value === true) { |
|
| 1010 | - return parent::setValue($this->getAttribute('value')); |
|
| 1011 | - } |
|
| 1012 | - if ($value != $this->getAttribute('value')) { |
|
| 1013 | - return false; |
|
| 1014 | - } |
|
| 1015 | - return parent::setValue($value); |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - /** |
|
| 1019 | - * Accessor for starting value. The default |
|
| 1020 | - * value is "on". |
|
| 1021 | - * @return string Parsed value. |
|
| 1022 | - * @access public |
|
| 1023 | - */ |
|
| 1024 | - function getDefault() { |
|
| 1025 | - if ($this->getAttribute('checked') !== false) { |
|
| 1026 | - return $this->getAttribute('value'); |
|
| 1027 | - } |
|
| 1028 | - return false; |
|
| 1029 | - } |
|
| 1030 | - } |
|
| 1031 | - |
|
| 1032 | - /** |
|
| 1033 | - * A group of multiple widgets with some shared behaviour. |
|
| 972 | + */ |
|
| 973 | + class SimpleCheckboxTag extends SimpleWidget { |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * Starts with attributes only. |
|
| 977 | + * @param hash $attributes Attribute names and |
|
| 978 | + * string values. |
|
| 979 | + */ |
|
| 980 | + function SimpleCheckboxTag($attributes) { |
|
| 981 | + $this->SimpleWidget('input', $attributes); |
|
| 982 | + if ($this->getAttribute('value') === false) { |
|
| 983 | + $this->_setAttribute('value', 'on'); |
|
| 984 | + } |
|
| 985 | + } |
|
| 986 | + |
|
| 987 | + /** |
|
| 988 | + * Tag contains no content. |
|
| 989 | + * @return boolean False. |
|
| 990 | + * @access public |
|
| 991 | + */ |
|
| 992 | + function expectEndTag() { |
|
| 993 | + return false; |
|
| 994 | + } |
|
| 995 | + |
|
| 996 | + /** |
|
| 997 | + * The only allowed value in the one in the |
|
| 998 | + * "value" attribute. The default for this |
|
| 999 | + * attribute is "on". If this widget is set to |
|
| 1000 | + * true, then the usual value will be taken. |
|
| 1001 | + * @param string $value New value. |
|
| 1002 | + * @return boolean True if allowed. |
|
| 1003 | + * @access public |
|
| 1004 | + */ |
|
| 1005 | + function setValue($value) { |
|
| 1006 | + if ($value === false) { |
|
| 1007 | + return parent::setValue($value); |
|
| 1008 | + } |
|
| 1009 | + if ($value === true) { |
|
| 1010 | + return parent::setValue($this->getAttribute('value')); |
|
| 1011 | + } |
|
| 1012 | + if ($value != $this->getAttribute('value')) { |
|
| 1013 | + return false; |
|
| 1014 | + } |
|
| 1015 | + return parent::setValue($value); |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + /** |
|
| 1019 | + * Accessor for starting value. The default |
|
| 1020 | + * value is "on". |
|
| 1021 | + * @return string Parsed value. |
|
| 1022 | + * @access public |
|
| 1023 | + */ |
|
| 1024 | + function getDefault() { |
|
| 1025 | + if ($this->getAttribute('checked') !== false) { |
|
| 1026 | + return $this->getAttribute('value'); |
|
| 1027 | + } |
|
| 1028 | + return false; |
|
| 1029 | + } |
|
| 1030 | + } |
|
| 1031 | + |
|
| 1032 | + /** |
|
| 1033 | + * A group of multiple widgets with some shared behaviour. |
|
| 1034 | 1034 | * @package SimpleTest |
| 1035 | 1035 | * @subpackage WebTester |
| 1036 | - */ |
|
| 1037 | - class SimpleTagGroup { |
|
| 1038 | - protected $_widgets = array(); |
|
| 1039 | - |
|
| 1040 | - /** |
|
| 1041 | - * Adds a tag to the group. |
|
| 1042 | - * @param SimpleWidget $widget |
|
| 1043 | - * @access public |
|
| 1044 | - */ |
|
| 1045 | - function addWidget($widget) { |
|
| 1046 | - $this->_widgets[] = $widget; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - /** |
|
| 1050 | - * Accessor to widget set. |
|
| 1051 | - * @return array All widgets. |
|
| 1052 | - * @access protected |
|
| 1053 | - */ |
|
| 1054 | - function &_getWidgets() { |
|
| 1055 | - return $this->_widgets; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * Accessor for an attribute. |
|
| 1060 | - * @param string $label Attribute name. |
|
| 1061 | - * @return boolean Always false. |
|
| 1062 | - * @access public |
|
| 1063 | - */ |
|
| 1064 | - function getAttribute($label) { |
|
| 1065 | - return false; |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - /** |
|
| 1069 | - * Fetches the name for the widget from the first |
|
| 1070 | - * member. |
|
| 1071 | - * @return string Name of widget. |
|
| 1072 | - * @access public |
|
| 1073 | - */ |
|
| 1074 | - function getName() { |
|
| 1075 | - if (count($this->_widgets) > 0) { |
|
| 1076 | - return $this->_widgets[0]->getName(); |
|
| 1077 | - } |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - /** |
|
| 1081 | - * Scans the widgets for one with the appropriate |
|
| 1082 | - * ID field. |
|
| 1083 | - * @param string $id ID value to try. |
|
| 1084 | - * @return boolean True if matched. |
|
| 1085 | - * @access public |
|
| 1086 | - */ |
|
| 1087 | - function isId($id) { |
|
| 1088 | - for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { |
|
| 1089 | - if ($this->_widgets[$i]->isId($id)) { |
|
| 1090 | - return true; |
|
| 1091 | - } |
|
| 1092 | - } |
|
| 1093 | - return false; |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - /** |
|
| 1097 | - * Scans the widgets for one with the appropriate |
|
| 1098 | - * attached label. |
|
| 1099 | - * @param string $label Attached label to try. |
|
| 1100 | - * @return boolean True if matched. |
|
| 1101 | - * @access public |
|
| 1102 | - */ |
|
| 1103 | - function isLabel($label) { |
|
| 1104 | - for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { |
|
| 1105 | - if ($this->_widgets[$i]->isLabel($label)) { |
|
| 1106 | - return true; |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - return false; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - /** |
|
| 1113 | - * Dispatches the value into the form encoded packet. |
|
| 1114 | - * @param SimpleEncoding $encoding Form packet. |
|
| 1115 | - * @access public |
|
| 1116 | - */ |
|
| 1117 | - function write($encoding) { |
|
| 1118 | - $encoding->add($this->getName(), $this->getValue()); |
|
| 1119 | - } |
|
| 1120 | - } |
|
| 1121 | - |
|
| 1122 | - /** |
|
| 1123 | - * A group of tags with the same name within a form. |
|
| 1036 | + */ |
|
| 1037 | + class SimpleTagGroup { |
|
| 1038 | + protected $_widgets = array(); |
|
| 1039 | + |
|
| 1040 | + /** |
|
| 1041 | + * Adds a tag to the group. |
|
| 1042 | + * @param SimpleWidget $widget |
|
| 1043 | + * @access public |
|
| 1044 | + */ |
|
| 1045 | + function addWidget($widget) { |
|
| 1046 | + $this->_widgets[] = $widget; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + /** |
|
| 1050 | + * Accessor to widget set. |
|
| 1051 | + * @return array All widgets. |
|
| 1052 | + * @access protected |
|
| 1053 | + */ |
|
| 1054 | + function &_getWidgets() { |
|
| 1055 | + return $this->_widgets; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * Accessor for an attribute. |
|
| 1060 | + * @param string $label Attribute name. |
|
| 1061 | + * @return boolean Always false. |
|
| 1062 | + * @access public |
|
| 1063 | + */ |
|
| 1064 | + function getAttribute($label) { |
|
| 1065 | + return false; |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + /** |
|
| 1069 | + * Fetches the name for the widget from the first |
|
| 1070 | + * member. |
|
| 1071 | + * @return string Name of widget. |
|
| 1072 | + * @access public |
|
| 1073 | + */ |
|
| 1074 | + function getName() { |
|
| 1075 | + if (count($this->_widgets) > 0) { |
|
| 1076 | + return $this->_widgets[0]->getName(); |
|
| 1077 | + } |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + /** |
|
| 1081 | + * Scans the widgets for one with the appropriate |
|
| 1082 | + * ID field. |
|
| 1083 | + * @param string $id ID value to try. |
|
| 1084 | + * @return boolean True if matched. |
|
| 1085 | + * @access public |
|
| 1086 | + */ |
|
| 1087 | + function isId($id) { |
|
| 1088 | + for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { |
|
| 1089 | + if ($this->_widgets[$i]->isId($id)) { |
|
| 1090 | + return true; |
|
| 1091 | + } |
|
| 1092 | + } |
|
| 1093 | + return false; |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + /** |
|
| 1097 | + * Scans the widgets for one with the appropriate |
|
| 1098 | + * attached label. |
|
| 1099 | + * @param string $label Attached label to try. |
|
| 1100 | + * @return boolean True if matched. |
|
| 1101 | + * @access public |
|
| 1102 | + */ |
|
| 1103 | + function isLabel($label) { |
|
| 1104 | + for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) { |
|
| 1105 | + if ($this->_widgets[$i]->isLabel($label)) { |
|
| 1106 | + return true; |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + return false; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + /** |
|
| 1113 | + * Dispatches the value into the form encoded packet. |
|
| 1114 | + * @param SimpleEncoding $encoding Form packet. |
|
| 1115 | + * @access public |
|
| 1116 | + */ |
|
| 1117 | + function write($encoding) { |
|
| 1118 | + $encoding->add($this->getName(), $this->getValue()); |
|
| 1119 | + } |
|
| 1120 | + } |
|
| 1121 | + |
|
| 1122 | + /** |
|
| 1123 | + * A group of tags with the same name within a form. |
|
| 1124 | 1124 | * @package SimpleTest |
| 1125 | 1125 | * @subpackage WebTester |
| 1126 | - */ |
|
| 1127 | - class SimpleCheckboxGroup extends SimpleTagGroup { |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Accessor for current selected widget or false |
|
| 1131 | - * if none. |
|
| 1132 | - * @return string/array Widget values or false if none. |
|
| 1133 | - * @access public |
|
| 1134 | - */ |
|
| 1135 | - function getValue() { |
|
| 1136 | - $values = array(); |
|
| 1137 | - $widgets = $this->_getWidgets(); |
|
| 1138 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1139 | - if ($widgets[$i]->getValue() !== false) { |
|
| 1140 | - $values[] = $widgets[$i]->getValue(); |
|
| 1141 | - } |
|
| 1142 | - } |
|
| 1143 | - return $this->_coerceValues($values); |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * Accessor for starting value that is active. |
|
| 1148 | - * @return string/array Widget values or false if none. |
|
| 1149 | - * @access public |
|
| 1150 | - */ |
|
| 1151 | - function getDefault() { |
|
| 1152 | - $values = array(); |
|
| 1153 | - $widgets = $this->_getWidgets(); |
|
| 1154 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1155 | - if ($widgets[$i]->getDefault() !== false) { |
|
| 1156 | - $values[] = $widgets[$i]->getDefault(); |
|
| 1157 | - } |
|
| 1158 | - } |
|
| 1159 | - return $this->_coerceValues($values); |
|
| 1160 | - } |
|
| 1161 | - |
|
| 1162 | - /** |
|
| 1163 | - * Accessor for current set values. |
|
| 1164 | - * @param string/array/boolean $values Either a single string, a |
|
| 1165 | - * hash or false for nothing set. |
|
| 1166 | - * @return boolean True if all values can be set. |
|
| 1167 | - * @access public |
|
| 1168 | - */ |
|
| 1169 | - function setValue($values) { |
|
| 1170 | - $values = $this->_makeArray($values); |
|
| 1171 | - if (! $this->_valuesArePossible($values)) { |
|
| 1172 | - return false; |
|
| 1173 | - } |
|
| 1174 | - $widgets = $this->_getWidgets(); |
|
| 1175 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1176 | - $possible = $widgets[$i]->getAttribute('value'); |
|
| 1177 | - if (in_array($widgets[$i]->getAttribute('value'), $values)) { |
|
| 1178 | - $widgets[$i]->setValue($possible); |
|
| 1179 | - } else { |
|
| 1180 | - $widgets[$i]->setValue(false); |
|
| 1181 | - } |
|
| 1182 | - } |
|
| 1183 | - return true; |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Tests to see if a possible value set is legal. |
|
| 1188 | - * @param string/array/boolean $values Either a single string, a |
|
| 1189 | - * hash or false for nothing set. |
|
| 1190 | - * @return boolean False if trying to set a |
|
| 1191 | - * missing value. |
|
| 1192 | - * @access private |
|
| 1193 | - */ |
|
| 1194 | - function _valuesArePossible($values) { |
|
| 1195 | - $matches = array(); |
|
| 1196 | - $widgets = $this->_getWidgets(); |
|
| 1197 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1198 | - $possible = $widgets[$i]->getAttribute('value'); |
|
| 1199 | - if (in_array($possible, $values)) { |
|
| 1200 | - $matches[] = $possible; |
|
| 1201 | - } |
|
| 1202 | - } |
|
| 1203 | - return ($values == $matches); |
|
| 1204 | - } |
|
| 1205 | - |
|
| 1206 | - /** |
|
| 1207 | - * Converts the output to an appropriate format. This means |
|
| 1208 | - * that no values is false, a single value is just that |
|
| 1209 | - * value and only two or more are contained in an array. |
|
| 1210 | - * @param array $values List of values of widgets. |
|
| 1211 | - * @return string/array/boolean Expected format for a tag. |
|
| 1212 | - * @access private |
|
| 1213 | - */ |
|
| 1214 | - function _coerceValues($values) { |
|
| 1215 | - if (count($values) == 0) { |
|
| 1216 | - return false; |
|
| 1217 | - } elseif (count($values) == 1) { |
|
| 1218 | - return $values[0]; |
|
| 1219 | - } else { |
|
| 1220 | - return $values; |
|
| 1221 | - } |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - /** |
|
| 1225 | - * Converts false or string into array. The opposite of |
|
| 1226 | - * the coercian method. |
|
| 1227 | - * @param string/array/boolean $value A single item is converted |
|
| 1228 | - * to a one item list. False |
|
| 1229 | - * gives an empty list. |
|
| 1230 | - * @return array List of values, possibly empty. |
|
| 1231 | - * @access private |
|
| 1232 | - */ |
|
| 1233 | - function _makeArray($value) { |
|
| 1234 | - if ($value === false) { |
|
| 1235 | - return array(); |
|
| 1236 | - } |
|
| 1237 | - if (is_string($value)) { |
|
| 1238 | - return array($value); |
|
| 1239 | - } |
|
| 1240 | - return $value; |
|
| 1241 | - } |
|
| 1242 | - } |
|
| 1243 | - |
|
| 1244 | - /** |
|
| 1245 | - * A group of tags with the same name within a form. |
|
| 1246 | - * Used for radio buttons. |
|
| 1126 | + */ |
|
| 1127 | + class SimpleCheckboxGroup extends SimpleTagGroup { |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Accessor for current selected widget or false |
|
| 1131 | + * if none. |
|
| 1132 | + * @return string/array Widget values or false if none. |
|
| 1133 | + * @access public |
|
| 1134 | + */ |
|
| 1135 | + function getValue() { |
|
| 1136 | + $values = array(); |
|
| 1137 | + $widgets = $this->_getWidgets(); |
|
| 1138 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1139 | + if ($widgets[$i]->getValue() !== false) { |
|
| 1140 | + $values[] = $widgets[$i]->getValue(); |
|
| 1141 | + } |
|
| 1142 | + } |
|
| 1143 | + return $this->_coerceValues($values); |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * Accessor for starting value that is active. |
|
| 1148 | + * @return string/array Widget values or false if none. |
|
| 1149 | + * @access public |
|
| 1150 | + */ |
|
| 1151 | + function getDefault() { |
|
| 1152 | + $values = array(); |
|
| 1153 | + $widgets = $this->_getWidgets(); |
|
| 1154 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1155 | + if ($widgets[$i]->getDefault() !== false) { |
|
| 1156 | + $values[] = $widgets[$i]->getDefault(); |
|
| 1157 | + } |
|
| 1158 | + } |
|
| 1159 | + return $this->_coerceValues($values); |
|
| 1160 | + } |
|
| 1161 | + |
|
| 1162 | + /** |
|
| 1163 | + * Accessor for current set values. |
|
| 1164 | + * @param string/array/boolean $values Either a single string, a |
|
| 1165 | + * hash or false for nothing set. |
|
| 1166 | + * @return boolean True if all values can be set. |
|
| 1167 | + * @access public |
|
| 1168 | + */ |
|
| 1169 | + function setValue($values) { |
|
| 1170 | + $values = $this->_makeArray($values); |
|
| 1171 | + if (! $this->_valuesArePossible($values)) { |
|
| 1172 | + return false; |
|
| 1173 | + } |
|
| 1174 | + $widgets = $this->_getWidgets(); |
|
| 1175 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1176 | + $possible = $widgets[$i]->getAttribute('value'); |
|
| 1177 | + if (in_array($widgets[$i]->getAttribute('value'), $values)) { |
|
| 1178 | + $widgets[$i]->setValue($possible); |
|
| 1179 | + } else { |
|
| 1180 | + $widgets[$i]->setValue(false); |
|
| 1181 | + } |
|
| 1182 | + } |
|
| 1183 | + return true; |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Tests to see if a possible value set is legal. |
|
| 1188 | + * @param string/array/boolean $values Either a single string, a |
|
| 1189 | + * hash or false for nothing set. |
|
| 1190 | + * @return boolean False if trying to set a |
|
| 1191 | + * missing value. |
|
| 1192 | + * @access private |
|
| 1193 | + */ |
|
| 1194 | + function _valuesArePossible($values) { |
|
| 1195 | + $matches = array(); |
|
| 1196 | + $widgets = $this->_getWidgets(); |
|
| 1197 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1198 | + $possible = $widgets[$i]->getAttribute('value'); |
|
| 1199 | + if (in_array($possible, $values)) { |
|
| 1200 | + $matches[] = $possible; |
|
| 1201 | + } |
|
| 1202 | + } |
|
| 1203 | + return ($values == $matches); |
|
| 1204 | + } |
|
| 1205 | + |
|
| 1206 | + /** |
|
| 1207 | + * Converts the output to an appropriate format. This means |
|
| 1208 | + * that no values is false, a single value is just that |
|
| 1209 | + * value and only two or more are contained in an array. |
|
| 1210 | + * @param array $values List of values of widgets. |
|
| 1211 | + * @return string/array/boolean Expected format for a tag. |
|
| 1212 | + * @access private |
|
| 1213 | + */ |
|
| 1214 | + function _coerceValues($values) { |
|
| 1215 | + if (count($values) == 0) { |
|
| 1216 | + return false; |
|
| 1217 | + } elseif (count($values) == 1) { |
|
| 1218 | + return $values[0]; |
|
| 1219 | + } else { |
|
| 1220 | + return $values; |
|
| 1221 | + } |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + /** |
|
| 1225 | + * Converts false or string into array. The opposite of |
|
| 1226 | + * the coercian method. |
|
| 1227 | + * @param string/array/boolean $value A single item is converted |
|
| 1228 | + * to a one item list. False |
|
| 1229 | + * gives an empty list. |
|
| 1230 | + * @return array List of values, possibly empty. |
|
| 1231 | + * @access private |
|
| 1232 | + */ |
|
| 1233 | + function _makeArray($value) { |
|
| 1234 | + if ($value === false) { |
|
| 1235 | + return array(); |
|
| 1236 | + } |
|
| 1237 | + if (is_string($value)) { |
|
| 1238 | + return array($value); |
|
| 1239 | + } |
|
| 1240 | + return $value; |
|
| 1241 | + } |
|
| 1242 | + } |
|
| 1243 | + |
|
| 1244 | + /** |
|
| 1245 | + * A group of tags with the same name within a form. |
|
| 1246 | + * Used for radio buttons. |
|
| 1247 | 1247 | * @package SimpleTest |
| 1248 | 1248 | * @subpackage WebTester |
| 1249 | - */ |
|
| 1250 | - class SimpleRadioGroup extends SimpleTagGroup { |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * Each tag is tried in turn until one is |
|
| 1254 | - * successfully set. The others will be |
|
| 1255 | - * unchecked if successful. |
|
| 1256 | - * @param string $value New value. |
|
| 1257 | - * @return boolean True if any allowed. |
|
| 1258 | - * @access public |
|
| 1259 | - */ |
|
| 1260 | - function setValue($value) { |
|
| 1261 | - if (! $this->_valueIsPossible($value)) { |
|
| 1262 | - return false; |
|
| 1263 | - } |
|
| 1264 | - $index = false; |
|
| 1265 | - $widgets = $this->_getWidgets(); |
|
| 1266 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1267 | - if (! $widgets[$i]->setValue($value)) { |
|
| 1268 | - $widgets[$i]->setValue(false); |
|
| 1269 | - } |
|
| 1270 | - } |
|
| 1271 | - return true; |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - /** |
|
| 1275 | - * Tests to see if a value is allowed. |
|
| 1276 | - * @param string Attempted value. |
|
| 1277 | - * @return boolean True if a valid value. |
|
| 1278 | - * @access private |
|
| 1279 | - */ |
|
| 1280 | - function _valueIsPossible($value) { |
|
| 1281 | - $widgets = $this->_getWidgets(); |
|
| 1282 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1283 | - if ($widgets[$i]->getAttribute('value') == $value) { |
|
| 1284 | - return true; |
|
| 1285 | - } |
|
| 1286 | - } |
|
| 1287 | - return false; |
|
| 1288 | - } |
|
| 1289 | - |
|
| 1290 | - /** |
|
| 1291 | - * Accessor for current selected widget or false |
|
| 1292 | - * if none. |
|
| 1293 | - * @return string/boolean Value attribute or |
|
| 1294 | - * content of opton. |
|
| 1295 | - * @access public |
|
| 1296 | - */ |
|
| 1297 | - function getValue() { |
|
| 1298 | - $widgets = $this->_getWidgets(); |
|
| 1299 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1300 | - if ($widgets[$i]->getValue() !== false) { |
|
| 1301 | - return $widgets[$i]->getValue(); |
|
| 1302 | - } |
|
| 1303 | - } |
|
| 1304 | - return false; |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /** |
|
| 1308 | - * Accessor for starting value that is active. |
|
| 1309 | - * @return string/boolean Value of first checked |
|
| 1310 | - * widget or false if none. |
|
| 1311 | - * @access public |
|
| 1312 | - */ |
|
| 1313 | - function getDefault() { |
|
| 1314 | - $widgets = $this->_getWidgets(); |
|
| 1315 | - for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1316 | - if ($widgets[$i]->getDefault() !== false) { |
|
| 1317 | - return $widgets[$i]->getDefault(); |
|
| 1318 | - } |
|
| 1319 | - } |
|
| 1320 | - return false; |
|
| 1321 | - } |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - /** |
|
| 1325 | - * Tag to keep track of labels. |
|
| 1249 | + */ |
|
| 1250 | + class SimpleRadioGroup extends SimpleTagGroup { |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * Each tag is tried in turn until one is |
|
| 1254 | + * successfully set. The others will be |
|
| 1255 | + * unchecked if successful. |
|
| 1256 | + * @param string $value New value. |
|
| 1257 | + * @return boolean True if any allowed. |
|
| 1258 | + * @access public |
|
| 1259 | + */ |
|
| 1260 | + function setValue($value) { |
|
| 1261 | + if (! $this->_valueIsPossible($value)) { |
|
| 1262 | + return false; |
|
| 1263 | + } |
|
| 1264 | + $index = false; |
|
| 1265 | + $widgets = $this->_getWidgets(); |
|
| 1266 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1267 | + if (! $widgets[$i]->setValue($value)) { |
|
| 1268 | + $widgets[$i]->setValue(false); |
|
| 1269 | + } |
|
| 1270 | + } |
|
| 1271 | + return true; |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + /** |
|
| 1275 | + * Tests to see if a value is allowed. |
|
| 1276 | + * @param string Attempted value. |
|
| 1277 | + * @return boolean True if a valid value. |
|
| 1278 | + * @access private |
|
| 1279 | + */ |
|
| 1280 | + function _valueIsPossible($value) { |
|
| 1281 | + $widgets = $this->_getWidgets(); |
|
| 1282 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1283 | + if ($widgets[$i]->getAttribute('value') == $value) { |
|
| 1284 | + return true; |
|
| 1285 | + } |
|
| 1286 | + } |
|
| 1287 | + return false; |
|
| 1288 | + } |
|
| 1289 | + |
|
| 1290 | + /** |
|
| 1291 | + * Accessor for current selected widget or false |
|
| 1292 | + * if none. |
|
| 1293 | + * @return string/boolean Value attribute or |
|
| 1294 | + * content of opton. |
|
| 1295 | + * @access public |
|
| 1296 | + */ |
|
| 1297 | + function getValue() { |
|
| 1298 | + $widgets = $this->_getWidgets(); |
|
| 1299 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1300 | + if ($widgets[$i]->getValue() !== false) { |
|
| 1301 | + return $widgets[$i]->getValue(); |
|
| 1302 | + } |
|
| 1303 | + } |
|
| 1304 | + return false; |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + /** |
|
| 1308 | + * Accessor for starting value that is active. |
|
| 1309 | + * @return string/boolean Value of first checked |
|
| 1310 | + * widget or false if none. |
|
| 1311 | + * @access public |
|
| 1312 | + */ |
|
| 1313 | + function getDefault() { |
|
| 1314 | + $widgets = $this->_getWidgets(); |
|
| 1315 | + for ($i = 0, $count = count($widgets); $i < $count; $i++) { |
|
| 1316 | + if ($widgets[$i]->getDefault() !== false) { |
|
| 1317 | + return $widgets[$i]->getDefault(); |
|
| 1318 | + } |
|
| 1319 | + } |
|
| 1320 | + return false; |
|
| 1321 | + } |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + /** |
|
| 1325 | + * Tag to keep track of labels. |
|
| 1326 | 1326 | * @package SimpleTest |
| 1327 | 1327 | * @subpackage WebTester |
| 1328 | - */ |
|
| 1329 | - class SimpleLabelTag extends SimpleTag { |
|
| 1330 | - |
|
| 1331 | - /** |
|
| 1332 | - * Starts with a named tag with attributes only. |
|
| 1333 | - * @param hash $attributes Attribute names and |
|
| 1334 | - * string values. |
|
| 1335 | - */ |
|
| 1336 | - function SimpleLabelTag($attributes) { |
|
| 1337 | - $this->SimpleTag('label', $attributes); |
|
| 1338 | - } |
|
| 1339 | - |
|
| 1340 | - /** |
|
| 1341 | - * Access for the ID to attach the label to. |
|
| 1342 | - * @return string For attribute. |
|
| 1343 | - * @access public |
|
| 1344 | - */ |
|
| 1345 | - function getFor() { |
|
| 1346 | - return $this->getAttribute('for'); |
|
| 1347 | - } |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - /** |
|
| 1351 | - * Tag to aid parsing the form. |
|
| 1328 | + */ |
|
| 1329 | + class SimpleLabelTag extends SimpleTag { |
|
| 1330 | + |
|
| 1331 | + /** |
|
| 1332 | + * Starts with a named tag with attributes only. |
|
| 1333 | + * @param hash $attributes Attribute names and |
|
| 1334 | + * string values. |
|
| 1335 | + */ |
|
| 1336 | + function SimpleLabelTag($attributes) { |
|
| 1337 | + $this->SimpleTag('label', $attributes); |
|
| 1338 | + } |
|
| 1339 | + |
|
| 1340 | + /** |
|
| 1341 | + * Access for the ID to attach the label to. |
|
| 1342 | + * @return string For attribute. |
|
| 1343 | + * @access public |
|
| 1344 | + */ |
|
| 1345 | + function getFor() { |
|
| 1346 | + return $this->getAttribute('for'); |
|
| 1347 | + } |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + /** |
|
| 1351 | + * Tag to aid parsing the form. |
|
| 1352 | 1352 | * @package SimpleTest |
| 1353 | 1353 | * @subpackage WebTester |
| 1354 | - */ |
|
| 1355 | - class SimpleFormTag extends SimpleTag { |
|
| 1356 | - |
|
| 1357 | - /** |
|
| 1358 | - * Starts with a named tag with attributes only. |
|
| 1359 | - * @param hash $attributes Attribute names and |
|
| 1360 | - * string values. |
|
| 1361 | - */ |
|
| 1362 | - function SimpleFormTag($attributes) { |
|
| 1363 | - $this->SimpleTag('form', $attributes); |
|
| 1364 | - } |
|
| 1365 | - } |
|
| 1366 | - |
|
| 1367 | - /** |
|
| 1368 | - * Tag to aid parsing the frames in a page. |
|
| 1354 | + */ |
|
| 1355 | + class SimpleFormTag extends SimpleTag { |
|
| 1356 | + |
|
| 1357 | + /** |
|
| 1358 | + * Starts with a named tag with attributes only. |
|
| 1359 | + * @param hash $attributes Attribute names and |
|
| 1360 | + * string values. |
|
| 1361 | + */ |
|
| 1362 | + function SimpleFormTag($attributes) { |
|
| 1363 | + $this->SimpleTag('form', $attributes); |
|
| 1364 | + } |
|
| 1365 | + } |
|
| 1366 | + |
|
| 1367 | + /** |
|
| 1368 | + * Tag to aid parsing the frames in a page. |
|
| 1369 | 1369 | * @package SimpleTest |
| 1370 | 1370 | * @subpackage WebTester |
| 1371 | - */ |
|
| 1372 | - class SimpleFrameTag extends SimpleTag { |
|
| 1373 | - |
|
| 1374 | - /** |
|
| 1375 | - * Starts with a named tag with attributes only. |
|
| 1376 | - * @param hash $attributes Attribute names and |
|
| 1377 | - * string values. |
|
| 1378 | - */ |
|
| 1379 | - function SimpleFrameTag($attributes) { |
|
| 1380 | - $this->SimpleTag('frame', $attributes); |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - /** |
|
| 1384 | - * Tag contains no content. |
|
| 1385 | - * @return boolean False. |
|
| 1386 | - * @access public |
|
| 1387 | - */ |
|
| 1388 | - function expectEndTag() { |
|
| 1389 | - return false; |
|
| 1390 | - } |
|
| 1391 | - } |
|
| 1392 | 1371 | \ No newline at end of file |
| 1372 | + */ |
|
| 1373 | + class SimpleFrameTag extends SimpleTag { |
|
| 1374 | + |
|
| 1375 | + /** |
|
| 1376 | + * Starts with a named tag with attributes only. |
|
| 1377 | + * @param hash $attributes Attribute names and |
|
| 1378 | + * string values. |
|
| 1379 | + */ |
|
| 1380 | + function SimpleFrameTag($attributes) { |
|
| 1381 | + $this->SimpleTag('frame', $attributes); |
|
| 1382 | + } |
|
| 1383 | + |
|
| 1384 | + /** |
|
| 1385 | + * Tag contains no content. |
|
| 1386 | + * @return boolean False. |
|
| 1387 | + * @access public |
|
| 1388 | + */ |
|
| 1389 | + function expectEndTag() { |
|
| 1390 | + return false; |
|
| 1391 | + } |
|
| 1392 | + } |
|
| 1393 | 1393 | \ No newline at end of file |