PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/simpletest/mock_objects.php

https://github.com/chregu/fluxcms
PHP | 1249 lines | 593 code | 78 blank | 578 comment | 55 complexity | 601dfc6c30c79eace66c0a320fab64ab MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage MockObjects
  6. * @version $Id: mock_objects.php,v 1.86 2005/09/10 23:01:56 lastcraft Exp $
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. require_once(dirname(__FILE__) . '/expectation.php');
  12. require_once(dirname(__FILE__) . '/simpletest.php');
  13. require_once(dirname(__FILE__) . '/dumper.php');
  14. if (version_compare(phpversion(), '5') >= 0) {
  15. require_once(dirname(__FILE__) . '/reflection_php5.php');
  16. } else {
  17. require_once(dirname(__FILE__) . '/reflection_php4.php');
  18. }
  19. /**#@-*/
  20. /**
  21. * Default character simpletest will substitute for any value
  22. */
  23. if (! defined('MOCK_ANYTHING')) {
  24. define('MOCK_ANYTHING', '*');
  25. }
  26. /**
  27. * A wildcard expectation always matches.
  28. * @package SimpleTest
  29. * @subpackage MockObjects
  30. */
  31. class AnythingExpectation extends SimpleExpectation {
  32. /**
  33. * Tests the expectation. Always true.
  34. * @param mixed $compare Ignored.
  35. * @return boolean True.
  36. * @access public
  37. */
  38. function test($compare) {
  39. return true;
  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. */
  48. function testMessage($compare) {
  49. $dumper = &$this->_getDumper();
  50. return 'Anything always matches [' . $dumper->describeValue($compare) . ']';
  51. }
  52. }
  53. /**
  54. * Parameter comparison assertion.
  55. * @package SimpleTest
  56. * @subpackage MockObjects
  57. */
  58. class ParametersExpectation extends SimpleExpectation {
  59. var $_expected;
  60. /**
  61. * Sets the expected parameter list.
  62. * @param array $parameters Array of parameters including
  63. * those that are wildcarded.
  64. * If the value is not an array
  65. * then it is considered to match any.
  66. * @param mixed $wildcard Any parameter matching this
  67. * will always match.
  68. * @param string $message Customised message on failure.
  69. * @access public
  70. */
  71. function ParametersExpectation($expected = false, $message = '%s') {
  72. $this->SimpleExpectation($message);
  73. $this->_expected = $expected;
  74. }
  75. /**
  76. * Tests the assertion. True if correct.
  77. * @param array $parameters Comparison values.
  78. * @return boolean True if correct.
  79. * @access public
  80. */
  81. function test($parameters) {
  82. if (! is_array($this->_expected)) {
  83. return true;
  84. }
  85. if (count($this->_expected) != count($parameters)) {
  86. return false;
  87. }
  88. for ($i = 0; $i < count($this->_expected); $i++) {
  89. if (! $this->_testParameter($parameters[$i], $this->_expected[$i])) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * Tests an individual parameter.
  97. * @param mixed $parameter Value to test.
  98. * @param mixed $expected Comparison value.
  99. * @return boolean True if expectation
  100. * fulfilled.
  101. * @access private
  102. */
  103. function _testParameter($parameter, $expected) {
  104. $comparison = $this->_coerceToExpectation($expected);
  105. return $comparison->test($parameter);
  106. }
  107. /**
  108. * Returns a human readable test message.
  109. * @param array $comparison Incoming parameter list.
  110. * @return string Description of success
  111. * or failure.
  112. * @access public
  113. */
  114. function testMessage($parameters) {
  115. if ($this->test($parameters)) {
  116. return "Expectation of " . count($this->_expected) .
  117. " arguments of [" . $this->_renderArguments($this->_expected) .
  118. "] is correct";
  119. } else {
  120. return $this->_describeDifference($this->_expected, $parameters);
  121. }
  122. }
  123. /**
  124. * Message to display if expectation differs from
  125. * the parameters actually received.
  126. * @param array $expected Expected parameters as list.
  127. * @param array $parameters Actual parameters received.
  128. * @return string Description of difference.
  129. * @access private
  130. */
  131. function _describeDifference($expected, $parameters) {
  132. if (count($expected) != count($parameters)) {
  133. return "Expected " . count($expected) .
  134. " arguments of [" . $this->_renderArguments($expected) .
  135. "] but got " . count($parameters) .
  136. " arguments of [" . $this->_renderArguments($parameters) . "]";
  137. }
  138. $messages = array();
  139. for ($i = 0; $i < count($expected); $i++) {
  140. $comparison = $this->_coerceToExpectation($expected[$i]);
  141. if (! $comparison->test($parameters[$i])) {
  142. $messages[] = "parameter " . ($i + 1) . " with [" .
  143. $comparison->overlayMessage($parameters[$i]) . "]";
  144. }
  145. }
  146. return "Parameter expectation differs at " . implode(" and ", $messages);
  147. }
  148. /**
  149. * Creates an identical expectation if the
  150. * object/value is not already some type
  151. * of expectation.
  152. * @param mixed $expected Expected value.
  153. * @return SimpleExpectation Expectation object.
  154. * @access private
  155. */
  156. function _coerceToExpectation($expected) {
  157. if (SimpleExpectation::isExpectation($expected)) {
  158. return $expected;
  159. }
  160. return new IdenticalExpectation($expected);
  161. }
  162. /**
  163. * Renders the argument list as a string for
  164. * messages.
  165. * @param array $args Incoming arguments.
  166. * @return string Simple description of type and value.
  167. * @access private
  168. */
  169. function _renderArguments($args) {
  170. $descriptions = array();
  171. if (is_array($args)) {
  172. foreach ($args as $arg) {
  173. $dumper = &new SimpleDumper();
  174. $descriptions[] = $dumper->describeValue($arg);
  175. }
  176. }
  177. return implode(', ', $descriptions);
  178. }
  179. }
  180. /**
  181. * Confirms that the number of calls on a method is as expected.
  182. */
  183. class CallCountExpectation extends SimpleExpectation {
  184. var $_method;
  185. var $_count;
  186. /**
  187. * Stashes the method and expected count for later
  188. * reporting.
  189. * @param string $method Name of method to confirm against.
  190. * @param integer $count Expected number of calls.
  191. * @param string $message Custom error message.
  192. */
  193. function CallCountExpectation($method, $count, $message = '%s') {
  194. $this->_method = $method;
  195. $this->_count = $count;
  196. $this->SimpleExpectation($message);
  197. }
  198. /**
  199. * Tests the assertion. True if correct.
  200. * @param integer $compare Measured call count.
  201. * @return boolean True if expected.
  202. * @access public
  203. */
  204. function test($compare) {
  205. return ($this->_count == $compare);
  206. }
  207. /**
  208. * Reports the comparison.
  209. * @param integer $compare Measured call count.
  210. * @return string Message to show.
  211. * @access public
  212. */
  213. function testMessage($compare) {
  214. return 'Expected call count for [' . $this->_method .
  215. '] was [' . $this->_count .
  216. '] got [' . $compare . ']';
  217. }
  218. }
  219. /**
  220. * Confirms that the number of calls on a method is as expected.
  221. */
  222. class MinimumCallCountExpectation extends SimpleExpectation {
  223. var $_method;
  224. var $_count;
  225. /**
  226. * Stashes the method and expected count for later
  227. * reporting.
  228. * @param string $method Name of method to confirm against.
  229. * @param integer $count Minimum number of calls.
  230. * @param string $message Custom error message.
  231. */
  232. function MinimumCallCountExpectation($method, $count, $message = '%s') {
  233. $this->_method = $method;
  234. $this->_count = $count;
  235. $this->SimpleExpectation($message);
  236. }
  237. /**
  238. * Tests the assertion. True if correct.
  239. * @param integer $compare Measured call count.
  240. * @return boolean True if enough.
  241. * @access public
  242. */
  243. function test($compare) {
  244. return ($this->_count <= $compare);
  245. }
  246. /**
  247. * Reports the comparison.
  248. * @param integer $compare Measured call count.
  249. * @return string Message to show.
  250. * @access public
  251. */
  252. function testMessage($compare) {
  253. return 'Minimum call count for [' . $this->_method .
  254. '] was [' . $this->_count .
  255. '] got [' . $compare . ']';
  256. }
  257. }
  258. /**
  259. * Confirms that the number of calls on a method is as expected.
  260. */
  261. class MaximumCallCountExpectation extends SimpleExpectation {
  262. var $_method;
  263. var $_count;
  264. /**
  265. * Stashes the method and expected count for later
  266. * reporting.
  267. * @param string $method Name of method to confirm against.
  268. * @param integer $count Minimum number of calls.
  269. * @param string $message Custom error message.
  270. */
  271. function MaximumCallCountExpectation($method, $count, $message = '%s') {
  272. $this->_method = $method;
  273. $this->_count = $count;
  274. $this->SimpleExpectation($message);
  275. }
  276. /**
  277. * Tests the assertion. True if correct.
  278. * @param integer $compare Measured call count.
  279. * @return boolean True if not over.
  280. * @access public
  281. */
  282. function test($compare) {
  283. return ($this->_count >= $compare);
  284. }
  285. /**
  286. * Reports the comparison.
  287. * @param integer $compare Measured call count.
  288. * @return string Message to show.
  289. * @access public
  290. */
  291. function testMessage($compare) {
  292. return 'Maximum call count for [' . $this->_method .
  293. '] was [' . $this->_count .
  294. '] got [' . $compare . ']';
  295. }
  296. }
  297. /**
  298. * Retrieves values and references by searching the
  299. * parameter lists until a match is found.
  300. * @package SimpleTest
  301. * @subpackage MockObjects
  302. */
  303. class CallMap {
  304. var $_map;
  305. /**
  306. * Creates an empty call map.
  307. * @access public
  308. */
  309. function CallMap() {
  310. $this->_map = array();
  311. }
  312. /**
  313. * Stashes a value against a method call.
  314. * @param array $parameters Arguments including wildcards.
  315. * @param mixed $value Value copied into the map.
  316. * @access public
  317. */
  318. function addValue($parameters, $value) {
  319. $this->addReference($parameters, $value);
  320. }
  321. /**
  322. * Stashes a reference against a method call.
  323. * @param array $parameters Array of arguments (including wildcards).
  324. * @param mixed $reference Array reference placed in the map.
  325. * @access public
  326. */
  327. function addReference($parameters, &$reference) {
  328. $place = count($this->_map);
  329. $this->_map[$place] = array();
  330. $this->_map[$place]["params"] = new ParametersExpectation($parameters);
  331. $this->_map[$place]["content"] = &$reference;
  332. }
  333. /**
  334. * Searches the call list for a matching parameter
  335. * set. Returned by reference.
  336. * @param array $parameters Parameters to search by
  337. * without wildcards.
  338. * @return object Object held in the first matching
  339. * slot, otherwise null.
  340. * @access public
  341. */
  342. function &findFirstMatch($parameters) {
  343. $slot = $this->_findFirstSlot($parameters);
  344. if (!isset($slot)) {
  345. $null = null;
  346. return $null;
  347. }
  348. return $slot["content"];
  349. }
  350. /**
  351. * Searches the call list for a matching parameter
  352. * set. True if successful.
  353. * @param array $parameters Parameters to search by
  354. * without wildcards.
  355. * @return boolean True if a match is present.
  356. * @access public
  357. */
  358. function isMatch($parameters) {
  359. return ($this->_findFirstSlot($parameters) != null);
  360. }
  361. /**
  362. * Searches the map for a matching item.
  363. * @param array $parameters Parameters to search by
  364. * without wildcards.
  365. * @return array Reference to slot or null.
  366. * @access private
  367. */
  368. function &_findFirstSlot($parameters) {
  369. $count = count($this->_map);
  370. for ($i = 0; $i < $count; $i++) {
  371. if ($this->_map[$i]["params"]->test($parameters)) {
  372. return $this->_map[$i];
  373. }
  374. }
  375. $null = null;
  376. return $null;
  377. }
  378. }
  379. /**
  380. * An empty collection of methods that can have their
  381. * return values set and expectations made of the
  382. * calls upon them. The mock will assert the
  383. * expectations against it's attached test case in
  384. * addition to the server stub behaviour.
  385. * @package SimpleTest
  386. * @subpackage MockObjects
  387. */
  388. class SimpleMock {
  389. var $_wildcard = MOCK_ANYTHING;
  390. var $_is_strict = true;
  391. var $_returns;
  392. var $_return_sequence;
  393. var $_call_counts;
  394. var $_expected_counts;
  395. var $_max_counts;
  396. var $_expected_args;
  397. var $_expected_args_at;
  398. /**
  399. * Creates an empty return list and expectation list.
  400. * All call counts are set to zero.
  401. * @param SimpleTestCase $test Test case to test expectations in.
  402. * @param mixed $wildcard Parameter matching wildcard.
  403. * @param boolean $is_strict Enables method name checks on
  404. * expectations.
  405. */
  406. function SimpleMock() {
  407. $this->_returns = array();
  408. $this->_return_sequence = array();
  409. $this->_call_counts = array();
  410. $test = &$this->_getCurrentTestCase();
  411. $test->tell($this);
  412. $this->_expected_counts = array();
  413. $this->_max_counts = array();
  414. $this->_expected_args = array();
  415. $this->_expected_args_at = array();
  416. }
  417. /**
  418. * Disables a name check when setting expectations.
  419. * This hack is needed for the partial mocks.
  420. * @access public
  421. */
  422. function disableExpectationNameChecks() {
  423. $this->_is_strict = false;
  424. }
  425. /**
  426. * Changes the default wildcard object.
  427. * @param mixed $wildcard Parameter matching wildcard.
  428. * @access public
  429. */
  430. function setWildcard($wildcard) {
  431. $this->_wildcard = $wildcard;
  432. }
  433. /**
  434. * Finds currently running test.
  435. * @return SimpeTestCase Current test case.
  436. * @access protected
  437. */
  438. function &_getCurrentTestCase() {
  439. return SimpleTest::getCurrent();
  440. }
  441. /**
  442. * Die if bad arguments array is passed
  443. * @param mixed $args The arguments value to be checked.
  444. * @param string $task Description of task attempt.
  445. * @return boolean Valid arguments
  446. * @access private
  447. */
  448. function _checkArgumentsIsArray($args, $task) {
  449. if (! is_array($args)) {
  450. trigger_error(
  451. "Cannot $task as \$args parameter is not an array",
  452. E_USER_ERROR);
  453. }
  454. }
  455. /**
  456. * Triggers a PHP error if the method is not part
  457. * of this object.
  458. * @param string $method Name of method.
  459. * @param string $task Description of task attempt.
  460. * @access protected
  461. */
  462. function _dieOnNoMethod($method, $task) {
  463. if ($this->_is_strict && ! method_exists($this, $method)) {
  464. trigger_error(
  465. "Cannot $task as no ${method}() in class " . get_class($this),
  466. E_USER_ERROR);
  467. }
  468. }
  469. /**
  470. * Replaces wildcard matches with wildcard
  471. * expectations in the argument list.
  472. * @param array $args Raw argument list.
  473. * @return array Argument list with
  474. * expectations.
  475. * @access private
  476. */
  477. function _replaceWildcards($args) {
  478. if ($args === false) {
  479. return false;
  480. }
  481. for ($i = 0; $i < count($args); $i++) {
  482. if ($args[$i] === $this->_wildcard) {
  483. $args[$i] = new AnythingExpectation();
  484. }
  485. }
  486. return $args;
  487. }
  488. /**
  489. * Adds one to the call count of a method.
  490. * @param string $method Method called.
  491. * @param array $args Arguments as an array.
  492. * @access protected
  493. */
  494. function _addCall($method, $args) {
  495. if (!isset($this->_call_counts[$method])) {
  496. $this->_call_counts[$method] = 0;
  497. }
  498. $this->_call_counts[$method]++;
  499. }
  500. /**
  501. * Fetches the call count of a method so far.
  502. * @param string $method Method name called.
  503. * @return Number of calls so far.
  504. * @access public
  505. */
  506. function getCallCount($method) {
  507. $this->_dieOnNoMethod($method, "get call count");
  508. $method = strtolower($method);
  509. if (! isset($this->_call_counts[$method])) {
  510. return 0;
  511. }
  512. return $this->_call_counts[$method];
  513. }
  514. /**
  515. * Sets a return for a parameter list that will
  516. * be passed by value for all calls to this method.
  517. * @param string $method Method name.
  518. * @param mixed $value Result of call passed by value.
  519. * @param array $args List of parameters to match
  520. * including wildcards.
  521. * @access public
  522. */
  523. function setReturnValue($method, $value, $args = false) {
  524. $this->_dieOnNoMethod($method, "set return value");
  525. $args = $this->_replaceWildcards($args);
  526. $method = strtolower($method);
  527. if (! isset($this->_returns[$method])) {
  528. $this->_returns[$method] = new CallMap();
  529. }
  530. $this->_returns[$method]->addValue($args, $value);
  531. }
  532. /**
  533. * Sets a return for a parameter list that will
  534. * be passed by value only when the required call count
  535. * is reached.
  536. * @param integer $timing Number of calls in the future
  537. * to which the result applies. If
  538. * not set then all calls will return
  539. * the value.
  540. * @param string $method Method name.
  541. * @param mixed $value Result of call passed by value.
  542. * @param array $args List of parameters to match
  543. * including wildcards.
  544. * @access public
  545. */
  546. function setReturnValueAt($timing, $method, $value, $args = false) {
  547. $this->_dieOnNoMethod($method, "set return value sequence");
  548. $args = $this->_replaceWildcards($args);
  549. $method = strtolower($method);
  550. if (! isset($this->_return_sequence[$method])) {
  551. $this->_return_sequence[$method] = array();
  552. }
  553. if (! isset($this->_return_sequence[$method][$timing])) {
  554. $this->_return_sequence[$method][$timing] = new CallMap();
  555. }
  556. $this->_return_sequence[$method][$timing]->addValue($args, $value);
  557. }
  558. /**
  559. * Sets a return for a parameter list that will
  560. * be passed by reference for all calls.
  561. * @param string $method Method name.
  562. * @param mixed $reference Result of the call will be this object.
  563. * @param array $args List of parameters to match
  564. * including wildcards.
  565. * @access public
  566. */
  567. function setReturnReference($method, &$reference, $args = false) {
  568. $this->_dieOnNoMethod($method, "set return reference");
  569. $args = $this->_replaceWildcards($args);
  570. $method = strtolower($method);
  571. if (! isset($this->_returns[$method])) {
  572. $this->_returns[$method] = new CallMap();
  573. }
  574. $this->_returns[$method]->addReference($args, $reference);
  575. }
  576. /**
  577. * Sets a return for a parameter list that will
  578. * be passed by value only when the required call count
  579. * is reached.
  580. * @param integer $timing Number of calls in the future
  581. * to which the result applies. If
  582. * not set then all calls will return
  583. * the value.
  584. * @param string $method Method name.
  585. * @param mixed $reference Result of the call will be this object.
  586. * @param array $args List of parameters to match
  587. * including wildcards.
  588. * @access public
  589. */
  590. function setReturnReferenceAt($timing, $method, &$reference, $args = false) {
  591. $this->_dieOnNoMethod($method, "set return reference sequence");
  592. $args = $this->_replaceWildcards($args);
  593. $method = strtolower($method);
  594. if (! isset($this->_return_sequence[$method])) {
  595. $this->_return_sequence[$method] = array();
  596. }
  597. if (! isset($this->_return_sequence[$method][$timing])) {
  598. $this->_return_sequence[$method][$timing] = new CallMap();
  599. }
  600. $this->_return_sequence[$method][$timing]->addReference($args, $reference);
  601. }
  602. /**
  603. * Sets up an expected call with a set of
  604. * expected parameters in that call. All
  605. * calls will be compared to these expectations
  606. * regardless of when the call is made.
  607. * @param string $method Method call to test.
  608. * @param array $args Expected parameters for the call
  609. * including wildcards.
  610. * @param string $message Overridden message.
  611. * @access public
  612. */
  613. function expectArguments($method, $args, $message = '%s') {
  614. $this->_dieOnNoMethod($method, 'set expected arguments');
  615. $this->_checkArgumentsIsArray($args, 'set expected arguments');
  616. $args = $this->_replaceWildcards($args);
  617. $message .= Mock::getExpectationLine(' at line [%d]');
  618. $this->_expected_args[strtolower($method)] =
  619. new ParametersExpectation($args, $message);
  620. }
  621. /**
  622. * Sets up an expected call with a set of
  623. * expected parameters in that call. The
  624. * expected call count will be adjusted if it
  625. * is set too low to reach this call.
  626. * @param integer $timing Number of calls in the future at
  627. * which to test. Next call is 0.
  628. * @param string $method Method call to test.
  629. * @param array $args Expected parameters for the call
  630. * including wildcards.
  631. * @param string $message Overridden message.
  632. * @access public
  633. */
  634. function expectArgumentsAt($timing, $method, $args, $message = '%s') {
  635. $this->_dieOnNoMethod($method, 'set expected arguments at time');
  636. $this->_checkArgumentsIsArray($args, 'set expected arguments at time');
  637. $args = $this->_replaceWildcards($args);
  638. if (! isset($this->_expected_args_at[$timing])) {
  639. $this->_expected_args_at[$timing] = array();
  640. }
  641. $method = strtolower($method);
  642. $message .= Mock::getExpectationLine(' at line [%d]');
  643. $this->_expected_args_at[$timing][$method] =
  644. new ParametersExpectation($args, $message);
  645. }
  646. /**
  647. * Sets an expectation for the number of times
  648. * a method will be called. The tally method
  649. * is used to check this.
  650. * @param string $method Method call to test.
  651. * @param integer $count Number of times it should
  652. * have been called at tally.
  653. * @param string $message Overridden message.
  654. * @access public
  655. */
  656. function expectCallCount($method, $count, $message = '%s') {
  657. $this->_dieOnNoMethod($method, 'set expected call count');
  658. $message .= Mock::getExpectationLine(' at line [%d]');
  659. $this->_expected_counts[strtolower($method)] =
  660. new CallCountExpectation($method, $count, $message);
  661. }
  662. /**
  663. * Sets the number of times a method may be called
  664. * before a test failure is triggered.
  665. * @param string $method Method call to test.
  666. * @param integer $count Most number of times it should
  667. * have been called.
  668. * @param string $message Overridden message.
  669. * @access public
  670. */
  671. function expectMaximumCallCount($method, $count, $message = '%s') {
  672. $this->_dieOnNoMethod($method, 'set maximum call count');
  673. $message .= Mock::getExpectationLine(' at line [%d]');
  674. $this->_max_counts[strtolower($method)] =
  675. new MaximumCallCountExpectation($method, $count, $message);
  676. }
  677. /**
  678. * Sets the number of times to call a method to prevent
  679. * a failure on the tally.
  680. * @param string $method Method call to test.
  681. * @param integer $count Least number of times it should
  682. * have been called.
  683. * @param string $message Overridden message.
  684. * @access public
  685. */
  686. function expectMinimumCallCount($method, $count, $message = '%s') {
  687. $this->_dieOnNoMethod($method, 'set minimum call count');
  688. $message .= Mock::getExpectationLine(' at line [%d]');
  689. $this->_expected_counts[strtolower($method)] =
  690. new MinimumCallCountExpectation($method, $count, $message);
  691. }
  692. /**
  693. * Convenience method for barring a method
  694. * call.
  695. * @param string $method Method call to ban.
  696. * @param string $message Overridden message.
  697. * @access public
  698. */
  699. function expectNever($method, $message = '%s') {
  700. $this->expectMaximumCallCount($method, 0, $message);
  701. }
  702. /**
  703. * Convenience method for a single method
  704. * call.
  705. * @param string $method Method call to track.
  706. * @param array $args Expected argument list or
  707. * false for any arguments.
  708. * @param string $message Overridden message.
  709. * @access public
  710. */
  711. function expectOnce($method, $args = false, $message = '%s') {
  712. $this->expectCallCount($method, 1, $message);
  713. if ($args !== false) {
  714. $this->expectArguments($method, $args, $message);
  715. }
  716. }
  717. /**
  718. * Convenience method for requiring a method
  719. * call.
  720. * @param string $method Method call to track.
  721. * @param array $args Expected argument list or
  722. * false for any arguments.
  723. * @param string $message Overridden message.
  724. * @access public
  725. */
  726. function expectAtLeastOnce($method, $args = false, $message = '%s') {
  727. $this->expectMinimumCallCount($method, 1, $message);
  728. if ($args !== false) {
  729. $this->expectArguments($method, $args, $message);
  730. }
  731. }
  732. /**
  733. * @deprecated
  734. */
  735. function tally() {
  736. }
  737. /**
  738. * Receives event from unit test that the current
  739. * test method has finished. Totals up the call
  740. * counts and triggers a test assertion if a test
  741. * is present for expected call counts.
  742. * @param string $method Current method name.
  743. * @access public
  744. */
  745. function atTestEnd($method) {
  746. foreach ($this->_expected_counts as $method => $expectation) {
  747. $this->_assertTrue(
  748. $expectation->test($this->getCallCount($method)),
  749. $expectation->overlayMessage($this->getCallCount($method)));
  750. }
  751. foreach ($this->_max_counts as $method => $expectation) {
  752. if ($expectation->test($this->getCallCount($method))) {
  753. $this->_assertTrue(
  754. true,
  755. $expectation->overlayMessage($this->getCallCount($method)));
  756. }
  757. }
  758. }
  759. /**
  760. * Returns the expected value for the method name
  761. * and checks expectations. Will generate any
  762. * test assertions as a result of expectations
  763. * if there is a test present.
  764. * @param string $method Name of method to simulate.
  765. * @param array $args Arguments as an array.
  766. * @return mixed Stored return.
  767. * @access private
  768. */
  769. function &_invoke($method, $args) {
  770. $method = strtolower($method);
  771. $step = $this->getCallCount($method);
  772. $this->_addCall($method, $args);
  773. $this->_checkExpectations($method, $args, $step);
  774. $result = &$this->_getReturn($method, $args, $step);
  775. return $result;
  776. }
  777. /**
  778. * Finds the return value matching the incoming
  779. * arguments. If there is no matching value found
  780. * then an error is triggered.
  781. * @param string $method Method name.
  782. * @param array $args Calling arguments.
  783. * @param integer $step Current position in the
  784. * call history.
  785. * @return mixed Stored return.
  786. * @access protected
  787. */
  788. function &_getReturn($method, $args, $step) {
  789. if (isset($this->_return_sequence[$method][$step])) {
  790. if ($this->_return_sequence[$method][$step]->isMatch($args)) {
  791. $result = &$this->_return_sequence[$method][$step]->findFirstMatch($args);
  792. return $result;
  793. }
  794. }
  795. if (isset($this->_returns[$method])) {
  796. $result = &$this->_returns[$method]->findFirstMatch($args);
  797. return $result;
  798. }
  799. $null = null;
  800. return $null;
  801. }
  802. /**
  803. * Tests the arguments against expectations.
  804. * @param string $method Method to check.
  805. * @param array $args Argument list to match.
  806. * @param integer $timing The position of this call
  807. * in the call history.
  808. * @access private
  809. */
  810. function _checkExpectations($method, $args, $timing) {
  811. if (isset($this->_max_counts[$method])) {
  812. if (! $this->_max_counts[$method]->test($timing + 1)) {
  813. $this->_assertTrue(
  814. false,
  815. $this->_max_counts[$method]->overlayMessage($timing + 1));
  816. }
  817. }
  818. if (isset($this->_expected_args_at[$timing][$method])) {
  819. $this->_assertTrue(
  820. $this->_expected_args_at[$timing][$method]->test($args),
  821. "Mock method [$method] at [$timing] -> " .
  822. $this->_expected_args_at[$timing][$method]->overlayMessage($args));
  823. } elseif (isset($this->_expected_args[$method])) {
  824. $this->_assertTrue(
  825. $this->_expected_args[$method]->test($args),
  826. "Mock method [$method] -> " . $this->_expected_args[$method]->overlayMessage($args));
  827. }
  828. }
  829. /**
  830. * Triggers an assertion on the held test case.
  831. * Should be overridden when using another test
  832. * framework other than the SimpleTest one if the
  833. * assertion method has a different name.
  834. * @param boolean $assertion True will pass.
  835. * @param string $message Message that will go with
  836. * the test event.
  837. * @access protected
  838. */
  839. function _assertTrue($assertion, $message) {
  840. $test = &$this->_getCurrentTestCase();
  841. $test->assertTrue($assertion, $message);
  842. }
  843. }
  844. /**
  845. * Static methods only service class for code generation of
  846. * mock objects.
  847. * @package SimpleTest
  848. * @subpackage MockObjects
  849. */
  850. class Mock {
  851. /**
  852. * Factory for mock object classes.
  853. * @access public
  854. */
  855. function Mock() {
  856. trigger_error('Mock factory methods are class only.');
  857. }
  858. /**
  859. * Clones a class' interface and creates a mock version
  860. * that can have return values and expectations set.
  861. * @param string $class Class to clone.
  862. * @param string $mock_class New class name. Default is
  863. * the old name with "Mock"
  864. * prepended.
  865. * @param array $methods Additional methods to add beyond
  866. * those in th cloned class. Use this
  867. * to emulate the dynamic addition of
  868. * methods in the cloned class or when
  869. * the class hasn't been written yet.
  870. * @static
  871. * @access public
  872. */
  873. function generate($class, $mock_class = false, $methods = false) {
  874. $generator = new MockGenerator($class, $mock_class);
  875. return $generator->generate($methods);
  876. }
  877. /**
  878. * Generates a version of a class with selected
  879. * methods mocked only. Inherits the old class
  880. * and chains the mock methods of an aggregated
  881. * mock object.
  882. * @param string $class Class to clone.
  883. * @param string $mock_class New class name.
  884. * @param array $methods Methods to be overridden
  885. * with mock versions.
  886. * @static
  887. * @access public
  888. */
  889. function generatePartial($class, $mock_class, $methods) {
  890. $generator = new MockGenerator($class, $mock_class);
  891. return $generator->generatePartial($methods);
  892. }
  893. /**
  894. * Uses a stack trace to find the line of an assertion.
  895. * @param string $format String formatting.
  896. * @param array $stack Stack frames top most first. Only
  897. * needed if not using the PHP
  898. * backtrace function.
  899. * @return string Line number of first expect*
  900. * method embedded in format string.
  901. * @access public
  902. * @static
  903. */
  904. function getExpectationLine($format = '%d', $stack = false) {
  905. if ($stack === false) {
  906. $stack = SimpleTestCompatibility::getStackTrace();
  907. }
  908. return SimpleDumper::getFormattedAssertionLine($stack, $format, 'expect');
  909. }
  910. }
  911. /**
  912. * @deprecated
  913. */
  914. class Stub extends Mock {
  915. }
  916. /**
  917. * Service class for code generation of mock objects.
  918. * @package SimpleTest
  919. * @subpackage MockObjects
  920. */
  921. class MockGenerator {
  922. var $_class;
  923. var $_mock_class;
  924. var $_mock_base;
  925. var $_reflection;
  926. function MockGenerator($class, $mock_class) {
  927. $this->_class = $class;
  928. $this->_mock_class = $mock_class;
  929. $this->_mock_base = SimpleTest::getMockBaseClass();
  930. $this->_reflection = new SimpleReflection($this->_class);
  931. }
  932. /**
  933. * Clones a class' interface and creates a mock version
  934. * that can have return values and expectations set.
  935. * @param array $methods Additional methods to add beyond
  936. * those in th cloned class. Use this
  937. * to emulate the dynamic addition of
  938. * methods in the cloned class or when
  939. * the class hasn't been written yet.
  940. * @access public
  941. */
  942. function generate($methods) {
  943. if (! $this->_reflection->classOrInterfaceExists()) {
  944. return false;
  945. }
  946. if (! $this->_mock_class) {
  947. $this->_mock_class = 'Mock' . $this->_class;
  948. }
  949. $mock_reflection = new SimpleReflection($this->_mock_class);
  950. if ($mock_reflection->classExistsSansAutoload()) {
  951. return false;
  952. }
  953. return eval(
  954. $this->_createClassCode($methods ? $methods : array()) .
  955. " return true;");
  956. }
  957. /**
  958. * Generates a version of a class with selected
  959. * methods mocked only. Inherits the old class
  960. * and chains the mock methods of an aggregated
  961. * mock object.
  962. * @param array $methods Methods to be overridden
  963. * with mock versions.
  964. * @access public
  965. */
  966. function generatePartial($methods) {
  967. if (! $this->_reflection->classExists($this->_class)) {
  968. return false;
  969. }
  970. $mock_reflection = new SimpleReflection($this->_mock_class);
  971. if ($mock_reflection->classExistsSansAutoload()) {
  972. trigger_error("Partial mock class [$mock_class] already exists");
  973. return false;
  974. }
  975. return eval($this->_extendClassCode($methods));
  976. }
  977. /**
  978. * The new mock class code as a string.
  979. * @param array $methods Additional methods.
  980. * @return string Code for new mock class.
  981. * @access private
  982. */
  983. function _createClassCode($methods) {
  984. $implements = '';
  985. $interfaces = $this->_reflection->getInterfaces();
  986. if (count($interfaces) > 0) {
  987. $implements = 'implements ' . implode(', ', $interfaces);
  988. }
  989. $code = "class " . $this->_mock_class . " extends " . $this->_mock_base . " $implements {\n";
  990. $code .= " function " . $this->_mock_class . "() {\n";
  991. $code .= " \$this->" . $this->_mock_base . "();\n";
  992. $code .= " }\n";
  993. $code .= $this->_createHandlerCode($methods);
  994. $code .= "}\n";
  995. return $code;
  996. }
  997. /**
  998. * The extension class code as a string. The class
  999. * composites a mock object and chains mocked methods
  1000. * to it.
  1001. * @param array $methods Mocked methods.
  1002. * @return string Code for a new class.
  1003. * @access private
  1004. */
  1005. function _extendClassCode($methods) {
  1006. $code = "class " . $this->_mock_class . " extends " . $this->_class . " {\n";
  1007. $code .= " var \$_mock;\n";
  1008. $code .= $this->_addMethodList($methods);
  1009. $code .= "\n";
  1010. $code .= " function " . $this->_mock_class . "() {\n";
  1011. $code .= " \$this->_mock = &new " . $this->_mock_base . "();\n";
  1012. $code .= " \$this->_mock->disableExpectationNameChecks();\n";
  1013. $code .= " }\n";
  1014. $code .= $this->_chainMockReturns();
  1015. $code .= $this->_chainMockExpectations();
  1016. $code .= $this->_overrideMethods($methods);
  1017. $code .= "}\n";
  1018. return $code;
  1019. }
  1020. /**
  1021. * Creates code within a class to generate replaced
  1022. * methods. All methods call the _invoke() handler
  1023. * with the method name and the arguments in an
  1024. * array.
  1025. * @param array $methods Additional methods.
  1026. * @access private
  1027. */
  1028. function _createHandlerCode($methods) {
  1029. $code = '';
  1030. $methods = array_merge($methods, $this->_reflection->getMethods());
  1031. foreach ($methods as $method) {
  1032. if ($this->_isConstructor($method)) {
  1033. continue;
  1034. }
  1035. $mock_reflection = new SimpleReflection($this->_mock_base);
  1036. if (in_array($method, $mock_reflection->getMethods())) {
  1037. continue;
  1038. }
  1039. $code .= " " . $this->_reflection->getSignature($method) . " {\n";
  1040. $code .= " \$args = func_get_args();\n";
  1041. $code .= " \$result = &\$this->_invoke(\"$method\", \$args);\n";
  1042. $code .= " return \$result;\n";
  1043. $code .= " }\n";
  1044. }
  1045. return $code;
  1046. }
  1047. /**
  1048. * Tests to see if a special PHP method is about to
  1049. * be stubbed by mistake.
  1050. * @param string $method Method name.
  1051. * @return boolean True if special.
  1052. * @access private
  1053. */
  1054. function _isConstructor($method) {
  1055. return in_array(
  1056. strtolower($method),
  1057. array('__construct', '__destruct', '__clone'));
  1058. }
  1059. /**
  1060. * Creates a list of mocked methods for error checking.
  1061. * @param array $methods Mocked methods.
  1062. * @return string Code for a method list.
  1063. * @access private
  1064. */
  1065. function _addMethodList($methods) {
  1066. return " var \$_mocked_methods = array('" . implode("', '", $methods) . "');\n";
  1067. }
  1068. /**
  1069. * Creates code to abandon the expectation if not mocked.
  1070. * @param string $alias Parameter name of method name.
  1071. * @return string Code for bail out.
  1072. * @access private
  1073. */
  1074. function _bailOutIfNotMocked($alias) {
  1075. $code = " if (! in_array($alias, \$this->_mocked_methods)) {\n";
  1076. $code .= " trigger_error(\"Method [$alias] is not mocked\");\n";
  1077. $code .= " \$null = null;\n";
  1078. $code .= " return \$null;\n";
  1079. $code .= " }\n";
  1080. return $code;
  1081. }
  1082. /**
  1083. * Creates source code for chaining to the composited
  1084. * mock object.
  1085. * @return string Code for mock set up.
  1086. * @access private
  1087. */
  1088. function _chainMockReturns() {
  1089. $code = " function setReturnValue(\$method, \$value, \$args = false) {\n";
  1090. $code .= $this->_bailOutIfNotMocked("\$method");
  1091. $code .= " \$this->_mock->setReturnValue(\$method, \$value, \$args);\n";
  1092. $code .= " }\n";
  1093. $code .= " function setReturnValueAt(\$timing, \$method, \$value, \$args = false) {\n";
  1094. $code .= $this->_bailOutIfNotMocked("\$method");
  1095. $code .= " \$this->_mock->setReturnValueAt(\$timing, \$method, \$value, \$args);\n";
  1096. $code .= " }\n";
  1097. $code .= " function setReturnReference(\$method, &\$ref, \$args = false) {\n";
  1098. $code .= $this->_bailOutIfNotMocked("\$method");
  1099. $code .= " \$this->_mock->setReturnReference(\$method, \$ref, \$args);\n";
  1100. $code .= " }\n";
  1101. $code .= " function setReturnReferenceAt(\$timing, \$method, &\$ref, \$args = false) {\n";
  1102. $code .= $this->_bailOutIfNotMocked("\$method");
  1103. $code .= " \$this->_mock->setReturnReferenceAt(\$timing, \$method, \$ref, \$args);\n";
  1104. $code .= " }\n";
  1105. return $code;
  1106. }
  1107. /**
  1108. * Creates source code for chaining to an aggregated
  1109. * mock object.
  1110. * @return string Code for expectations.
  1111. * @access private
  1112. */
  1113. function _chainMockExpectations() {
  1114. $code = " function expectArguments(\$method, \$args = false) {\n";
  1115. $code .= $this->_bailOutIfNotMocked("\$method");
  1116. $code .= " \$this->_mock->expectArguments(\$method, \$args);\n";
  1117. $code .= " }\n";
  1118. $code .= " function expectArgumentsAt(\$timing, \$method, \$args = false) {\n";
  1119. $code .= $this->_bailOutIfNotMocked("\$method");
  1120. $code .= " \$this->_mock->expectArgumentsAt(\$timing, \$method, \$args);\n";
  1121. $code .= " }\n";
  1122. $code .= " function expectCallCount(\$method, \$count) {\n";
  1123. $code .= $this->_bailOutIfNotMocked("\$method");
  1124. $code .= " \$this->_mock->expectCallCount(\$method, \$count);\n";
  1125. $code .= " }\n";
  1126. $code .= " function expectMaximumCallCount(\$method, \$count) {\n";
  1127. $code .= $this->_bailOutIfNotMocked("\$method");
  1128. $code .= " \$this->_mock->expectMaximumCallCount(\$method, \$count);\n";
  1129. $code .= " }\n";
  1130. $code .= " function expectMinimumCallCount(\$method, \$count) {\n";
  1131. $code .= $this->_bailOutIfNotMocked("\$method");
  1132. $code .= " \$this->_mock->expectMinimumCallCount(\$method, \$count);\n";
  1133. $code .= " }\n";
  1134. $code .= " function expectNever(\$method) {\n";
  1135. $code .= $this->_bailOutIfNotMocked("\$method");
  1136. $code .= " \$this->_mock->expectNever(\$method);\n";
  1137. $code .= " }\n";
  1138. $code .= " function expectOnce(\$method, \$args = false) {\n";
  1139. $code .= $this->_bailOutIfNotMocked("\$method");
  1140. $code .= " \$this-…

Large files files are truncated, but you can click here to view the full file