101 lines
3.3 KiB
PHP
101 lines
3.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Post;
|
|
|
|
use App\Post\Post;
|
|
use DateTime;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
|
|
|
final class PostModelTest extends TestCase
|
|
{
|
|
public function testConstructAndGettersExposePostData(): void
|
|
{
|
|
$createdAt = new DateTime('2026-01-01 10:00:00');
|
|
$updatedAt = new DateTime('2026-01-02 11:00:00');
|
|
|
|
$post = new Post(
|
|
10,
|
|
'Été en forêt',
|
|
'<p>Contenu</p>',
|
|
'ete-en-foret-2',
|
|
5,
|
|
'julien',
|
|
3,
|
|
'Tech',
|
|
'tech',
|
|
$createdAt,
|
|
$updatedAt,
|
|
);
|
|
|
|
self::assertSame(10, $post->getId());
|
|
self::assertSame('Été en forêt', $post->getTitle());
|
|
self::assertSame('<p>Contenu</p>', $post->getContent());
|
|
self::assertSame('ete-en-foret-2', $post->getStoredSlug());
|
|
self::assertSame('ete-en-foret', $post->generateSlug());
|
|
self::assertSame(5, $post->getAuthorId());
|
|
self::assertSame('julien', $post->getAuthorUsername());
|
|
self::assertSame(3, $post->getCategoryId());
|
|
self::assertSame('Tech', $post->getCategoryName());
|
|
self::assertSame('tech', $post->getCategorySlug());
|
|
self::assertSame($createdAt, $post->getCreatedAt());
|
|
self::assertSame($updatedAt, $post->getUpdatedAt());
|
|
}
|
|
|
|
public function testFromArrayHydratesOptionalFields(): void
|
|
{
|
|
$post = Post::fromArray([
|
|
'id' => '7',
|
|
'title' => 'Titre',
|
|
'content' => '<p>Texte</p>',
|
|
'slug' => 'titre',
|
|
'author_id' => '9',
|
|
'author_username' => 'alice',
|
|
'category_id' => '2',
|
|
'category_name' => 'Actualités',
|
|
'category_slug' => 'actualites',
|
|
'created_at' => '2026-02-01 10:00:00',
|
|
'updated_at' => '2026-02-02 11:00:00',
|
|
]);
|
|
|
|
self::assertSame(7, $post->getId());
|
|
self::assertSame('alice', $post->getAuthorUsername());
|
|
self::assertSame('Actualités', $post->getCategoryName());
|
|
self::assertSame('2026-02-01 10:00:00', $post->getCreatedAt()->format('Y-m-d H:i:s'));
|
|
}
|
|
|
|
public function testValidationRejectsEmptyTitle(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Le titre ne peut pas être vide');
|
|
|
|
new Post(1, '', '<p>Contenu</p>');
|
|
}
|
|
|
|
public function testValidationRejectsTooLongTitle(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Le titre ne peut pas dépasser 255 caractères');
|
|
|
|
new Post(1, str_repeat('a', 256), '<p>Contenu</p>');
|
|
}
|
|
|
|
public function testValidationRejectsEmptyContent(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Le contenu ne peut pas être vide');
|
|
|
|
new Post(1, 'Titre', '');
|
|
}
|
|
|
|
public function testValidationRejectsTooLongContent(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Le contenu ne peut pas dépasser 65 535 caractères');
|
|
|
|
new Post(1, 'Titre', str_repeat('a', 65536));
|
|
}
|
|
}
|