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\ExpectationFailedException;
|
Chris@14
|
13 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
|
Chris@14
|
14
|
Chris@14
|
15 /**
|
Chris@14
|
16 * Invocation matcher which checks if a method was invoked at a certain index.
|
Chris@14
|
17 *
|
Chris@14
|
18 * If the expected index number does not match the current invocation index it
|
Chris@14
|
19 * will not match which means it skips all method and parameter matching. Only
|
Chris@14
|
20 * once the index is reached will the method and parameter start matching and
|
Chris@14
|
21 * verifying.
|
Chris@14
|
22 *
|
Chris@14
|
23 * If the index is never reached it will throw an exception in index.
|
Chris@14
|
24 */
|
Chris@14
|
25 class InvokedAtIndex implements Invocation
|
Chris@14
|
26 {
|
Chris@14
|
27 /**
|
Chris@14
|
28 * @var int
|
Chris@14
|
29 */
|
Chris@14
|
30 private $sequenceIndex;
|
Chris@14
|
31
|
Chris@14
|
32 /**
|
Chris@14
|
33 * @var int
|
Chris@14
|
34 */
|
Chris@14
|
35 private $currentIndex = -1;
|
Chris@14
|
36
|
Chris@14
|
37 /**
|
Chris@14
|
38 * @param int $sequenceIndex
|
Chris@14
|
39 */
|
Chris@14
|
40 public function __construct($sequenceIndex)
|
Chris@14
|
41 {
|
Chris@14
|
42 $this->sequenceIndex = $sequenceIndex;
|
Chris@14
|
43 }
|
Chris@14
|
44
|
Chris@14
|
45 /**
|
Chris@14
|
46 * @return string
|
Chris@14
|
47 */
|
Chris@14
|
48 public function toString()
|
Chris@14
|
49 {
|
Chris@14
|
50 return 'invoked at sequence index ' . $this->sequenceIndex;
|
Chris@14
|
51 }
|
Chris@14
|
52
|
Chris@14
|
53 /**
|
Chris@14
|
54 * @param BaseInvocation $invocation
|
Chris@14
|
55 *
|
Chris@14
|
56 * @return bool
|
Chris@14
|
57 */
|
Chris@14
|
58 public function matches(BaseInvocation $invocation)
|
Chris@14
|
59 {
|
Chris@14
|
60 $this->currentIndex++;
|
Chris@14
|
61
|
Chris@14
|
62 return $this->currentIndex == $this->sequenceIndex;
|
Chris@14
|
63 }
|
Chris@14
|
64
|
Chris@14
|
65 /**
|
Chris@14
|
66 * @param BaseInvocation $invocation
|
Chris@14
|
67 */
|
Chris@14
|
68 public function invoked(BaseInvocation $invocation)
|
Chris@14
|
69 {
|
Chris@14
|
70 }
|
Chris@14
|
71
|
Chris@14
|
72 /**
|
Chris@14
|
73 * Verifies that the current expectation is valid. If everything is OK the
|
Chris@14
|
74 * code should just return, if not it must throw an exception.
|
Chris@14
|
75 *
|
Chris@14
|
76 * @throws ExpectationFailedException
|
Chris@14
|
77 */
|
Chris@14
|
78 public function verify()
|
Chris@14
|
79 {
|
Chris@14
|
80 if ($this->currentIndex < $this->sequenceIndex) {
|
Chris@14
|
81 throw new ExpectationFailedException(
|
Chris@14
|
82 \sprintf(
|
Chris@14
|
83 'The expected invocation at index %s was never reached.',
|
Chris@14
|
84 $this->sequenceIndex
|
Chris@14
|
85 )
|
Chris@14
|
86 );
|
Chris@14
|
87 }
|
Chris@14
|
88 }
|
Chris@14
|
89 }
|