Killing a few "mutants"
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Jens True 2023-11-03 12:02:44 +00:00
parent 61001edaa0
commit 66ccb3d8d6
5 changed files with 37 additions and 12 deletions

@ -14,6 +14,8 @@ use InvalidArgumentException;
*/ */
class Ntfy implements NotificationInterface class Ntfy implements NotificationInterface
{ {
const TOPIC_MAX_LENGTH = 256;
const MESSAGE_MAX_LENGTH = 4096;
private string $topic = 'default'; private string $topic = 'default';
/** /**
@ -42,11 +44,11 @@ class Ntfy implements NotificationInterface
/** /**
* Set the topic of the notification message. * 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 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"); throw new InvalidArgumentException("Invalid topic length");
} }
@ -66,11 +68,11 @@ class Ntfy implements NotificationInterface
*/ */
public function send(string $title, string $message): void 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"); 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"); throw new InvalidArgumentException("Invalid message length");
} }

@ -38,7 +38,7 @@ class TwigExtension extends AbstractExtension
public function formatBytes($bytes, $precision = 2) public function formatBytes($bytes, $precision = 2)
{ {
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; $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]; return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
} }
} }

@ -30,12 +30,19 @@ final class NtfyTest extends TestCase
$config = ['domain' => 'https://test.com', 'topic' => 'something']; $config = ['domain' => 'https://test.com', 'topic' => 'something'];
$instance = Ntfy::factory($config); $instance = Ntfy::factory($config);
$this->assertInstanceOf(Ntfy::class, $instance); $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->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<int, array<int, string>> */ /** @return array<int, array<int, string>> */
@ -45,8 +52,8 @@ final class NtfyTest extends TestCase
['', ''], ['', ''],
['', 'text'], ['', 'text'],
['title', ''], ['title', ''],
[str_repeat("t", 256),'text'], [str_repeat("t", Ntfy::TOPIC_MAX_LENGTH+1),'text'],
['title',str_repeat("t", 4096)], ['title',str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH+1)],
]; ];
} }
@ -59,7 +66,11 @@ final class NtfyTest extends TestCase
public function testSetTopic(): void 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->instance->setTopic($topic);
$this->assertEquals($topic, $this->instance->getTopic()); $this->assertEquals($topic, $this->instance->getTopic());
} }
@ -69,8 +80,8 @@ final class NtfyTest extends TestCase
{ {
return [ return [
[''], [''],
[str_repeat("t", 256)], [str_repeat("t", Ntfy::TOPIC_MAX_LENGTH+1)],
[str_repeat("t", 4096)], [str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH+1)],
]; ];
} }

@ -14,6 +14,7 @@ final class TwigExtensionTest extends \PHPUnit\Framework\TestCase
{ {
$obj = new TwigExtension(); $obj = new TwigExtension();
$filters = $obj->getFilters(); $filters = $obj->getFilters();
$this->assertNotEmpty($filters, "Filters must not be empty");
$this->assertContainsOnlyInstancesOf(TwigFilter::class, $filters); $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('1.00GB', $obj->formatBytes(1024 ** 3, 2));
$this->assertEquals('512.00GB', $obj->formatBytes(1024 ** 4 / 2, 2)); $this->assertEquals('512.00GB', $obj->formatBytes(1024 ** 4 / 2, 2));
$this->assertEquals('1.00TB', $obj->formatBytes(1024 ** 4, 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));
} }
} }

@ -17,4 +17,12 @@ final class TwigTest extends \PHPUnit\Framework\TestCase
$output = $template->render(['var' => 'middle']); $output = $template->render(['var' => 'middle']);
$this->assertEquals('start middle end', $output); $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);
}
} }