From b477fb696fb64dbf47fc7d025eb35de58bcfef96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 12 May 2024 00:50:42 +0200 Subject: [PATCH] test(tests): UT for NotificationEvent service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Core/Events/EventDispatcherBase.php | 2 +- lib/SP/Domain/Auth/Providers/AclHandler.php | 2 +- .../Domain/Common/Services/EventReceiver.php | 5 + lib/SP/Domain/Core/Events/EventReceiver.php | 2 +- .../Domain/Log/Providers/DatabaseHandler.php | 2 +- lib/SP/Domain/Log/Providers/LogHandler.php | 2 +- .../Notification/Services/MailEvent.php | 2 +- .../Services/NotificationEvent.php | 40 +--- tests/SP/Core/Events/EventDispatcherTest.php | 6 +- .../Log/Providers/DatabaseHandlerTest.php | 4 +- .../MailEventTest.php} | 24 +- .../Services/NotificationEventTest.php | 220 ++++++++++++++++++ .../Domain/Plugin/Services/PluginBaseTest.php | 2 +- 13 files changed, 259 insertions(+), 54 deletions(-) rename tests/SP/Domain/Notification/{Providers/MailHandlerTest.php => Services/MailEventTest.php} (92%) create mode 100644 tests/SP/Domain/Notification/Services/NotificationEventTest.php diff --git a/lib/SP/Core/Events/EventDispatcherBase.php b/lib/SP/Core/Events/EventDispatcherBase.php index 81b9c58d..ecf93234 100644 --- a/lib/SP/Core/Events/EventDispatcherBase.php +++ b/lib/SP/Core/Events/EventDispatcherBase.php @@ -94,7 +94,7 @@ abstract class EventDispatcherBase implements EventDispatcherInterface { /** @var EventReceiver $receiver */ foreach ($this->receivers as $receiver) { - $events = $receiver->getEventsString(); + $events = $receiver->getEvents(); if ($events === '*' || preg_match(sprintf('/%s/i', $events), $eventName)) { $receiver->update($eventName, $event); diff --git a/lib/SP/Domain/Auth/Providers/AclHandler.php b/lib/SP/Domain/Auth/Providers/AclHandler.php index 2a52c61a..142d9f48 100644 --- a/lib/SP/Domain/Auth/Providers/AclHandler.php +++ b/lib/SP/Domain/Auth/Providers/AclHandler.php @@ -82,7 +82,7 @@ final class AclHandler extends Provider implements EventReceiver * * @return string */ - public function getEventsString(): string + public function getEvents(): string { return $this->events; } diff --git a/lib/SP/Domain/Common/Services/EventReceiver.php b/lib/SP/Domain/Common/Services/EventReceiver.php index d3d4565b..2b703422 100644 --- a/lib/SP/Domain/Common/Services/EventReceiver.php +++ b/lib/SP/Domain/Common/Services/EventReceiver.php @@ -37,6 +37,11 @@ trait EventReceiver { private readonly string $events; + public function getEvents(): string + { + return $this->events; + } + private function setupEvents(array $userEvents = []): void { $reflectionClass = new ReflectionClass($this); diff --git a/lib/SP/Domain/Core/Events/EventReceiver.php b/lib/SP/Domain/Core/Events/EventReceiver.php index e2976afa..ffd78fa4 100644 --- a/lib/SP/Domain/Core/Events/EventReceiver.php +++ b/lib/SP/Domain/Core/Events/EventReceiver.php @@ -45,5 +45,5 @@ interface EventReceiver * * @return string|null */ - public function getEventsString(): ?string; + public function getEvents(): ?string; } diff --git a/lib/SP/Domain/Log/Providers/DatabaseHandler.php b/lib/SP/Domain/Log/Providers/DatabaseHandler.php index 63dc0fea..58a8ba6d 100644 --- a/lib/SP/Domain/Log/Providers/DatabaseHandler.php +++ b/lib/SP/Domain/Log/Providers/DatabaseHandler.php @@ -107,7 +107,7 @@ final class DatabaseHandler extends Provider implements EventReceiver * * @return string|null */ - public function getEventsString(): ?string + public function getEvents(): ?string { return $this->events; } diff --git a/lib/SP/Domain/Log/Providers/LogHandler.php b/lib/SP/Domain/Log/Providers/LogHandler.php index 3edbd5d8..2512625c 100644 --- a/lib/SP/Domain/Log/Providers/LogHandler.php +++ b/lib/SP/Domain/Log/Providers/LogHandler.php @@ -35,7 +35,7 @@ final class LogHandler extends LoggerBase /** * @inheritDoc */ - public function getEventsString(): ?string + public function getEvents(): ?string { return $this->events; } diff --git a/lib/SP/Domain/Notification/Services/MailEvent.php b/lib/SP/Domain/Notification/Services/MailEvent.php index 1024d58a..a4302f6a 100644 --- a/lib/SP/Domain/Notification/Services/MailEvent.php +++ b/lib/SP/Domain/Notification/Services/MailEvent.php @@ -81,7 +81,7 @@ final class MailEvent extends Service implements EventReceiver * * @return string */ - public function getEventsString(): string + public function getEvents(): string { return $this->events; } diff --git a/lib/SP/Domain/Notification/Services/NotificationEvent.php b/lib/SP/Domain/Notification/Services/NotificationEvent.php index 96cfb0f6..b930389c 100644 --- a/lib/SP/Domain/Notification/Services/NotificationEvent.php +++ b/lib/SP/Domain/Notification/Services/NotificationEvent.php @@ -29,7 +29,8 @@ namespace SP\Domain\Notification\Services; use Exception; use SP\Core\Application; use SP\Core\Events\Event; -use SP\Domain\Common\Providers\EventsTrait; +use SP\Domain\Common\Attributes\EventReceiver as EventReceiverAttribute; +use SP\Domain\Common\Services\EventReceiver as EventReceiverTrait; use SP\Domain\Common\Services\Service; use SP\Domain\Core\Events\EventReceiver; use SP\Domain\Notification\Models\Notification; @@ -41,16 +42,11 @@ use function SP\processException; /** * Class NotificationEvent */ +#[EventReceiverAttribute('request.account')] +#[EventReceiverAttribute('show.account.link')] final class NotificationEvent extends Service implements EventReceiver { - use EventsTrait; - - public const EVENTS = [ - 'request.account', - 'show.account.link', - ]; - - private readonly string $events; + use EventReceiverTrait; public function __construct( Application $application, @@ -58,22 +54,7 @@ final class NotificationEvent extends Service implements EventReceiver ) { parent::__construct($application); - $this->setup(); - } - - private function setup(): void - { - $this->events = $this->parseEventsToRegex(self::EVENTS); - } - - /** - * Devuelve los eventos que implementa el observador en formato cadena - * - * @return string - */ - public function getEventsString(): string - { - return $this->events; + $this->setupEvents(); } /** @@ -100,7 +81,7 @@ final class NotificationEvent extends Service implements EventReceiver private function requestAccountNotification(Event $event): void { $eventMessage = $event->getEventMessage(); - $userIds = $eventMessage !== null ? $eventMessage->getExtra('userId') : []; + $userIds = $eventMessage?->getExtra('userId') ?? []; foreach ($userIds as $userId) { $notification = new Notification( @@ -134,11 +115,10 @@ final class NotificationEvent extends Service implements EventReceiver private function showAccountLinkNotification(Event $event): void { $eventMessage = $event->getEventMessage(); - $notify = $eventMessage !== null ? $eventMessage->getExtra('notify') : []; - - if ($notify[0] === true) { - $userId = $eventMessage->getExtra('userId')[0]; + $notify = $eventMessage?->getExtra('notify')[0] ?? null; + $userId = $eventMessage?->getExtra('userId')[0] ?? null; + if ($notify === true && $userId) { $notification = new Notification( [ 'type' => __('Notification'), diff --git a/tests/SP/Core/Events/EventDispatcherTest.php b/tests/SP/Core/Events/EventDispatcherTest.php index 7cc0d3d1..45db4a26 100644 --- a/tests/SP/Core/Events/EventDispatcherTest.php +++ b/tests/SP/Core/Events/EventDispatcherTest.php @@ -76,7 +76,7 @@ class EventDispatcherTest extends UnitaryTestCase $event = new Event($this); $this->eventReceiver->expects(self::once()) - ->method('getEventsString') + ->method('getEvents') ->willReturn(self::VALID_EVENTS); $this->eventReceiver->expects(self::once()) @@ -92,7 +92,7 @@ class EventDispatcherTest extends UnitaryTestCase $event = new Event($this); $this->eventReceiver->expects(self::once()) - ->method('getEventsString') + ->method('getEvents') ->willReturn('*'); $this->eventReceiver->expects(self::once()) @@ -106,7 +106,7 @@ class EventDispatcherTest extends UnitaryTestCase public function testNotifyWithInvalidEvent() { $this->eventReceiver->expects(self::once()) - ->method('getEventsString') + ->method('getEvents') ->willReturn('anotherEvent'); $this->eventReceiver->expects(self::never()) diff --git a/tests/SP/Domain/Log/Providers/DatabaseHandlerTest.php b/tests/SP/Domain/Log/Providers/DatabaseHandlerTest.php index cc2d1f60..ddb2e3f7 100644 --- a/tests/SP/Domain/Log/Providers/DatabaseHandlerTest.php +++ b/tests/SP/Domain/Log/Providers/DatabaseHandlerTest.php @@ -56,7 +56,7 @@ class DatabaseHandlerTest extends UnitaryTestCase public function testGetEventsString() { $expected = 'test_a\.|test_b\.|upgrade\.|acl\.deny|plugin\.load\.error|show\.authToken|clear\.eventlog|clear\.track|refresh\.masterPassword|update\.masterPassword\.start|update\.masterPassword\.end|request\.account|edit\.user\.password|save\.config\.|create\.tempMasterPassword|run\.import\.start|run\.import\.end'; - $out = $this->databaseHandler->getEventsString(); + $out = $this->databaseHandler->getEvents(); $this->assertEquals($expected, $out); } @@ -68,7 +68,7 @@ class DatabaseHandlerTest extends UnitaryTestCase $this->configData->setLogEvents([]); $databaseHandler = new DatabaseHandler($this->application, $this->eventLogService, $this->language); - $out = $databaseHandler->getEventsString(); + $out = $databaseHandler->getEvents(); $this->assertEquals($expected, $out); } diff --git a/tests/SP/Domain/Notification/Providers/MailHandlerTest.php b/tests/SP/Domain/Notification/Services/MailEventTest.php similarity index 92% rename from tests/SP/Domain/Notification/Providers/MailHandlerTest.php rename to tests/SP/Domain/Notification/Services/MailEventTest.php index 0acb76f3..8eaea518 100644 --- a/tests/SP/Domain/Notification/Providers/MailHandlerTest.php +++ b/tests/SP/Domain/Notification/Services/MailEventTest.php @@ -24,7 +24,7 @@ declare(strict_types=1); -namespace SP\Tests\Domain\Notification\Providers; +namespace SP\Tests\Domain\Notification\Services; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; @@ -44,12 +44,12 @@ use SP\Tests\UnitaryTestCase; * Class MailHandlerTest */ #[Group('unitary')] -class MailHandlerTest extends UnitaryTestCase +class MailEventTest extends UnitaryTestCase { private MockObject|MailService $mailService; private RequestService|MockObject $requestService; - private MailEvent $mailHandler; + private MailEvent $mailEvent; private ConfigData $configData; public function testUpdate() @@ -85,7 +85,7 @@ class MailHandlerTest extends UnitaryTestCase }) ); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testUpdateWithConfiguredEmail() @@ -122,7 +122,7 @@ class MailHandlerTest extends UnitaryTestCase }) ); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testUpdateWithNoEmail() @@ -137,7 +137,7 @@ class MailHandlerTest extends UnitaryTestCase ->expects($this->never()) ->method('send'); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testUpdateWithNoDescriptionAndDetails() @@ -170,7 +170,7 @@ class MailHandlerTest extends UnitaryTestCase }) ); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testUpdateWithEmptyRecipients() @@ -206,7 +206,7 @@ class MailHandlerTest extends UnitaryTestCase }) ); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testUpdateWithException() @@ -223,13 +223,13 @@ class MailHandlerTest extends UnitaryTestCase ->method('send') ->willThrowException(new RuntimeException('test')); - $this->mailHandler->update('test_a.update', $event); + $this->mailEvent->update('test_a.update', $event); } public function testGetEventsString() { $expected = 'test_a\.|test_b\.|clear\.eventlog|refresh\.masterPassword|update\.masterPassword\.start|update\.masterPassword\.end|request\.account|edit\.user\.password|save\.config\.|create\.tempMasterPassword'; - $out = $this->mailHandler->getEventsString(); + $out = $this->mailEvent->getEvents(); $this->assertEquals($expected, $out); } @@ -241,7 +241,7 @@ class MailHandlerTest extends UnitaryTestCase $this->configData->setMailEvents([]); $databaseHandler = new MailEvent($this->application, $this->mailService, $this->requestService); - $out = $databaseHandler->getEventsString(); + $out = $databaseHandler->getEvents(); $this->assertEquals($expected, $out); } @@ -264,6 +264,6 @@ class MailHandlerTest extends UnitaryTestCase $this->mailService = $this->createMock(MailService::class); $this->requestService = $this->createMock(RequestService::class); - $this->mailHandler = new MailEvent($this->application, $this->mailService, $this->requestService); + $this->mailEvent = new MailEvent($this->application, $this->mailService, $this->requestService); } } diff --git a/tests/SP/Domain/Notification/Services/NotificationEventTest.php b/tests/SP/Domain/Notification/Services/NotificationEventTest.php new file mode 100644 index 00000000..d0580872 --- /dev/null +++ b/tests/SP/Domain/Notification/Services/NotificationEventTest.php @@ -0,0 +1,220 @@ +. + */ + +declare(strict_types=1); + +namespace SP\Tests\Domain\Notification\Services; + +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\MockObject\MockObject; +use RuntimeException; +use SP\Core\Events\Event; +use SP\Core\Events\EventMessage; +use SP\Domain\Notification\Models\Notification; +use SP\Domain\Notification\Ports\NotificationService; +use SP\Domain\Notification\Services\NotificationEvent; +use SP\Tests\UnitaryTestCase; + +/** + * Class NotificationEventTest + */ +#[Group('unitary')] +class NotificationEventTest extends UnitaryTestCase +{ + + private MockObject|NotificationService $notificationService; + private NotificationEvent $notificationEvent; + + public function testUpdateWithRequestAccount() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('userId', [100, 200]); + + $event = new Event($this, $eventMessage); + + $invokedCount = $this->exactly(2); + + $this->notificationService + ->expects($invokedCount) + ->method('create') + ->with( + self::callback(static function (Notification $notification) use ($invokedCount) { + $userId = match ($invokedCount->numberOfInvocations()) { + 1 => 100, + 2 => 200 + }; + + return $notification->getType() == 'Request' + && $notification->getComponent() === 'Accounts' + && $notification->getUserId() === $userId + && !empty($notification->getDescription()); + }) + ); + + $this->notificationEvent->update('request.account', $event); + } + + public function testUpdateWithRequestAccountAndNoUserId() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value'); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->never()) + ->method('create'); + + $this->notificationEvent->update('request.account', $event); + } + + public function testUpdateWithRequestAccountAndException() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('userId', [100, 200]); + + $event = new Event($this, $eventMessage); + + $invokedCount = $this->exactly(2); + + $this->notificationService + ->expects($invokedCount) + ->method('create') + ->willThrowException(new RuntimeException('test')); + + $this->notificationEvent->update('request.account', $event); + } + + public function testUpdateWithShowLink() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('notify', [true]) + ->setExtra('userId', [100]); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->once()) + ->method('create') + ->with( + self::callback(static function (Notification $notification) { + return $notification->getType() == 'Notification' + && $notification->getComponent() === 'Accounts' + && $notification->getUserId() === 100 + && !empty($notification->getDescription()); + }) + ); + + $this->notificationEvent->update('show.account.link', $event); + } + + public function testUpdateWithShowLinkAndException() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('notify', [true]) + ->setExtra('userId', [100]); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->once()) + ->method('create') + ->willThrowException(new RuntimeException('test')); + + $this->notificationEvent->update('show.account.link', $event); + } + + public function testUpdateWithShowLinkAndNoNotify() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('userId', [100]); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->never()) + ->method('create'); + + $this->notificationEvent->update('show.account.link', $event); + } + + public function testUpdateWithShowLinkAndFalseNoNotify() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('notify', [false]) + ->setExtra('userId', [100]); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->never()) + ->method('create'); + + $this->notificationEvent->update('show.account.link', $event); + } + + public function testUpdateWithShowLinkAndNoUserId() + { + $eventMessage = EventMessage::factory() + ->addDescription('a_description') + ->addDetail('a_detail', 'a_value') + ->setExtra('notify', [true]); + + $event = new Event($this, $eventMessage); + + $this->notificationService + ->expects($this->never()) + ->method('create'); + + $this->notificationEvent->update('show.account.link', $event); + } + + public function testGetEvents() + { + $expected = 'request\.account|show\.account\.link'; + + $this->assertEquals($expected, $this->notificationEvent->getEvents()); + } + + protected function setUp(): void + { + parent::setUp(); + + $this->notificationService = $this->createMock(NotificationService::class); + $this->notificationEvent = new NotificationEvent($this->application, $this->notificationService); + } +} diff --git a/tests/SP/Domain/Plugin/Services/PluginBaseTest.php b/tests/SP/Domain/Plugin/Services/PluginBaseTest.php index 3bc7cfef..93c1c4f8 100644 --- a/tests/SP/Domain/Plugin/Services/PluginBaseTest.php +++ b/tests/SP/Domain/Plugin/Services/PluginBaseTest.php @@ -147,7 +147,7 @@ class PluginBaseTest extends UnitaryTestCase // TODO: Implement update() method. } - public function getEventsString(): ?string + public function getEvents(): ?string { // TODO: Implement getEventsString() method. }