Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Invocation matcher which checks if a method has been invoked a certain amount Chris@0: * of times. Chris@0: * If the number of invocations exceeds the value it will immediately throw an Chris@0: * exception, Chris@0: * If the number is less it will later be checked in verify() and also throw an Chris@0: * exception. Chris@0: * Chris@0: * @since Class available since Release 1.0.0 Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder Chris@0: { Chris@0: /** Chris@0: * @var int Chris@0: */ Chris@0: protected $expectedCount; Chris@0: Chris@0: /** Chris@0: * @param int $expectedCount Chris@0: */ Chris@0: public function __construct($expectedCount) Chris@0: { Chris@0: $this->expectedCount = $expectedCount; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return bool Chris@0: */ Chris@0: public function isNever() Chris@0: { Chris@0: return $this->expectedCount == 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function toString() Chris@0: { Chris@0: return 'invoked ' . $this->expectedCount . ' time(s)'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @throws PHPUnit_Framework_ExpectationFailedException Chris@0: */ Chris@0: public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: parent::invoked($invocation); Chris@0: Chris@0: $count = $this->getInvocationCount(); Chris@0: Chris@0: if ($count > $this->expectedCount) { Chris@0: $message = $invocation->toString() . ' '; Chris@0: Chris@0: switch ($this->expectedCount) { Chris@0: case 0: { Chris@0: $message .= 'was not expected to be called.'; Chris@0: } Chris@0: break; Chris@0: Chris@0: case 1: { Chris@0: $message .= 'was not expected to be called more than once.'; Chris@0: } Chris@0: break; Chris@0: Chris@0: default: { Chris@0: $message .= sprintf( Chris@0: 'was not expected to be called more than %d times.', Chris@0: $this->expectedCount Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: throw new PHPUnit_Framework_ExpectationFailedException($message); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies that the current expectation is valid. If everything is OK the Chris@0: * code should just return, if not it must throw an exception. Chris@0: * Chris@0: * @throws PHPUnit_Framework_ExpectationFailedException Chris@0: */ Chris@0: public function verify() Chris@0: { Chris@0: $count = $this->getInvocationCount(); Chris@0: Chris@0: if ($count !== $this->expectedCount) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf( Chris@0: 'Method was expected to be called %d times, ' . Chris@0: 'actually called %d times.', Chris@0: $this->expectedCount, Chris@0: $count Chris@0: ) Chris@0: ); Chris@0: } Chris@0: } Chris@0: }