diff --git a/src/Notification/Ntfy.php b/src/Notification/Ntfy.php index 4d53937..b45c202 100644 --- a/src/Notification/Ntfy.php +++ b/src/Notification/Ntfy.php @@ -14,6 +14,8 @@ use InvalidArgumentException; */ class Ntfy implements NotificationInterface { + const TOPIC_MAX_LENGTH = 256; + const MESSAGE_MAX_LENGTH = 4096; private string $topic = 'default'; /** @@ -42,11 +44,11 @@ class Ntfy implements NotificationInterface /** * Set the topic of the notification message. * - * @param string $topic Topic length between 1 and 256 characters. + * @param string $topic Topic length between 1 and TOPIC_MAX_LENGTH characters. */ public function setTopic(string $topic): void { - if (strlen($topic) < 1 || strlen($topic) >= 256) { + if (!strlen($topic) || strlen($topic) > self::TOPIC_MAX_LENGTH) { throw new InvalidArgumentException("Invalid topic length"); } @@ -66,11 +68,11 @@ class Ntfy implements NotificationInterface */ public function send(string $title, string $message): void { - if (strlen($title) < 1 || strlen($title) >= 256) { + if (!strlen($title) || strlen($title) > self::TOPIC_MAX_LENGTH) { throw new InvalidArgumentException("Invalid title length"); } - if (strlen($message) < 1 || strlen($message) >= 4096) { + if (!strlen($message) || strlen($message) > self::MESSAGE_MAX_LENGTH) { throw new InvalidArgumentException("Invalid message length"); } diff --git a/src/Template/TwigExtension.php b/src/Template/TwigExtension.php index 44c1013..db081d1 100644 --- a/src/Template/TwigExtension.php +++ b/src/Template/TwigExtension.php @@ -38,7 +38,7 @@ class TwigExtension extends AbstractExtension public function formatBytes($bytes, $precision = 2) { $size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; - $fact = (int)(floor((strlen((string)$bytes) - 1) / 3)); + $fact = (int)floor((strlen((string)$bytes) - 1) / 3); return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact]; } } diff --git a/tests/Notification/NtfyTest.php b/tests/Notification/NtfyTest.php index dbb36bf..3af6c8a 100644 --- a/tests/Notification/NtfyTest.php +++ b/tests/Notification/NtfyTest.php @@ -30,12 +30,19 @@ final class NtfyTest extends TestCase $config = ['domain' => 'https://test.com', 'topic' => 'something']; $instance = Ntfy::factory($config); $this->assertInstanceOf(Ntfy::class, $instance); + $this->assertEquals($instance->getTopic(), "something"); } - public function testSend(): void + public function testSendShort(): void { $this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); - $this->instance->send('title', 'text'); + $this->instance->send('t', 's'); + } + + public function testSendLong(): void + { + $this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); + $this->instance->send(str_repeat("t", Ntfy::TOPIC_MAX_LENGTH), str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH)); } /** @return array> */ @@ -45,8 +52,8 @@ final class NtfyTest extends TestCase ['', ''], ['', 'text'], ['title', ''], - [str_repeat("t", 256),'text'], - ['title',str_repeat("t", 4096)], + [str_repeat("t", Ntfy::TOPIC_MAX_LENGTH+1),'text'], + ['title',str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH+1)], ]; } @@ -59,7 +66,11 @@ final class NtfyTest extends TestCase public function testSetTopic(): void { - $topic = "abcdefg"; + $topic = "a"; + $this->instance->setTopic($topic); + $this->assertEquals($topic, $this->instance->getTopic()); + + $topic = str_repeat("a", Ntfy::TOPIC_MAX_LENGTH); $this->instance->setTopic($topic); $this->assertEquals($topic, $this->instance->getTopic()); } @@ -69,8 +80,8 @@ final class NtfyTest extends TestCase { return [ [''], - [str_repeat("t", 256)], - [str_repeat("t", 4096)], + [str_repeat("t", Ntfy::TOPIC_MAX_LENGTH+1)], + [str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH+1)], ]; } diff --git a/tests/Template/TwigExtensionTest.php b/tests/Template/TwigExtensionTest.php index 343e92e..dab15da 100644 --- a/tests/Template/TwigExtensionTest.php +++ b/tests/Template/TwigExtensionTest.php @@ -14,6 +14,7 @@ final class TwigExtensionTest extends \PHPUnit\Framework\TestCase { $obj = new TwigExtension(); $filters = $obj->getFilters(); + $this->assertNotEmpty($filters, "Filters must not be empty"); $this->assertContainsOnlyInstancesOf(TwigFilter::class, $filters); } @@ -31,5 +32,8 @@ final class TwigExtensionTest extends \PHPUnit\Framework\TestCase $this->assertEquals('1.00GB', $obj->formatBytes(1024 ** 3, 2)); $this->assertEquals('512.00GB', $obj->formatBytes(1024 ** 4 / 2, 2)); $this->assertEquals('1.00TB', $obj->formatBytes(1024 ** 4, 2)); + + $this->assertEquals('1.00B', $obj->formatBytes(1)); + $this->assertEquals('1.00TB', $obj->formatBytes(1024 ** 4)); } } diff --git a/tests/Template/TwigTest.php b/tests/Template/TwigTest.php index 2dd456f..a2ce7be 100644 --- a/tests/Template/TwigTest.php +++ b/tests/Template/TwigTest.php @@ -17,4 +17,12 @@ final class TwigTest extends \PHPUnit\Framework\TestCase $output = $template->render(['var' => 'middle']); $this->assertEquals('start middle end', $output); } + + public function testExtensionsLoaded(): void + { + $obj = new Twig(['template' => 'start {{ var | formatBytes}} end']); + $template = $obj->load('template'); + $output = $template->render(['var' => 1]); + $this->assertEquals('start 1.00B end', $output); + } }