Matcher.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. namespace PHPUnit\Framework\MockObject;
  11. use PHPUnit\Framework\ExpectationFailedException;
  12. use PHPUnit\Framework\MockObject\Matcher\AnyInvokedCount;
  13. use PHPUnit\Framework\MockObject\Matcher\AnyParameters;
  14. use PHPUnit\Framework\MockObject\Matcher\Invocation as MatcherInvocation;
  15. use PHPUnit\Framework\MockObject\Matcher\InvokedCount;
  16. use PHPUnit\Framework\MockObject\Matcher\MethodName;
  17. use PHPUnit\Framework\MockObject\Matcher\Parameters;
  18. use PHPUnit\Framework\TestFailure;
  19. /**
  20. * Main matcher which defines a full expectation using method, parameter and
  21. * invocation matchers.
  22. * This matcher encapsulates all the other matchers and allows the builder to
  23. * set the specific matchers when the appropriate methods are called (once(),
  24. * where() etc.).
  25. *
  26. * All properties are public so that they can easily be accessed by the builder.
  27. */
  28. class Matcher implements MatcherInvocation
  29. {
  30. /**
  31. * @var MatcherInvocation
  32. */
  33. private $invocationMatcher;
  34. /**
  35. * @var mixed
  36. */
  37. private $afterMatchBuilderId = null;
  38. /**
  39. * @var bool
  40. */
  41. private $afterMatchBuilderIsInvoked = false;
  42. /**
  43. * @var MethodName
  44. */
  45. private $methodNameMatcher = null;
  46. /**
  47. * @var Parameters
  48. */
  49. private $parametersMatcher = null;
  50. /**
  51. * @var Stub
  52. */
  53. private $stub = null;
  54. /**
  55. * @param MatcherInvocation $invocationMatcher
  56. */
  57. public function __construct(MatcherInvocation $invocationMatcher)
  58. {
  59. $this->invocationMatcher = $invocationMatcher;
  60. }
  61. public function hasMatchers(): bool
  62. {
  63. return $this->invocationMatcher !== null && !$this->invocationMatcher instanceof AnyInvokedCount;
  64. }
  65. public function hasMethodNameMatcher(): bool
  66. {
  67. return $this->methodNameMatcher !== null;
  68. }
  69. public function getMethodNameMatcher(): MethodName
  70. {
  71. return $this->methodNameMatcher;
  72. }
  73. public function setMethodNameMatcher(MethodName $matcher)
  74. {
  75. $this->methodNameMatcher = $matcher;
  76. }
  77. public function hasParametersMatcher(): bool
  78. {
  79. return $this->parametersMatcher !== null;
  80. }
  81. public function getParametersMatcher(): Parameters
  82. {
  83. return $this->parametersMatcher;
  84. }
  85. public function setParametersMatcher($matcher)
  86. {
  87. $this->parametersMatcher = $matcher;
  88. }
  89. public function setStub($stub)
  90. {
  91. $this->stub = $stub;
  92. }
  93. public function setAfterMatchBuilderId($id)
  94. {
  95. $this->afterMatchBuilderId = $id;
  96. }
  97. /**
  98. * @param Invocation $invocation
  99. *
  100. * @return mixed
  101. *
  102. * @throws \Exception
  103. * @throws RuntimeException
  104. * @throws ExpectationFailedException
  105. */
  106. public function invoked(Invocation $invocation)
  107. {
  108. if ($this->invocationMatcher === null) {
  109. throw new RuntimeException(
  110. 'No invocation matcher is set'
  111. );
  112. }
  113. if ($this->methodNameMatcher === null) {
  114. throw new RuntimeException('No method matcher is set');
  115. }
  116. if ($this->afterMatchBuilderId !== null) {
  117. $builder = $invocation->getObject()
  118. ->__phpunit_getInvocationMocker()
  119. ->lookupId($this->afterMatchBuilderId);
  120. if (!$builder) {
  121. throw new RuntimeException(
  122. \sprintf(
  123. 'No builder found for match builder identification <%s>',
  124. $this->afterMatchBuilderId
  125. )
  126. );
  127. }
  128. $matcher = $builder->getMatcher();
  129. if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
  130. $this->afterMatchBuilderIsInvoked = true;
  131. }
  132. }
  133. $this->invocationMatcher->invoked($invocation);
  134. try {
  135. if ($this->parametersMatcher !== null &&
  136. !$this->parametersMatcher->matches($invocation)) {
  137. $this->parametersMatcher->verify();
  138. }
  139. } catch (ExpectationFailedException $e) {
  140. throw new ExpectationFailedException(
  141. \sprintf(
  142. "Expectation failed for %s when %s\n%s",
  143. $this->methodNameMatcher->toString(),
  144. $this->invocationMatcher->toString(),
  145. $e->getMessage()
  146. ),
  147. $e->getComparisonFailure()
  148. );
  149. }
  150. if ($this->stub) {
  151. return $this->stub->invoke($invocation);
  152. }
  153. return $invocation->generateReturnValue();
  154. }
  155. /**
  156. * @param Invocation $invocation
  157. *
  158. * @return bool
  159. *
  160. * @throws RuntimeException
  161. * @throws ExpectationFailedException
  162. */
  163. public function matches(Invocation $invocation)
  164. {
  165. if ($this->afterMatchBuilderId !== null) {
  166. $builder = $invocation->getObject()
  167. ->__phpunit_getInvocationMocker()
  168. ->lookupId($this->afterMatchBuilderId);
  169. if (!$builder) {
  170. throw new RuntimeException(
  171. \sprintf(
  172. 'No builder found for match builder identification <%s>',
  173. $this->afterMatchBuilderId
  174. )
  175. );
  176. }
  177. $matcher = $builder->getMatcher();
  178. if (!$matcher) {
  179. return false;
  180. }
  181. if (!$matcher->invocationMatcher->hasBeenInvoked()) {
  182. return false;
  183. }
  184. }
  185. if ($this->invocationMatcher === null) {
  186. throw new RuntimeException(
  187. 'No invocation matcher is set'
  188. );
  189. }
  190. if ($this->methodNameMatcher === null) {
  191. throw new RuntimeException('No method matcher is set');
  192. }
  193. if (!$this->invocationMatcher->matches($invocation)) {
  194. return false;
  195. }
  196. try {
  197. if (!$this->methodNameMatcher->matches($invocation)) {
  198. return false;
  199. }
  200. } catch (ExpectationFailedException $e) {
  201. throw new ExpectationFailedException(
  202. \sprintf(
  203. "Expectation failed for %s when %s\n%s",
  204. $this->methodNameMatcher->toString(),
  205. $this->invocationMatcher->toString(),
  206. $e->getMessage()
  207. ),
  208. $e->getComparisonFailure()
  209. );
  210. }
  211. return true;
  212. }
  213. /**
  214. * @throws RuntimeException
  215. * @throws ExpectationFailedException
  216. */
  217. public function verify()
  218. {
  219. if ($this->invocationMatcher === null) {
  220. throw new RuntimeException(
  221. 'No invocation matcher is set'
  222. );
  223. }
  224. if ($this->methodNameMatcher === null) {
  225. throw new RuntimeException('No method matcher is set');
  226. }
  227. try {
  228. $this->invocationMatcher->verify();
  229. if ($this->parametersMatcher === null) {
  230. $this->parametersMatcher = new AnyParameters;
  231. }
  232. $invocationIsAny = $this->invocationMatcher instanceof AnyInvokedCount;
  233. $invocationIsNever = $this->invocationMatcher instanceof InvokedCount && $this->invocationMatcher->isNever();
  234. if (!$invocationIsAny && !$invocationIsNever) {
  235. $this->parametersMatcher->verify();
  236. }
  237. } catch (ExpectationFailedException $e) {
  238. throw new ExpectationFailedException(
  239. \sprintf(
  240. "Expectation failed for %s when %s.\n%s",
  241. $this->methodNameMatcher->toString(),
  242. $this->invocationMatcher->toString(),
  243. TestFailure::exceptionToString($e)
  244. )
  245. );
  246. }
  247. }
  248. /**
  249. * @return string
  250. */
  251. public function toString()
  252. {
  253. $list = [];
  254. if ($this->invocationMatcher !== null) {
  255. $list[] = $this->invocationMatcher->toString();
  256. }
  257. if ($this->methodNameMatcher !== null) {
  258. $list[] = 'where ' . $this->methodNameMatcher->toString();
  259. }
  260. if ($this->parametersMatcher !== null) {
  261. $list[] = 'and ' . $this->parametersMatcher->toString();
  262. }
  263. if ($this->afterMatchBuilderId !== null) {
  264. $list[] = 'after ' . $this->afterMatchBuilderId;
  265. }
  266. if ($this->stub !== null) {
  267. $list[] = 'will ' . $this->stub->toString();
  268. }
  269. return \implode(' ', $list);
  270. }
  271. }