Expectations.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. //require 'Yay/Expectation.php';
  15. //require 'Yay/InvocationRecorder.php';
  16. //require 'Yay/InvocationProxy.php';
  17. //require 'Yay/Invocation.php';
  18. //require 'Yay/State.php';
  19. //require 'Yay/StatePredicate.php';
  20. //require 'Yay/Sequence.php';
  21. //require 'Yay/Expectations/ExactlyExpectation.php';
  22. //require 'Yay/Expectations/AtLeastExpectation.php';
  23. //require 'Yay/Expectations/AtMostExpectation.php';
  24. //require 'Yay/Expectations/BetweenExpectation.php';
  25. //require 'Yay/Action.php';
  26. //require 'Yay/Actions/ReturnValueAction.php';
  27. //require 'Yay/Actions/ReturnReferenceAction.php';
  28. //require 'Yay/Actions/ThrowAction.php';
  29. //require 'Yay/Actions/CallbackAction.php';
  30. /**
  31. * A group of expectations which can be specified in a fluid manner.
  32. * Generally speaking this is where all expectations should be made for the sake
  33. * of abstraction.
  34. * @author Chris Corbyn <chris@w3style.co.uk>
  35. * @package Yay
  36. */
  37. class Yay_Expectations implements Yay_InvocationRecorder
  38. {
  39. /**
  40. * The Expectation stack.
  41. * @var array
  42. * @access private
  43. */
  44. private $_expectations = array();
  45. /**
  46. * The current Expectation to proxy any recording to.
  47. * @var Yay_Expectation
  48. * @access private
  49. */
  50. private $_currentEndpoint;
  51. /**
  52. * Create a new instance of Expectations.
  53. * @return Yay_Expectations
  54. */
  55. final public static function create()
  56. {
  57. return new self();
  58. }
  59. /**
  60. * Expect one Invocation on the $mock object.
  61. * Returns the mock object in record mode.
  62. * @param Yay_MockObject $mock
  63. * @return Yay_Expectations
  64. */
  65. public function one(Yay_MockObject $mock)
  66. {
  67. return $this->exactly(1)->of($mock);
  68. }
  69. /**
  70. * Expect exactly $n Invocations on a mock object specified with a following
  71. * of() clause.
  72. * Example: <code> Expectations::create()->exactly(2)->of($mock); </code>
  73. * @param int $n
  74. * @return Yay_Expectations
  75. */
  76. public function exactly($n)
  77. {
  78. return $this->_setEndpoint(new Yay_Expectations_ExactlyExpectation($n));
  79. }
  80. /**
  81. * Expect at least $n Invocations on a mock object specified with a following
  82. * of() clause.
  83. * Example: <code> Expectations::create()->atLeast(2)->of($mock); </code>
  84. * @param int $n
  85. * @return Yay_Expectations
  86. */
  87. public function atLeast($n)
  88. {
  89. return $this->_setEndpoint(new Yay_Expectations_AtLeastExpectation($n));
  90. }
  91. /**
  92. * Expect at most $n Invocations on a mock object specified with a following
  93. * of() clause.
  94. * Example: <code> Expectations::create()->atMost(2)->of($mock); </code>
  95. * @param int $n
  96. * @return Yay_Expectations
  97. */
  98. public function atMost($n)
  99. {
  100. return $this->_setEndpoint(new Yay_Expectations_AtMostExpectation($n));
  101. }
  102. /**
  103. * Expect at between $min and $max Invocations on a mock object specified
  104. * with a following of() clause.
  105. * Example: <code> Expectations::create()->atLeast(2)->of($mock); </code>
  106. * @param int $n
  107. * @return Yay_Expectations
  108. */
  109. public function between($min, $max)
  110. {
  111. return $this->_setEndpoint(new Yay_Expectations_BetweenExpectation($min, $max));
  112. }
  113. /**
  114. * Ignore Invocations on the $mock object specified.
  115. * @param Yay_MockObject $mock
  116. * @return Yay_Expectations
  117. */
  118. public function ignoring(Yay_MockObject $mock)
  119. {
  120. return $this->atLeast(0)->of($mock);
  121. }
  122. /**
  123. * Allow Invocations on the $mock object specified.
  124. * This does exactly the same thing as ignoring() but it allows a semantically
  125. * different meaning in the test case.
  126. * @param Yay_MockObject $mock
  127. * @return Yay_Expectations
  128. */
  129. public function allowing(Yay_MockObject $mock)
  130. {
  131. return $this->ignoring($mock);
  132. }
  133. /**
  134. * Deny Invocations on the $mock object specified.
  135. * @param Yay_MockObject $mock
  136. * @return Yay_Expectations
  137. */
  138. public function never(Yay_MockObject $mock)
  139. {
  140. return $this->exactly(0)->of($mock);
  141. }
  142. /**
  143. * Specify the MockObject which the Invocation will occur.
  144. * This method returns the mock object in record mode.
  145. * @param Yay_MockObject $mock
  146. * @return Yay_InvocationProxy
  147. */
  148. public function of(Yay_MockObject $mock)
  149. {
  150. $this->_getEndpoint()->of($mock);
  151. return new Yay_InvocationProxy($this, $mock);
  152. }
  153. /**
  154. * Specify the Action to run if a match occurs.
  155. * @param Yay_Action $action
  156. */
  157. public function will(Yay_Action $action)
  158. {
  159. $this->_getEndpoint()->will($action);
  160. return $this;
  161. }
  162. /**
  163. * Only be expected when in the given State predicate.
  164. * @param Yay_StatePredicate $predicate
  165. */
  166. public function when(Yay_StatePredicate $predicate)
  167. {
  168. $this->_getEndpoint()->when($predicate);
  169. return $this;
  170. }
  171. /**
  172. * Activate the given $state if a match occurs.
  173. * @param Yay_State $state
  174. */
  175. public function then(Yay_State $state)
  176. {
  177. $this->_getEndpoint()->then($state);
  178. return $this;
  179. }
  180. /**
  181. * Constrain the current expectation to occur in the given sequence.
  182. * @param Yay_Sequence $seq
  183. */
  184. public function inSequence(Yay_Sequence $seq)
  185. {
  186. $this->_getEndpoint()->inSequence($seq);
  187. return $this;
  188. }
  189. /**
  190. * A wrapper for will(Yay::returnValue($value)).
  191. * @param mixed $value
  192. */
  193. public function returns($value)
  194. {
  195. $this->_getEndpoint()->will(new Yay_Actions_ReturnValueAction($value));
  196. return $this;
  197. }
  198. /**
  199. * A wrapper for will(Yay::returnReference($ref)).
  200. * @param mixed $ref
  201. */
  202. public function returnsReference(&$ref)
  203. {
  204. $this->_getEndpoint()->will(new Yay_Actions_ReturnReferenceAction($ref));
  205. return $this;
  206. }
  207. /**
  208. * A wrapper for will(Yay::throwException($e)).
  209. * @param Exception $e
  210. */
  211. public function throws(Exception $e)
  212. {
  213. $this->_getEndpoint()->will(new Yay_Actions_ThrowAction($e));
  214. return $this;
  215. }
  216. /**
  217. * A wrapper for will(Yay::call($callback)).
  218. * @param callback $callback
  219. */
  220. public function calls($callback)
  221. {
  222. $this->_getEndpoint()->will(new Yay_Actions_CallbackAction($callback));
  223. return $this;
  224. }
  225. /**
  226. * Record any Invocations on the MockObject whilst it's in record mode.
  227. * @param Yay_Invocation $invocation
  228. */
  229. public function recordInvocation(Yay_Invocation $invocation)
  230. {
  231. $this->_getEndpoint()->recordInvocation($invocation);
  232. }
  233. /**
  234. * Returns the Expectation stack.
  235. * @return Yay_Expectation
  236. */
  237. public function getExpectations()
  238. {
  239. return $this->_expectations;
  240. }
  241. // -- Private methods
  242. /**
  243. * Apply a new Expectation to the stack and tag it as the endpoint for recording.
  244. * @param Yay_Expectation $expectation
  245. * @return Yay_Expectations
  246. * @access private
  247. */
  248. private function _setEndpoint(Yay_Expectation $expectation)
  249. {
  250. $this->_expectations[] = $expectation;
  251. $this->_currentEndpoint = $expectation;
  252. return $this;
  253. }
  254. /**
  255. * Gets the current endpoint (current expectation).
  256. * @return Yay_Expectation
  257. * @access private
  258. */
  259. private function _getEndpoint()
  260. {
  261. if (!isset($this->_currentEndpoint))
  262. {
  263. throw new BadMethodCallException(
  264. 'No cardinality clause has yet been made. First call one(), atLeast(), ' .
  265. 'atMost(), exactly(), between(), ignoring(), allowing() or never() ' .
  266. 'before performing this operation.'
  267. );
  268. }
  269. else
  270. {
  271. return $this->_currentEndpoint;
  272. }
  273. }
  274. }