php 单元测试中的预言、模拟比较和期望

Prophecy, mocks comparisons and expectations in php Unit testing

我有一个测试失败了,同时比较了一些接口和模拟 prophecy 与 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));
    }
}

这些是结果:

Failed asserting that two variables reference the same object.

如有任何帮助,我们将不胜感激。谢谢

下一段代码是我正在测试的代码。

<?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;
    }
}
/** @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));