PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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