Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
extensions: swoole
coverage: pcov
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
tools: composer:v2, cs2pr
Expand Down
5 changes: 5 additions & 0 deletions .laminas-ci/pre-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
JOB=$3
PHP_VERSION=$(echo "${JOB}" | jq -r '.php')

apt update
apt install -y "php${PHP_VERSION}-swoole"
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
},
"autoload-dev": {
"psr-4": {
"QueueTest\\Swoole\\": "test/Swoole"
"QueueTest\\App\\": "test/App/",
"QueueTest\\Swoole\\": "test/Swoole/"
}
},
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/Swoole/Command/GetFailedMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
)]
class GetFailedMessagesCommand extends Command
{
protected static string $defaultName = 'failed';
/** @var string $defaultName */
protected static $defaultName = 'failed';

#[Inject()]
public function __construct()
Expand Down
3 changes: 2 additions & 1 deletion src/Swoole/Command/GetProcessedMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
)]
class GetProcessedMessagesCommand extends Command
{
protected static string $defaultName = 'processed';
/** @var string $defaultName */
protected static $defaultName = 'processed';

#[Inject()]
public function __construct()
Expand Down
3 changes: 2 additions & 1 deletion src/Swoole/Command/GetQueuedMessagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
)]
class GetQueuedMessagesCommand extends Command
{
protected static string $defaultName = 'inventory';
/** @var string $defaultName */
protected static $defaultName = 'inventory';

private Redis $redis;

Expand Down
23 changes: 23 additions & 0 deletions test/App/AppConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace QueueTest\App;

use PHPUnit\Framework\TestCase;
use Queue\App\ConfigProvider;

class AppConfigProviderTest extends TestCase
{
private array $config;

public function setUp(): void
{
$this->config = (new ConfigProvider())();
}

public function testHasDependencies(): void
{
$this->assertArrayHasKey('dependencies', $this->config);
}
}
153 changes: 153 additions & 0 deletions test/App/Message/MessageHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

namespace QueueTest\App\Message;

use Dot\Log\Logger;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Queue\App\Message\Message;
use Queue\App\Message\MessageHandler;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;

class MessageHandlerTest extends TestCase
{
private MessageBusInterface|MockObject $bus;
private Logger $logger;
private array $config;
private MessageHandler $handler;

/**
* @throws Exception
* @throws ContainerExceptionInterface
*/
protected function setUp(): void
{
$this->bus = $this->createMock(MessageBusInterface::class);
$this->logger = new Logger([
'writers' => [
'FileWriter' => [
'name' => 'null',
'level' => Logger::ALERT,
],
],
]);
$this->config = [
'fail-safe' => [
'first_retry' => 1000,
'second_retry' => 2000,
'third_retry' => 3000,
],
'notification' => [
'server' => [
'protocol' => 'tcp',
'host' => 'localhost',
'port' => '8556',
'eof' => "\n",
],
],
'application' => [
'name' => 'dotkernel',
],
];

$this->handler = new MessageHandler($this->bus, $this->logger, $this->config);
}

/**
* @throws Exception
*/
public function testInvokeSuccessfulProcessing(): void
{
$payload = ['foo' => 'control'];
$message = $this->createMock(Message::class);
$message->method('getPayload')->willReturn($payload);

$this->handler->__invoke($message);

$this->expectNotToPerformAssertions();
}

/**
* @throws Exception
*/
public function testInvokeFailureTriggersFirstRetry(): void
{
$payload = ['foo' => 'fail'];
$message = $this->createMock(Message::class);
$message->method('getPayload')->willReturn($payload);

$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['foo'] === 'fail'
&& $msg->getPayload()['retry'] === 1;
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 1000;
})
)
->willReturn(new Envelope($message));

$this->handler->__invoke($message);
}

/**
* @throws ExceptionInterface
*/
public function testRetrySecondTime(): void
{
$payload = ['foo' => 'retry_test', 'retry' => 1];

$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['retry'] === 2
&& $msg->getPayload()['foo'] === 'retry_test';
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 2000;
})
)
->willReturn(new Envelope(new Message($payload)));

$this->handler->retry($payload);
}

/**
* @throws ExceptionInterface
*/
public function testRetryThirdTime(): void
{
$payload = ['foo' => 'retry_test', 'retry' => 2];

$this->bus->expects($this->once())
->method('dispatch')
->with(
$this->callback(function ($msg) {
return $msg instanceof Message
&& $msg->getPayload()['retry'] === 3
&& $msg->getPayload()['foo'] === 'retry_test';
}),
$this->callback(function ($stamps) {
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
&& $stamps[0]->getDelay() === 3000;
})
)
->willReturn(new Envelope(new Message($payload)));

$this->handler->retry($payload);
}
}
17 changes: 17 additions & 0 deletions test/App/Message/MessageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace QueueTest\App\Message;

use PHPUnit\Framework\TestCase;
use Queue\App\Message\Message;

class MessageTest extends TestCase
{
public function testMessageAccessors(): void
{
$admin = new Message(["payload" => "test message payload"]);
$this->assertSame(["payload" => "test message payload"], $admin->getPayload());
}
}
27 changes: 27 additions & 0 deletions test/Swoole/Command/Factory/StartCommandFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace QueueTest\Swoole\Command\Factory;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Queue\Swoole\Command\Factory\StartCommandFactory;
use Queue\Swoole\Command\StartCommand;

class StartCommandFactoryTest extends TestCase
{
/**
* @throws Exception
*/
public function testFactoryReturnsStartCommandInstance(): void
{
$container = $this->createMock(ContainerInterface::class);

$factory = new StartCommandFactory();
$command = $factory($container);

$this->assertContainsOnlyInstancesOf(StartCommand::class, [$command]);
}
}
35 changes: 35 additions & 0 deletions test/Swoole/Command/Factory/StopCommandFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace QueueTest\Swoole\Command\Factory;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Queue\Swoole\Command\Factory\StopCommandFactory;
use Queue\Swoole\Command\StopCommand;
use Queue\Swoole\PidManager;

class StopCommandFactoryTest extends TestCase
{
/**
* @throws Exception
*/
public function testFactoryReturnsStopCommandInstance(): void
{
$pidManager = $this->createMock(PidManager::class);

$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())
->method('get')
->with(PidManager::class)
->willReturn($pidManager);

$factory = new StopCommandFactory();

$command = $factory($container);

$this->assertContainsOnlyInstancesOf(StopCommand::class, [$command]);
}
}
Loading
Loading