MethodProphecySpec.php 15 KB

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