expectation.php 27 KB

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