/blog/vendor/phpspec/prophecy/spec/Prophecy/Prophecy/MethodProphecySpec.php

https://gitlab.com/zan_zan/laravel_sample · PHP · 381 lines · 243 code · 70 blank · 68 comment · 0 complexity · bc9656352e033222b065ca256fd83b7f MD5 · raw file

  1. <?php
  2. namespace spec\Prophecy\Prophecy;
  3. use PhpSpec\ObjectBehavior;
  4. class MethodProphecySpec extends ObjectBehavior
  5. {
  6. /**
  7. * @param \Prophecy\Prophecy\ObjectProphecy $objectProphecy
  8. * @param \ReflectionClass $reflection
  9. */
  10. function let($objectProphecy, $reflection)
  11. {
  12. $objectProphecy->reveal()->willReturn($reflection);
  13. $this->beConstructedWith($objectProphecy, 'getName', null);
  14. }
  15. function it_is_initializable()
  16. {
  17. $this->shouldHaveType('Prophecy\Prophecy\MethodProphecy');
  18. }
  19. function its_constructor_throws_MethodNotFoundException_for_unexisting_method($objectProphecy)
  20. {
  21. $this->shouldThrow('Prophecy\Exception\Doubler\MethodNotFoundException')->during(
  22. '__construct', array($objectProphecy, 'getUnexisting', null)
  23. );
  24. }
  25. function its_constructor_throws_MethodProphecyException_for_final_methods($objectProphecy, ClassWithFinalMethod $subject)
  26. {
  27. $objectProphecy->reveal()->willReturn($subject);
  28. $this->shouldThrow('Prophecy\Exception\Prophecy\MethodProphecyException')->during(
  29. '__construct', array($objectProphecy, 'finalMethod', null)
  30. );
  31. }
  32. function its_constructor_transforms_array_passed_as_3rd_argument_to_ArgumentsWildcard(
  33. $objectProphecy
  34. )
  35. {
  36. $this->beConstructedWith($objectProphecy, 'getName', array(42, 33));
  37. $wildcard = $this->getArgumentsWildcard();
  38. $wildcard->shouldNotBe(null);
  39. $wildcard->__toString()->shouldReturn('exact(42), exact(33)');
  40. }
  41. function its_constructor_does_not_touch_third_argument_if_it_is_null($objectProphecy)
  42. {
  43. $this->beConstructedWith($objectProphecy, 'getName', null);
  44. $wildcard = $this->getArgumentsWildcard();
  45. $wildcard->shouldBe(null);
  46. }
  47. /**
  48. * @param \Prophecy\Promise\PromiseInterface $promise
  49. */
  50. function it_records_promise_through_will_method($promise, $objectProphecy)
  51. {
  52. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  53. $this->will($promise);
  54. $this->getPromise()->shouldReturn($promise);
  55. }
  56. /**
  57. * @param \Prophecy\Promise\PromiseInterface $promise
  58. */
  59. function it_adds_itself_to_ObjectProphecy_during_call_to_will($objectProphecy, $promise)
  60. {
  61. $objectProphecy->addMethodProphecy($this)->shouldBeCalled();
  62. $this->will($promise);
  63. }
  64. function it_adds_ReturnPromise_during_willReturn_call($objectProphecy)
  65. {
  66. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  67. $this->willReturn(42);
  68. $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ReturnPromise');
  69. }
  70. function it_adds_ThrowPromise_during_willThrow_call($objectProphecy)
  71. {
  72. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  73. $this->willThrow('RuntimeException');
  74. $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ThrowPromise');
  75. }
  76. function it_adds_ReturnArgumentPromise_during_willReturnArgument_call($objectProphecy)
  77. {
  78. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  79. $this->willReturnArgument();
  80. $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ReturnArgumentPromise');
  81. }
  82. function it_adds_ReturnArgumentPromise_during_willReturnArgument_call_with_index_argument($objectProphecy)
  83. {
  84. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  85. $this->willReturnArgument(1);
  86. $promise = $this->getPromise();
  87. $promise->shouldBeAnInstanceOf('Prophecy\Promise\ReturnArgumentPromise');
  88. $promise->execute(array('one', 'two'), $objectProphecy, $this)->shouldReturn('two');
  89. }
  90. function it_adds_CallbackPromise_during_will_call_with_callback_argument($objectProphecy)
  91. {
  92. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  93. $callback = function () {};
  94. $this->will($callback);
  95. $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\CallbackPromise');
  96. }
  97. /**
  98. * @param \Prophecy\Prediction\PredictionInterface $prediction
  99. */
  100. function it_records_prediction_through_should_method($prediction, $objectProphecy)
  101. {
  102. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  103. $this->callOnWrappedObject('should', array($prediction));
  104. $this->getPrediction()->shouldReturn($prediction);
  105. }
  106. function it_adds_CallbackPrediction_during_should_call_with_callback_argument($objectProphecy)
  107. {
  108. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  109. $callback = function () {};
  110. $this->callOnWrappedObject('should', array($callback));
  111. $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallbackPrediction');
  112. }
  113. /**
  114. * @param \Prophecy\Prediction\PredictionInterface $prediction
  115. */
  116. function it_adds_itself_to_ObjectProphecy_during_call_to_should($objectProphecy, $prediction)
  117. {
  118. $objectProphecy->addMethodProphecy($this)->shouldBeCalled();
  119. $this->callOnWrappedObject('should', array($prediction));
  120. }
  121. function it_adds_CallPrediction_during_shouldBeCalled_call($objectProphecy)
  122. {
  123. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  124. $this->callOnWrappedObject('shouldBeCalled', array());
  125. $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallPrediction');
  126. }
  127. function it_adds_NoCallsPrediction_during_shouldNotBeCalled_call($objectProphecy)
  128. {
  129. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  130. $this->callOnWrappedObject('shouldNotBeCalled', array());
  131. $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\NoCallsPrediction');
  132. }
  133. function it_adds_CallTimesPrediction_during_shouldBeCalledTimes_call($objectProphecy)
  134. {
  135. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  136. $this->callOnWrappedObject('shouldBeCalledTimes', array(5));
  137. $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallTimesPrediction');
  138. }
  139. /**
  140. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  141. * @param \Prophecy\Prediction\PredictionInterface $prediction
  142. * @param \Prophecy\Call\Call $call1
  143. * @param \Prophecy\Call\Call $call2
  144. */
  145. function it_checks_prediction_via_shouldHave_method_call(
  146. $objectProphecy, $arguments, $prediction, $call1, $call2
  147. )
  148. {
  149. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  150. $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
  151. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  152. $this->withArguments($arguments);
  153. $this->callOnWrappedObject('shouldHave', array($prediction));
  154. }
  155. /**
  156. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  157. * @param \Prophecy\Prediction\PredictionInterface $prediction
  158. * @param \Prophecy\Call\Call $call1
  159. * @param \Prophecy\Call\Call $call2
  160. */
  161. function it_sets_return_promise_during_shouldHave_call_if_none_was_set_before(
  162. $objectProphecy, $arguments, $prediction, $call1, $call2
  163. )
  164. {
  165. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  166. $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
  167. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  168. $this->withArguments($arguments);
  169. $this->callOnWrappedObject('shouldHave', array($prediction));
  170. $this->getPromise()->shouldReturnAnInstanceOf('Prophecy\Promise\ReturnPromise');
  171. }
  172. /**
  173. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  174. * @param \Prophecy\Prediction\PredictionInterface $prediction
  175. * @param \Prophecy\Call\Call $call1
  176. * @param \Prophecy\Call\Call $call2
  177. * @param \Prophecy\Promise\PromiseInterface $promise
  178. */
  179. function it_does_not_set_return_promise_during_shouldHave_call_if_it_was_set_before(
  180. $objectProphecy, $arguments, $prediction, $call1, $call2, $promise
  181. )
  182. {
  183. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  184. $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
  185. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  186. $this->will($promise);
  187. $this->withArguments($arguments);
  188. $this->callOnWrappedObject('shouldHave', array($prediction));
  189. $this->getPromise()->shouldReturn($promise);
  190. }
  191. /**
  192. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  193. * @param \Prophecy\Prediction\PredictionInterface $prediction1
  194. * @param \Prophecy\Prediction\PredictionInterface $prediction2
  195. * @param \Prophecy\Call\Call $call1
  196. * @param \Prophecy\Call\Call $call2
  197. * @param \Prophecy\Promise\PromiseInterface $promise
  198. */
  199. function it_records_checked_predictions(
  200. $objectProphecy, $arguments, $prediction1, $prediction2, $call1, $call2, $promise
  201. )
  202. {
  203. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  204. $prediction1->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willReturn();
  205. $prediction2->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willReturn();
  206. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  207. $this->will($promise);
  208. $this->withArguments($arguments);
  209. $this->callOnWrappedObject('shouldHave', array($prediction1));
  210. $this->callOnWrappedObject('shouldHave', array($prediction2));
  211. $this->getCheckedPredictions()->shouldReturn(array($prediction1, $prediction2));
  212. }
  213. /**
  214. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  215. * @param \Prophecy\Prediction\PredictionInterface $prediction
  216. * @param \Prophecy\Call\Call $call1
  217. * @param \Prophecy\Call\Call $call2
  218. * @param \Prophecy\Promise\PromiseInterface $promise
  219. */
  220. function it_records_even_failed_checked_predictions(
  221. $objectProphecy, $arguments, $prediction, $call1, $call2, $promise
  222. )
  223. {
  224. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  225. $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willThrow(new \RuntimeException());
  226. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  227. $this->will($promise);
  228. $this->withArguments($arguments);
  229. try {
  230. $this->callOnWrappedObject('shouldHave', array($prediction));
  231. } catch (\Exception $e) {}
  232. $this->getCheckedPredictions()->shouldReturn(array($prediction));
  233. }
  234. /**
  235. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  236. * @param \Prophecy\Prediction\PredictionInterface $prediction
  237. * @param \Prophecy\Call\Call $call1
  238. * @param \Prophecy\Call\Call $call2
  239. */
  240. function it_checks_prediction_via_shouldHave_method_call_with_callback(
  241. $objectProphecy, $arguments, $prediction, $call1, $call2
  242. )
  243. {
  244. $callback = function ($calls, $object, $method) {
  245. throw new \RuntimeException;
  246. };
  247. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  248. $this->withArguments($arguments);
  249. $this->shouldThrow('RuntimeException')->duringShouldHave($callback);
  250. }
  251. function it_does_nothing_during_checkPrediction_if_no_prediction_set()
  252. {
  253. $this->checkPrediction()->shouldReturn(null);
  254. }
  255. /**
  256. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  257. * @param \Prophecy\Prediction\PredictionInterface $prediction
  258. * @param \Prophecy\Call\Call $call1
  259. * @param \Prophecy\Call\Call $call2
  260. */
  261. function it_checks_set_prediction_during_checkPrediction(
  262. $objectProphecy, $arguments, $prediction, $call1, $call2
  263. )
  264. {
  265. $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
  266. $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
  267. $objectProphecy->addMethodProphecy($this)->willReturn(null);
  268. $this->withArguments($arguments);
  269. $this->callOnWrappedObject('should', array($prediction));
  270. $this->checkPrediction();
  271. }
  272. function it_links_back_to_ObjectProphecy_through_getter($objectProphecy)
  273. {
  274. $this->getObjectProphecy()->shouldReturn($objectProphecy);
  275. }
  276. function it_has_MethodName()
  277. {
  278. $this->getMethodName()->shouldReturn('getName');
  279. }
  280. /**
  281. * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
  282. */
  283. function it_contains_ArgumentsWildcard_it_was_constructed_with($objectProphecy, $wildcard)
  284. {
  285. $this->beConstructedWith($objectProphecy, 'getName', $wildcard);
  286. $this->getArgumentsWildcard()->shouldReturn($wildcard);
  287. }
  288. /**
  289. * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
  290. */
  291. function its_ArgumentWildcard_is_mutable_through_setter($wildcard)
  292. {
  293. $this->withArguments($wildcard);
  294. $this->getArgumentsWildcard()->shouldReturn($wildcard);
  295. }
  296. function its_withArguments_transforms_passed_array_into_ArgumentsWildcard()
  297. {
  298. $this->withArguments(array(42, 33));
  299. $wildcard = $this->getArgumentsWildcard();
  300. $wildcard->shouldNotBe(null);
  301. $wildcard->__toString()->shouldReturn('exact(42), exact(33)');
  302. }
  303. function its_withArguments_throws_exception_if_wrong_arguments_provided()
  304. {
  305. $this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringWithArguments(42);
  306. }
  307. }
  308. class ClassWithFinalMethod
  309. {
  310. final public function finalMethod() {}
  311. }