More tests and error handling
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-07-03 13:01:31 +00:00
parent 27001e63c2
commit 49141719b5
4 changed files with 103 additions and 10 deletions

16
.vscode/launch.json vendored
View File

@ -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",

View File

@ -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

View File

@ -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);

View File

@ -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 );
}
}