예제 #1
0
 /**
  * Tests that there was at least one call.
  *
  * @param Call[] $calls
  * @param ObjectProphecy $object
  * @param MethodProphecy $method
  *
  * @throws \Prophecy\Exception\Prediction\NoCallsException
  */
 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
 {
     if (count($calls)) {
         return;
     }
     $methodCalls = $object->findProphecyMethodCalls($method->getMethodName(), new ArgumentsWildcard(array(new AnyValuesToken())));
     if (count($methodCalls)) {
         throw new NoCallsException(sprintf("No calls have been made that match:\n" . "  %s->%s(%s)\n" . "but expected at least one.\n" . "Recorded `%s(...)` calls:\n%s", get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard(), $method->getMethodName(), $this->util->stringifyCalls($methodCalls)), $method);
     }
     throw new NoCallsException(sprintf("No calls have been made that match:\n" . "  %s->%s(%s)\n" . "but expected at least one.", get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard()), $method);
 }
 /**
  * Tests that there was exact amount of calls made.
  *
  * @param Call[] $calls        	
  * @param ObjectProphecy $object        	
  * @param MethodProphecy $method        	
  *
  * @throws \Prophecy\Exception\Prediction\UnexpectedCallsCountException
  */
 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
 {
     if ($this->times == count($calls)) {
         return;
     }
     $methodCalls = $object->findProphecyMethodCalls($method->getMethodName(), new ArgumentsWildcard(array(new AnyValuesToken())));
     if (count($calls)) {
         $message = sprintf("Expected exactly %d calls that match:\n" . "  %s->%s(%s)\n" . "but %d were made:\n%s", $this->times, get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard(), count($calls), $this->util->stringifyCalls($calls));
     } elseif (count($methodCalls)) {
         $message = sprintf("Expected exactly %d calls that match:\n" . "  %s->%s(%s)\n" . "but none were made.\n" . "Recorded `%s(...)` calls:\n%s", $this->times, get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard(), $method->getMethodName(), $this->util->stringifyCalls($methodCalls));
     } else {
         $message = sprintf("Expected exactly %d calls that match:\n" . "  %s->%s(%s)\n" . "but none were made.", $this->times, get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard());
     }
     throw new UnexpectedCallsCountException($message, $method, $this->times, $calls);
 }
 /**
  * Tests that there were no calls made.
  *
  * @param Call[]         $calls
  * @param ObjectProphecy $object
  * @param MethodProphecy $method
  *
  * @throws \Prophecy\Exception\Prediction\UnexpectedCallsException
  */
 public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
 {
     if (!count($calls)) {
         return;
     }
     throw new UnexpectedCallsException(sprintf("No calls expected that match:\n" . "  %s->%s(%s)\n" . "but %d were made:\n%s", get_class($object->reveal()), $method->getMethodName(), $method->getArgumentsWildcard(), count($calls), $this->util->stringifyCalls($calls)), $method, $calls);
 }
 /**
  * @param \Prophecy\Prophecy\ObjectProphecy    $object
  * @param \Prophecy\Prophecy\MethodProphecy    $method
  * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  */
 function it_throws_NoCallsException_if_no_calls_found($object, $method, $arguments)
 {
     $method->getObjectProphecy()->willReturn($object);
     $method->getMethodName()->willReturn('getName');
     $method->getArgumentsWildcard()->willReturn($arguments);
     $arguments->__toString()->willReturn('123');
     $object->reveal()->willReturn(new \stdClass());
     $object->findProphecyMethodCalls('getName', Argument::any())->willReturn(array());
     $this->shouldThrow('Prophecy\\Exception\\Prediction\\NoCallsException')->duringCheck(array(), $object, $method);
 }
예제 #5
0
 function it_returns_null_if_method_prophecy_that_matches_makeCall_arguments_has_no_promise($objectProphecy, MethodProphecy $method, ArgumentsWildcard $arguments)
 {
     $method->getMethodName()->willReturn('getName');
     $method->getArgumentsWildcard()->willReturn($arguments);
     $method->getPromise()->willReturn(null);
     $arguments->scoreArguments(array('world', 'everything'))->willReturn(100);
     $objectProphecy->getMethodProphecies()->willReturn(array($method));
     $objectProphecy->getMethodProphecies('getName')->willReturn(array($method));
     $this->makeCall($objectProphecy, 'getName', array('world', 'everything'))->shouldReturn(null);
 }
 /**
  * @param \Prophecy\Prophecy\ObjectProphecy    $object
  * @param \Prophecy\Prophecy\MethodProphecy    $method
  * @param \Prophecy\Call\Call                  $call
  * @param \Prophecy\Argument\ArgumentsWildcard $arguments
  */
 function it_throws_UnexpectedCallsCountException_if_calls_found($object, $method, $call, $arguments)
 {
     $method->getObjectProphecy()->willReturn($object);
     $method->getMethodName()->willReturn('getName');
     $method->getArgumentsWildcard()->willReturn($arguments);
     $arguments->__toString()->willReturn('123');
     $call->getMethodName()->willReturn('getName');
     $call->getArguments()->willReturn(array(5, 4, 'three'));
     $call->getCallPlace()->willReturn('unknown');
     $this->shouldThrow('Prophecy\\Exception\\Prediction\\UnexpectedCallsCountException')->duringCheck(array($call), $object, $method);
 }
