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