1

我有一个测试失败,同时将预言中的一些接口和模拟结果与 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;
    }
}
4

1 回答 1

2
/** @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));
于 2016-12-03T18:43:21.077 回答