From a07084929373953a86c8905eaf6bfc169b293b42 Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 3 Jul 2023 14:28:53 +0000 Subject: [PATCH] More test coverage. --- src/Notification/Notification.php | 23 ++++++++++-- tests/Notification/NotificationTest.php | 50 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 tests/Notification/NotificationTest.php diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index d172cdb..b045690 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -9,7 +9,7 @@ class Notification /** * @var NotificationInterface[] $notifiers */ - public array $notifiers = array(); + private array $notifiers = array(); /** * Load multiple configurations @@ -28,7 +28,7 @@ class Notification * * @param string $key Notification class * @param string[] $config Implementation specific configuration - * @SuppressWarnings(PHPMD) + * @SuppressWarnings(PHPMD) */ public function loadSingle(string $key, array $config): void { @@ -36,16 +36,31 @@ class Notification case 'ntfy': case 'Ntfy': case 'NTFY': - $this->notifiers[] = Ntfy::factory($config); + $this->addNotifier(Ntfy::factory($config)); break; default: break; } } + public function addNotifier(NotificationInterface $instance): void + { + $this->notifiers[] = $instance; + } + + /** + * Get all active notifiers. + * + * @return NotificationInterface[] All notifiers. + */ + public function getNotifiers(): array + { + return $this->notifiers; + } + public function send(string $title, string $message): void { - foreach ($this->notifiers as $notifier) { + foreach ($this->getNotifiers() as $notifier) { $notifier->send($title, $message); } } diff --git a/tests/Notification/NotificationTest.php b/tests/Notification/NotificationTest.php new file mode 100644 index 0000000..9c21da9 --- /dev/null +++ b/tests/Notification/NotificationTest.php @@ -0,0 +1,50 @@ +'ntfy', 'domain'=>'https://test.com', 'topic'=>'testing']; + + public function testloadSingle(): void + { + $dut = new Notification(); + $dut->loadSingle('ntfy', self::$config); + $this->assertEquals(1, count($dut->getNotifiers())); + + $dut->loadSingle('Ntfy', self::$config); + $this->assertEquals(2, count($dut->getNotifiers())); + + $dut->loadSingle('NTFY', self::$config); + $this->assertEquals(3, count($dut->getNotifiers())); + + $dut->loadSingle('invalid', self::$config); + $this->assertEquals(3, count($dut->getNotifiers())); + } + + public function testloadMany(): void + { + $dut = new Notification(); + + $arr = []; + $arr[] = self::$config; + $arr[] = self::$config; + $arr[] = self::$config; + $arr[] = self::$config; + $dut->loadMany($arr); + + $this->assertEquals(4, count($dut->getNotifiers())); + } + + public function testSend(): void + { + $dut = new Notification(); + $mock = $this->createMock(NotificationInterface::class); + $dut->addNotifier($mock); + $mock->expects($this->once())->method('send'); + $dut->send('title', 'topic'); + } +} \ No newline at end of file