PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpspec/prophecy/src/Prophecy/Prophecy/MethodProphecy.php

https://gitlab.com/Laolballs/evotting
PHP | 407 lines | 192 code | 47 blank | 168 comment | 15 complexity | 983a8e7e7a74f6eecda5e534aa90b4ed MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Prophecy.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. * Marcello Duarte <marcello.duarte@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Prophecy\Prophecy;
  11. use Prophecy\Argument;
  12. use Prophecy\Promise;
  13. use Prophecy\Prediction;
  14. use Prophecy\Exception\Doubler\MethodNotFoundException;
  15. use Prophecy\Exception\InvalidArgumentException;
  16. use Prophecy\Exception\Prophecy\MethodProphecyException;
  17. /**
  18. * Method prophecy.
  19. *
  20. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  21. */
  22. class MethodProphecy
  23. {
  24. private $objectProphecy;
  25. private $methodName;
  26. private $argumentsWildcard;
  27. private $promise;
  28. private $prediction;
  29. private $checkedPredictions = array();
  30. private $bound = false;
  31. /**
  32. * Initializes method prophecy.
  33. *
  34. * @param ObjectProphecy $objectProphecy
  35. * @param string $methodName
  36. * @param null|Argument\ArgumentsWildcard|array $arguments
  37. *
  38. * @throws \Prophecy\Exception\Doubler\MethodNotFoundException If method not found
  39. */
  40. public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null)
  41. {
  42. $double = $objectProphecy->reveal();
  43. if (!method_exists($double, $methodName)) {
  44. throw new MethodNotFoundException(sprintf(
  45. 'Method `%s::%s()` is not defined.', get_class($double), $methodName
  46. ), get_class($double), $methodName);
  47. }
  48. $this->objectProphecy = $objectProphecy;
  49. $this->methodName = $methodName;
  50. $reflectedMethod = new \ReflectionMethod($double, $methodName);
  51. if ($reflectedMethod->isFinal()) {
  52. throw new MethodProphecyException(sprintf(
  53. "Can not add prophecy for a method `%s::%s()`\n".
  54. "as it is a final method.",
  55. get_class($double),
  56. $methodName
  57. ), $this);
  58. }
  59. if (null !== $arguments) {
  60. $this->withArguments($arguments);
  61. }
  62. }
  63. /**
  64. * Sets argument wildcard.
  65. *
  66. * @param array|Argument\ArgumentsWildcard $arguments
  67. *
  68. * @return $this
  69. *
  70. * @throws \Prophecy\Exception\InvalidArgumentException
  71. */
  72. public function withArguments($arguments)
  73. {
  74. if (is_array($arguments)) {
  75. $arguments = new Argument\ArgumentsWildcard($arguments);
  76. }
  77. if (!$arguments instanceof Argument\ArgumentsWildcard) {
  78. throw new InvalidArgumentException(sprintf(
  79. "Either an array or an instance of ArgumentsWildcard expected as\n".
  80. 'a `MethodProphecy::withArguments()` argument, but got %s.',
  81. gettype($arguments)
  82. ));
  83. }
  84. $this->argumentsWildcard = $arguments;
  85. return $this;
  86. }
  87. /**
  88. * Sets custom promise to the prophecy.
  89. *
  90. * @param callable|Promise\PromiseInterface $promise
  91. *
  92. * @return $this
  93. *
  94. * @throws \Prophecy\Exception\InvalidArgumentException
  95. */
  96. public function will($promise)
  97. {
  98. if (is_callable($promise)) {
  99. $promise = new Promise\CallbackPromise($promise);
  100. }
  101. if (!$promise instanceof Promise\PromiseInterface) {
  102. throw new InvalidArgumentException(sprintf(
  103. 'Expected callable or instance of PromiseInterface, but got %s.',
  104. gettype($promise)
  105. ));
  106. }
  107. $this->bindToObjectProphecy();
  108. $this->promise = $promise;
  109. return $this;
  110. }
  111. /**
  112. * Sets return promise to the prophecy.
  113. *
  114. * @see Prophecy\Promise\ReturnPromise
  115. *
  116. * @return $this
  117. */
  118. public function willReturn()
  119. {
  120. return $this->will(new Promise\ReturnPromise(func_get_args()));
  121. }
  122. /**
  123. * Sets return argument promise to the prophecy.
  124. *
  125. * @see Prophecy\Promise\ReturnArgumentPromise
  126. *
  127. * @return $this
  128. */
  129. public function willReturnArgument()
  130. {
  131. return $this->will(new Promise\ReturnArgumentPromise);
  132. }
  133. /**
  134. * Sets throw promise to the prophecy.
  135. *
  136. * @see Prophecy\Promise\ThrowPromise
  137. *
  138. * @param string|\Exception $exception Exception class or instance
  139. *
  140. * @return $this
  141. */
  142. public function willThrow($exception)
  143. {
  144. return $this->will(new Promise\ThrowPromise($exception));
  145. }
  146. /**
  147. * Sets custom prediction to the prophecy.
  148. *
  149. * @param callable|Prediction\PredictionInterface $prediction
  150. *
  151. * @return $this
  152. *
  153. * @throws \Prophecy\Exception\InvalidArgumentException
  154. */
  155. public function should($prediction)
  156. {
  157. if (is_callable($prediction)) {
  158. $prediction = new Prediction\CallbackPrediction($prediction);
  159. }
  160. if (!$prediction instanceof Prediction\PredictionInterface) {
  161. throw new InvalidArgumentException(sprintf(
  162. 'Expected callable or instance of PredictionInterface, but got %s.',
  163. gettype($prediction)
  164. ));
  165. }
  166. $this->bindToObjectProphecy();
  167. $this->prediction = $prediction;
  168. return $this;
  169. }
  170. /**
  171. * Sets call prediction to the prophecy.
  172. *
  173. * @see Prophecy\Prediction\CallPrediction
  174. *
  175. * @return $this
  176. */
  177. public function shouldBeCalled()
  178. {
  179. return $this->should(new Prediction\CallPrediction);
  180. }
  181. /**
  182. * Sets no calls prediction to the prophecy.
  183. *
  184. * @see Prophecy\Prediction\NoCallsPrediction
  185. *
  186. * @return $this
  187. */
  188. public function shouldNotBeCalled()
  189. {
  190. return $this->should(new Prediction\NoCallsPrediction);
  191. }
  192. /**
  193. * Sets call times prediction to the prophecy.
  194. *
  195. * @see Prophecy\Prediction\CallTimesPrediction
  196. *
  197. * @param $count
  198. *
  199. * @return $this
  200. */
  201. public function shouldBeCalledTimes($count)
  202. {
  203. return $this->should(new Prediction\CallTimesPrediction($count));
  204. }
  205. /**
  206. * Checks provided prediction immediately.
  207. *
  208. * @param callable|Prediction\PredictionInterface $prediction
  209. *
  210. * @return $this
  211. *
  212. * @throws \Prophecy\Exception\InvalidArgumentException
  213. */
  214. public function shouldHave($prediction)
  215. {
  216. if (is_callable($prediction)) {
  217. $prediction = new Prediction\CallbackPrediction($prediction);
  218. }
  219. if (!$prediction instanceof Prediction\PredictionInterface) {
  220. throw new InvalidArgumentException(sprintf(
  221. 'Expected callable or instance of PredictionInterface, but got %s.',
  222. gettype($prediction)
  223. ));
  224. }
  225. if (null === $this->promise) {
  226. $this->willReturn();
  227. }
  228. $calls = $this->getObjectProphecy()->findProphecyMethodCalls(
  229. $this->getMethodName(),
  230. $this->getArgumentsWildcard()
  231. );
  232. try {
  233. $prediction->check($calls, $this->getObjectProphecy(), $this);
  234. $this->checkedPredictions[] = $prediction;
  235. } catch (\Exception $e) {
  236. $this->checkedPredictions[] = $prediction;
  237. throw $e;
  238. }
  239. return $this;
  240. }
  241. /**
  242. * Checks call prediction.
  243. *
  244. * @see Prophecy\Prediction\CallPrediction
  245. *
  246. * @return $this
  247. */
  248. public function shouldHaveBeenCalled()
  249. {
  250. return $this->shouldHave(new Prediction\CallPrediction);
  251. }
  252. /**
  253. * Checks no calls prediction.
  254. *
  255. * @see Prophecy\Prediction\NoCallsPrediction
  256. *
  257. * @return $this
  258. */
  259. public function shouldNotHaveBeenCalled()
  260. {
  261. return $this->shouldHave(new Prediction\NoCallsPrediction);
  262. }
  263. /**
  264. * Checks no calls prediction.
  265. *
  266. * @see Prophecy\Prediction\NoCallsPrediction
  267. * @deprecated
  268. *
  269. * @return $this
  270. */
  271. public function shouldNotBeenCalled()
  272. {
  273. return $this->shouldNotHaveBeenCalled();
  274. }
  275. /**
  276. * Checks call times prediction.
  277. *
  278. * @see Prophecy\Prediction\CallTimesPrediction
  279. *
  280. * @param int $count
  281. *
  282. * @return $this
  283. */
  284. public function shouldHaveBeenCalledTimes($count)
  285. {
  286. return $this->shouldHave(new Prediction\CallTimesPrediction($count));
  287. }
  288. /**
  289. * Checks currently registered [with should(...)] prediction.
  290. */
  291. public function checkPrediction()
  292. {
  293. if (null === $this->prediction) {
  294. return;
  295. }
  296. $this->shouldHave($this->prediction);
  297. }
  298. /**
  299. * Returns currently registered promise.
  300. *
  301. * @return null|Promise\PromiseInterface
  302. */
  303. public function getPromise()
  304. {
  305. return $this->promise;
  306. }
  307. /**
  308. * Returns currently registered prediction.
  309. *
  310. * @return null|Prediction\PredictionInterface
  311. */
  312. public function getPrediction()
  313. {
  314. return $this->prediction;
  315. }
  316. /**
  317. * Returns predictions that were checked on this object.
  318. *
  319. * @return PredictionInterface[]
  320. */
  321. public function getCheckedPredictions()
  322. {
  323. return $this->checkedPredictions;
  324. }
  325. /**
  326. * Returns object prophecy this method prophecy is tied to.
  327. *
  328. * @return ObjectProphecy
  329. */
  330. public function getObjectProphecy()
  331. {
  332. return $this->objectProphecy;
  333. }
  334. /**
  335. * Returns method name.
  336. *
  337. * @return string
  338. */
  339. public function getMethodName()
  340. {
  341. return $this->methodName;
  342. }
  343. /**
  344. * Returns arguments wildcard.
  345. *
  346. * @return Argument\ArgumentsWildcard
  347. */
  348. public function getArgumentsWildcard()
  349. {
  350. return $this->argumentsWildcard;
  351. }
  352. private function bindToObjectProphecy()
  353. {
  354. if ($this->bound) {
  355. return;
  356. }
  357. $this->getObjectProphecy()->addMethodProphecy($this);
  358. $this->bound = true;
  359. }
  360. }