Skip to content

Commit 711e4cd

Browse files
committed
Issue #25: Tests to improve code coverage
Signed-off-by: bota <[email protected]>
1 parent 56a9939 commit 711e4cd

18 files changed

+1005
-8
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
},
8585
"autoload-dev": {
8686
"psr-4": {
87-
"DotTest\\Mail\\": "test/"
87+
"QueueTest\\App\\": "test/App/",
88+
"QueueTest\\Swoole\\": "test/Swoole/"
8889
}
8990
},
9091
"scripts": {

config/autoload/log.local.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ return [
1010
'writers' => [
1111
'FileWriter' => [
1212
'name' => 'stream',
13-
'level' => \Dot\Log\Logger::ALERT, // this is equal to 1
13+
'priority' => \Dot\Log\Logger::ALERT, // this is equal to 1
1414
'options' => [
1515
'stream' => __DIR__ . '/../../log/queue-log.log',
1616
'formatter' => [

src/App/Message/ExampleMessageHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public function __invoke(ExampleMessage $message): void
4242
if ($payload !== null && isset($payload['userUuid'])) {
4343
$this->logger->info("message: " . $payload['userUuid']);
4444
$this->args = $payload;
45-
}
4645

47-
try {
48-
$this->perform();
49-
} catch (Exception $exception) {
46+
try {
47+
$this->perform();
48+
} catch (Exception $exception) {
49+
$this->logger->err("message: " . $exception->getMessage());
50+
}
5051
}
5152
}
5253

@@ -64,7 +65,7 @@ public function perform(): void
6465
public function sendWelcomeMail(): bool
6566
{
6667
$user = $this->userRepository->find($this->args['userUuid']);
67-
$this->mailService->getMessage()->addTo('[email protected]', 'sergiu');
68+
$this->mailService->getMessage()->addTo($user->getEmail(), $user->getName());
6869
$this->mailService->setSubject('Welcome to ' . $this->config['application']['name']);
6970
$body = $this->templateRenderer->render('notification-email::welcome', [
7071
'user' => $user,

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: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\App\Message;
6+
7+
use Core\User\Repository\UserRepository;
8+
use Dot\Log\Logger;
9+
use Dot\Mail\Email;
10+
use Dot\Mail\Exception\MailException;
11+
use Dot\Mail\Service\MailService;
12+
use Exception;
13+
use Mezzio\Template\TemplateRendererInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
use Queue\App\Message\ExampleMessage;
17+
use Queue\App\Message\ExampleMessageHandler;
18+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
19+
20+
use function json_encode;
21+
22+
class ExampleMessageHandlerTest extends TestCase
23+
{
24+
protected MailService|MockObject $mailService;
25+
protected TemplateRendererInterface|MockObject $renderer;
26+
protected UserRepository|MockObject $userRepository;
27+
protected Logger $logger;
28+
protected array $config;
29+
private ExampleMessageHandler $handler;
30+
31+
/**
32+
* @throws Exception|\PHPUnit\Framework\MockObject\Exception
33+
*/
34+
public function setUp(): void
35+
{
36+
parent::setUp();
37+
38+
$this->mailService = $this->createMock(MailService::class);
39+
$this->renderer = $this->createMock(TemplateRendererInterface::class);
40+
$this->userRepository = $this->createMock(UserRepository::class);
41+
$this->logger = new Logger([
42+
'writers' => [
43+
'FileWriter' => [
44+
'name' => 'null',
45+
'priority' => Logger::ALERT,
46+
],
47+
],
48+
]);
49+
$this->config = [
50+
'notification' => [
51+
'server' => [
52+
'protocol' => 'tcp',
53+
'host' => 'localhost',
54+
'port' => '8556',
55+
'eof' => "\n",
56+
],
57+
],
58+
'application' => [
59+
'name' => 'dotkernel',
60+
],
61+
];
62+
63+
$this->handler = new ExampleMessageHandler(
64+
$this->mailService,
65+
$this->renderer,
66+
$this->userRepository,
67+
$this->logger,
68+
$this->config
69+
);
70+
}
71+
72+
/**
73+
* @throws \PHPUnit\Framework\MockObject\Exception
74+
*/
75+
public function testInvokeHandlesExceptionThrownByPerform(): void
76+
{
77+
$uuid = '1234';
78+
79+
$message = $this->createMock(ExampleMessage::class);
80+
$message->method('getPayload')->willReturn([
81+
'foo' => json_encode(['userUuid' => $uuid]),
82+
]);
83+
84+
$handlerMock = $this->getMockBuilder(ExampleMessageHandler::class)
85+
->setConstructorArgs([
86+
$this->mailService,
87+
$this->renderer,
88+
$this->userRepository,
89+
$this->logger,
90+
$this->config,
91+
])
92+
->onlyMethods(['perform'])
93+
->getMock();
94+
95+
$handlerMock
96+
->expects($this->once())
97+
->method('perform')
98+
->willThrowException(new \RuntimeException('Test exception'));
99+
100+
$handlerMock->__invoke($message);
101+
}
102+
103+
/**
104+
* @throws \PHPUnit\Framework\MockObject\Exception
105+
*/
106+
public function testInvokeWithValidPayload(): void
107+
{
108+
$uuid = '1245';
109+
$email = '[email protected]';
110+
$name = 'dotkernel';
111+
112+
$message = $this->createMock(ExampleMessage::class);
113+
$message->method('getPayload')->willReturn([
114+
'foo' => json_encode(['userUuid' => $uuid]),
115+
]);
116+
117+
$user = $this->getMockBuilder(\stdClass::class)
118+
->addMethods(['getEmail', 'getName'])
119+
->getMock();
120+
$user->method('getEmail')->willReturn($email);
121+
$user->method('getName')->willReturn($name);
122+
123+
$this->userRepository
124+
->expects($this->once())
125+
->method('find')
126+
->with($uuid)
127+
->willReturn($user);
128+
129+
$mailMessage = $this->createMock(Email::class);
130+
$mailMessage->expects($this->once())
131+
->method('addTo')
132+
->with($email, $name);
133+
134+
$this
135+
->mailService
136+
->method('getMessage')
137+
->willReturn($mailMessage);
138+
139+
$this
140+
->mailService
141+
->expects($this->once())
142+
->method('setSubject')
143+
->with('Welcome to dotkernel');
144+
145+
$this->renderer->method('render')->willReturn('Rendered email body');
146+
147+
$this->mailService->expects($this->once())
148+
->method('setBody')
149+
->with('Rendered email body');
150+
151+
$this->handler->__invoke($message);
152+
}
153+
154+
/**
155+
* @throws MailException
156+
* @throws \PHPUnit\Framework\MockObject\Exception
157+
*/
158+
public function testSendWelcomeMailHandlesMailException(): void
159+
{
160+
$uuid = '1234';
161+
162+
$reflection = new \ReflectionClass($this->handler);
163+
$argsProperty = $reflection->getProperty('args');
164+
$argsProperty->setValue($this->handler, ['userUuid' => $uuid]);
165+
166+
$user = $this->getMockBuilder(\stdClass::class)
167+
->addMethods(['getEmail', 'getName'])
168+
->getMock();
169+
$user->method('getEmail')->willReturn('[email protected]');
170+
$user->method('getName')->willReturn('dotkernel');
171+
172+
$this->userRepository->method('find')->willReturn($user);
173+
$this->mailService->method('getMessage')->willReturn($this->createMock(Email::class));
174+
$this->mailService->method('setSubject');
175+
$this->renderer->method('render')->willReturn('Rendered content');
176+
$this->mailService->method('setBody');
177+
178+
$this->mailService->method('send')
179+
->willThrowException($this->createMock(TransportExceptionInterface::class));
180+
181+
$result = $this->handler->sendWelcomeMail();
182+
$this->assertFalse($result);
183+
}
184+
185+
/**
186+
* @throws \PHPUnit\Framework\MockObject\Exception
187+
*/
188+
public function testInvokeWithInvalidJsonSkipsPerform(): void
189+
{
190+
$message = $this->createMock(ExampleMessage::class);
191+
$message->method('getPayload')->willReturn([
192+
'foo' => '{"userUuid":',
193+
]);
194+
195+
$this->userRepository->expects($this->never())->method('find');
196+
$this->mailService->expects($this->never())->method('send');
197+
198+
$this->handler->__invoke($message);
199+
}
200+
}
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\ExampleMessage;
9+
10+
class ExampleMessageTest extends TestCase
11+
{
12+
public function testMessageAccessors(): void
13+
{
14+
$admin = new ExampleMessage(["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+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace QueueTest\Swoole\Command;
6+
7+
use PHPUnit\Framework\MockObject\Exception;
8+
use PHPUnit\Framework\TestCase;
9+
use Queue\Swoole\Command\IsRunningTrait;
10+
use Queue\Swoole\PidManager;
11+
12+
class IsRunningTraitTest extends TestCase
13+
{
14+
private object $traitUser;
15+
16+
/**
17+
* @throws Exception
18+
*/
19+
protected function setUp(): void
20+
{
21+
$this->traitUser = new class {
22+
use IsRunningTrait;
23+
24+
public PidManager $pidManager;
25+
};
26+
27+
$this->traitUser->pidManager = $this->createMock(PidManager::class);
28+
}
29+
30+
public function testIsRunningReturnsFalseWhenNoPids(): void
31+
{
32+
$this->traitUser->pidManager->method('read')->willReturn([]);
33+
$this->assertFalse($this->traitUser->isRunning());
34+
}
35+
36+
public function testIsRunningReturnsFalseWhenPidsAreZero(): void
37+
{
38+
$this->traitUser->pidManager->method('read')->willReturn([0, 0]);
39+
$this->assertFalse($this->traitUser->isRunning());
40+
}
41+
}

0 commit comments

Comments
 (0)