diff --git a/.vscode/launch.json b/.vscode/launch.json index 56952b1..1c237be 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,22 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Debug test script", + "type": "php", + "request": "launch", + "program": "", + "cwd": "${workspaceFolder}", + "port": 0, + "args": ["vendor/bin/phpunit", "${file}"], + "runtimeArgs": [ + "-dxdebug.start_with_request=yes" + ], + "env": { + "XDEBUG_MODE": "debug,develop", + "XDEBUG_CONFIG": "client_port=${port}" + } + }, { "name": "Launch currently open script", "type": "php", diff --git a/Makefile b/Makefile index 05fa947..16fc6f8 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ analyze-phpmd: analyze-phpstan: vendor/bin/phpstan analyze --level=7 --error-format=raw src/ backup analyze-psalm: - vendor/bin/psalm + vendor/bin/psalm --no-cache analyze-phpcs: vendor/bin/phpcs src backup --report=emacs --standard=PSR12 diff --git a/src/Notification/Ntfy.php b/src/Notification/Ntfy.php index d9aeff2..ea71c0a 100644 --- a/src/Notification/Ntfy.php +++ b/src/Notification/Ntfy.php @@ -5,6 +5,8 @@ namespace App\Notification; use Ntfy\Server; use Ntfy\Message; use Ntfy\Client; +use Exception; +use InvalidArgumentException; class Ntfy implements NotificationInterface { @@ -31,18 +33,30 @@ class Ntfy implements NotificationInterface public function setTopic(string $topic): void { + if (strlen($topic) < 1 || strlen($topic) >= 256) { + throw new InvalidArgumentException("Invalid topic length"); + } + $this->topic = $topic; } + public function getTopic(): string + { + return $this->topic; + } + public function send(string $title, string $message): void { - assert(strlen($title) > 0); - assert(strlen($title) < 256); - assert(strlen($message) > 0); - assert(strlen($message) < 4096); + if (strlen($title) < 1 || strlen($title) >= 256) { + throw new InvalidArgumentException("Invalid title length"); + } + + if (strlen($message) < 1 || strlen($message) >= 4096) { + throw new InvalidArgumentException("Invalid message length"); + } $msg = new Message(); - $msg->topic($this->topic); + $msg->topic($this->getTopic()); $msg->title($title); $msg->body($message); $this->client->send($msg); diff --git a/tests/Notification/NtfyTest.php b/tests/Notification/NtfyTest.php index 77cee4c..876c356 100644 --- a/tests/Notification/NtfyTest.php +++ b/tests/Notification/NtfyTest.php @@ -4,14 +4,77 @@ use PHPUnit\Framework\TestCase; use App\Notification\Ntfy; use Ntfy\Client; use Ntfy\Message; +use PHPUnit\Framework\Attributes\DataProvider; final class NtfyTest extends TestCase { + + private ?Ntfy $instance; + + protected function setUp(): void + { + $this->client = $this->createMock(Client::class); + $this->instance = new Ntfy($this->client); + } + + protected function tearDown(): void + { + $this->instance = null; + } + + public function testFactory(): void + { + $config = ['domain'=>'https://test.com', 'topic'=>'something']; + $instance = Ntfy::factory($config); + $this->assertInstanceOf(Ntfy::class, $instance); + } + public function testSend(): void { - $client = $this->createMock(Client::class); - $sut = new Ntfy($client); - $client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); - $sut->send('title','text'); + $this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); + $this->instance->send('title','text'); + } + + + public static function sendBadParameterProvider(): array + { + return [ + ['', ''], + ['', 'text'], + ['title', ''], + [str_repeat("t", 256),'text'], + ['title',str_repeat("t", 4096)], + ]; + } + + #[DataProvider('sendBadParameterProvider')] + public function testSendInvalidParameters($title, $message): void + { + $this->expectException(InvalidArgumentException::class); + $this->instance->send($title, $message); + } + + public function testSetTopic(): void + { + $topic = "abcdefg"; + $this->instance->setTopic($topic); + $this->assertEquals($topic, $this->instance->getTopic()); + } + + public static function topicBadParameterProvider(): array + { + return [ + [''], + [str_repeat("t", 256)], + [str_repeat("t", 4096)], + ]; + } + + #[DataProvider('topicBadParameterProvider')] + public function testSetTopicInvalidParameters( $topic ): void + { + $this->expectException(InvalidArgumentException::class); + $this->instance->setTopic( $topic ); + } } \ No newline at end of file