Skip to content

Commit

Permalink
IBX-7867: Added unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx committed Feb 29, 2024
1 parent a266300 commit 1ffbcfa
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/lib/Mapper/NotificationMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Notifications\Mapper;

use Ibexa\Contracts\Notifications\Value\Notification\SymfonyNotificationAdapter;
use Ibexa\Contracts\Notifications\Value\NotificationInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Notifications\Mapper\NotificationMapper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Notification\Notification;

final class NotificationMapperTest extends TestCase
{
private NotificationMapper $mapper;

public function setUp(): void
{
$this->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)
);
}
}
46 changes: 46 additions & 0 deletions tests/lib/Mapper/RecipientMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Notifications\Mapper;

use Ibexa\Contracts\Notifications\Value\Recipent\SymfonyRecipientAdapter;
use Ibexa\Contracts\Notifications\Value\RecipientInterface;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;
use Ibexa\Notifications\Mapper\RecipientMapper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Recipient\Recipient;

final class RecipientMapperTest extends TestCase
{
private RecipientMapper $mapper;

public function setUp(): void
{
$this->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)
);
}
}
73 changes: 73 additions & 0 deletions tests/lib/SubscriptionResolver/ChainSubscriptionResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Notifications\SubscriptionResolver;

use Ibexa\Contracts\Notifications\Value\NotificationInterface;
use Ibexa\Notifications\SubscriptionResolver\ChainSubscriptionResolver;
use Ibexa\Notifications\SubscriptionResolver\SubscriptionResolverInterface;
use PHPUnit\Framework\TestCase;

final class ChainSubscriptionResolverTest extends TestCase
{
/**
* @dataProvider provideForTestResolve
*
* @param array<\Ibexa\Notifications\SubscriptionResolver\SubscriptionResolverInterface> $resolvers
* @param array<string|null> $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<string, array{
* array<\Ibexa\Notifications\SubscriptionResolver\SubscriptionResolverInterface|null>,
* array<string>,
* }>
*/
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<string|null> $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;
}
}
103 changes: 103 additions & 0 deletions tests/lib/SubscriptionResolver/ConfigBasedSubscriptionResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Notifications\SubscriptionResolver;

use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Contracts\Notifications\Value\NotificationInterface;
use Ibexa\Notifications\SubscriptionResolver\ConfigBasedSubscriptionResolver;
use Ibexa\Notifications\Value\ChannelSubscription;
use PHPUnit\Framework\TestCase;

/**
* @phpstan-type TSubscriptionsConfig array<string, array{channels: array<string>}>
*/
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<string> $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<string, array{
* TSubscriptionsConfig,
* string,
* array<string>,
* }>
*/
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',
[],
];
}
}

0 comments on commit 1ffbcfa

Please sign in to comment.