2021-02-02 11:59:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\Messenger\Beanstalkd\Tests\Transport;
|
|
|
|
|
|
|
|
use Pheanstalk\Contract\PheanstalkInterface;
|
|
|
|
use Pheanstalk\Job;
|
|
|
|
use Pheanstalk\Response\ArrayResponse;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2023-09-30 19:15:45 +03:00
|
|
|
use RetailCrm\Messenger\Beanstalkd\Storage\LockStorageInterface;
|
2021-02-02 11:59:25 +03:00
|
|
|
use RetailCrm\Messenger\Beanstalkd\Transport\BeanstalkSender;
|
|
|
|
use RetailCrm\Messenger\Beanstalkd\Transport\Connection;
|
|
|
|
use Symfony\Component\Messenger\Envelope;
|
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
|
|
|
|
|
|
|
|
class BeanstalkSenderTest extends TestCase
|
|
|
|
{
|
|
|
|
private $connection;
|
|
|
|
private $serializer;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->connection = $this->createMock(Connection::class);
|
|
|
|
$this->serializer = $this->createMock(SerializerInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendWithoutCheck(): void
|
|
|
|
{
|
|
|
|
$message = ['body' => 'Test message', 'headers' => []];
|
|
|
|
$envelope = new Envelope(new class {
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->serializer->expects(static::once())->method('encode')->with($envelope)->willReturn($message);
|
|
|
|
$this->connection->expects(static::once())->method('serializeJob')->willReturn(json_encode($message));
|
|
|
|
$this->connection->expects(static::once())->method('isNotSendIfExists')->willReturn(false);
|
|
|
|
$this->connection->expects(static::once())->method('send');
|
|
|
|
|
|
|
|
$sender = new BeanstalkSender($this->connection, $this->serializer);
|
|
|
|
$sender->send($envelope);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendWithCheckExist(): void
|
|
|
|
{
|
|
|
|
$message = ['body' => 'Test message', 'headers' => []];
|
|
|
|
$envelope = new Envelope(new class {
|
|
|
|
});
|
|
|
|
|
2023-09-30 19:15:45 +03:00
|
|
|
$lockStorage = $this->createMock(LockStorageInterface::class);
|
|
|
|
$lockStorage->expects(static::once())
|
|
|
|
->method('setLock')
|
|
|
|
->willReturn(false);
|
2021-02-02 11:59:25 +03:00
|
|
|
|
|
|
|
$this->serializer->expects(static::once())->method('encode')->with($envelope)->willReturn($message);
|
|
|
|
$this->connection->expects(static::once())->method('serializeJob')->willReturn(json_encode($message));
|
|
|
|
$this->connection->expects(static::once())->method('isNotSendIfExists')->willReturn(true);
|
|
|
|
$this->connection->expects(static::never())->method('send');
|
2023-09-30 19:15:45 +03:00
|
|
|
$this->connection->method('getLockStorage')->willReturn($lockStorage);
|
2021-02-02 11:59:25 +03:00
|
|
|
|
|
|
|
$sender = new BeanstalkSender($this->connection, $this->serializer);
|
|
|
|
$sender->send($envelope);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendWithCheckNotExist(): void
|
|
|
|
{
|
|
|
|
$message = ['body' => 'Test message', 'headers' => []];
|
|
|
|
$envelope = new Envelope(new class {
|
|
|
|
});
|
|
|
|
|
2023-09-30 19:15:45 +03:00
|
|
|
$lockStorage = $this->createMock(LockStorageInterface::class);
|
|
|
|
$lockStorage->expects(static::never())
|
|
|
|
->method('setLock');
|
2021-02-02 11:59:25 +03:00
|
|
|
|
|
|
|
$this->serializer->expects(static::once())->method('encode')->with($envelope)->willReturn($message);
|
2023-09-30 19:15:45 +03:00
|
|
|
$this->connection->expects(static::once())->method('serializeJob')->willReturn(json_encode($message));
|
|
|
|
$this->connection->expects(static::once())->method('isNotSendIfExists')->willReturn(false);
|
2021-02-02 11:59:25 +03:00
|
|
|
$this->connection->expects(static::once())->method('send');
|
2023-09-30 19:15:45 +03:00
|
|
|
$this->connection->method('getLockStorage')->willReturn($lockStorage);
|
2021-02-02 11:59:25 +03:00
|
|
|
|
|
|
|
$sender = new BeanstalkSender($this->connection, $this->serializer);
|
|
|
|
$sender->send($envelope);
|
|
|
|
}
|
|
|
|
}
|