No Description

CallCenterSpec.php 7.7KB

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