comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Matcher/InvokedAtIndex.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 */
23 class 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 * @return bool
54 */
55 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
56 {
57 $this->currentIndex++;
58
59 return $this->currentIndex == $this->sequenceIndex;
60 }
61
62 /**
63 * @param PHPUnit_Framework_MockObject_Invocation $invocation
64 */
65 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
66 {
67 }
68
69 /**
70 * Verifies that the current expectation is valid. If everything is OK the
71 * code should just return, if not it must throw an exception.
72 *
73 * @throws PHPUnit_Framework_ExpectationFailedException
74 */
75 public function verify()
76 {
77 if ($this->currentIndex < $this->sequenceIndex) {
78 throw new PHPUnit_Framework_ExpectationFailedException(
79 sprintf(
80 'The expected invocation at index %s was never reached.',
81 $this->sequenceIndex
82 )
83 );
84 }
85 }
86 }