3 * This file is part of the PHPUnit_MockObject package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Invocation matcher which checks if a method has been invoked a certain amount
14 * If the number of invocations exceeds the value it will immediately throw an
16 * If the number is less it will later be checked in verify() and also throw an
19 * @since Class available since Release 1.0.0
21 class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
26 protected $expectedCount;
29 * @param int $expectedCount
31 public function __construct($expectedCount)
33 $this->expectedCount = $expectedCount;
39 public function isNever()
41 return $this->expectedCount == 0;
47 public function toString()
49 return 'invoked ' . $this->expectedCount . ' time(s)';
53 * @param PHPUnit_Framework_MockObject_Invocation $invocation
54 * @throws PHPUnit_Framework_ExpectationFailedException
56 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
58 parent::invoked($invocation);
60 $count = $this->getInvocationCount();
62 if ($count > $this->expectedCount) {
63 $message = $invocation->toString() . ' ';
65 switch ($this->expectedCount) {
67 $message .= 'was not expected to be called.';
72 $message .= 'was not expected to be called more than once.';
78 'was not expected to be called more than %d times.',
84 throw new PHPUnit_Framework_ExpectationFailedException($message);
89 * Verifies that the current expectation is valid. If everything is OK the
90 * code should just return, if not it must throw an exception.
92 * @throws PHPUnit_Framework_ExpectationFailedException
94 public function verify()
96 $count = $this->getInvocationCount();
98 if ($count !== $this->expectedCount) {
99 throw new PHPUnit_Framework_ExpectationFailedException(
101 'Method was expected to be called %d times, ' .
102 'actually called %d times.',
103 $this->expectedCount,