Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MethodInvokedAtIndex.php
Go to the documentation of this file.
1 <?php
8 
9 use PHPUnit\Framework\ExpectationFailedException;
10 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
11 
24 class MethodInvokedAtIndex implements \PHPUnit\Framework\MockObject\Matcher\Invocation
25 {
29  private $sequenceIndex;
30 
34  private $currentIndex = -1;
35 
39  private $indexes = [];
40 
44  public function __construct($sequenceIndex)
45  {
46  $this->sequenceIndex = $sequenceIndex;
47  }
48 
52  public function toString(): string
53  {
54  return 'invoked at sequence index ' . $this->sequenceIndex;
55  }
56 
61  public function matches(BaseInvocation $invocation): bool
62  {
64  if (!isset($this->indexes[$invocation->getMethodName()])) {
66  $this->indexes[$invocation->getMethodName()] = 0;
67  } else {
69  $this->indexes[$invocation->getMethodName()]++;
70  }
71  $this->currentIndex++;
72 
74  return $this->indexes[$invocation->getMethodName()] == $this->sequenceIndex;
75  }
76 
82  public function invoked(BaseInvocation $invocation)
83  {
84  }
85 
92  public function verify(): void
93  {
94  if ($this->currentIndex < $this->sequenceIndex) {
95  throw new ExpectationFailedException(
96  \sprintf(
97  'The expected invocation at index %s was never reached.',
98  $this->sequenceIndex
99  )
100  );
101  }
102  }
103 }