Skip to content

Commit fe00850

Browse files
committed
Issue #50: Tests to improve code coverage
Signed-off-by: bota <[email protected]>
1 parent 8722b73 commit fe00850

21 files changed

+1392
-15
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
},
7070
"autoload-dev": {
7171
"psr-4": {
72-
"QueueTest\\Swoole\\": "test/Swoole"
72+
"QueueTest\\App\\": "test/App/",
73+
"QueueTest\\Swoole\\": "test/Swoole/"
7374
}
7475
},
7576
"scripts": {

src/App/ConfigProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Netglue\PsrContainer\Messenger\Container\Middleware\MessageHandlerMiddlewareStaticFactory;
1111
use Netglue\PsrContainer\Messenger\Container\Middleware\MessageSenderMiddlewareStaticFactory;
1212
use Netglue\PsrContainer\Messenger\HandlerLocator\OneToManyFqcnContainerHandlerLocator;
13-
use Queue\App\Message\ExampleMessage;
14-
use Queue\App\Message\ExampleMessageHandler;
13+
use Queue\App\Message\Message;
14+
use Queue\App\Message\MessageHandler;
1515
use Symfony\Component\Messenger\MessageBusInterface;
1616

1717
class ConfigProvider
@@ -37,7 +37,7 @@ private function getDependencies(): array
3737
"message_bus_stamp_middleware" => [BusNameStampMiddlewareStaticFactory::class, "message_bus"],
3838
"message_bus_sender_middleware" => [MessageSenderMiddlewareStaticFactory::class, "message_bus"],
3939
"message_bus_handler_middleware" => [MessageHandlerMiddlewareStaticFactory::class, "message_bus"],
40-
ExampleMessageHandler::class => AttributedServiceFactory::class,
40+
MessageHandler::class => AttributedServiceFactory::class,
4141
],
4242
"aliases" => [
4343
MessageBusInterface::class => "message_bus",
@@ -81,7 +81,7 @@ private function busConfig(): array
8181
*/
8282
'handler_locator' => OneToManyFqcnContainerHandlerLocator::class,
8383
'handlers' => [
84-
ExampleMessage::class => [ExampleMessageHandler::class],
84+
Message::class => [MessageHandler::class],
8585
],
8686

8787
/**
@@ -97,7 +97,7 @@ private function busConfig(): array
9797
* Route specific messages to specific transports by using the message name as the key.
9898
*/
9999
'routes' => [
100-
ExampleMessage::class => ["redis_transport"],
100+
Message::class => ["redis_transport"],
101101
],
102102
],
103103
];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Queue\App\Message;
66

