InvocationProxy.php 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/ExpectationProvider.php';
  15. //require 'Yay/InvocationRecorder.php';
  16. //require 'Yay/MockObject.php';
  17. //require 'Yay/SimpleInvocation.php';
  18. /**
  19. * Proxies Invocations on a Mock object to the recorder.
  20. * @author Chris Corbyn <chris@w3style.co.uk>
  21. * @package Yay
  22. */
  23. class Yay_InvocationProxy implements Yay_ExpectationProvider
  24. {
  25. /**
  26. * The InvocationRecorder which is using this InvocationProxy.
  27. * @var Yay_InvocationRecorder
  28. * @access private
  29. */
  30. private $_recorder;
  31. /**
  32. * The Mock object where Invocations are recorded from.
  33. * @var Yay_MockObject
  34. * @access private
  35. */
  36. private $_mock;
  37. /**
  38. * Create a new InvocationProxy for $recorder and $mock.
  39. * @param Yay_InvocationRecorder $recorder
  40. * @param Yay_MockObject $mock
  41. */
  42. public function __construct(Yay_InvocationRecorder $recorder, Yay_MockObject $mock)
  43. {
  44. $this->_recorder = $recorder;
  45. $this->_mock = $mock;
  46. }
  47. /**
  48. * Direct all invocations to the recorder.
  49. * @param string $method
  50. * @param array $args
  51. * @return Yay_InvocationRecorder
  52. */
  53. public function __call($method, $args)
  54. {
  55. if (is_callable(array($this->_mock, $method)))
  56. {
  57. $invocation = new Yay_SimpleInvocation($this->_mock, $method, $args);
  58. $this->_recorder->recordInvocation($invocation);
  59. return $this->_recorder;
  60. }
  61. elseif (is_callable(array($this->_recorder, $method)))
  62. {
  63. return call_user_func_array(array($this->_recorder, $method), $args);
  64. }
  65. else
  66. {
  67. throw new BadMethodCallException('Mock method ' . $method . ' does not exist');
  68. }
  69. }
  70. /**
  71. * Returns the Expectation list.
  72. * @return array of Yay_Expectation
  73. */
  74. public function getExpectations()
  75. {
  76. return $this->__call(__FUNCTION__, array());
  77. }
  78. }