| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace spec\Prophecy\Prophecy;
- use PhpSpec\ObjectBehavior;
- use Prophecy\Argument;
- class ObjectProphecySpec extends ObjectBehavior
- {
- /**
- * @param \Prophecy\Doubler\LazyDouble $lazyDouble
- * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
- */
- function let($lazyDouble, $double)
- {
- $this->beConstructedWith($lazyDouble);
- $lazyDouble->getInstance()->willReturn($double);
- }
- function it_implements_ProphecyInterface()
- {
- $this->shouldBeAnInstanceOf('Prophecy\Prophecy\ProphecyInterface');
- }
- function it_sets_parentClass_during_willExtend_call($lazyDouble)
- {
- $lazyDouble->setParentClass('123')->shouldBeCalled();
- $this->willExtend('123');
- }
- function it_adds_interface_during_willImplement_call($lazyDouble)
- {
- $lazyDouble->addInterface('222')->shouldBeCalled();
- $this->willImplement('222');
- }
- function it_sets_constructor_arguments_during_willBeConstructedWith_call($lazyDouble)
- {
- $lazyDouble->setArguments(array(1, 2, 5))->shouldBeCalled();
- $this->willBeConstructedWith(array(1, 2, 5));
- }
- function it_does_not_have_method_prophecies_by_default()
- {
- $this->getMethodProphecies()->shouldHaveCount(0);
- }
- /**
- * @param \Prophecy\Prophecy\MethodProphecy $method1
- * @param \Prophecy\Prophecy\MethodProphecy $method2
- * @param \Prophecy\Argument\ArgumentsWildcard $arguments
- */
- function it_should_get_method_prophecies_by_method_name($method1, $method2, $arguments)
- {
- $method1->getMethodName()->willReturn('getName');
- $method1->getArgumentsWildcard()->willReturn($arguments);
- $method2->getMethodName()->willReturn('setName');
- $method2->getArgumentsWildcard()->willReturn($arguments);
- $this->addMethodProphecy($method1);
- $this->addMethodProphecy($method2);
- $methods = $this->getMethodProphecies('setName');
- $methods->shouldHaveCount(1);
- $methods[0]->getMethodName()->shouldReturn('setName');
- }
- function it_should_return_empty_array_if_no_method_prophecies_found()
- {
- $methods = $this->getMethodProphecies('setName');
- $methods->shouldHaveCount(0);
- }
- /**
- * @param \Prophecy\Call\CallCenter $callCenter
- */
- function it_should_proxy_makeProphecyMethodCall_to_CallCenter($lazyDouble, $callCenter)
- {
- $this->beConstructedWith($lazyDouble, $callCenter);
- $callCenter->makeCall($this->getWrappedObject(), 'setName', array('everzet'))->willReturn(42);
- $this->makeProphecyMethodCall('setName', array('everzet'))->shouldReturn(42);
- }
- /**
- * @param \Prophecy\Call\CallCenter $callCenter
- * @param \Prophecy\Prophecy\RevealerInterface $revealer
- */
- function it_should_reveal_arguments_and_return_values_from_callCenter(
- $lazyDouble, $callCenter, $revealer
- )
- {
- $this->beConstructedWith($lazyDouble, $callCenter, $revealer);
- $revealer->reveal(array('question'))->willReturn(array('life'));
- $revealer->reveal('answer')->willReturn(42);
- $callCenter->makeCall($this->getWrappedObject(), 'setName', array('life'))->willReturn('answer');
- $this->makeProphecyMethodCall('setName', array('question'))->shouldReturn(42);
- }
- /**
- * @param \Prophecy\Call\CallCenter $callCenter
- * @param \Prophecy\Argument\ArgumentsWildcard $wildcard
- * @param \Prophecy\Call\Call $call
- */
- function it_should_proxy_getProphecyMethodCalls_to_CallCenter(
- $lazyDouble, $callCenter, $wildcard, $call
- )
- {
- $this->beConstructedWith($lazyDouble, $callCenter);
- $callCenter->findCalls('setName', $wildcard)->willReturn(array($call));
- $this->findProphecyMethodCalls('setName', $wildcard)->shouldReturn(array($call));
- }
- /**
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy
- * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard
- */
- function its_addMethodProphecy_adds_method_prophecy(
- $methodProphecy, $argumentsWildcard
- )
- {
- $methodProphecy->getArgumentsWildcard()->willReturn($argumentsWildcard);
- $methodProphecy->getMethodName()->willReturn('getUsername');
- $this->addMethodProphecy($methodProphecy);
- $this->getMethodProphecies()->shouldReturn(array(
- 'getUsername' => array($methodProphecy)
- ));
- }
- /**
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy1
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy2
- * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
- * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
- */
- function its_addMethodProphecy_handles_prophecies_with_different_arguments(
- $methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2
- )
- {
- $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
- $methodProphecy1->getMethodName()->willReturn('getUsername');
- $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
- $methodProphecy2->getMethodName()->willReturn('getUsername');
- $this->addMethodProphecy($methodProphecy1);
- $this->addMethodProphecy($methodProphecy2);
- $this->getMethodProphecies()->shouldReturn(array(
- 'getUsername' => array(
- $methodProphecy1,
- $methodProphecy2,
- )
- ));
- }
- /**
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy1
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy2
- * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard1
- * @param \Prophecy\Argument\ArgumentsWildcard $argumentsWildcard2
- */
- function its_addMethodProphecy_handles_prophecies_for_different_methods(
- $methodProphecy1, $methodProphecy2, $argumentsWildcard1, $argumentsWildcard2
- )
- {
- $methodProphecy1->getArgumentsWildcard()->willReturn($argumentsWildcard1);
- $methodProphecy1->getMethodName()->willReturn('getUsername');
- $methodProphecy2->getArgumentsWildcard()->willReturn($argumentsWildcard2);
- $methodProphecy2->getMethodName()->willReturn('isUsername');
- $this->addMethodProphecy($methodProphecy1);
- $this->addMethodProphecy($methodProphecy2);
- $this->getMethodProphecies()->shouldReturn(array(
- 'getUsername' => array(
- $methodProphecy1
- ),
- 'isUsername' => array(
- $methodProphecy2
- )
- ));
- }
- /**
- * @param \Prophecy\Prophecy\MethodProphecy $methodProphecy
- */
- function its_addMethodProphecy_throws_exception_when_method_has_no_ArgumentsWildcard(
- $methodProphecy
- )
- {
- $methodProphecy->getArgumentsWildcard()->willReturn(null);
- $methodProphecy->getObjectProphecy()->willReturn($this);
- $methodProphecy->getMethodName()->willReturn('getTitle');
- $this->shouldThrow('Prophecy\Exception\Prophecy\MethodProphecyException')->duringAddMethodProphecy(
- $methodProphecy
- );
- }
- function it_returns_null_after_checkPredictions_call_if_there_is_no_method_prophecies()
- {
- $this->checkProphecyMethodsPredictions()->shouldReturn(null);
- }
- /**
- * @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();
- }
- /**
- * @param \Prophecy\Doubler\Doubler $doubler
- * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
- */
- function it_returns_new_MethodProphecy_instance_for_arbitrary_call($doubler, $reflection)
- {
- $doubler->double(Argument::any())->willReturn($reflection);
- $return = $this->getProphecy();
- $return->shouldBeAnInstanceOf('Prophecy\Prophecy\MethodProphecy');
- $return->getMethodName()->shouldReturn('getProphecy');
- }
- /**
- * @param \Prophecy\Doubler\Doubler $doubler
- * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
- */
- function it_returns_same_MethodProphecy_for_same_registered_signature($doubler, $reflection)
- {
- $doubler->double(Argument::any())->willReturn($reflection);
- $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(1, 2, 3));
- $methodProphecy2 = $this->getProphecy(1, 2, 3);
- $methodProphecy2->shouldBe($methodProphecy1);
- }
- /**
- * @param \Prophecy\Doubler\Doubler $doubler
- * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
- */
- function it_returns_new_MethodProphecy_for_different_signatures($doubler, $reflection)
- {
- $doubler->double(Argument::any())->willReturn($reflection);
- $value = new ObjectProphecySpecFixtureB('ABC');
- $value2 = new ObjectProphecySpecFixtureB('CBA');
- $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(1, 2, 3, $value));
- $methodProphecy2 = $this->getProphecy(1, 2, 3, $value2);
- $methodProphecy2->shouldNotBe($methodProphecy1);
- }
- /**
- * @param \Prophecy\Doubler\Doubler $doubler
- * @param \Prophecy\Prophecy\ProphecySubjectInterface $reflection
- */
- function it_returns_new_MethodProphecy_for_all_callback_signatures($doubler, $reflection)
- {
- $doubler->double(Argument::any())->willReturn($reflection);
- $this->addMethodProphecy($methodProphecy1 = $this->getProphecy(function(){}));
- $methodProphecy2 = $this->getProphecy(function(){});
- $methodProphecy2->shouldNotBe($methodProphecy1);
- }
- }
- class ObjectProphecySpecFixtureA
- {
- public $errors;
- }
- class ObjectProphecySpecFixtureB extends ObjectProphecySpecFixtureA
- {
- public $errors;
- public $value = null;
- public function __construct($value)
- {
- $this->value = $value;
- }
- }
|