| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * base include file for SimpleTest 4 * @package SimpleTest 5 * @subpackage UnitTester 6 * @version $Id: expectation.php 5999 2007-06-18 13:13:08Z pachanga $ 7 */ 8 9 /**#@+ 10 * include other SimpleTest class files 11 */ 12 require_once(dirname(__FILE__) . '/dumper.php'); 13 require_once(dirname(__FILE__) . '/compatibility.php'); 14 /**#@-*/ 15 16 /** 17 * Assertion that can display failure information. 18 * Also includes various helper methods. 19 * @package SimpleTest 20 * @subpackage UnitTester 21 * @abstract 22 */ 23 class SimpleExpectation { 24 var $_dumper = false; 25 var $_message; 26 27 /** 28 * Creates a dumper for displaying values and sets 29 * the test message. 30 * @param string $message Customised message on failure. 31 */ 32 function SimpleExpectation($message = '%s') { 33 $this->_message = $message; 34 } 35 36 /** 37 * Tests the expectation. True if correct. 38 * @param mixed $compare Comparison value. 39 * @return boolean True if correct. 40 * @access public 41 * @abstract 42 */ 43 function test($compare) { 44 } 45 46 /** 47 * Returns a human readable test message. 48 * @param mixed $compare Comparison value. 49 * @return string Description of success 50 * or failure. 51 * @access public 52 * @abstract 53 */ 54 function testMessage($compare) { 55 } 56 57 /** 58 * Overlays the generated message onto the stored user 59 * message. An additional message can be interjected. 60 * @param mixed $compare Comparison value. 61 * @param SimpleDumper $dumper For formatting the results. 62 * @return string Description of success 63 * or failure. 64 * @access public 65 */ 66 function overlayMessage($compare, $dumper) { 67 $this->_dumper = $dumper; 68 return sprintf($this->_message, $this->testMessage($compare)); 69 } 70 71 /** 72 * Accessor for the dumper. 73 * @return SimpleDumper Current value dumper. 74 * @access protected 75 */ 76 function &_getDumper() { 77 if (! $this->_dumper) { 78 $dumper = &new SimpleDumper(); 79 return $dumper; 80 } 81 return $this->_dumper; 82 } 83 84 /** 85 * Test to see if a value is an expectation object. 86 * A useful utility method. 87 * @param mixed $expectation Hopefully an Epectation 88 * class. 89 * @return boolean True if descended from 90 * this class. 91 * @access public 92 * @static 93 */ 94 function isExpectation($expectation) { 95 return is_object($expectation) && 96 SimpleTestCompatibility::isA($expectation, 'SimpleExpectation'); 97 } 98 } 99 100 /** 101 * A wildcard expectation always matches. 102 * @package SimpleTest 103 * @subpackage MockObjects 104 */ 105 class AnythingExpectation extends SimpleExpectation { 106 107 /** 108 * Tests the expectation. Always true. 109 * @param mixed $compare Ignored. 110 * @return boolean True. 111 * @access public 112 */ 113 function test($compare) { 114 return true; 115 } 116 117 /** 118 * Returns a human readable test message. 119 * @param mixed $compare Comparison value. 120 * @return string Description of success 121 * or failure. 122 * @access public 123 */ 124 function testMessage($compare) { 125 $dumper = &$this->_getDumper(); 126 return 'Anything always matches [' . $dumper->describeValue($compare) . ']'; 127 } 128 } 129 130 /** 131 * An expectation that never matches. 132 * @package SimpleTest 133 * @subpackage MockObjects 134 */ 135 class FailedExpectation extends SimpleExpectation { 136 137 /** 138 * Tests the expectation. Always false. 139 * @param mixed $compare Ignored. 140 * @return boolean True. 141 * @access public 142 */ 143 function test($compare) { 144 return false; 145 } 146 147 /** 148 * Returns a human readable test message. 149 * @param mixed $compare Comparison value. 150 * @return string Description of failure. 151 * @access public 152 */ 153 function testMessage($compare) { 154 $dumper = &$this->_getDumper(); 155 return 'Failed expectation never matches [' . $dumper->describeValue($compare) . ']'; 156 } 157 } 158 159 /** 160 * An expectation that passes on boolean true. 161 * @package SimpleTest 162 * @subpackage MockObjects 163 */ 164 class TrueExpectation extends SimpleExpectation { 165 166 /** 167 * Tests the expectation. 168 * @param mixed $compare Should be true. 169 * @return boolean True on match. 170 * @access public 171 */ 172 function test($compare) { 173 return (boolean)$compare; 174 } 175 176 /** 177 * Returns a human readable test message. 178 * @param mixed $compare Comparison value. 179 * @return string Description of success 180 * or failure. 181 * @access public 182 */ 183 function testMessage($compare) { 184 $dumper = &$this->_getDumper(); 185 return 'Expected true, got [' . $dumper->describeValue($compare) . ']'; 186 } 187 } 188 189 /** 190 * An expectation that passes on boolean false. 191 * @package SimpleTest 192 * @subpackage MockObjects 193 */ 194 class FalseExpectation extends SimpleExpectation { 195 196 /** 197 * Tests the expectation. 198 * @param mixed $compare Should be false. 199 * @return boolean True on match. 200 * @access public 201 */ 202 function test($compare) { 203 return ! (boolean)$compare; 204 } 205 206 /** 207 * Returns a human readable test message. 208 * @param mixed $compare Comparison value. 209 * @return string Description of success 210 * or failure. 211 * @access public 212 */ 213 function testMessage($compare) { 214 $dumper = &$this->_getDumper(); 215 return 'Expected false, got [' . $dumper->describeValue($compare) . ']'; 216 } 217 } 218 219 /** 220 * Test for equality. 221 * @package SimpleTest 222 * @subpackage UnitTester 223 */ 224 class EqualExpectation extends SimpleExpectation { 225 var $_value; 226 227 /** 228 * Sets the value to compare against. 229 * @param mixed $value Test value to match. 230 * @param string $message Customised message on failure. 231 * @access public 232 */ 233 function EqualExpectation($value, $message = '%s') { 234 $this->SimpleExpectation($message); 235 $this->_value = $value; 236 } 237 238 /** 239 * Tests the expectation. True if it matches the 240 * held value. 241 * @param mixed $compare Comparison value. 242 * @return boolean True if correct. 243 * @access public 244 */ 245 function test($compare) { 246 return (($this->_value == $compare) && ($compare == $this->_value)); 247 } 248 249 /** 250 * Returns a human readable test message. 251 * @param mixed $compare Comparison value. 252 * @return string Description of success 253 * or failure. 254 * @access public 255 */ 256 function testMessage($compare) { 257 if ($this->test($compare)) { 258 return "Equal expectation [" . $this->_dumper->describeValue($this->_value) . "]"; 259 } else { 260 return "Equal expectation fails " . 261 $this->_dumper->describeDifference($this->_value, $compare); 262 } 263 } 264 265 /** 266 * Accessor for comparison value. 267 * @return mixed Held value to compare with. 268 * @access protected 269 */ 270 function _getValue() { 271 return $this->_value; 272 } 273 } 274 275 /** 276 * Test for inequality. 277 * @package SimpleTest 278 * @subpackage UnitTester 279 */ 280 class NotEqualExpectation extends EqualExpectation { 281 282 /** 283 * Sets the value to compare against. 284 * @param mixed $value Test value to match. 285 * @param string $message Customised message on failure. 286 * @access public 287 */ 288 function NotEqualExpectation($value, $message = '%s') { 289 $this->EqualExpectation($value, $message); 290 } 291 292 /** 293 * Tests the expectation. True if it differs from the 294 * held value. 295 * @param mixed $compare Comparison value. 296 * @return boolean True if correct. 297 * @access public 298 */ 299 function test($compare) { 300 return ! parent::test($compare); 301 } 302 303 /** 304 * Returns a human readable test message. 305 * @param mixed $compare Comparison value. 306 * @return string Description of success 307 * or failure. 308 * @access public 309 */ 310 function testMessage($compare) { 311 $dumper = &$this->_getDumper(); 312 if ($this->test($compare)) { 313 return "Not equal expectation passes " . 314 $dumper->describeDifference($this->_getValue(), $compare); 315 } else { 316 return "Not equal expectation fails [" . 317 $dumper->describeValue($this->_getValue()) . 318 "] matches"; 319 } 320 } 321 } 322 323 /** 324 * Test for being within a range. 325 * @package SimpleTest 326 * @subpackage UnitTester 327 */ 328 class WithinMarginExpectation extends SimpleExpectation { 329 var $_upper; 330 var $_lower; 331 332 /** 333 * Sets the value to compare against and the fuzziness of 334 * the match. Used for comparing floating point values. 335 * @param mixed $value Test value to match. 336 * @param mixed $margin Fuzziness of match. 337 * @param string $message Customised message on failure. 338 * @access public 339 */ 340 function WithinMarginExpectation($value, $margin, $message = '%s') { 341 $this->SimpleExpectation($message); 342 $this->_upper = $value + $margin; 343 $this->_lower = $value - $margin; 344 } 345 346 /** 347 * Tests the expectation. True if it matches the 348 * held value. 349 * @param mixed $compare Comparison value. 350 * @return boolean True if correct. 351 * @access public 352 */ 353 function test($compare) { 354 return (($compare <= $this->_upper) && ($compare >= $this->_lower)); 355 } 356 357 /** 358 * Returns a human readable test message. 359 * @param mixed $compare Comparison value. 360 * @return string Description of success 361 * or failure. 362 * @access public 363 */ 364 function testMessage($compare) { 365 if ($this->test($compare)) { 366 return $this->_withinMessage($compare); 367 } else { 368 return $this->_outsideMessage($compare); 369 } 370 } 371 372 /** 373 * Creates a the message for being within the range. 374 * @param mixed $compare Value being tested. 375 * @access private 376 */ 377 function _withinMessage($compare) { 378 return "Within expectation [" . $this->_dumper->describeValue($this->_lower) . "] and [" . 379 $this->_dumper->describeValue($this->_upper) . "]"; 380 } 381 382 /** 383 * Creates a the message for being within the range. 384 * @param mixed $compare Value being tested. 385 * @access private 386 */ 387 function _outsideMessage($compare) { 388 if ($compare > $this->_upper) { 389 return "Outside expectation " . 390 $this->_dumper->describeDifference($compare, $this->_upper); 391 } else { 392 return "Outside expectation " . 393 $this->_dumper->describeDifference($compare, $this->_lower); 394 } 395 } 396 } 397 398 /** 399 * Test for being outside of a range. 400 * @package SimpleTest 401 * @subpackage UnitTester 402 */ 403 class OutsideMarginExpectation extends WithinMarginExpectation { 404 405 /** 406 * Sets the value to compare against and the fuzziness of 407 * the match. Used for comparing floating point values. 408 * @param mixed $value Test value to not match. 409 * @param mixed $margin Fuzziness of match. 410 * @param string $message Customised message on failure. 411 * @access public 412 */ 413 function OutsideMarginExpectation($value, $margin, $message = '%s') { 414 $this->WithinMarginExpectation($value, $margin, $message); 415 } 416 417 /** 418 * Tests the expectation. True if it matches the 419 * held value. 420 * @param mixed $compare Comparison value. 421 * @return boolean True if correct. 422 * @access public 423 */ 424 function test($compare) { 425 return ! parent::test($compare); 426 } 427 428 /** 429 * Returns a human readable test message. 430 * @param mixed $compare Comparison value. 431 * @return string Description of success 432 * or failure. 433 * @access public 434 */ 435 function testMessage($compare) { 436 if (! $this->test($compare)) { 437 return $this->_withinMessage($compare); 438 } else { 439 return $this->_outsideMessage($compare); 440 } 441 } 442 } 443 444 /** 445 * Test for reference. 446 * @package SimpleTest 447 * @subpackage UnitTester 448 */ 449 class ReferenceExpectation extends SimpleExpectation { 450 var $_value; 451 452 /** 453 * Sets the reference value to compare against. 454 * @param mixed $value Test reference to match. 455 * @param string $message Customised message on failure. 456 * @access public 457 */ 458 function ReferenceExpectation(&$value, $message = '%s') { 459 $this->SimpleExpectation($message); 460 $this->_value =& $value; 461 } 462 463 /** 464 * Tests the expectation. True if it exactly 465 * references the held value. 466 * @param mixed $compare Comparison reference. 467 * @return boolean True if correct. 468 * @access public 469 */ 470 function test(&$compare) { 471 return SimpleTestCompatibility::isReference($this->_value, $compare); 472 } 473 474 /** 475 * Returns a human readable test message. 476 * @param mixed $compare Comparison value. 477 * @return string Description of success 478 * or failure. 479 * @access public 480 */ 481 function testMessage($compare) { 482 if ($this->test($compare)) { 483 return "Reference expectation [" . $this->_dumper->describeValue($this->_value) . "]"; 484 } else { 485 return "Reference expectation fails " . 486 $this->_dumper->describeDifference($this->_value, $compare); 487 } 488 } 489 490 function _getValue() { 491 return $this->_value; 492 } 493 } 494 495 /** 496 * Test for identity. 497 * @package SimpleTest 498 * @subpackage UnitTester 499 */ 500 class IdenticalExpectation extends EqualExpectation { 501 502 /** 503 * Sets the value to compare against. 504 * @param mixed $value Test value to match. 505 * @param string $message Customised message on failure. 506 * @access public 507 */ 508 function IdenticalExpectation($value, $message = '%s') { 509 $this->EqualExpectation($value, $message); 510 } 511 512 /** 513 * Tests the expectation. True if it exactly 514 * matches the held value. 515 * @param mixed $compare Comparison value. 516 * @return boolean True if correct. 517 * @access public 518 */ 519 function test($compare) { 520 return SimpleTestCompatibility::isIdentical($this->_getValue(), $compare); 521 } 522 523 /** 524 * Returns a human readable test message. 525 * @param mixed $compare Comparison value. 526 * @return string Description of success 527 * or failure. 528 * @access public 529 */ 530 function testMessage($compare) { 531 $dumper = &$this->_getDumper(); 532 if ($this->test($compare)) { 533 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . "]"; 534 } else { 535 return "Identical expectation [" . $dumper->describeValue($this->_getValue()) . 536 "] fails with [" . 537 $dumper->describeValue($compare) . "] " . 538 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS); 539 } 540 } 541 } 542 543 /** 544 * Test for non-identity. 545 * @package SimpleTest 546 * @subpackage UnitTester 547 */ 548 class NotIdenticalExpectation extends IdenticalExpectation { 549 550 /** 551 * Sets the value to compare against. 552 * @param mixed $value Test value to match. 553 * @param string $message Customised message on failure. 554 * @access public 555 */ 556 function NotIdenticalExpectation($value, $message = '%s') { 557 $this->IdenticalExpectation($value, $message); 558 } 559 560 /** 561 * Tests the expectation. True if it differs from the 562 * held value. 563 * @param mixed $compare Comparison value. 564 * @return boolean True if correct. 565 * @access public 566 */ 567 function test($compare) { 568 return ! parent::test($compare); 569 } 570 571 /** 572 * Returns a human readable test message. 573 * @param mixed $compare Comparison value. 574 * @return string Description of success 575 * or failure. 576 * @access public 577 */ 578 function testMessage($compare) { 579 $dumper = &$this->_getDumper(); 580 if ($this->test($compare)) { 581 return "Not identical expectation passes " . 582 $dumper->describeDifference($this->_getValue(), $compare, TYPE_MATTERS); 583 } else { 584 return "Not identical expectation [" . $dumper->describeValue($this->_getValue()) . "] matches"; 585 } 586 } 587 } 588 589 /** 590 * Test for a pattern using Perl regex rules. 591 * @package SimpleTest 592 * @subpackage UnitTester 593 */ 594 class PatternExpectation extends SimpleExpectation { 595 var $_pattern; 596 597 /** 598 * Sets the value to compare against. 599 * @param string $pattern Pattern to search for. 600 * @param string $message Customised message on failure. 601 * @access public 602 */ 603 function PatternExpectation($pattern, $message = '%s') { 604 $this->SimpleExpectation($message); 605 $this->_pattern = $pattern; 606 } 607 608 /** 609 * Accessor for the pattern. 610 * @return string Perl regex as string. 611 * @access protected 612 */ 613 function _getPattern() { 614 return $this->_pattern; 615 } 616 617 /** 618 * Tests the expectation. True if the Perl regex 619 * matches the comparison value. 620 * @param string $compare Comparison value. 621 * @return boolean True if correct. 622 * @access public 623 */ 624 function test($compare) { 625 return (boolean)preg_match($this->_getPattern(), $compare); 626 } 627 628 /** 629 * Returns a human readable test message. 630 * @param mixed $compare Comparison value. 631 * @return string Description of success 632 * or failure. 633 * @access public 634 */ 635 function testMessage($compare) { 636 if ($this->test($compare)) { 637 return $this->_describePatternMatch(