LimeMockInvocationMatcherTimes.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Requires a method to be invoked a specific number of times.
  13. *
  14. * The expected number of method invokations must be passed to the constructor.
  15. *
  16. * @package Lime
  17. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  18. * @version SVN: $Id: LimeMockInvocationMatcherTimes.php 23701 2009-11-08 21:23:40Z bschussek $
  19. * @see LimeMockInvocationMatcherInterface
  20. */
  21. class LimeMockInvocationMatcherTimes implements LimeMockInvocationMatcherInterface
  22. {
  23. private
  24. $expected = 0,
  25. $actual = 0;
  26. /**
  27. * Constructor.
  28. *
  29. * @param integer $times The expected number of method invokations0
  30. */
  31. public function __construct($times)
  32. {
  33. $this->expected = $times;
  34. }
  35. /**
  36. * (non-PHPdoc)
  37. * @see mock/matcher/LimeMockInvocationMatcherInterface#invoke($invocation)
  38. */
  39. public function invoke(LimeMockInvocation $invocation)
  40. {
  41. if ($this->actual < $this->expected)
  42. {
  43. $this->actual++;
  44. }
  45. else
  46. {
  47. if ($this->expected == 0)
  48. {
  49. throw new LimeMockInvocationMatcherException('should not be called');
  50. }
  51. else
  52. {
  53. $times = $this->getMessage();
  54. throw new LimeMockInvocationMatcherException(sprintf('should only be called %s', $times));
  55. }
  56. }
  57. }
  58. /**
  59. * (non-PHPdoc)
  60. * @see mock/matcher/LimeMockInvocationMatcherInterface#isInvokable()
  61. */
  62. public function isInvokable()
  63. {
  64. return $this->actual < $this->expected;
  65. }
  66. /**
  67. * (non-PHPdoc)
  68. * @see mock/matcher/LimeMockInvocationMatcherInterface#isSatisfied()
  69. */
  70. public function isSatisfied()
  71. {
  72. return $this->actual >= $this->expected;
  73. }
  74. /**
  75. * (non-PHPdoc)
  76. * @see mock/matcher/LimeMockInvocationMatcherInterface#getMessage()
  77. */
  78. public function getMessage()
  79. {
  80. return $this->expected == 1 ? 'once' : $this->expected.' times';
  81. }
  82. }