Geen omschrijving

CallCenterSpec.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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->getMethodName()->willReturn('getName');
  43. $method1->getArgumentsWildcard()->willReturn($arguments1);
  44. $arguments1->scoreArguments(array('world', 'everything'))->willReturn(false);
  45. $method2->getMethodName()->willReturn('setTitle');
  46. $method2->getArgumentsWildcard()->willReturn($arguments2);
  47. $arguments2->scoreArguments(array('world', 'everything'))->willReturn(false);
  48. $method3->getMethodName()->willReturn('getName');
  49. $method3->getArgumentsWildcard()->willReturn($arguments3);
  50. $method3->getPromise()->willReturn($promise);
  51. $arguments3->scoreArguments(array('world', 'everything'))->willReturn(200);
  52. $objectProphecy->getMethodProphecies()->willReturn(array(
  53. 'method1' => array($method1),
  54. 'method2' => array($method2, $method3)
  55. ));
  56. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method1, $method3));
  57. $objectProphecy->reveal()->willReturn(new \stdClass());
  58. $promise->execute(array('world', 'everything'), $objectProphecy->getWrappedObject(), $method3)->willReturn(42);
  59. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))->shouldReturn(42);
  60. $calls = $this->findCalls('getName', $arguments3);
  61. $calls->shouldHaveCount(1);
  62. $calls[0]->getReturnValue()->shouldReturn(42);
  63. }
  64. function it_executes_promise_of_method_prophecy_that_matches_with_highest_score_to_makeCall(
  65. $objectProphecy,
  66. MethodProphecy $method1,
  67. MethodProphecy $method2,
  68. MethodProphecy $method3,
  69. ArgumentsWildcard $arguments1,
  70. ArgumentsWildcard $arguments2,
  71. ArgumentsWildcard $arguments3,
  72. PromiseInterface $promise
  73. ) {
  74. $method1->getMethodName()->willReturn('getName');
  75. $method1->getArgumentsWildcard()->willReturn($arguments1);
  76. $arguments1->scoreArguments(array('world', 'everything'))->willReturn(50);
  77. $method2->getMethodName()->willReturn('getName');
  78. $method2->getArgumentsWildcard()->willReturn($arguments2);
  79. $method2->getPromise()->willReturn($promise);
  80. $arguments2->scoreArguments(array('world', 'everything'))->willReturn(300);
  81. $method3->getMethodName()->willReturn('getName');
  82. $method3->getArgumentsWildcard()->willReturn($arguments3);
  83. $arguments3->scoreArguments(array('world', 'everything'))->willReturn(200);
  84. $objectProphecy->getMethodProphecies()->willReturn(array(
  85. 'method1' => array($method1),
  86. 'method2' => array($method2, $method3)
  87. ));
  88. $objectProphecy->getMethodProphecies('getName')->willReturn(array(
  89. $method1, $method2, $method3
  90. ));
  91. $objectProphecy->reveal()->willReturn(new \stdClass());
  92. $promise->execute(array('world', 'everything'), $objectProphecy->getWrappedObject(), $method2)
  93. ->willReturn('second');
  94. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))
  95. ->shouldReturn('second');
  96. }
  97. function it_throws_exception_if_call_does_not_match_any_of_defined_method_prophecies(
  98. $objectProphecy,
  99. MethodProphecy $method,
  100. ArgumentsWildcard $arguments
  101. ) {
  102. $method->getMethodName()->willReturn('getName');
  103. $method->getArgumentsWildcard()->willReturn($arguments);
  104. $arguments->scoreArguments(array('world', 'everything'))->willReturn(false);
  105. $arguments->__toString()->willReturn('arg1, arg2');
  106. $objectProphecy->getMethodProphecies()->willReturn(array('method1' => array($method)));
  107. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method));
  108. $this->shouldThrow('Prophecy\Exception\Call\UnexpectedCallException')
  109. ->duringMakeCall($objectProphecy, 'getName', array('world', 'everything'));
  110. }
  111. function it_returns_null_if_method_prophecy_that_matches_makeCall_arguments_has_no_promise(
  112. $objectProphecy,
  113. MethodProphecy $method,
  114. ArgumentsWildcard $arguments
  115. ) {
  116. $method->getMethodName()->willReturn('getName');
  117. $method->getArgumentsWildcard()->willReturn($arguments);
  118. $method->getPromise()->willReturn(null);
  119. $arguments->scoreArguments(array('world', 'everything'))->willReturn(100);
  120. $objectProphecy->getMethodProphecies()->willReturn(array($method));
  121. $objectProphecy->getMethodProphecies('getName')->willReturn(array($method));
  122. $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))
  123. ->shouldReturn(null);
  124. }
  125. function it_finds_recorded_calls_by_a_method_name_and_arguments_wildcard(
  126. $objectProphecy,
  127. ArgumentsWildcard $wildcard
  128. ) {
  129. $objectProphecy->getMethodProphecies()->willReturn(array());
  130. $this->makeCall($objectProphecy, 'getName', array('world'));
  131. $this->makeCall($objectProphecy, 'getName', array('everything'));
  132. $this->makeCall($objectProphecy, 'setName', array(42));
  133. $wildcard->scoreArguments(array('world'))->willReturn(false);
  134. $wildcard->scoreArguments(array('everything'))->willReturn(10);
  135. $calls = $this->findCalls('getName', $wildcard);
  136. $calls->shouldHaveCount(1);
  137. $calls[0]->getMethodName()->shouldReturn('getName');
  138. $calls[0]->getArguments()->shouldReturn(array('everything'));
  139. }
  140. }