|
6 | 6 |
|
7 | 7 | namespace Magento\Framework\TestFramework\Unit\Matcher;
|
8 | 8 |
|
| 9 | +use PHPUnit\Framework\ExpectationFailedException; |
| 10 | +use PHPUnit\Framework\MockObject\Invocation; |
| 11 | + |
9 | 12 | /**
|
10 | 13 | * Class MethodInvokedAtIndex
|
11 | 14 | * Matches invocations per 'method' at 'position'
|
12 |
| - * Example: |
| 15 | + * Example:§ |
13 | 16 | * $mock->expects(new MethodInvokedAtIndex(0))->method('getMethod')->willReturn(1);
|
14 | 17 | * $mock->expects(new MethodInvokedAtIndex(1))->method('getMethod')->willReturn(2);
|
15 | 18 | *
|
|
18 | 21 | *
|
19 | 22 | * @package Magento\TestFramework\Matcher
|
20 | 23 | */
|
21 |
| -class MethodInvokedAtIndex extends \PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex |
| 24 | +class MethodInvokedAtIndex implements \PHPUnit\Framework\MockObject\Matcher\Invocation |
22 | 25 | {
|
23 | 26 | /**
|
24 | 27 | * @var array
|
25 | 28 | */
|
26 | 29 | protected $indexes = [];
|
27 | 30 |
|
28 | 31 | /**
|
29 |
| - * @param \PHPUnit_Framework_MockObject_Invocation $invocation |
| 32 | + * @var int |
| 33 | + */ |
| 34 | + private $currentIndex = -1; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var int |
| 38 | + */ |
| 39 | + private $sequenceIndex; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param int $sequenceIndex |
| 43 | + */ |
| 44 | + public function __construct($sequenceIndex) |
| 45 | + { |
| 46 | + $this->sequenceIndex = $sequenceIndex; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + public function toString(): string |
| 53 | + { |
| 54 | + return 'invoked at sequence index ' . $this->sequenceIndex; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param \PHPUnit\Framework\MockObject\Invocation $invocation |
30 | 59 | * @return boolean
|
31 | 60 | */
|
32 |
| - public function matches(\PHPUnit_Framework_MockObject_Invocation $invocation) |
| 61 | + public function matches(Invocation $invocation): bool |
33 | 62 | {
|
34 | 63 | /** @noinspection PhpUndefinedFieldInspection */
|
35 |
| - if (!isset($this->indexes[$invocation->methodName])) { |
| 64 | + if (!isset($this->indexes[$invocation->getMethodName()])) { |
36 | 65 | /** @noinspection PhpUndefinedFieldInspection */
|
37 |
| - $this->indexes[$invocation->methodName] = 0; |
| 66 | + $this->indexes[$invocation->getMethodName()] = 0; |
38 | 67 | } else {
|
39 | 68 | /** @noinspection PhpUndefinedFieldInspection */
|
40 |
| - $this->indexes[$invocation->methodName]++; |
| 69 | + $this->indexes[$invocation->getMethodName()]++; |
41 | 70 | }
|
42 | 71 | $this->currentIndex++;
|
43 | 72 |
|
44 | 73 | /** @noinspection PhpUndefinedFieldInspection */
|
45 |
| - return $this->indexes[$invocation->methodName] == $this->sequenceIndex; |
| 74 | + return $this->indexes[$invocation->getMethodName()] === $this->sequenceIndex; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Verifies that the current expectation is valid. If everything is OK the |
| 79 | + * code should just return, if not it must throw an exception. |
| 80 | + * |
| 81 | + * @throws ExpectationFailedException |
| 82 | + */ |
| 83 | + public function verify(): void |
| 84 | + { |
| 85 | + if ($this->currentIndex < $this->sequenceIndex) { |
| 86 | + throw new ExpectationFailedException( |
| 87 | + \sprintf( |
| 88 | + 'The expected invocation at index %s was never reached.', |
| 89 | + $this->sequenceIndex |
| 90 | + ) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param Invocation $invocation Object containing information on a mocked or stubbed method which was invoked |
| 97 | + * @return mixed |
| 98 | + */ |
| 99 | + public function invoked(Invocation $invocation) |
| 100 | + { |
46 | 101 | }
|
47 | 102 | }
|
0 commit comments