CallCenterSpec.php 7.3 KB

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