7-
class ExampleMessage
7+
class Message
88
{
99
public function __construct(
1010
private array $payload,
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Symfony\Component\Messenger\MessageBusInterface;
1111
use Symfony\Component\Messenger\Stamp\DelayStamp;
1212

13-
class ExampleMessageHandler
13+
class MessageHandler
1414
{
1515
#[Inject(
1616
MessageBusInterface::class,
@@ -24,7 +24,7 @@ public function __construct(
2424
) {
2525
}
2626

27-
public function __invoke(ExampleMessage $message): void
27+
public function __invoke(Message $message): void
2828
{
2929
$payload = $message->getPayload();
3030

@@ -50,21 +50,21 @@ public function __invoke(ExampleMessage $message): void
5050
public function retry(array $payload): void
5151
{
5252
if (! isset($payload['retry'])) {
53-
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => 1]), [
53+
$this->bus->dispatch(new Message(["foo" => $payload['foo'], 'retry' => 1]), [
5454
new DelayStamp($this->config['fail-safe']['first_retry']),
5555
]);
5656
} else {
5757
$retry = $payload['retry'];
5858
switch ($retry) {
5959
case 1:
6060
$delay = $this->config['fail-safe']['second_retry'];
61-
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => ++$retry]), [
61+
$this->bus->dispatch(new Message(["foo" => $payload['foo'], 'retry' => ++$retry]), [
6262
new DelayStamp($delay),
6363
]);
6464
break;
6565
case 2:
6666
$delay = $this->config['fail-safe']['third_retry'];
67-
$this->bus->dispatch(new ExampleMessage(["foo" => $payload['foo'], 'retry' => ++$retry]), [
67+
$this->bus->dispatch(new Message(["foo" => $payload['foo'], 'retry' => ++$retry]), [
6868
new DelayStamp($delay),
6969
]);
7070
break;

src/Swoole/Delegators/TCPServerDelegator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Queue\Swoole\Delegators;
66

77
use Psr\Container\ContainerInterface;
8-
use Queue\App\Message\ExampleMessage;
8+
use Queue\App\Message\Message;
99
use Queue\Swoole\Command\GetFailedMessagesCommand;
1010
use Queue\Swoole\Command\GetProcessedMessagesCommand;
1111
use Queue\Swoole\Command\GetQueuedMessagesCommand;
@@ -79,8 +79,8 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
7979
$logger->error("Error running command: " . $e->getMessage());
8080
}
8181
} else {
82-
$bus->dispatch(new ExampleMessage(["foo" => $message]));
83-
$bus->dispatch(new ExampleMessage(["foo" => "with 5 seconds delay"]), [
82+
$bus->dispatch(new Message(["foo" => $message]));
83+
$bus->dispatch(new Message(["foo" => "with 5 seconds delay"]), [
8484
new DelayStamp(5000),
8585
]);
8686

test/App/AppConfigProviderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\App;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Queue\App\ConfigProvider;
9+
10+
class AppConfigProviderTest extends TestCase
11+
{
12+
private array $config;
13+
14+
public function setUp(): void
15+
{
16+
$this->config = (new ConfigProvider())();
17+
}
18+
19+
public function testHasDependencies(): void
20+
{
21+
$this->assertArrayHasKey('dependencies', $this->config);
22+
}
23+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\App\Message;
6+
7+
use Dot\Log\Logger;
8+
use PHPUnit\Framework\MockObject\Exception;
9+
use PHPUnit\Framework\MockObject\MockObject;
10+
use PHPUnit\Framework\TestCase;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Queue\App\Message\Message;
13+
use Queue\App\Message\MessageHandler;
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\ExceptionInterface;
16+
use Symfony\Component\Messenger\MessageBusInterface;
17+
use Symfony\Component\Messenger\Stamp\DelayStamp;
18+
19+
class MessageHandlerTest extends TestCase
20+
{
21+
private MessageBusInterface|MockObject $bus;
22+
private Logger $logger;
23+
private array $config;
24+
private MessageHandler $handler;
25+
26+
/**
27+
* @throws Exception
28+
* @throws ContainerExceptionInterface
29+
*/
30+
protected function setUp(): void
31+
{
32+
$this->bus = $this->createMock(MessageBusInterface::class);
33+
$this->logger = new Logger([
34+
'writers' => [
35+
'FileWriter' => [
36+
'name' => 'null',
37+
'level' => Logger::ALERT,
38+
],
39+
],
40+
]);
41+
$this->config = [
42+
'fail-safe' => [
43+
'first_retry' => 1000,
44+
'second_retry' => 2000,
45+
'third_retry' => 3000,
46+
],
47+
'notification' => [
48+
'server' => [
49+
'protocol' => 'tcp',
50+
'host' => 'localhost',
51+
'port' => '8556',
52+
'eof' => "\n",
53+
],
54+
],
55+
'application' => [
56+
'name' => 'dotkernel',
57+
],
58+
];
59+
60+
$this->handler = new MessageHandler($this->bus, $this->logger, $this->config);
61+
}
62+
63+
/**
64+
* @throws Exception
65+
*/
66+
public function testInvokeSuccessfulProcessing(): void
67+
{
68+
$payload = ['foo' => 'control'];
69+
$message = $this->createMock(Message::class);
70+
$message->method('getPayload')->willReturn($payload);
71+
72+
$this->handler->__invoke($message);
73+
74+
$this->expectNotToPerformAssertions();
75+
}
76+
77+
/**
78+
* @throws Exception
79+
*/
80+
public function testInvokeFailureTriggersFirstRetry(): void
81+
{
82+
$payload = ['foo' => 'fail'];
83+
$message = $this->createMock(Message::class);
84+
$message->method('getPayload')->willReturn($payload);
85+
86+
$this->bus->expects($this->once())
87+
->method('dispatch')
88+
->with(
89+
$this->callback(function ($msg) {
90+
return $msg instanceof Message
91+
&& $msg->getPayload()['foo'] === 'fail'
92+
&& $msg->getPayload()['retry'] === 1;
93+
}),
94+
$this->callback(function ($stamps) {
95+
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
96+
&& $stamps[0]->getDelay() === 1000;
97+
})
98+
)
99+
->willReturn(new Envelope($message));
100+
101+
$this->handler->__invoke($message);
102+
}
103+
104+
/**
105+
* @throws ExceptionInterface
106+
*/
107+
public function testRetrySecondTime(): void
108+
{
109+
$payload = ['foo' => 'retry_test', 'retry' => 1];
110+
111+
$this->bus->expects($this->once())
112+
->method('dispatch')
113+
->with(
114+
$this->callback(function ($msg) {
115+
return $msg instanceof Message
116+
&& $msg->getPayload()['retry'] === 2
117+
&& $msg->getPayload()['foo'] === 'retry_test';
118+
}),
119+
$this->callback(function ($stamps) {
120+
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
121+
&& $stamps[0]->getDelay() === 2000;
122+
})
123+
)
124+
->willReturn(new Envelope(new Message($payload)));
125+
126+
$this->handler->retry($payload);
127+
}
128+
129+
/**
130+
* @throws ExceptionInterface
131+
*/
132+
public function testRetryThirdTime(): void
133+
{
134+
$payload = ['foo' => 'retry_test', 'retry' => 2];
135+
136+
$this->bus->expects($this->once())
137+
->method('dispatch')
138+
->with(
139+
$this->callback(function ($msg) {
140+
return $msg instanceof Message
141+
&& $msg->getPayload()['retry'] === 3
142+
&& $msg->getPayload()['foo'] === 'retry_test';
143+
}),
144+
$this->callback(function ($stamps) {
145+
return isset($stamps[0]) && $stamps[0] instanceof DelayStamp
146+
&& $stamps[0]->getDelay() === 3000;
147+
})
148+
)
149+
->willReturn(new Envelope(new Message($payload)));
150+
151+
$this->handler->retry($payload);
152+
}
153+
}

test/App/Message/MessageTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\App\Message;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Queue\App\Message\Message;
9+
10+
class MessageTest extends TestCase
11+
{
12+
public function testMessageAccessors(): void
13+
{
14+
$admin = new Message(["payload" => "test message payload"]);
15+
$this->assertSame(["payload" => "test message payload"], $admin->getPayload());
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\Swoole\Command\Factory;
6+
7+
use PHPUnit\Framework\MockObject\Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use Psr\Container\ContainerInterface;
10+
use Queue\Swoole\Command\Factory\StartCommandFactory;
11+
use Queue\Swoole\Command\StartCommand;
12+
13+
class StartCommandFactoryTest extends TestCase
14+
{
15+
/**
16+
* @throws Exception
17+
*/
18+
public function testFactoryReturnsStartCommandInstance(): void
19+
{
20+
$container = $this->createMock(ContainerInterface::class);
21+
22+
$factory = new StartCommandFactory();
23+
$command = $factory($container);
24+
25+
$this->assertContainsOnlyInstancesOf(StartCommand::class, [$command]);
26+
}
27+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\Swoole\Command\Factory;
6+
7+
use PHPUnit\Framework\MockObject\Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use Psr\Container\ContainerInterface;
10+
use Queue\Swoole\Command\Factory\StopCommandFactory;
11+
use Queue\Swoole\Command\StopCommand;
12+
use Queue\Swoole\PidManager;
13+
14+
class StopCommandFactoryTest extends TestCase
15+
{
16+
/**
17+
* @throws Exception
18+
*/
19+
public function testFactoryReturnsStopCommandInstance(): void
20+
{
21+
$pidManager = $this->createMock(PidManager::class);
22+
23+
$container = $this->createMock(ContainerInterface::class);
24+
$container->expects($this->once())
25+
->method('get')
26+
->with(PidManager::class)
27+
->willReturn($pidManager);
28+
29+
$factory = new StopCommandFactory();
30+
31+
$command = $factory($container);
32+
33+
$this->assertContainsOnlyInstancesOf(StopCommand::class, [$command]);
34+
}
35+
}

0 commit comments

Comments
 (0)