Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
},
"autoload-dev": {
"psr-4": {
"DotTest\\Mail\\": "test/"
"QueueTest\\App\\": "test/App/",
"QueueTest\\Swoole\\": "test/Swoole/"
}
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/log.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return [
'writers' => [
'FileWriter' => [
'name' => 'stream',
'level' => \Dot\Log\Logger::ALERT, // this is equal to 1
'priority' => \Dot\Log\Logger::ALERT, // this is equal to 1
'options' => [
'stream' => __DIR__ . '/../../log/queue-log.log',
'formatter' => [
Expand Down
11 changes: 6 additions & 5 deletions src/App/Message/ExampleMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public function __invoke(ExampleMessage $message): void
if ($payload !== null && isset($payload['userUuid'])) {
$this->logger->info("message: " . $payload['userUuid']);
$this->args = $payload;
}

try {
$this->perform();
} catch (Exception $exception) {
try {
$this->perform();
} catch (Exception $exception) {
$this->logger->err("message: " . $exception->getMessage());
}
}
}

Expand All @@ -64,7 +65,7 @@ public function perform(): void
public function sendWelcomeMail(): bool
{
$user = $this->userRepository->find($this->args['userUuid']);
$this->mailService->getMessage()->addTo('[email protected]', 'sergiu');
$this->mailService->getMessage()->addTo($user->getEmail(), $user->getName());
$this->mailService->setSubject('Welcome to ' . $this->config['application']['name']);
$body = $this->templateRenderer->render('notification-email::welcome', [
'user' => $user,
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);
}
}
200 changes: 200 additions & 0 deletions test/App/Message/ExampleMessageHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

declare(strict_types=1);

namespace QueueTest\App\Message;

use Core\User\Repository\UserRepository;
use Dot\Log\Logger;
use Dot\Mail\Email;
use Dot\Mail\Exception\MailException;
use Dot\Mail\Service\MailService;
use Exception;
use Mezzio\Template\TemplateRendererInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Queue\App\Message\ExampleMessage;
use Queue\App\Message\ExampleMessageHandler;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;

use function json_encode;

class ExampleMessageHandlerTest extends TestCase
{
protected MailService|MockObject $mailService;
protected TemplateRendererInterface|MockObject $renderer;
protected UserRepository|MockObject $userRepository;
protected Logger $logger;
protected array $config;
private ExampleMessageHandler $handler;

/**
* @throws Exception|\PHPUnit\Framework\MockObject\Exception
*/
public function setUp(): void
{
parent::setUp();

$this->mailService = $this->createMock(MailService::class);
$this->renderer = $this->createMock(TemplateRendererInterface::class);
$this->userRepository = $this->createMock(UserRepository::class);
$this->logger = new Logger([
'writers' => [
'FileWriter' => [
'name' => 'null',
'priority' => Logger::ALERT,
],
],
]);
$this->config = [
'notification' => [
'server' => [
'protocol' => 'tcp',
'host' => 'localhost',
'port' => '8556',
'eof' => "\n",
],
],
'application' => [
'name' => 'dotkernel',
],
];

$this->handler = new ExampleMessageHandler(
$this->mailService,
$this->renderer,
$this->userRepository,
$this->logger,
$this->config
);
}

/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testInvokeHandlesExceptionThrownByPerform(): void
{
$uuid = '1234';

$message = $this->createMock(ExampleMessage::class);
$message->method('getPayload')->willReturn([
'foo' => json_encode(['userUuid' => $uuid]),
]);

$handlerMock = $this->getMockBuilder(ExampleMessageHandler::class)
->setConstructorArgs([
$this->mailService,
$this->renderer,
$this->userRepository,
$this->logger,
$this->config,
])
->onlyMethods(['perform'])
->getMock();

$handlerMock
->expects($this->once())
->method('perform')
->willThrowException(new \RuntimeException('Test exception'));

$handlerMock->__invoke($message);
}

/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testInvokeWithValidPayload(): void
{
$uuid = '1245';
$email = '[email protected]';
$name = 'dotkernel';

$message = $this->createMock(ExampleMessage::class);
$message->method('getPayload')->willReturn([
'foo' => json_encode(['userUuid' => $uuid]),
]);

$user = $this->getMockBuilder(\stdClass::class)
->addMethods(['getEmail', 'getName'])
->getMock();
$user->method('getEmail')->willReturn($email);
$user->method('getName')->willReturn($name);

$this->userRepository
->expects($this->once())
->method('find')
->with($uuid)
->willReturn($user);

$mailMessage = $this->createMock(Email::class);
$mailMessage->expects($this->once())
->method('addTo')
->with($email, $name);

$this
->mailService
->method('getMessage')
->willReturn($mailMessage);

$this
->mailService
->expects($this->once())
->method('setSubject')
->with('Welcome to dotkernel');

$this->renderer->method('render')->willReturn('Rendered email body');

$this->mailService->expects($this->once())
->method('setBody')
->with('Rendered email body');

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

/**
* @throws MailException
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testSendWelcomeMailHandlesMailException(): void
{
$uuid = '1234';

$reflection = new \ReflectionClass($this->handler);
$argsProperty = $reflection->getProperty('args');
$argsProperty->setValue($this->handler, ['userUuid' => $uuid]);

$user = $this->getMockBuilder(\stdClass::class)
->addMethods(['getEmail', 'getName'])
->getMock();
$user->method('getEmail')->willReturn('[email protected]');
$user->method('getName')->willReturn('dotkernel');

$this->userRepository->method('find')->willReturn($user);
$this->mailService->method('getMessage')->willReturn($this->createMock(Email::class));
$this->mailService->method('setSubject');
$this->renderer->method('render')->willReturn('Rendered content');
$this->mailService->method('setBody');

$this->mailService->method('send')
->willThrowException($this->createMock(TransportExceptionInterface::class));

$result = $this->handler->sendWelcomeMail();
$this->assertFalse($result);
}

/**
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testInvokeWithInvalidJsonSkipsPerform(): void
{
$message = $this->createMock(ExampleMessage::class);
$message->method('getPayload')->willReturn([
'foo' => '{"userUuid":',
]);

$this->userRepository->expects($this->never())->method('find');
$this->mailService->expects($this->never())->method('send');

$this->handler->__invoke($message);
}
}
17 changes: 17 additions & 0 deletions test/App/Message/ExampleMessageTest.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\ExampleMessage;

class ExampleMessageTest extends TestCase
{
public function testMessageAccessors(): void
{
$admin = new ExampleMessage(["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]);
}
}
39 changes: 39 additions & 0 deletions test/Swoole/Command/Factory/StopCommandFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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
{
if (! \extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension not loaded.');
}

$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]);
}
}
41 changes: 41 additions & 0 deletions test/Swoole/Command/IsRunningTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace QueueTest\Swoole\Command;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Queue\Swoole\Command\IsRunningTrait;
use Queue\Swoole\PidManager;

class IsRunningTraitTest extends TestCase
{
private object $traitUser;

/**
* @throws Exception
*/
protected function setUp(): void
{
$this->traitUser = new class {
use IsRunningTrait;

public PidManager $pidManager;
};

$this->traitUser->pidManager = $this->createMock(PidManager::class);
}

public function testIsRunningReturnsFalseWhenNoPids(): void
{
$this->traitUser->pidManager->method('read')->willReturn([]);
$this->assertFalse($this->traitUser->isRunning());
}

public function testIsRunningReturnsFalseWhenPidsAreZero(): void
{
$this->traitUser->pidManager->method('read')->willReturn([0, 0]);
$this->assertFalse($this->traitUser->isRunning());
}
}
Loading
Loading