first commit
This commit is contained in:
77
tests/Post/PostExtensionTest.php
Normal file
77
tests/Post/PostExtensionTest.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Post;
|
||||
|
||||
use App\Post\Post;
|
||||
use App\Post\PostExtension;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
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 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 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');
|
||||
$emptyLike = new Post(3, 'A', '<p>Contenu</p>', 'slug-3');
|
||||
|
||||
self::assertSame('AB', $this->call('post_initials', $post));
|
||||
self::assertSame('A', $this->call('post_initials', $single));
|
||||
self::assertSame('A', $this->call('post_initials', $emptyLike));
|
||||
}
|
||||
|
||||
private function call(string $name, mixed ...$args): mixed
|
||||
{
|
||||
$callable = $this->functions[$name]->getCallable();
|
||||
|
||||
return $callable(...$args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user