/lib/simpletestlib/expectation.php

https://github.com/jarednipper/HSU-common-code · PHP · 811 lines · 297 code · 70 blank · 444 comment · 28 complexity · 0b972ec350fdcd3081899f14eb89bc80 MD5 · raw file

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