96 lines
3.1 KiB
PHP
96 lines
3.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Post;
|
|
|
|
use App\Post\Post;
|
|
use App\Post\PostExtension;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Twig\TwigFunction;
|
|
|
|
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
|
final class PostExtensionTest extends TestCase
|
|
{
|
|
/** @var array<string, TwigFunction> */
|
|
private array $functions;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$extension = new PostExtension();
|
|
$this->functions = [];
|
|
|
|
foreach ($extension->getFunctions() as $function) {
|
|
$this->functions[$function->getName()] = $function;
|
|
}
|
|
}
|
|
|
|
public function testRegistersExpectedTwigFunctions(): void
|
|
{
|
|
self::assertSame([
|
|
'post_excerpt',
|
|
'post_url',
|
|
'post_thumbnail',
|
|
'post_initials',
|
|
], array_keys($this->functions));
|
|
}
|
|
|
|
public function testPostUrlUsesStoredSlug(): void
|
|
{
|
|
$post = new Post(1, 'Mon article', '<p>Contenu</p>', 'mon-article-2');
|
|
|
|
self::assertSame('/article/mon-article-2', $this->call('post_url', $post));
|
|
}
|
|
|
|
public function testPostExcerptKeepsSafeTagsAndTruncatesHtml(): void
|
|
{
|
|
$html = '<p><strong>Bonjour</strong> <script>alert(1)</script><em>monde</em> ' . str_repeat('x', 30) . '</p>';
|
|
$post = new Post(1, 'Titre', $html, 'titre');
|
|
|
|
$excerpt = $this->call('post_excerpt', $post, 20);
|
|
|
|
self::assertStringContainsString('<strong>Bonjour</strong>', $excerpt);
|
|
self::assertStringContainsString('<em>', $excerpt);
|
|
self::assertStringNotContainsString('<script>', $excerpt);
|
|
self::assertStringEndsWith('…', $excerpt);
|
|
}
|
|
|
|
public function testPostExcerptReturnsOriginalHtmlWhenAlreadyShortEnough(): void
|
|
{
|
|
$post = new Post(10, 'Titre', '<p><strong>Bonjour</strong> <em>monde</em></p>', 'titre-10');
|
|
|
|
self::assertSame('<strong>Bonjour</strong> <em>monde</em>', $this->call('post_excerpt', $post, 100));
|
|
}
|
|
|
|
public function testPostThumbnailReturnsFirstImageSource(): void
|
|
{
|
|
$post = new Post(1, 'Titre', '<p><img src="/media/a.webp" alt="a"><img src="/media/b.webp"></p>', 'titre');
|
|
|
|
self::assertSame('/media/a.webp', $this->call('post_thumbnail', $post));
|
|
}
|
|
|
|
public function testPostThumbnailReturnsNullWhenMissing(): void
|
|
{
|
|
$post = new Post(1, 'Titre', '<p>Sans image</p>', 'titre');
|
|
|
|
self::assertNull($this->call('post_thumbnail', $post));
|
|
}
|
|
|
|
public function testPostInitialsUseMeaningfulWordsAndFallback(): void
|
|
{
|
|
$post = new Post(1, 'Article de Blog', '<p>Contenu</p>', 'slug');
|
|
$single = new Post(2, 'A B', '<p>Contenu</p>', 'slug-2');
|
|
$stopWordsOnly = new Post(3, 'de la', '<p>Contenu</p>', 'slug-3');
|
|
|
|
self::assertSame('AB', $this->call('post_initials', $post));
|
|
self::assertSame('A', $this->call('post_initials', $single));
|
|
self::assertSame('D', $this->call('post_initials', $stopWordsOnly));
|
|
}
|
|
|
|
private function call(string $name, mixed ...$args): mixed
|
|
{
|
|
$callable = $this->functions[$name]->getCallable();
|
|
|
|
return $callable(...$args);
|
|
}
|
|
}
|