LazyDoubleSpec.php 2.72 KB
Newer Older
jhon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<?php

namespace spec\Prophecy\Doubler;

use PhpSpec\ObjectBehavior;

class LazyDoubleSpec extends ObjectBehavior
{
    /**
     * @param \Prophecy\Doubler\Doubler $doubler
     */
    function let($doubler)
    {
        $this->beConstructedWith($doubler);
    }

    /**
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
     */
    function it_returns_anonymous_double_instance_by_default($doubler, $double)
    {
        $doubler->double(null, array())->willReturn($double);

        $this->getInstance()->shouldReturn($double);
    }

    /**
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
     * @param \ReflectionClass                            $class
     */
    function it_returns_class_double_instance_if_set($doubler, $double, $class)
    {
        $doubler->double($class, array())->willReturn($double);

        $this->setParentClass($class);

        $this->getInstance()->shouldReturn($double);
    }

    /**
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double1
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double2
     */
    function it_returns_same_double_instance_if_called_2_times(
        $doubler, $double1, $double2
    )
    {
        $doubler->double(null, array())->willReturn($double1);
        $doubler->double(null, array())->willReturn($double2);

        $this->getInstance()->shouldReturn($double2);
        $this->getInstance()->shouldReturn($double2);
    }

    function its_setParentClass_throws_ClassNotFoundException_if_class_not_found()
    {
        $this->shouldThrow('Prophecy\Exception\Doubler\ClassNotFoundException')
            ->duringSetParentClass('SomeUnexistingClass');
    }

    /**
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
     */
    function its_setParentClass_throws_exception_if_prophecy_is_already_created(
        $doubler, $double
    )
    {
        $doubler->double(null, array())->willReturn($double);

        $this->getInstance();

        $this->shouldThrow('Prophecy\Exception\Doubler\DoubleException')
            ->duringSetParentClass('stdClass');
    }

    function its_addInterface_throws_InterfaceNotFoundException_if_no_interface_found()
    {
        $this->shouldThrow('Prophecy\Exception\Doubler\InterfaceNotFoundException')
            ->duringAddInterface('SomeUnexistingInterface');
    }

    /**
     * @param \Prophecy\Prophecy\ProphecySubjectInterface $double
     */
    function its_addInterface_throws_exception_if_prophecy_is_already_created(
        $doubler, $double
    )
    {
        $doubler->double(null, array())->willReturn($double);

        $this->getInstance();

        $this->shouldThrow('Prophecy\Exception\Doubler\DoubleException')
            ->duringAddInterface('ArrayAccess');
    }
}