1<?php
2/*
3 * This file is part of the PHPUnit_MockObject 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/**
12 * Invocation matcher which looks for a specific method name in the invocations.
13 *
14 * Checks the method name all incoming invocations, the name is checked against
15 * the defined constraint $constraint. If the constraint is met it will return
16 * true in matches().
17 *
18 * @since Class available since Release 1.0.0
19 */
20class PHPUnit_Framework_MockObject_Matcher_MethodName extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
21{
22    /**
23     * @var PHPUnit_Framework_Constraint
24     */
25    protected $constraint;
26
27    /**
28     * @param  PHPUnit_Framework_Constraint|string
29     *
30     * @throws PHPUnit_Framework_Constraint
31     */
32    public function __construct($constraint)
33    {
34        if (!$constraint instanceof PHPUnit_Framework_Constraint) {
35            if (!is_string($constraint)) {
36                throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
37            }
38
39            $constraint = new PHPUnit_Framework_Constraint_IsEqual(
40                $constraint,
41                0,
42                10,
43                false,
44                true
45            );
46        }
47
48        $this->constraint = $constraint;
49    }
50
51    /**
52     * @return string
53     */
54    public function toString()
55    {
56        return 'method name ' . $this->constraint->toString();
57    }
58
59    /**
60     * @param PHPUnit_Framework_MockObject_Invocation $invocation
61     *
62     * @return bool
63     */
64    public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
65    {
66        return $this->constraint->evaluate($invocation->methodName, '', true);
67    }
68}
69