Chris@14
|
1 <?php
|
Chris@14
|
2 /*
|
Chris@14
|
3 * This file is part of the phpunit-mock-objects package.
|
Chris@14
|
4 *
|
Chris@14
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@14
|
6 *
|
Chris@14
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
8 * file that was distributed with this source code.
|
Chris@14
|
9 */
|
Chris@14
|
10 namespace PHPUnit\Framework\MockObject\Matcher;
|
Chris@14
|
11
|
Chris@14
|
12 use PHPUnit\Framework\Constraint\Constraint;
|
Chris@14
|
13 use PHPUnit\Framework\Constraint\IsEqual;
|
Chris@14
|
14 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
|
Chris@14
|
15 use PHPUnit\Util\InvalidArgumentHelper;
|
Chris@14
|
16
|
Chris@14
|
17 /**
|
Chris@14
|
18 * Invocation matcher which looks for a specific method name in the invocations.
|
Chris@14
|
19 *
|
Chris@14
|
20 * Checks the method name all incoming invocations, the name is checked against
|
Chris@14
|
21 * the defined constraint $constraint. If the constraint is met it will return
|
Chris@14
|
22 * true in matches().
|
Chris@14
|
23 */
|
Chris@14
|
24 class MethodName extends StatelessInvocation
|
Chris@14
|
25 {
|
Chris@14
|
26 /**
|
Chris@14
|
27 * @var Constraint
|
Chris@14
|
28 */
|
Chris@14
|
29 private $constraint;
|
Chris@14
|
30
|
Chris@14
|
31 /**
|
Chris@14
|
32 * @param Constraint|string
|
Chris@14
|
33 *
|
Chris@14
|
34 * @throws Constraint
|
Chris@14
|
35 * @throws \PHPUnit\Framework\Exception
|
Chris@14
|
36 */
|
Chris@14
|
37 public function __construct($constraint)
|
Chris@14
|
38 {
|
Chris@14
|
39 if (!$constraint instanceof Constraint) {
|
Chris@14
|
40 if (!\is_string($constraint)) {
|
Chris@14
|
41 throw InvalidArgumentHelper::factory(1, 'string');
|
Chris@14
|
42 }
|
Chris@14
|
43
|
Chris@14
|
44 $constraint = new IsEqual(
|
Chris@14
|
45 $constraint,
|
Chris@14
|
46 0,
|
Chris@14
|
47 10,
|
Chris@14
|
48 false,
|
Chris@14
|
49 true
|
Chris@14
|
50 );
|
Chris@14
|
51 }
|
Chris@14
|
52
|
Chris@14
|
53 $this->constraint = $constraint;
|
Chris@14
|
54 }
|
Chris@14
|
55
|
Chris@14
|
56 /**
|
Chris@14
|
57 * @return string
|
Chris@14
|
58 */
|
Chris@14
|
59 public function toString()
|
Chris@14
|
60 {
|
Chris@14
|
61 return 'method name ' . $this->constraint->toString();
|
Chris@14
|
62 }
|
Chris@14
|
63
|
Chris@14
|
64 /**
|
Chris@14
|
65 * @param BaseInvocation $invocation
|
Chris@14
|
66 *
|
Chris@14
|
67 * @return bool
|
Chris@14
|
68 */
|
Chris@14
|
69 public function matches(BaseInvocation $invocation)
|
Chris@14
|
70 {
|
Chris@14
|
71 return $this->constraint->evaluate($invocation->getMethodName(), '', true);
|
Chris@14
|
72 }
|
Chris@14
|
73 }
|