LimeMockInvocationMatcherParameter.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 call to satisfy a constraint for one parameter.
  13. *
  14. * The index of the parameter is given to the constructor. The desired
  15. * constraint can be configured by calling any of the methods of this class.
  16. *
  17. * @package Lime
  18. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  19. * @version SVN: $Id: LimeMockInvocationMatcherParameter.php 23864 2009-11-13 18:06:20Z bschussek $
  20. * @see LimeMockInvocationMatcherInterface
  21. */
  22. class LimeMockInvocationMatcherParameter implements LimeMockInvocationMatcherInterface
  23. {
  24. private
  25. $index = null,
  26. $parent = null,
  27. $constraint = null;
  28. /**
  29. * Constructor.
  30. *
  31. * @param integer $index
  32. * @param LimeMockInvocationExpectation $parent
  33. */
  34. public function __construct($index, LimeMockInvocationExpectation $parent)
  35. {
  36. $this->index = $index;
  37. $this->parent = $parent;
  38. }
  39. /**
  40. * Returns the parameter index of this matcher.
  41. *
  42. * @return integer
  43. */
  44. public function getIndex()
  45. {
  46. return $this->index;
  47. }
  48. /**
  49. * (non-PHPdoc)
  50. * @see mock/matcher/LimeMockInvocationMatcherInterface#invoke($invocation)
  51. */
  52. public function invoke(LimeMockInvocation $invocation)
  53. {
  54. try
  55. {
  56. if (!is_null($this->constraint))
  57. {
  58. $this->constraint->evaluate($invocation->getParameter($this->index-1));
  59. }
  60. }
  61. catch (LimeConstraintException $e)
  62. {
  63. $message = LimeTools::indent($e->getMessage(), 2);
  64. throw new LimeMockInvocationMatcherException("was called with wrong parameter $this->index\n".$message);
  65. }
  66. catch (OutOfRangeException $e)
  67. {
  68. throw new LimeMockInvocationMatcherException("was not called with $this->index or more parameters");
  69. }
  70. }
  71. /**
  72. * Returns whether this matcher matches the given invocation.
  73. *
  74. * @param LimeMockInvocation $invocation
  75. * @return boolean
  76. */
  77. public function matches(LimeMockInvocation $invocation)
  78. {
  79. try
  80. {
  81. if (!is_null($this->constraint))
  82. {
  83. $this->constraint->evaluate($invocation->getParameter($this->index-1));
  84. }
  85. }
  86. catch (LimeConstraintException $e)
  87. {
  88. return false;
  89. }
  90. catch (OutOfRangeException $e)
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * (non-PHPdoc)
  98. * @see mock/matcher/LimeMockInvocationMatcherInterface#isInvokable()
  99. */
  100. public function isInvokable()
  101. {
  102. return true;
  103. }
  104. /**
  105. * (non-PHPdoc)
  106. * @see mock/matcher/LimeMockInvocationMatcherInterface#isSatisfied()
  107. */
  108. public function isSatisfied()
  109. {
  110. return true;
  111. }
  112. /**
  113. * (non-PHPdoc)
  114. * @see mock/matcher/LimeMockInvocationMatcherInterface#getMessage()
  115. */
  116. public function getMessage()
  117. {
  118. return '';
  119. }
  120. /**
  121. * Sets the constraint and returns the related invocation expectation object.
  122. *
  123. * @param LimeConstraintInterface $constraint
  124. * @return LimeMockInvocationExpectation
  125. */
  126. private function setConstraint(LimeConstraintInterface $constraint)
  127. {
  128. $this->constraint = $constraint;
  129. return $this->parent;
  130. }
  131. /**
  132. * Requires the parameter to be equal to the given value.
  133. *
  134. * @param LimeConstraintInterface $constraint
  135. * @return LimeMockInvocationExpectation
  136. * @see LimeConstraintIs
  137. */
  138. public function is($expected)
  139. {
  140. return $this->setConstraint(new LimeConstraintIs($expected));
  141. }
  142. /**
  143. * Requires the parameter to be not equal to the given value.
  144. *
  145. * @param LimeConstraintInterface $constraint
  146. * @return LimeMockInvocationExpectation
  147. * @see LimeConstraintIsNot
  148. */
  149. public function isnt($expected)
  150. {
  151. return $this->setConstraint(new LimeConstraintIsNot($expected));
  152. }
  153. /**
  154. * Requires the parameter to be identical to the given value.
  155. *
  156. * @param LimeConstraintInterface $constraint
  157. * @return LimeMockInvocationExpectation
  158. * @see LimeConstraintSame
  159. */
  160. public function same($expected)
  161. {
  162. return $this->setConstraint(new LimeConstraintSame($expected));
  163. }
  164. /**
  165. * Requires the parameter to be not identical to the given value.
  166. *
  167. * @param LimeConstraintInterface $constraint
  168. * @return LimeMockInvocationExpectation
  169. * @see LimeConstraintNotSame
  170. */
  171. public function isntSame($expected)
  172. {
  173. return $this->setConstraint(new LimeConstraintNotSame($expected));
  174. }
  175. /**
  176. * Requires the parameter to be like the given value.
  177. *
  178. * @param LimeConstraintInterface $constraint
  179. * @return LimeMockInvocationExpectation
  180. * @see LimeConstraintLike
  181. */
  182. public function like($expected)
  183. {
  184. return $this->setConstraint(new LimeConstraintLike($expected));
  185. }
  186. /**
  187. * Requires the parameter to be unlike the given value.
  188. *
  189. * @param LimeConstraintInterface $constraint
  190. * @return LimeMockInvocationExpectation
  191. * @see LimeConstraintUnlike
  192. */
  193. public function unlike($expected)
  194. {
  195. return $this->setConstraint(new LimeConstraintUnlike($expected));
  196. }
  197. /**
  198. * Requires the parameter to contain the given value.
  199. *
  200. * @param LimeConstraintInterface $constraint
  201. * @return LimeMockInvocationExpectation
  202. * @see LimeConstraintContains
  203. */
  204. public function contains($expected)
  205. {
  206. return $this->setConstraint(new LimeConstraintContains($expected));
  207. }
  208. /**
  209. * Requires the parameter to not contain the given value.
  210. *
  211. * @param LimeConstraintInterface $constraint
  212. * @return LimeMockInvocationExpectation
  213. * @see LimeConstraintContainsNot
  214. */
  215. public function containsNot($expected)
  216. {
  217. return $this->setConstraint(new LimeConstraintContainsNot($expected));
  218. }
  219. /**
  220. * Requires the parameter to be greater than the given value.
  221. *
  222. * @param LimeConstraintInterface $constraint
  223. * @return LimeMockInvocationExpectation
  224. * @see LimeConstraintGreaterThan
  225. */
  226. public function greaterThan($expected)
  227. {
  228. return $this->setConstraint(new LimeConstraintGreaterThan($expected));
  229. }
  230. /**
  231. * Requires the parameter to be greater than or equal to the given value.
  232. *
  233. * @param LimeConstraintInterface $constraint
  234. * @return LimeMockInvocationExpectation
  235. * @see LimeConstraintGreaterThanEqual
  236. */
  237. public function greaterThanEqual($expected)
  238. {
  239. return $this->setConstraint(new LimeConstraintGreaterThanEqual($expected));
  240. }
  241. /**
  242. * Requires the parameter to be less than the given value.
  243. *
  244. * @param LimeConstraintInterface $constraint
  245. * @return LimeMockInvocationExpectation
  246. * @see LimeConstraintLessThan
  247. */
  248. public function lessThan($expected)
  249. {
  250. return $this->setConstraint(new LimeConstraintLessThan($expected));
  251. }
  252. /**
  253. * Requires the parameter to be less than or equal to the given value.
  254. *
  255. * @param LimeConstraintInterface $constraint
  256. * @return LimeMockInvocationExpectation
  257. * @see LimeConstraintLessThanEqual
  258. */
  259. public function lessThanEqual($expected)
  260. {
  261. return $this->setConstraint(new LimeConstraintLessThanEqual($expected));
  262. }
  263. }