예제 #7
0
 function it_should_return_empty_string_if_argument_counts_do_not_match(UnexpectedCallException $exception, ObjectProphecy $objectProphecy, MethodProphecy $prophecy, ArgumentsWildcard $wildcard)
 {
     $exception->getObjectProphecy()->willReturn($objectProphecy);
     $exception->getArguments()->willReturn(array('a', 'b'));
     $exception->getMethodName()->shouldBeCalled();
     $objectProphecy->getMethodProphecies(Argument::any())->willReturn(array($prophecy));
     $objectProphecy->findProphecyMethodCalls(Argument::any(), Argument::any())->willReturn(array());
     $prophecy->getArgumentsWildcard()->willReturn($wildcard);
     $wildcard->getTokens()->willReturn(array('a'));
     $this->presentDifference($exception)->shouldReturn('');
 }
 /**
  * @param \Prophecy\Prophecy\MethodProphecy    $method1
  * @param \Prophecy\Prophecy\MethodProphecy    $method2
  * @param \Prophecy\Argument\ArgumentsWildcard $arguments1
  * @param \Prophecy\Argument\ArgumentsWildcard $arguments2
  */
 function it_throws_AggregateException_if_defined_predictions_fail($method1, $method2, $arguments1, $arguments2)
 {
     $method1->getMethodName()->willReturn('getName');
     $method1->getArgumentsWildcard()->willReturn($arguments1);
     $method1->checkPrediction()->willReturn(null);
     $method2->getMethodName()->willReturn('isSet');
     $method2->getArgumentsWildcard()->willReturn($arguments2);
     $method2->checkPrediction()->willThrow('Prophecy\\Exception\\Prediction\\AggregateException');
     $this->prophesize()->addMethodProphecy($method1);
     $this->prophesize()->addMethodProphecy($method2);
     $this->shouldThrow('Prophecy\\Exception\\Prediction\\AggregateException')->duringCheckPredictions();
 }
 /**
  * @param \Prophecy\Prophecy\MethodProphecy    $methodProphecy1
  * @param \Prophecy\Prophecy\MethodProphecy    $methodProphecy2
  * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
  * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
  */
 function it_throws_AggregateException_during_checkPredictions_if_predictions_fail($methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2)
 {
     $methodProphecy1->getMethodName()->willReturn('getName');
     $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
     $methodProphecy1->checkPrediction()->willThrow('Prophecy\\Exception\\Prediction\\AggregateException');
     $methodProphecy2->getMethodName()->willReturn('setName');
     $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
     $methodProphecy2->checkPrediction()->willThrow('Prophecy\\Exception\\Prediction\\AggregateException');
     $this->addMethodProphecy($methodProphecy1);
     $this->addMethodProphecy($methodProphecy2);
     $this->shouldThrow('Prophecy\\Exception\\Prediction\\AggregateException')->duringCheckProphecyMethodsPredictions();
 }
예제 #10
0
 /**
  * Adds method prophecy to object prophecy.
  *
  * @param MethodProphecy $methodProphecy
  *
  * @throws \Prophecy\Exception\Prophecy\MethodProphecyException If method prophecy doesn't
  *                                                              have arguments wildcard
  */
 public function addMethodProphecy(MethodProphecy $methodProphecy)
 {
     $argumentsWildcard = $methodProphecy->getArgumentsWildcard();
     if (null === $argumentsWildcard) {
         throw new MethodProphecyException(sprintf("Can not add prophecy for a method `%s::%s()`\n" . "as you did not specify arguments wildcard for it.", get_class($this->reveal()), $methodProphecy->getMethodName()), $methodProphecy);
     }
     $methodName = $methodProphecy->getMethodName();
     if (!isset($this->methodProphecies[$methodName])) {
         $this->methodProphecies[$methodName] = array();
     }
     $this->methodProphecies[$methodName][] = $methodProphecy;
 }