phpunit-testingphpunitprophecy

Prophecy, mocks comparisons and expectations in php Unit testing


I have a test failing, while comparing some interface and mocks resutls in prophecy with phpunit:

<?php

namespace AppBundle\Test\Service\Message;


use AppBundle\Service\Message\RabbitMqMessageProducerProvider;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use PHPUnit_Framework_TestCase;
use Prophecy\Prophecy\ObjectProphecy;

/**
 * Class RabbitMqMessageProducerProviderTest
 * @package AppBundle\Test\Service\Message
 */
class RabbitMqMessageProducerProviderTest extends PHPUnit_Framework_TestCase
{
    public function testProvide()
    {
        /** @var ProducerInterface|ObjectProphecy $lowProducer */
        $lowProducer = self::prophesize(ProducerInterface::class);

        /** @var ProducerInterface|ObjectProphecy $mediumProducer */
        $mediumProducer = self::prophesize(ProducerInterface::class);

        /** @var ProducerInterface|ObjectProphecy $highProducer */
        $highProducer = self::prophesize(ProducerInterface::class);

        $provider = new RabbitMqMessageProducerProvider(
            $lowProducer->reveal(),
            $mediumProducer->reveal(),
            $highProducer->reveal()
        );

        self::assertSame($lowProducer, $provider->provide(1));
        self::assertSame($mediumProducer, $provider->provide(11));
        self::assertSame($highProducer, $provider->provide(101));
    }
}

These are the resutls:

Failed asserting that two variables reference the same object.

Any help will be appreciated. Thanks.

The next chunk of code is the code that I am testing out.

<?php

namespace AppBundle\Service\Message;


use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;

/**
 * Class RabbitMqMessageProducerProvider
 * @package AppBundle\Service\Message
 */
class RabbitMqMessageProducerProvider
{
    /**
     * @var ProducerInterface
     */
    private $lowPriorityProducer;

    /**
     * @var ProducerInterface
     */
    private $mediumPriorityProducer;

    /**
     * @var ProducerInterface
     */
    private $highPriorityProducer;

    /**
     * RabbitMqMessageProducerProvider constructor.
     * 
     * @param ProducerInterface $lowPriorityProducer
     * @param ProducerInterface $mediumPriorityProducer
     * @param ProducerInterface $highPriorityProducer
     */
    public function __construct(
        ProducerInterface $lowPriorityProducer,
        ProducerInterface $mediumPriorityProducer,
        ProducerInterface $highPriorityProducer
    )
    {
        $this->lowPriorityProducer = $lowPriorityProducer;
        $this->mediumPriorityProducer = $mediumPriorityProducer;
        $this->highPriorityProducer = $highPriorityProducer;
    }


    /**
     * @param int $priority
     * @return ProducerInterface
     */
    public function provide(int $priority)
    {
        $priorityRange = log10($priority);

        if ($priorityRange < 2) {
            return $this->lowPriorityProducer;
        }

        if ($priorityRange < 3) {
            return $this->mediumPriorityProducer;
        }

        return $this->highPriorityProducer;
    }
}

Solution

  • /** @var ProducerInterface|ObjectProphecy $lowProducer */
    $lowProducer = self::prophesize(ProducerInterface::class);
    $lowProducer = $lowProducer->reveal();
    
    /** @var ProducerInterface|ObjectProphecy $mediumProducer */
    $mediumProducer = self::prophesize(ProducerInterface::class);
    $mediumProducer = $mediumProducer->reveal();
    
    /** @var ProducerInterface|ObjectProphecy $highProducer */
    $highProducer = self::prophesize(ProducerInterface::class);
    $highProducer = $highProducer->reveal();
    
    $provider = new RabbitMqMessageProducerProvider(
        $lowProducer,
        $mediumProducer,
        $highProducer
    );
    
    self::assertSame($lowProducer, $provider->provide(1));
    self::assertSame($mediumProducer, $provider->provide(101));
    self::assertSame($highProducer, $provider->provide(1001));