InvokedAtLeastCount.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_InvokedAtLeastCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
  16. {
  17. /**
  18. * @var int
  19. */
  20. private $requiredInvocations;
  21. /**
  22. * @param int $requiredInvocations
  23. */
  24. public function __construct($requiredInvocations)
  25. {
  26. $this->requiredInvocations = $requiredInvocations;
  27. }
  28. /**
  29. * @return string
  30. */
  31. public function toString()
  32. {
  33. return 'invoked at least ' . $this->requiredInvocations . ' 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->requiredInvocations) {
  45. throw new ExpectationFailedException(
  46. 'Expected invocation at least ' . $this->requiredInvocations .
  47. ' times but it occurred ' . $count . ' time(s).'
  48. );
  49. }
  50. }
  51. }