InvocationMocker.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Mocker for invocations which are sent from
  12. * PHPUnit_Framework_MockObject_MockObject objects.
  13. *
  14. * Keeps track of all expectations and stubs as well as registering
  15. * identifications for builders.
  16. */
  17. class PHPUnit_Framework_MockObject_InvocationMocker implements PHPUnit_Framework_MockObject_Stub_MatcherCollection, PHPUnit_Framework_MockObject_Invokable, PHPUnit_Framework_MockObject_Builder_Namespace
  18. {
  19. /**
  20. * @var PHPUnit_Framework_MockObject_Matcher_Invocation[]
  21. */
  22. protected $matchers = [];
  23. /**
  24. * @var PHPUnit_Framework_MockObject_Builder_Match[]
  25. */
  26. protected $builderMap = [];
  27. /**
  28. * @var string[]
  29. */
  30. private $configurableMethods = [];
  31. /**
  32. * @param array $configurableMethods
  33. */
  34. public function __construct(array $configurableMethods)
  35. {
  36. $this->configurableMethods = $configurableMethods;
  37. }
  38. /**
  39. * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
  40. */
  41. public function addMatcher(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
  42. {
  43. $this->matchers[] = $matcher;
  44. }
  45. public function hasMatchers()
  46. {
  47. foreach ($this->matchers as $matcher) {
  48. if ($matcher->hasMatchers()) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * @param mixed $id
  56. *
  57. * @return bool|null
  58. */
  59. public function lookupId($id)
  60. {
  61. if (isset($this->builderMap[$id])) {
  62. return $this->builderMap[$id];
  63. }
  64. return;
  65. }
  66. /**
  67. * @param mixed $id
  68. * @param PHPUnit_Framework_MockObject_Builder_Match $builder
  69. *
  70. * @throws PHPUnit_Framework_MockObject_RuntimeException
  71. */
  72. public function registerId($id, PHPUnit_Framework_MockObject_Builder_Match $builder)
  73. {
  74. if (isset($this->builderMap[$id])) {
  75. throw new PHPUnit_Framework_MockObject_RuntimeException(
  76. 'Match builder with id <' . $id . '> is already registered.'
  77. );
  78. }
  79. $this->builderMap[$id] = $builder;
  80. }
  81. /**
  82. * @param PHPUnit_Framework_MockObject_Matcher_Invocation $matcher
  83. *
  84. * @return PHPUnit_Framework_MockObject_Builder_InvocationMocker
  85. */
  86. public function expects(PHPUnit_Framework_MockObject_Matcher_Invocation $matcher)
  87. {
  88. return new PHPUnit_Framework_MockObject_Builder_InvocationMocker(
  89. $this,
  90. $matcher,
  91. $this->configurableMethods
  92. );
  93. }
  94. /**
  95. * @param PHPUnit_Framework_MockObject_Invocation $invocation
  96. *
  97. * @return mixed
  98. *
  99. * @throws Exception
  100. */
  101. public function invoke(PHPUnit_Framework_MockObject_Invocation $invocation)
  102. {
  103. $exception = null;
  104. $hasReturnValue = false;
  105. $returnValue = null;
  106. foreach ($this->matchers as $match) {
  107. try {
  108. if ($match->matches($invocation)) {
  109. $value = $match->invoked($invocation);
  110. if (!$hasReturnValue) {
  111. $returnValue = $value;
  112. $hasReturnValue = true;
  113. }
  114. }
  115. } catch (Exception $e) {
  116. $exception = $e;
  117. }
  118. }
  119. if ($exception !== null) {
  120. throw $exception;
  121. }
  122. if ($hasReturnValue) {
  123. return $returnValue;
  124. } elseif (strtolower($invocation->methodName) == '__tostring') {
  125. return '';
  126. }
  127. return $invocation->generateReturnValue();
  128. }
  129. /**
  130. * @param PHPUnit_Framework_MockObject_Invocation $invocation
  131. *
  132. * @return bool
  133. */
  134. public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
  135. {
  136. foreach ($this->matchers as $matcher) {
  137. if (!$matcher->matches($invocation)) {
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * @return bool
  145. */
  146. public function verify()
  147. {
  148. foreach ($this->matchers as $matcher) {
  149. $matcher->verify();
  150. }
  151. }
  152. }