From 31891f3e537984bd2378a9b1ca330af3c9768a75 Mon Sep 17 00:00:00 2001 From: Jens True Date: Mon, 14 Aug 2023 14:24:19 +0000 Subject: [PATCH] More tests --- composer.lock | 48 +++++++++++----------- src/Notification/Notification.php | 11 ++++- src/Notification/NotificationInterface.php | 1 + tests/Notification/NotificationTest.php | 40 ++++++++++++++++-- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/composer.lock b/composer.lock index 56acfbe..54aea1e 100644 --- a/composer.lock +++ b/composer.lock @@ -591,21 +591,21 @@ }, { "name": "nette/schema", - "version": "v1.2.3", + "version": "v1.2.4", "source": { "type": "git", "url": "https://github.com/nette/schema.git", - "reference": "abbdbb70e0245d5f3bf77874cea1dfb0c930d06f" + "reference": "c9ff517a53903b3d4e29ec547fb20feecb05b8ab" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/schema/zipball/abbdbb70e0245d5f3bf77874cea1dfb0c930d06f", - "reference": "abbdbb70e0245d5f3bf77874cea1dfb0c930d06f", + "url": "https://api.github.com/repos/nette/schema/zipball/c9ff517a53903b3d4e29ec547fb20feecb05b8ab", + "reference": "c9ff517a53903b3d4e29ec547fb20feecb05b8ab", "shasum": "" }, "require": { "nette/utils": "^2.5.7 || ^3.1.5 || ^4.0", - "php": ">=7.1 <8.3" + "php": "7.1 - 8.3" }, "require-dev": { "nette/tester": "^2.3 || ^2.4", @@ -647,9 +647,9 @@ ], "support": { "issues": "https://github.com/nette/schema/issues", - "source": "https://github.com/nette/schema/tree/v1.2.3" + "source": "https://github.com/nette/schema/tree/v1.2.4" }, - "time": "2022-10-13T01:24:26+00:00" + "time": "2023-08-05T18:56:25+00:00" }, { "name": "nette/utils", @@ -2701,16 +2701,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.16.0", + "version": "v4.17.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17" + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/19526a33fb561ef417e822e85f08a00db4059c17", - "reference": "19526a33fb561ef417e822e85f08a00db4059c17", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", + "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", "shasum": "" }, "require": { @@ -2751,9 +2751,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.16.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1" }, - "time": "2023-06-25T14:52:30+00:00" + "time": "2023-08-13T19:53:39+00:00" }, { "name": "pdepend/pdepend", @@ -3041,16 +3041,16 @@ }, { "name": "phpdocumentor/type-resolver", - "version": "1.7.2", + "version": "1.7.3", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "b2fe4d22a5426f38e014855322200b97b5362c0d" + "reference": "3219c6ee25c9ea71e3d9bbaf39c67c9ebd499419" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/b2fe4d22a5426f38e014855322200b97b5362c0d", - "reference": "b2fe4d22a5426f38e014855322200b97b5362c0d", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/3219c6ee25c9ea71e3d9bbaf39c67c9ebd499419", + "reference": "3219c6ee25c9ea71e3d9bbaf39c67c9ebd499419", "shasum": "" }, "require": { @@ -3093,9 +3093,9 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.7.2" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.7.3" }, - "time": "2023-05-30T18:13:47+00:00" + "time": "2023-08-12T11:01:26+00:00" }, { "name": "phpmd/phpmd", @@ -3297,16 +3297,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.27", + "version": "1.10.28", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640" + "reference": "e4545b55904ebef470423d3ddddb74fa7325497a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/a9f44dcea06f59d1363b100bb29f297b311fa640", - "reference": "a9f44dcea06f59d1363b100bb29f297b311fa640", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e4545b55904ebef470423d3ddddb74fa7325497a", + "reference": "e4545b55904ebef470423d3ddddb74fa7325497a", "shasum": "" }, "require": { @@ -3355,7 +3355,7 @@ "type": "tidelift" } ], - "time": "2023-08-05T09:57:55+00:00" + "time": "2023-08-08T12:33:42+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Notification/Notification.php b/src/Notification/Notification.php index 1e9eaae..baf95cb 100644 --- a/src/Notification/Notification.php +++ b/src/Notification/Notification.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Notification; use App\Notification\Ntfy; +use Psr\Log\NullLogger; class Notification { @@ -13,6 +14,10 @@ class Notification */ private array $notifiers = array(); + public function __construct(private NullLogger $logger = new NullLogger()) + { + } + /** * Load multiple configurations * @@ -63,7 +68,11 @@ class Notification public function send(string $title, string $message): void { foreach ($this->getNotifiers() as $notifier) { - $notifier->send($title, $message); + try { + $notifier->send($title, $message); + } catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } } } } diff --git a/src/Notification/NotificationInterface.php b/src/Notification/NotificationInterface.php index 9d2ef40..11841de 100644 --- a/src/Notification/NotificationInterface.php +++ b/src/Notification/NotificationInterface.php @@ -6,5 +6,6 @@ namespace App\Notification; interface NotificationInterface { + static function factory(array $config): self; public function send(string $title, string $message): void; } diff --git a/tests/Notification/NotificationTest.php b/tests/Notification/NotificationTest.php index 239218e..cb7534a 100644 --- a/tests/Notification/NotificationTest.php +++ b/tests/Notification/NotificationTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase; use App\Notification\Notification; use App\Notification\NotificationInterface; use PHPUnit\Framework\Attributes\DataProvider; +use Exception; final class NotificationTest extends TestCase { @@ -44,12 +45,43 @@ final class NotificationTest extends TestCase $this->assertEquals(4, count($dut->getNotifiers())); } - public function testSend(): void + public function testSendNoNotifier(): void { $dut = new Notification(); - $mock = $this->createMock(NotificationInterface::class); - $dut->addNotifier($mock); - $mock->expects($this->once())->method('send'); + $dut->send('title', 'topic'); + $this->assertEquals(0, count($dut->getNotifiers())); + } + + public function testSendOneNotifier(): void + { + $dut = new Notification(); + $mock1 = $this->createMock(NotificationInterface::class); + $dut->addNotifier($mock1); + $mock1->expects($this->once())->method('send'); + $dut->send('title', 'topic'); + } + + public function testSendMoreNotifiers(): void + { + $dut = new Notification(); + $mock1 = $this->createMock(NotificationInterface::class); + $mock2 = $this->createMock(NotificationInterface::class); + $dut->addNotifier($mock1); + $dut->addNotifier($mock2); + $mock1->expects($this->once())->method('send'); + $mock2->expects($this->once())->method('send'); + $dut->send('title', 'topic'); + } + + public function testSendErrorInNotifiers(): void + { + $dut = new Notification(); + $mock1 = $this->createMock(NotificationInterface::class); + $mock2 = $this->createMock(NotificationInterface::class); + $dut->addNotifier($mock1); + $dut->addNotifier($mock2); + $mock1->expects($this->once())->method('send')->willThrowException(new Exception()); + $mock2->expects($this->once())->method('send'); $dut->send('title', 'topic'); } }