mocked_class.tpl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * This file is part of the Lime framework.
  3. *
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. <?php echo $class_declaration ?>
  11. {
  12. private
  13. $class = null,
  14. $state = null,
  15. $output = null,
  16. $behaviour = null,
  17. $stubMethods = true;
  18. public function __construct($class, LimeMockBehaviourInterface $behaviour, LimeOutputInterface $output, $stubMethods = true)
  19. {
  20. $this->class = $class;
  21. $this->behaviour = $behaviour;
  22. $this->output = $output;
  23. $this->stubMethods = $stubMethods;
  24. $this->__lime_reset();
  25. }
  26. public function __call($method, $parameters)
  27. {
  28. try
  29. {
  30. $method = new LimeMockMethod($this->class, $method);
  31. // if $stubMethods is set to FALSE, methods that are not configured are
  32. // passed to the real implementation
  33. if ($this->stubMethods || $this->state->isInvokable($method))
  34. {
  35. return $this->state->invoke($method, $parameters);
  36. }
  37. else if (method_exists($this->class, $method->getMethod()))
  38. {
  39. // THIS METHOD CALL WILL LEAD TO SEGFAULTS WHEN EXECUTED IN A
  40. // WEBSERVER ENVIRONMENT!!!
  41. if (PHP_VERSION_ID < 50300)
  42. {
  43. return call_user_func_array(array($this, 'parent::'.$method->getMethod()), $parameters);
  44. }
  45. else
  46. {
  47. return call_user_func_array('parent::'.$method->getMethod(), $parameters);
  48. }
  49. }
  50. }
  51. catch (LimeMockInvocationException $e)
  52. {
  53. // hide the internal trace to not distract when debugging test errors
  54. throw new LimeMockException($e->getMessage());
  55. }
  56. }
  57. public function __lime_replay()
  58. {
  59. $this->state = new LimeMockReplayState($this->behaviour);
  60. }
  61. public function __lime_reset()
  62. {
  63. $this->behaviour->reset();
  64. if (!$this->state instanceof LimeMockRecordState)
  65. {
  66. $this->state = new LimeMockRecordState($this->behaviour, $this->output);
  67. }
  68. }
  69. public function __lime_getState()
  70. {
  71. return $this->state;
  72. }
  73. <?php if ($generate_controls): ?>
  74. public function replay() { return LimeMock::replay($this); }
  75. public function any($methodName) { return LimeMock::any($this, $methodName); }
  76. public function reset() { return LimeMock::reset($this); }
  77. public function verify() { return LimeMock::verify($this); }
  78. public function setExpectNothing() { return LimeMock::setExpectNothing($this); }
  79. <?php endif ?>
  80. <?php echo $methods ?>
  81. }