123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- namespace spec\Prophecy\Prophecy;
- use PhpSpec\ObjectBehavior;
- use Prophecy\Argument;
- use Prophecy\Argument\ArgumentsWildcard;
- use Prophecy\Call\Call;
- use Prophecy\Prediction\PredictionInterface;
- use Prophecy\Promise\CallbackPromise;
- use Prophecy\Promise\PromiseInterface;
- use Prophecy\Prophecy\ObjectProphecy;
- class ClassWithFinalMethod
- {
- final public function finalMethod() {}
- }
- class MethodProphecySpec extends ObjectBehavior
- {
- function let(ObjectProphecy $objectProphecy, \ReflectionClass $reflection)
- {
- $objectProphecy->reveal()->willReturn($reflection);
- $this->beConstructedWith($objectProphecy, 'getName', null);
- }
- function it_is_initializable()
- {
- $this->shouldHaveType('Prophecy\Prophecy\MethodProphecy');
- }
- function its_constructor_throws_MethodNotFoundException_for_unexisting_method($objectProphecy)
- {
- $this->shouldThrow('Prophecy\Exception\Doubler\MethodNotFoundException')->during(
- '__construct', array($objectProphecy, 'getUnexisting', null)
- );
- }
- function its_constructor_throws_MethodProphecyException_for_final_methods($objectProphecy, ClassWithFinalMethod $subject)
- {
- $objectProphecy->reveal()->willReturn($subject);
- $this->shouldThrow('Prophecy\Exception\Prophecy\MethodProphecyException')->during(
- '__construct', array($objectProphecy, 'finalMethod', null)
- );
- }
- function its_constructor_transforms_array_passed_as_3rd_argument_to_ArgumentsWildcard(
- $objectProphecy
- )
- {
- $this->beConstructedWith($objectProphecy, 'getName', array(42, 33));
- $wildcard = $this->getArgumentsWildcard();
- $wildcard->shouldNotBe(null);
- $wildcard->__toString()->shouldReturn('exact(42), exact(33)');
- }
- function its_constructor_does_not_touch_third_argument_if_it_is_null($objectProphecy)
- {
- $this->beConstructedWith($objectProphecy, 'getName', null);
- $wildcard = $this->getArgumentsWildcard();
- $wildcard->shouldBe(null);
- }
- function its_constructor_records_default_callback_promise_for_return_type_hinted_methods(
- $objectProphecy, ClassWithVoidTypeHintedMethods $subject
- )
- {
- // Return void type hint language feature only introduced in >=7.1
- if (version_compare(PHP_VERSION, '7.1', '>='))
- {
- $objectProphecy->addMethodProphecy(Argument::cetera())->willReturn(null);
- $objectProphecy->reveal()->willReturn($subject);
- $this->beConstructedWith($objectProphecy, 'getVoid');
- $this->getPromise()->shouldBeAnInstanceOf(CallbackPromise::class);
- }
- }
- function its_constructor_records_promise_that_returns_null_for_void_type_hinted_methods(
- $objectProphecy, ClassWithVoidTypeHintedMethods $subject
- )
- {
- // Return void type hint language feature only introduced in >=7.1
- if (version_compare(PHP_VERSION, '7.1', '>='))
- {
- $objectProphecy->addMethodProphecy(Argument::cetera())->willReturn(null);
- $objectProphecy->reveal()->willReturn($subject);
- $this->beConstructedWith($objectProphecy, 'getVoid');
- $this->getPromise()->execute([], $objectProphecy, $this)->shouldBeNull();
- }
- }
- function its_constructor_adds_itself_to_ObjectProphecy_for_return_type_hinted_methods(
- $objectProphecy, ClassWithVoidTypeHintedMethods $subject
- )
- {
- // Return void type hint language feature only introduced in >=7.1
- if (version_compare(PHP_VERSION, '7.1', '>='))
- {
- $objectProphecy->addMethodProphecy(Argument::cetera())->willReturn(null);
- $objectProphecy->reveal()->willReturn($subject);
- $this->beConstructedWith($objectProphecy, 'getVoid');
- $objectProphecy->addMethodProphecy($this)->shouldHaveBeenCalled();
- }
- }
- function it_records_promise_through_will_method(PromiseInterface $promise, $objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->will($promise);
- $this->getPromise()->shouldReturn($promise);
- }
- function it_adds_itself_to_ObjectProphecy_during_call_to_will(PromiseInterface $objectProphecy, $promise)
- {
- $objectProphecy->addMethodProphecy($this)->shouldBeCalled();
- $this->will($promise);
- }
- function it_adds_ReturnPromise_during_willReturn_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->willReturn(42);
- $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ReturnPromise');
- }
- function it_adds_ThrowPromise_during_willThrow_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->willThrow('RuntimeException');
- $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ThrowPromise');
- }
- function it_adds_ReturnArgumentPromise_during_willReturnArgument_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->willReturnArgument();
- $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\ReturnArgumentPromise');
- }
- function it_adds_ReturnArgumentPromise_during_willReturnArgument_call_with_index_argument($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->willReturnArgument(1);
- $promise = $this->getPromise();
- $promise->shouldBeAnInstanceOf('Prophecy\Promise\ReturnArgumentPromise');
- $promise->execute(array('one', 'two'), $objectProphecy, $this)->shouldReturn('two');
- }
- function it_adds_CallbackPromise_during_will_call_with_callback_argument($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $callback = function () {};
- $this->will($callback);
- $this->getPromise()->shouldBeAnInstanceOf('Prophecy\Promise\CallbackPromise');
- }
- function it_records_prediction_through_should_method(PredictionInterface $prediction, $objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->callOnWrappedObject('should', array($prediction));
- $this->getPrediction()->shouldReturn($prediction);
- }
- function it_adds_CallbackPrediction_during_should_call_with_callback_argument($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $callback = function () {};
- $this->callOnWrappedObject('should', array($callback));
- $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallbackPrediction');
- }
- function it_adds_itself_to_ObjectProphecy_during_call_to_should($objectProphecy, PredictionInterface $prediction)
- {
- $objectProphecy->addMethodProphecy($this)->shouldBeCalled();
- $this->callOnWrappedObject('should', array($prediction));
- }
- function it_adds_CallPrediction_during_shouldBeCalled_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->callOnWrappedObject('shouldBeCalled', array());
- $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallPrediction');
- }
- function it_adds_NoCallsPrediction_during_shouldNotBeCalled_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->callOnWrappedObject('shouldNotBeCalled', array());
- $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\NoCallsPrediction');
- }
- function it_adds_CallTimesPrediction_during_shouldBeCalledTimes_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->callOnWrappedObject('shouldBeCalledTimes', array(5));
- $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallTimesPrediction');
- }
- function it_adds_CallTimesPrediction_during_shouldBeCalledOnce_call($objectProphecy)
- {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->callOnWrappedObject('shouldBeCalledOnce');
- $this->getPrediction()->shouldBeAnInstanceOf('Prophecy\Prediction\CallTimesPrediction');
- }
- function it_checks_prediction_via_shouldHave_method_call(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction,
- Call $call1,
- Call $call2
- ) {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->withArguments($arguments);
- $this->callOnWrappedObject('shouldHave', array($prediction));
- }
- function it_sets_return_promise_during_shouldHave_call_if_none_was_set_before(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction,
- Call $call1,
- Call $call2
- ) {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->withArguments($arguments);
- $this->callOnWrappedObject('shouldHave', array($prediction));
- $this->getPromise()->shouldReturnAnInstanceOf('Prophecy\Promise\ReturnPromise');
- }
- function it_does_not_set_return_promise_during_shouldHave_call_if_it_was_set_before(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction,
- Call $call1,
- Call $call2,
- PromiseInterface $promise
- ) {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->will($promise);
- $this->withArguments($arguments);
- $this->callOnWrappedObject('shouldHave', array($prediction));
- $this->getPromise()->shouldReturn($promise);
- }
- function it_records_checked_predictions(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction1,
- PredictionInterface $prediction2,
- Call $call1,
- Call $call2,
- PromiseInterface $promise
- ) {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $prediction1->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willReturn();
- $prediction2->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willReturn();
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->will($promise);
- $this->withArguments($arguments);
- $this->callOnWrappedObject('shouldHave', array($prediction1));
- $this->callOnWrappedObject('shouldHave', array($prediction2));
- $this->getCheckedPredictions()->shouldReturn(array($prediction1, $prediction2));
- }
- function it_records_even_failed_checked_predictions(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction,
- Call $call1,
- Call $call2,
- PromiseInterface $promise
- ) {
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->willThrow(new \RuntimeException());
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->will($promise);
- $this->withArguments($arguments);
- try {
- $this->callOnWrappedObject('shouldHave', array($prediction));
- } catch (\Exception $e) {}
- $this->getCheckedPredictions()->shouldReturn(array($prediction));
- }
- function it_checks_prediction_via_shouldHave_method_call_with_callback(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- Call $call1,
- Call $call2
- ) {
- $callback = function ($calls, $object, $method) {
- throw new \RuntimeException;
- };
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $this->withArguments($arguments);
- $this->shouldThrow('RuntimeException')->duringShouldHave($callback);
- }
- function it_does_nothing_during_checkPrediction_if_no_prediction_set()
- {
- $this->checkPrediction()->shouldReturn(null);
- }
- function it_checks_set_prediction_during_checkPrediction(
- $objectProphecy,
- ArgumentsWildcard $arguments,
- PredictionInterface $prediction,
- Call $call1,
- Call $call2
- ) {
- $prediction->check(array($call1, $call2), $objectProphecy->getWrappedObject(), $this)->shouldBeCalled();
- $objectProphecy->findProphecyMethodCalls('getName', $arguments)->willReturn(array($call1, $call2));
- $objectProphecy->addMethodProphecy($this)->willReturn(null);
- $this->withArguments($arguments);
- $this->callOnWrappedObject('should', array($prediction));
- $this->checkPrediction();
- }
- function it_links_back_to_ObjectProphecy_through_getter($objectProphecy)
- {
- $this->getObjectProphecy()->shouldReturn($objectProphecy);
- }
- function it_has_MethodName()
- {
- $this->getMethodName()->shouldReturn('getName');
- }
- function it_contains_ArgumentsWildcard_it_was_constructed_with($objectProphecy, ArgumentsWildcard $wildcard)
- {
- $this->beConstructedWith($objectProphecy, 'getName', $wildcard);
- $this->getArgumentsWildcard()->shouldReturn($wildcard);
- }
- function its_ArgumentWildcard_is_mutable_through_setter(ArgumentsWildcard $wildcard)
- {
- $this->withArguments($wildcard);
- $this->getArgumentsWildcard()->shouldReturn($wildcard);
- }
- function its_withArguments_transforms_passed_array_into_ArgumentsWildcard()
- {
- $this->withArguments(array(42, 33));
- $wildcard = $this->getArgumentsWildcard();
- $wildcard->shouldNotBe(null);
- $wildcard->__toString()->shouldReturn('exact(42), exact(33)');
- }
- function its_withArguments_throws_exception_if_wrong_arguments_provided()
- {
- $this->shouldThrow('Prophecy\Exception\InvalidArgumentException')->duringWithArguments(42);
- }
- }
- // Return void type hint language feature only introduced in >=7.1
- if (version_compare(PHP_VERSION, '7.1', '>=')) {
- class ClassWithVoidTypeHintedMethods
- {
- public function getVoid(): void
- {
- }
- }
- } else {
- class ClassWithVoidTypeHintedMethods
- {
- }
- }
|