backupscript/tests/Notification/NtfyTest.php

94 lines
2.7 KiB
PHP
Raw Normal View History

2023-07-10 09:20:36 +00:00
<?php
declare(strict_types=1);
namespace App\Tests;
2023-06-15 14:10:17 +00:00
use PHPUnit\Framework\TestCase;
use App\Notification\Ntfy;
use Ntfy\Client;
use Ntfy\Message;
2023-07-03 13:01:31 +00:00
use PHPUnit\Framework\Attributes\DataProvider;
2023-07-10 09:20:36 +00:00
use PHPUnit\Framework\MockObject\MockObject;
2023-06-16 08:08:09 +00:00
2023-06-15 14:10:17 +00:00
final class NtfyTest extends TestCase
{
2023-07-10 09:20:36 +00:00
private Ntfy $instance;
private MockObject $client;
2023-07-03 13:01:31 +00:00
protected function setUp(): void
{
$this->client = $this->createMock(Client::class);
$this->instance = new Ntfy($this->client);
}
2023-07-10 09:20:36 +00:00
/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
2023-07-03 13:01:31 +00:00
public function testFactory(): void
{
2023-07-10 09:20:36 +00:00
$config = ['domain' => 'https://test.com', 'topic' => 'something'];
2023-07-03 13:01:31 +00:00
$instance = Ntfy::factory($config);
$this->assertInstanceOf(Ntfy::class, $instance);
2023-11-03 12:02:44 +00:00
$this->assertEquals($instance->getTopic(), "something");
2023-07-03 13:01:31 +00:00
}
2023-11-03 12:02:44 +00:00
public function testSendShort(): void
2023-06-15 14:10:17 +00:00
{
2023-07-03 13:01:31 +00:00
$this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class));
2023-11-03 12:02:44 +00:00
$this->instance->send('t', 's');
}
public function testSendLong(): void
{
$this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class));
2023-11-03 13:29:50 +00:00
$this->instance->send(str_repeat("t", Ntfy::TITLE_MAX_LENGTH), str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH));
2023-07-03 13:01:31 +00:00
}
2023-07-10 09:20:36 +00:00
/** @return array<int, array<int, string>> */
2023-07-03 13:01:31 +00:00
public static function sendBadParameterProvider(): array
{
return [
['', ''],
['', 'text'],
['title', ''],
2023-11-03 13:29:50 +00:00
[str_repeat("t", Ntfy::TITLE_MAX_LENGTH + 1), 'text'],
['title',str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH + 1)],
2023-07-03 13:01:31 +00:00
];
}
#[DataProvider('sendBadParameterProvider')]
2023-07-10 09:20:36 +00:00
public function testSendInvalidParameters(string $title, string $message): void
2023-07-03 13:01:31 +00:00
{
2023-07-10 09:20:36 +00:00
$this->expectException(\InvalidArgumentException::class);
2023-07-03 13:01:31 +00:00
$this->instance->send($title, $message);
}
public function testSetTopic(): void
{
2023-11-03 12:02:44 +00:00
$topic = "a";
$this->instance->setTopic($topic);
$this->assertEquals($topic, $this->instance->getTopic());
$topic = str_repeat("a", Ntfy::TOPIC_MAX_LENGTH);
2023-07-03 13:01:31 +00:00
$this->instance->setTopic($topic);
$this->assertEquals($topic, $this->instance->getTopic());
}
2023-07-10 09:20:36 +00:00
/** @return array<int, array<int, string>> */
2023-07-03 13:01:31 +00:00
public static function topicBadParameterProvider(): array
{
return [
[''],
2023-11-03 13:29:50 +00:00
[str_repeat("t", Ntfy::TOPIC_MAX_LENGTH + 1)],
2023-07-03 13:01:31 +00:00
];
}
#[DataProvider('topicBadParameterProvider')]
2023-07-10 09:20:36 +00:00
public function testSetTopicInvalidParameters(string $topic): void
2023-07-03 13:01:31 +00:00
{
2023-07-10 09:20:36 +00:00
$this->expectException(\InvalidArgumentException::class);
$this->instance->setTopic($topic);
2023-06-15 14:10:17 +00:00
}
2023-07-10 09:20:36 +00:00
}