ObjectProphecySpec.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace spec\Prophecy\Prophecy;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Argument;
  5. class ObjectProphecySpec extends ObjectBehavior
  6. {
  7. /**
  8. * @param \Prophecy\Doubler\LazyDouble $lazyDouble
  9. * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
  10. */
  11. function let($lazyDouble, $double)
  12. {
  13. $this->beConstructedWith($lazyDouble);
  14. $lazyDouble->getInstance()->willReturn($double);
  15. }
  16. function it_implements_ProphecyInterface()
  17. {
  18. $this->shouldBeAnInstanceOf('Prophecy\Prophecy\ProphecyInterface');
  19. }
  20. function it_sets_parentClass_during_willExtend_call($lazyDouble)
  21. {
  22. $lazyDouble->setParentClass('123')->shouldBeCalled();
  23. $this->willExtend('123');
  24. }
  25. function it_adds_interface_during_willImplement_call($lazyDouble)
  26. {
  27. $lazyDouble->addInterface('222')->shouldBeCalled();
  28. $this->willImplement('222');
  29. }
  30. function it_sets_constructor_arguments_during_willBeConstructedWith_call($lazyDouble)
  31. {
  32. $lazyDouble->setArguments(array(1, 2, 5))->shouldBeCalled();
  33. $this->willBeConstructedWith(array(1, 2, 5));
  34. }
  35. function it_does_not_have_method_prophecies_by_default()
  36. {
  37. $this->getMethodProphecies()->shouldHaveCount(0);
  38. }
  39. /**
  40. * @param \Prophecy\Prophecy\MethodProphecy $method1
  41. * @param \Prophecy\Prophecy\MethodProphecy $method2
  42. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  43. */
  44. function it_should_get_method_prophecies_by_method_name($method1, $method2, $arguments)
  45. {
  46. $method1->getMethodName()->willReturn('getName');
  47. $method1->getArgumentsWildcard()->willReturn($arguments);
  48. $method2->getMethodName()->willReturn('setName');
  49. $method2->getArgumentsWildcard()->willReturn($arguments);
  50. $this->addMethodProphecy($method1);
  51. $this->addMethodProphecy($method2);
  52. $methods = $this->getMethodProphecies('setName');
  53. $methods->shouldHaveCount(1);
  54. $methods[0]->getMethodName()->shouldReturn('setName');
  55. }
  56. function it_should_return_empty_array_if_no_method_prophecies_found()
  57. {
  58. $methods = $this->getMethodProphecies('setName');
  59. $methods->shouldHaveCount(0);
  60. }
  61. /**
  62. * @param \Prophecy\Call\CallCenter $callCenter
  63. */
  64. function it_should_proxy_makeProphecyMethodCall_to_CallCenter($lazyDouble, $callCenter)
  65. {
  66. $this->beConstructedWith($lazyDouble, $callCenter);
  67. $callCenter->makeCall($this->getWrappedObject(), 'setName', array('everzet'))->willReturn(42);
  68. $this->makeProphecyMethodCall('setName', array('everzet'))->shouldReturn(42);
  69. }
  70. /**
  71. * @param \Prophecy\Call\CallCenter $callCenter
  72. * @param \Prophecy\Prophecy\RevealerInterface $revealer
  73. */
  74. function it_should_reveal_arguments_and_return_values_from_callCenter(
  75. $lazyDouble, $callCenter, $revealer
  76. )
  77. {
  78. $this->beConstructedWith($lazyDouble, $callCenter, $revealer);
  79. $revealer->reveal(array('question'))->willReturn(array('life'));
  80. $revealer->reveal('answer')->willReturn(42);
  81. $callCenter->makeCall($this->getWrappedObject(), 'setName', array('life'))->willReturn('answer');
  82. $this->makeProphecyMethodCall('setName', array('question'))->shouldReturn(42);
  83. }
  84. /**
  85. * @param \Prophecy\Call\CallCenter $callCenter
  86. * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
  87. * @param \Prophecy\Call\Call $call
  88. */
  89. function it_should_proxy_getProphecyMethodCalls_to_CallCenter(
  90. $lazyDouble, $callCenter, $wildcard, $call
  91. )
  92. {
  93. $this->beConstructedWith($lazyDouble, $callCenter);
  94. $callCenter->findCalls('setName', $wildcard)->willReturn(array($call));
  95. $this->findProphecyMethodCalls('setName', $wildcard)->shouldReturn(array($call));
  96. }
  97. /**
  98. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy
  99. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard
  100. */
  101. function its_addMethodProphecy_adds_method_prophecy(
  102. $methodProphecy, $argumentsWildcard
  103. )
  104. {
  105. $methodProphecy->getArgumentsWildcard()->willReturn($argumentsWildcard);
  106. $methodProphecy->getMethodName()->willReturn('getUsername');
  107. $this->addMethodProphecy($methodProphecy);
  108. $this->getMethodProphecies()->shouldReturn(array(
  109. 'getUsername' => array($methodProphecy)
  110. ));
  111. }
  112. /**
  113. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy1
  114. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy2
  115. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
  116. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
  117. */
  118. function its_addMethodProphecy_handles_prophecies_with_different_arguments(
  119. $methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2
  120. )
  121. {
  122. $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
  123. $methodProphecy1->getMethodName()->willReturn('getUsername');
  124. $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
  125. $methodProphecy2->getMethodName()->willReturn('getUsername');
  126. $this->addMethodProphecy($methodProphecy1);
  127. $this->addMethodProphecy($methodProphecy2);
  128. $this->getMethodProphecies()->shouldReturn(array(
  129. 'getUsername' => array(
  130. $methodProphecy1,
  131. $methodProphecy2,
  132. )
  133. ));
  134. }
  135. /**
  136. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy1
  137. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy2
  138. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
  139. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
  140. */
  141. function its_addMethodProphecy_handles_prophecies_for_different_methods(
  142. $methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2
  143. )
  144. {
  145. $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
  146. $methodProphecy1->getMethodName()->willReturn('getUsername');
  147. $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
  148. $methodProphecy2->getMethodName()->willReturn('isUsername');
  149. $this->addMethodProphecy($methodProphecy1);
  150. $this->addMethodProphecy($methodProphecy2);
  151. $this->getMethodProphecies()->shouldReturn(array(
  152. 'getUsername' => array(
  153. $methodProphecy1
  154. ),
  155. 'isUsername' => array(
  156. $methodProphecy2
  157. )
  158. ));
  159. }
  160. /**
  161. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy
  162. */
  163. function its_addMethodProphecy_throws_exception_when_method_has_no_ArgumentsWildcard(
  164. $methodProphecy
  165. )
  166. {
  167. $methodProphecy->getArgumentsWildcard()->willReturn(null);
  168. $methodProphecy->getObjectProphecy()->willReturn($this);
  169. $methodProphecy->getMethodName()->willReturn('getTitle');
  170. $this->shouldThrow('Prophecy\Exception\Prophecy\MethodProphecyException')->duringAddMethodProphecy(
  171. $methodProphecy
  172. );
  173. }
  174. function it_returns_null_after_checkPredictions_call_if_there_is_no_method_prophecies()
  175. {
  176. $this->checkProphecyMethodsPredictions()->shouldReturn(null);
  177. }
  178. /**
  179. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy1
  180. * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy2
  181. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
  182. * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
  183. */
  184. function it_throws_AggregateException_during_checkPredictions_if_predictions_fail(
  185. $methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2
  186. )
  187. {
  188. $methodProphecy1->getMethodName()->willReturn('getName');
  189. $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
  190. $methodProphecy1->checkPrediction()
  191. ->willThrow('Prophecy\Exception\Prediction\AggregateException');
  192. $methodProphecy2->getMethodName()->willReturn('setName');
  193. $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
  194. $methodProphecy2->checkPrediction()
  195. ->willThrow('Prophecy\Exception\Prediction\AggregateException');
  196. $this->addMethodProphecy($methodProphecy1);
  197. $this->addMethodProphecy($methodProphecy2);
  198. $this->shouldThrow('Prophecy\Exception\Prediction\AggregateException')
  199. ->duringCheckProphecyMethodsPredictions();
  200. }
  201. /**
  202. * @param \Prophecy\Doubler\Doubler $doubler
  203. * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
  204. */
  205. function it_returns_new_MethodProphecy_instance_for_arbitrary_call($doubler, $reflection)
  206. {
  207. $doubler->double(Argument::any())->willReturn($reflection);
  208. $return = $this->getProphecy();
  209. $return->shouldBeAnInstanceOf('Prophecy\Prophecy\MethodProphecy');
  210. $return->getMethodName()->shouldReturn('getProphecy');
  211. }
  212. /**
  213. * @param \Prophecy\Doubler\Doubler $doubler
  214. * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
  215. */
  216. function it_returns_same_MethodProphecy_for_same_registered_signature($doubler, $reflection)
  217. {
  218. $doubler->double(Argument::any())->willReturn($reflection);
  219. $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(1, 2, 3));
  220. $methodProphecy2 = $this->getProphecy(1, 2, 3);
  221. $methodProphecy2->shouldBe($methodProphecy1);
  222. }
  223. /**
  224. * @param \Prophecy\Doubler\Doubler $doubler
  225. * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
  226. */
  227. function it_returns_new_MethodProphecy_for_different_signatures($doubler, $reflection)
  228. {
  229. $doubler->double(Argument::any())->willReturn($reflection);
  230. $value = new ObjectProphecySpecFixtureB('ABC');
  231. $value2 = new ObjectProphecySpecFixtureB('CBA');
  232. $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(1, 2, 3, $value));
  233. $methodProphecy2 = $this->getProphecy(1, 2, 3, $value2);
  234. $methodProphecy2->shouldNotBe($methodProphecy1);
  235. }
  236. /**
  237. * @param \Prophecy\Doubler\Doubler $doubler
  238. * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
  239. */
  240. function it_returns_new_MethodProphecy_for_all_callback_signatures($doubler, $reflection)
  241. {
  242. $doubler->double(Argument::any())->willReturn($reflection);
  243. $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(function(){}));
  244. $methodProphecy2 = $this->getProphecy(function(){});
  245. $methodProphecy2->shouldNotBe($methodProphecy1);
  246. }
  247. }
  248. class ObjectProphecySpecFixtureA
  249. {
  250. public $errors;
  251. }
  252. class ObjectProphecySpecFixtureB extends ObjectProphecySpecFixtureA
  253. {
  254. public $errors;
  255. public $value = null;
  256. public function __construct($value)
  257. {
  258. $this->value = $value;
  259. }
  260. }