Jens True
bedc666f7c
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use App\Notification\Ntfy;
|
|
use Ntfy\Client;
|
|
use Ntfy\Message;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
final class NtfyTest extends TestCase
|
|
{
|
|
private Ntfy $instance;
|
|
private MockObject $client;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->client = $this->createMock(Client::class);
|
|
$this->instance = new Ntfy($this->client);
|
|
}
|
|
|
|
/**
|
|
* @SuppressWarnings(PHPMD.StaticAccess)
|
|
*/
|
|
public function testFactory(): void
|
|
{
|
|
$config = ['domain' => 'https://test.com', 'topic' => 'something'];
|
|
$instance = Ntfy::factory($config);
|
|
$this->assertInstanceOf(Ntfy::class, $instance);
|
|
$this->assertEquals($instance->getTopic(), "something");
|
|
}
|
|
|
|
public function testSendShort(): void
|
|
{
|
|
$this->client->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class));
|
|
$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::TITLE_MAX_LENGTH), str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH));
|
|
}
|
|
|
|
/** @return array<int, array<int, string>> */
|
|
public static function sendBadParameterProvider(): array
|
|
{
|
|
return [
|
|
['', ''],
|
|
['', 'text'],
|
|
['title', ''],
|
|
[str_repeat("t", Ntfy::TITLE_MAX_LENGTH + 1), 'text'],
|
|
['title',str_repeat("t", Ntfy::MESSAGE_MAX_LENGTH + 1)],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('sendBadParameterProvider')]
|
|
public function testSendInvalidParameters(string $title, string $message): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->instance->send($title, $message);
|
|
}
|
|
|
|
public function testSetTopic(): void
|
|
{
|
|
$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());
|
|
}
|
|
|
|
/** @return array<int, array<int, string>> */
|
|
public static function topicBadParameterProvider(): array
|
|
{
|
|
return [
|
|
[''],
|
|
[str_repeat("t", Ntfy::TOPIC_MAX_LENGTH + 1)],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('topicBadParameterProvider')]
|
|
public function testSetTopicInvalidParameters(string $topic): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->instance->setTopic($topic);
|
|
}
|
|
}
|