From 022c1707e284690309a2d4bf848e4ee888c6672b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Mon, 17 Feb 2020 17:19:34 +0100 Subject: [PATCH] [Notifier] Add tests for AbstractChannel and ChannelPolicy --- .../Tests/Channel/AbstractChannelTest.php | 44 +++++++++++++++++ .../Tests/Channel/ChannelPolicyTest.php | 48 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php create mode 100644 src/Symfony/Component/Notifier/Tests/Channel/ChannelPolicyTest.php diff --git a/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php b/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php new file mode 100644 index 0000000000..abaf5a1692 --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Tests\Channel; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Channel\AbstractChannel; +use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Notification\Notification; +use Symfony\Component\Notifier\Recipient\Recipient; + +/** + * @author Jan Schädlich + */ +class AbstractChannelTest extends TestCase +{ + public function testChannelCannotBeConstructedWithoutTransportAndBus() + { + $this->expectException(LogicException::class); + + new DummyChannel(); + } +} + +class DummyChannel extends AbstractChannel +{ + public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + { + return; + } + + public function supports(Notification $notification, Recipient $recipient): bool + { + return false; + } +} diff --git a/src/Symfony/Component/Notifier/Tests/Channel/ChannelPolicyTest.php b/src/Symfony/Component/Notifier/Tests/Channel/ChannelPolicyTest.php new file mode 100644 index 0000000000..59acb33f0c --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/Channel/ChannelPolicyTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Tests\Channel; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Channel\ChannelPolicy; +use Symfony\Component\Notifier\Exception\InvalidArgumentException; + +/** + * @author Jan Schädlich + */ +class ChannelPolicyTest extends TestCase +{ + public function testCannotRetrieveChannelsUsingUnavailableImportance() + { + $this->expectException(InvalidArgumentException::class); + + $channelPolicy = new ChannelPolicy(['urgent' => ['chat']]); + $channelPolicy->getChannels('low'); + } + + /** + * @dataProvider provideValidPolicies + */ + public function testCanRetrieveChannels(array $policy, string $importance, array $expectedChannels) + { + $channelPolicy = new ChannelPolicy($policy); + $channels = $channelPolicy->getChannels($importance); + + $this->assertSame($expectedChannels, $channels); + } + + public function provideValidPolicies(): \Generator + { + yield [['urgent' => ['chat']], 'urgent', ['chat']]; + yield [['urgent' => ['chat', 'sms']], 'urgent', ['chat', 'sms']]; + yield [['urgent' => ['chat', 'chat/slack', 'sms']], 'urgent', ['chat', 'chat/slack', 'sms']]; + } +}