101 lines
3.9 KiB
PHP
101 lines
3.9 KiB
PHP
<?php
|
|
namespace Incoviba\Test\Service\MQTT;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
use xobotyi\beansclient\BeansClient;
|
|
use xobotyi\beansclient\Connection;
|
|
use xobotyi\beansclient\Exception\JobException;
|
|
use xobotyi\beansclient\Job;
|
|
use Incoviba\Exception\MQTT\MissingJob;
|
|
use Incoviba\Service\MQTT\Beanstalkd;
|
|
|
|
class BeanstalkdTest extends TestCase
|
|
{
|
|
protected LoggerInterface $logger;
|
|
protected BeansClient $client;
|
|
protected function setUp(): void
|
|
{
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
|
|
$this->client = $this->getMockBuilder(BeansClient::class)->disableOriginalConstructor()->getMock();
|
|
}
|
|
|
|
public function testExists(): void
|
|
{
|
|
$stats = ['current-jobs-ready' => 1];
|
|
$this->client->method('watchTube')->willReturn($this->client);
|
|
$this->client->method('statsTube')->willReturn($stats);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$this->assertTrue($service->exists());
|
|
}
|
|
public function testNotExists(): void
|
|
{
|
|
$stats = ['current-jobs-ready' => 0];
|
|
$this->client->method('watchTube')->willReturn($this->client);
|
|
$this->client->method('statsTube')->willReturn($stats);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$this->assertFalse($service->exists());
|
|
}
|
|
public function testGet(): void
|
|
{
|
|
$jobData = [
|
|
'id' => 1,
|
|
'configuration' => [
|
|
'type' => 'service',
|
|
],
|
|
'created_at' => '2020-01-01 00:00:00',
|
|
];
|
|
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
|
|
$connection->method('isActive')->willReturn(true);
|
|
$this->client->method('getConnection')->willReturn($connection);
|
|
$this->client->method('watchTube')->willReturn($this->client);
|
|
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
|
|
$job = new Job($this->client, 1, 'ready', json_encode($jobData));
|
|
$this->client->method('reserve')->willReturn($job);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$this->assertEquals(json_encode($jobData), $service->get());
|
|
}
|
|
public function testGetException(): void
|
|
{
|
|
$this->client->method('watchTube')->willReturn($this->client);
|
|
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 0]);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$this->expectException(MissingJob::class);
|
|
$service->get();
|
|
|
|
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
|
|
$exception = new JobException();
|
|
$this->client->method('reserve')->willThrowException($exception);
|
|
$this->expectException(MissingJob::class);
|
|
$service->get();
|
|
}
|
|
public function testSet(): void
|
|
{
|
|
$this->client->method('useTube')->willReturn($this->client);
|
|
$this->client->method('put');
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$service->set('test');
|
|
$this->assertTrue(true);
|
|
}
|
|
public function testSetException(): void
|
|
{
|
|
$this->client->method('useTube')->willReturn($this->client);
|
|
$exception = new JobException();
|
|
$this->client->method('put')->willThrowException($exception);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$service->set('test');
|
|
$this->assertTrue(true);
|
|
}
|
|
public function testRemove(): void
|
|
{
|
|
$this->client->method('useTube')->willReturn($this->client);
|
|
$this->client->method('delete')->willReturn(true);
|
|
$service = new Beanstalkd($this->logger, $this->client);
|
|
$service->remove(1);
|
|
$this->assertTrue(true);
|
|
|
|
$this->client->method('delete')->willReturn(false);
|
|
$service->remove(1);
|
|
$this->assertTrue(true);
|
|
}
|
|
} |