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