InvokedAtMostCount.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the phpunit-mock-objects 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. use PHPUnit\Framework\ExpectationFailedException;
  11. /**
  12. * Invocation matcher which checks if a method has been invoked at least
  13. * N times.
  14. */
  15. class PHPUnit_Framework_MockObject_Matcher_InvokedAtMostCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
  16. {
  17. /**
  18. * @var int
  19. */
  20. private $allowedInvocations;
  21. /**
  22. * @param int $allowedInvocations
  23. */
  24. public function __construct($allowedInvocations)
  25. {
  26. $this->allowedInvocations = $allowedInvocations;
  27. }
  28. /**
  29. * @return string
  30. */
  31. public function toString()
  32. {
  33. return 'invoked at most ' . $this->allowedInvocations . ' times';
  34. }
  35. /**
  36. * Verifies that the current expectation is valid. If everything is OK the
  37. * code should just return, if not it must throw an exception.
  38. *
  39. * @throws ExpectationFailedException
  40. */
  41. public function verify()
  42. {
  43. $count = $this->getInvocationCount();
  44. if ($count > $this->allowedInvocations) {
  45. throw new ExpectationFailedException(
  46. 'Expected invocation at most ' . $this->allowedInvocations .
  47. ' times but it occurred ' . $count . ' time(s).'
  48. );
  49. }
  50. }
  51. }