LimeMockInvocationMatcherBetween.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 called between X and Y times.
  13. *
  14. * The parameters X and Y are passed to the constructor. These values are
  15. * inclusive, that means that the matcher passes if the method is called
  16. * exactly X or Y times.
  17. *
  18. * @package Lime
  19. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  20. * @version SVN: $Id: LimeMockInvocationMatcherBetween.php 23701 2009-11-08 21:23:40Z bschussek $
  21. * @see LimeMockInvocationMatcherInterface
  22. */
  23. class LimeMockInvocationMatcherBetween implements LimeMockInvocationMatcherInterface
  24. {
  25. private
  26. $start = 0,
  27. $end = 0,
  28. $actual = 0;
  29. /**
  30. * Constructor.
  31. *
  32. * @param integer $start The lower limit of accepted invokation counts
  33. * @param integer $end The upper limit of accepted invokation counts
  34. */
  35. public function __construct($start, $end)
  36. {
  37. if ($start > $end)
  38. {
  39. $this->start = $end;
  40. $this->end = $start;
  41. }
  42. else
  43. {
  44. $this->start = $start;
  45. $this->end = $end;
  46. }
  47. }
  48. /**
  49. * (non-PHPdoc)
  50. * @see mock/matcher/LimeMockInvocationMatcherInterface#invoke($invocation)
  51. */
  52. public function invoke(LimeMockInvocation $invocation)
  53. {
  54. if ($this->actual < $this->end)
  55. {
  56. $this->actual++;
  57. }
  58. else
  59. {
  60. throw new LimeMockInvocationMatcherException(sprintf('should only be called %s', $this->getMessage()));
  61. }
  62. }
  63. /**
  64. * (non-PHPdoc)
  65. * @see mock/matcher/LimeMockInvocationMatcherInterface#isInvokable()
  66. */
  67. public function isInvokable()
  68. {
  69. return $this->actual < $this->end;
  70. }
  71. /**
  72. * (non-PHPdoc)
  73. * @see mock/matcher/LimeMockInvocationMatcherInterface#isSatisfied()
  74. */
  75. public function isSatisfied()
  76. {
  77. return $this->actual >= $this->start && $this->actual <= $this->end;
  78. }
  79. /**
  80. * (non-PHPdoc)
  81. * @see mock/matcher/LimeMockInvocationMatcherInterface#getMessage()
  82. */
  83. public function getMessage()
  84. {
  85. return sprintf('between %s and % times', $this->start, $this->end);
  86. }
  87. }