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: Chris@14: /** Chris@14: * Invocation matcher which checks if a method has been invoked at least Chris@14: * N times. Chris@14: */ Chris@14: class InvokedAtMostCount extends InvokedRecorder Chris@14: { Chris@14: /** Chris@14: * @var int Chris@14: */ Chris@14: private $allowedInvocations; Chris@14: Chris@14: /** Chris@14: * @param int $allowedInvocations Chris@14: */ Chris@14: public function __construct($allowedInvocations) Chris@14: { Chris@14: $this->allowedInvocations = $allowedInvocations; Chris@14: } Chris@14: Chris@14: /** Chris@14: * @return string Chris@14: */ Chris@14: public function toString() Chris@14: { Chris@14: return 'invoked at most ' . $this->allowedInvocations . ' times'; 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: $count = $this->getInvocationCount(); Chris@14: Chris@14: if ($count > $this->allowedInvocations) { Chris@14: throw new ExpectationFailedException( Chris@14: 'Expected invocation at most ' . $this->allowedInvocations . Chris@14: ' times but it occurred ' . $count . ' time(s).' Chris@14: ); Chris@14: } Chris@14: } Chris@14: }