CallCenterSpec.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace spec\Prophecy\Call;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Prophecy\ObjectProphecy;
  5. use Prophecy\Argument\ArgumentsWildcard;
  6. class CallCenterSpec extends ObjectBehavior
  7. {
  8. /**
  9. * @param \Prophecy\Prophecy\ObjectProphecy $objectProphecy
  10. */
  11. function let($objectProphecy)
  12. {
  13. }
  14. /**
  15. * @param \Prophecy\Prophecy\ObjectProphecy $objectProphecy
  16. * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
  17. */
  18. function it_records_calls_made_through_makeCall_method($objectProphecy, $wildcard)
  19. {
  20. $wildcard->scoreArguments(array(5, 2, 3))->willReturn(10);
  21. $objectProphecy->getMethodProphecies()->willReturn(array());
  22. $this->makeCall($objectProphecy, 'setValues', array(5, 2, 3));
  23. $calls = $this->findCalls('setValues', $wildcard);
  24. $calls->shouldHaveCount(1);
  25. $calls[0]->shouldBeAnInstanceOf('Prophecy\Call\Call');
  26. $calls[0]->getMethodName()->shouldReturn('setValues');
  27. $calls[0]->getArguments()->shouldReturn(array(5, 2, 3));
  28. $calls[0]->getReturnValue()->shouldReturn(null);
  29. }
  30. function it_returns_null_for_any_call_through_makeCall_if_no_method_prophecies_added(
  31. $objectProphecy
  32. )
  33. {
  34. $objectProphecy->getMethodProphecies()->willReturn(array());
  35. $this->makeCall($objectProphecy, 'setValues', array(5, 2, 3))->shouldReturn(null);
  36. }
  37. /**
  38. * @param \Prophecy\Prophecy\MethodProphecy $method1
  39. * @param \Prophecy\Prophecy\MethodProphecy $method2
  40. * @param \Prophecy\Prophecy\MethodProphecy $method3
  41. * @param \Prophecy\Argument\ArgumentsWildcard $arguments1
  42. * @param \Prophecy\Argument\ArgumentsWildcard $arguments2
  43. * @param \Prophecy\Argument\ArgumentsWildcard $arguments3
  44. * @param \Prophecy\Promise\PromiseInterface $promise
  45. */
  46. function it_executes_promise_of_method_prophecy_that_matches_signature_passed_to_makeCall(
  47. $objectProphecy, $method1, $method2, $method3, $arguments1, $arguments2, $arguments3,
  48. $promise
  49. )
  50. {
  51. $method1->getMethodName()->willReturn('getName');
  52. $method1->getArgumentsWildcard()->willReturn($arguments1);
  53. $arguments1->scoreArguments(array('world', 'everything'))->willReturn(false);
  54. $method2->getMethodName()->willReturn('setTitle');
  55. $method2->getArgumentsWildcard()->willReturn($arguments2);
  56. $arguments2->scoreArguments(array('world', 'everything'))->willReturn(false);
  57. $method3->getMethodName()->willReturn('getName');
  58. $method3->getArgumentsWildcard()->willReturn($arguments3);
  59. $method3->getPromise()->willReturn($promise);
  60. $arguments3->scoreArguments(array('world', 'everything'))->willReturn(200);
  61. $objectProphecy->getMethodProphecies()->willReturn(array(
  62. 'method1' => array($method1),
  63. 'method2' => array($method2, $method3)
  64. ));
  65. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method1, $method3));
  66. $objectProphecy->reveal()->willReturn(new \stdClass());
  67. $promise->execute(array('world', 'everything'), $objectProphecy->getWrappedObject(), $method3)->willReturn(42);
  68. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))->shouldReturn(42);
  69. $calls = $this->findCalls('getName', $arguments3);
  70. $calls->shouldHaveCount(1);
  71. $calls[0]->getReturnValue()->shouldReturn(42);
  72. }
  73. /**
  74. * @param \Prophecy\Prophecy\MethodProphecy $method1
  75. * @param \Prophecy\Prophecy\MethodProphecy $method2
  76. * @param \Prophecy\Prophecy\MethodProphecy $method3
  77. * @param \Prophecy\Argument\ArgumentsWildcard $arguments1
  78. * @param \Prophecy\Argument\ArgumentsWildcard $arguments2
  79. * @param \Prophecy\Argument\ArgumentsWildcard $arguments3
  80. * @param \Prophecy\Promise\PromiseInterface $promise
  81. */
  82. function it_executes_promise_of_method_prophecy_that_matches_with_highest_score_to_makeCall(
  83. $objectProphecy, $method1, $method2, $method3, $arguments1, $arguments2, $arguments3,
  84. $promise
  85. )
  86. {
  87. $method1->getMethodName()->willReturn('getName');
  88. $method1->getArgumentsWildcard()->willReturn($arguments1);
  89. $arguments1->scoreArguments(array('world', 'everything'))->willReturn(50);
  90. $method2->getMethodName()->willReturn('getName');
  91. $method2->getArgumentsWildcard()->willReturn($arguments2);
  92. $method2->getPromise()->willReturn($promise);
  93. $arguments2->scoreArguments(array('world', 'everything'))->willReturn(300);
  94. $method3->getMethodName()->willReturn('getName');
  95. $method3->getArgumentsWildcard()->willReturn($arguments3);
  96. $arguments3->scoreArguments(array('world', 'everything'))->willReturn(200);
  97. $objectProphecy->getMethodProphecies()->willReturn(array(
  98. 'method1' => array($method1),
  99. 'method2' => array($method2, $method3)
  100. ));
  101. $objectProphecy->getMethodProphecies('getName')->willReturn(array(
  102. $method1, $method2, $method3
  103. ));
  104. $objectProphecy->reveal()->willReturn(new \stdClass());
  105. $promise->execute(array('world', 'everything'), $objectProphecy->getWrappedObject(), $method2)
  106. ->willReturn('second');
  107. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))
  108. ->shouldReturn('second');
  109. }
  110. /**
  111. * @param \Prophecy\Prophecy\MethodProphecy $method
  112. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  113. */
  114. function it_throws_exception_if_call_does_not_match_any_of_defined_method_prophecies(
  115. $objectProphecy, $method, $arguments
  116. )
  117. {
  118. $method->getMethodName()->willReturn('getName');
  119. $method->getArgumentsWildcard()->willReturn($arguments);
  120. $arguments->scoreArguments(array('world', 'everything'))->willReturn(false);
  121. $arguments->__toString()->willReturn('arg1, arg2');
  122. $objectProphecy->getMethodProphecies()->willReturn(array('method1' => array($method)));
  123. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method));
  124. $this->shouldThrow('Prophecy\Exception\Call\UnexpectedCallException')
  125. ->duringMakeCall($objectProphecy, 'getName', array('world', 'everything'));
  126. }
  127. /**
  128. * @param \Prophecy\Prophecy\MethodProphecy $method
  129. * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  130. */
  131. function it_returns_null_if_method_prophecy_that_matches_makeCall_arguments_has_no_promise(
  132. $objectProphecy, $method, $arguments
  133. )
  134. {
  135. $method->getMethodName()->willReturn('getName');
  136. $method->getArgumentsWildcard()->willReturn($arguments);
  137. $method->getPromise()->willReturn(null);
  138. $arguments->scoreArguments(array('world', 'everything'))->willReturn(100);
  139. $objectProphecy->getMethodProphecies()->willReturn(array($method));
  140. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method));
  141. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))
  142. ->shouldReturn(null);
  143. }
  144. /**
  145. * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
  146. */
  147. function it_finds_recorded_calls_by_a_method_name_and_arguments_wildcard(
  148. $objectProphecy, $wildcard
  149. )
  150. {
  151. $objectProphecy->getMethodProphecies()->willReturn(array());
  152. $this->makeCall($objectProphecy, 'getName', array('world'));
  153. $this->makeCall($objectProphecy, 'getName', array('everything'));
  154. $this->makeCall($objectProphecy, 'setName', array(42));
  155. $wildcard->scoreArguments(array('world'))->willReturn(false);
  156. $wildcard->scoreArguments(array('everything'))->willReturn(10);
  157. $calls = $this->findCalls('getName', $wildcard);
  158. $calls->shouldHaveCount(1);
  159. $calls[0]->getMethodName()->shouldReturn('getName');
  160. $calls[0]->getArguments()->shouldReturn(array('everything'));
  161. }
  162. }