Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: namespace PHPUnit\Framework\MockObject\Matcher; Chris@14: Chris@14: use PHPUnit\Framework\ExpectationFailedException; Chris@14: use PHPUnit\Framework\MockObject\Invocation as BaseInvocation; Chris@14: Chris@14: /** Chris@14: * Invocation matcher which checks if a method was invoked at a certain index. Chris@14: * Chris@14: * If the expected index number does not match the current invocation index it Chris@14: * will not match which means it skips all method and parameter matching. Only Chris@14: * once the index is reached will the method and parameter start matching and Chris@14: * verifying. Chris@14: * Chris@14: * If the index is never reached it will throw an exception in index. Chris@14: */ Chris@14: class InvokedAtIndex implements Invocation Chris@14: { Chris@14: /** Chris@14: * @var int Chris@14: */ Chris@14: private $sequenceIndex; Chris@14: Chris@14: /** Chris@14: * @var int Chris@14: */ Chris@14: private $currentIndex = -1; Chris@14: Chris@14: /** Chris@14: * @param int $sequenceIndex Chris@14: */ Chris@14: public function __construct($sequenceIndex) Chris@14: { Chris@14: $this->sequenceIndex = $sequenceIndex; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @return string Chris@14: */ Chris@14: public function toString() Chris@14: { Chris@14: return 'invoked at sequence index ' . $this->sequenceIndex; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @param BaseInvocation $invocation Chris@14: * Chris@14: * @return bool Chris@14: */ Chris@14: public function matches(BaseInvocation $invocation) Chris@14: { Chris@14: $this->currentIndex++; Chris@14: Chris@14: return $this->currentIndex == $this->sequenceIndex; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @param BaseInvocation $invocation Chris@14: */ Chris@14: public function invoked(BaseInvocation $invocation) Chris@14: { Chris@14: } Chris@14: Chris@14: /** Chris@14: * Verifies that the current expectation is valid. If everything is OK the Chris@14: * code should just return, if not it must throw an exception. Chris@14: * Chris@14: * @throws ExpectationFailedException Chris@14: */ Chris@14: public function verify() Chris@14: { Chris@14: if ($this->currentIndex < $this->sequenceIndex) { Chris@14: throw new ExpectationFailedException( Chris@14: \sprintf( Chris@14: 'The expected invocation at index %s was never reached.', Chris@14: $this->sequenceIndex Chris@14: ) Chris@14: ); Chris@14: } Chris@14: } Chris@14: }