Add tests for events

This commit is contained in:
Alejandro Celaya 2025-11-18 09:30:30 +01:00
parent 7812a85b39
commit 9432a5ba78
3 changed files with 65 additions and 3 deletions

View File

@ -15,9 +15,7 @@ final readonly class ShortUrlCreated implements JsonSerializable, JsonUnserializ
public function jsonSerialize(): array
{
return [
'shortUrlId' => $this->shortUrlId,
];
return ['shortUrlId' => $this->shortUrlId];
}
public static function fromPayload(array $payload): self

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Event;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
class ShortUrlCreatedTest extends TestCase
{
#[Test]
public function jsonSerialization(): void
{
$shortUrlId = 'abc123';
self::assertEquals(['shortUrlId' => $shortUrlId], new ShortUrlCreated($shortUrlId)->jsonSerialize());
}
#[Test]
#[TestWith([['shortUrlId' => '123'], '123'])]
#[TestWith([[], ''])]
public function creationFromPayload(array $payload, string $expectedShortUrlId): void
{
$event = ShortUrlCreated::fromPayload($payload);
self::assertEquals($expectedShortUrlId, $event->shortUrlId);
}
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher\Event;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\EventDispatcher\Event\UrlVisited;
class UrlVisitedTest extends TestCase
{
#[Test]
public function jsonSerialization(): void
{
$visitId = 'abc123';
self::assertEquals(
['visitId' => $visitId, 'originalIpAddress' => null],
new UrlVisited($visitId)->jsonSerialize(),
);
}
#[Test]
#[TestWith([['visitId' => '123', 'originalIpAddress' => '1.2.3.4'], '123', '1.2.3.4'])]
#[TestWith([['visitId' => '123'], '123', null])]
#[TestWith([['originalIpAddress' => '1.2.3.4'], '', '1.2.3.4'])]
#[TestWith([[], '', null])]
public function creationFromPayload(array $payload, string $expectedVisitId, string|null $expectedIpAddress): void
{
$event = UrlVisited::fromPayload($payload);
self::assertEquals($expectedVisitId, $event->visitId);
self::assertEquals($expectedIpAddress, $event->originalIpAddress);
}
}