ObjectProphecySpec.php 9.6 KB

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