LimeMockBehaviour.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * Provides common methods of all implemented behaviours.
  13. *
  14. * Behaviours accept the following options for initialization:
  15. *
  16. * * strict: If set to TRUE, the behaviour initializes all mocked
  17. * methods with the modifier strict() to enable strict
  18. * type comparison. Default: FALSE
  19. * * nice: If set to TRUE, the behaviour will ignore unexpected
  20. * method calls. Mocked methods will be initialized
  21. * with the modifier any(). Default: FALSE
  22. * * no_exceptions: If set to TRUE, throwing of exceptions is
  23. * suppressed when unexpected methods are called.
  24. * The methods will be reported as errors when
  25. * verify() is called. Default: FALSE
  26. *
  27. * @package Lime
  28. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  29. * @version SVN: $Id: LimeMockBehaviour.php 23880 2009-11-14 10:14:34Z bschussek $
  30. * @see LimeMockBehaviourInterface
  31. */
  32. abstract class LimeMockBehaviour implements LimeMockBehaviourInterface
  33. {
  34. protected
  35. $options = array(),
  36. $verified = false,
  37. $invocations = array(),
  38. $expectNothing = false;
  39. /**
  40. * Constructor.
  41. *
  42. * @param array $options The options for initializing the behaviour.
  43. * @return unknown_type
  44. */
  45. public function __construct(array $options = array())
  46. {
  47. $this->options = array_merge(array(
  48. 'strict' => false,
  49. 'nice' => false,
  50. 'no_exceptions' => false,
  51. ), $options);
  52. }
  53. /**
  54. * (non-PHPdoc)
  55. * @see mock/LimeMockBehaviourInterface#expect($invocation)
  56. */
  57. public function expect(LimeMockInvocationExpectation $invocation)
  58. {
  59. $this->invocations[] = $invocation;
  60. if ($this->options['strict'])
  61. {
  62. $invocation->strict();
  63. }
  64. if ($this->options['nice'])
  65. {
  66. $invocation->any();
  67. }
  68. else
  69. {
  70. $invocation->once();
  71. }
  72. }
  73. /**
  74. * (non-PHPdoc)
  75. * @see mock/LimeMockBehaviourInterface#invoke($invocation)
  76. */
  77. public function invoke(LimeMockInvocation $invocation)
  78. {
  79. if (!$this->options['nice'] && !$this->verified && !$this->options['no_exceptions'] && ($this->expectNothing || count($this->invocations) > 0))
  80. {
  81. throw new LimeMockInvocationException($invocation, 'was not expected to be called');
  82. }
  83. }
  84. /**
  85. * (non-PHPdoc)
  86. * @see mock/LimeMockBehaviourInterface#isInvokable($method)
  87. */
  88. public function isInvokable(LimeMockMethod $method)
  89. {
  90. foreach ($this->invocations as $invocation)
  91. {
  92. if ($invocation->matches($method))
  93. {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * (non-PHPdoc)
  101. * @see mock/LimeMockBehaviourInterface#verify()
  102. */
  103. public function verify()
  104. {
  105. foreach ($this->invocations as $invocation)
  106. {
  107. $invocation->verify();
  108. }
  109. $this->verified = true;
  110. }
  111. /**
  112. * (non-PHPdoc)
  113. * @see mock/LimeMockBehaviourInterface#setExpectNothing()
  114. */
  115. public function setExpectNothing()
  116. {
  117. $this->expectNothing = true;
  118. }
  119. /**
  120. * (non-PHPdoc)
  121. * @see mock/LimeMockBehaviourInterface#reset()
  122. */
  123. public function reset()
  124. {
  125. $this->invocations = array();
  126. }
  127. }