Пророчество, фиктивные сравнения и ожидания в модульном тестировании php

У меня тест не пройден, хотя я сравниваю некоторые интерфейсы и макеты в пророчестве с 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;
}
}

1

Решение

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

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]