diff --git a/tests/lib/Mapper/NotificationMapperTest.php b/tests/lib/Mapper/NotificationMapperTest.php new file mode 100644 index 0000000..f2d29ff --- /dev/null +++ b/tests/lib/Mapper/NotificationMapperTest.php @@ -0,0 +1,46 @@ +mapper = new NotificationMapper(); + } + + public function testMapToSymfonyNotification(): void + { + $notification = $this->createMock(Notification::class); + + $symfonyNotification = $this->mapper->mapToSymfonyNotification( + new SymfonyNotificationAdapter($notification) + ); + + $this->assertSame($notification, $symfonyNotification); + } + + public function testMapToSymfonyNotificationThrowsExceptionOnInvalidNotification(): void + { + $this->expectException(InvalidArgumentException::class); + + $this->mapper->mapToSymfonyNotification( + $this->createMock(NotificationInterface::class) + ); + } +} diff --git a/tests/lib/Mapper/RecipientMapperTest.php b/tests/lib/Mapper/RecipientMapperTest.php new file mode 100644 index 0000000..ac85159 --- /dev/null +++ b/tests/lib/Mapper/RecipientMapperTest.php @@ -0,0 +1,46 @@ +mapper = new RecipientMapper(); + } + + public function testMapToSymfonyRecipient(): void + { + $recipient = $this->createMock(Recipient::class); + + $symfonyRecipient = $this->mapper->mapToSymfonyRecipient( + new SymfonyRecipientAdapter($recipient) + ); + + $this->assertSame($recipient, $symfonyRecipient); + } + + public function testMapToSymfonyRecipientThrowsExceptionOnInvalidRecipient(): void + { + $this->expectException(InvalidArgumentException::class); + + $this->mapper->mapToSymfonyRecipient( + $this->createMock(RecipientInterface::class) + ); + } +} diff --git a/tests/lib/SubscriptionResolver/ChainSubscriptionResolverTest.php b/tests/lib/SubscriptionResolver/ChainSubscriptionResolverTest.php new file mode 100644 index 0000000..614cd90 --- /dev/null +++ b/tests/lib/SubscriptionResolver/ChainSubscriptionResolverTest.php @@ -0,0 +1,73 @@ + $resolvers + * @param array $expectedChannels + */ + public function testResolve(array $resolvers, array $expectedChannels): void + { + $subscriptionResolver = new ChainSubscriptionResolver($resolvers); + + $notification = $this->createMock(NotificationInterface::class); + $channels = $subscriptionResolver->resolve($notification); + + $this->assertSame($expectedChannels, iterator_to_array($channels)); + } + + /** + * @return iterable, + * array, + * }> + */ + public function provideForTestResolve(): iterable + { + yield 'returns all results' => [ + [ + $this->mockResolver(['sms', 'mail']), + $this->mockResolver(['push']), + ], + ['sms', 'mail', 'push'], + ]; + + yield 'skips empty channels' => [ + [ + $this->mockResolver(['sms', null, null]), + $this->mockResolver([null, 'push', null]), + ], + ['sms', 'push'], + ]; + } + + /** + * @param array $channels + * + * @return \Ibexa\Notifications\SubscriptionResolver\SubscriptionResolverInterface&\PHPUnit\Framework\MockObject\MockObject + */ + private function mockResolver(array $channels): SubscriptionResolverInterface + { + $subscriptionResolver = $this->createMock(SubscriptionResolverInterface::class); + $subscriptionResolver + ->method('resolve') + ->willReturn($channels); + + return $subscriptionResolver; + } +} diff --git a/tests/lib/SubscriptionResolver/ConfigBasedSubscriptionResolverTest.php b/tests/lib/SubscriptionResolver/ConfigBasedSubscriptionResolverTest.php new file mode 100644 index 0000000..e5fdc30 --- /dev/null +++ b/tests/lib/SubscriptionResolver/ConfigBasedSubscriptionResolverTest.php @@ -0,0 +1,103 @@ +}> + */ +final class ConfigBasedSubscriptionResolverTest extends TestCase +{ + private ConfigBasedSubscriptionResolver $resolver; + + /** @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface|\PHPUnit\Framework\MockObject\MockObject */ + private ConfigResolverInterface $configResolver; + + public function setUp(): void + { + $this->configResolver = $this->createMock(ConfigResolverInterface::class); + $this->resolver = new ConfigBasedSubscriptionResolver($this->configResolver); + } + + /** + * @dataProvider provideForTestResolve + * + * @phpstan-param TSubscriptionsConfig $subscriptions + * + * @param array $expectedChannels + */ + public function testResolve( + array $subscriptions, + string $notificationType, + array $expectedChannels + ): void { + $this->configResolver + ->method('getParameter') + ->willReturn($subscriptions); + + $notification = $this->createMock(NotificationInterface::class); + $notification + ->method('getType') + ->willReturn($notificationType); + + $subscriptions = $this->resolver->resolve($notification); + $subscriptionsArray = iterator_to_array($subscriptions); + + $this->assertCount(count($expectedChannels), $subscriptionsArray); + $this->assertContainsOnlyInstancesOf(ChannelSubscription::class, $subscriptionsArray); + + $i = 0; + foreach ($subscriptionsArray as $channelSubscription) { + $this->assertSame($notificationType, $channelSubscription->getNotificationType()); + $this->assertSame($expectedChannels[$i++], $channelSubscription->getChannel()); + } + } + + /** + * @return iterable, + * }> + */ + public function provideForTestResolve(): iterable + { + $config = [ + NotificationInterface::class => [ + 'channels' => ['sms', 'mail', 'push'], + ], + '\\Vendor\\Package\\Notification\\ActionSuccess' => [ + 'channels' => ['browser'], + ], + ]; + + yield 'NotificationInterface type' => [ + $config, + NotificationInterface::class, + ['sms', 'mail', 'push'], + ]; + + yield '\\Vendor\\Package\\Notification\\ActionSuccess type' => [ + $config, + '\\Vendor\\Package\\Notification\\ActionSuccess', + ['browser'], + ]; + + yield 'no subscriptions when not configured' => [ + $config, + '\\Vendor\\Package\\Notification\\NotConfigured', + [], + ]; + } +}