Working state
This commit is contained in:
@@ -9,7 +9,6 @@ use PHPUnit\Framework\TestCase;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
|
||||
final class PostExtensionTest extends TestCase
|
||||
{
|
||||
/** @var array<string, TwigFunction> */
|
||||
@@ -25,6 +24,16 @@ final class PostExtensionTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
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');
|
||||
@@ -45,6 +54,13 @@ final class PostExtensionTest extends TestCase
|
||||
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');
|
||||
@@ -63,11 +79,11 @@ final class PostExtensionTest extends TestCase
|
||||
{
|
||||
$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');
|
||||
$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('A', $this->call('post_initials', $emptyLike));
|
||||
self::assertSame('D', $this->call('post_initials', $stopWordsOnly));
|
||||
}
|
||||
|
||||
private function call(string $name, mixed ...$args): mixed
|
||||
|
||||
66
tests/Post/PostServiceCoverageTest.php
Normal file
66
tests/Post/PostServiceCoverageTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Post;
|
||||
|
||||
use App\Post\Post;
|
||||
use App\Post\PostRepositoryInterface;
|
||||
use App\Post\PostService;
|
||||
use App\Shared\Html\HtmlSanitizerInterface;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class PostServiceCoverageTest extends TestCase
|
||||
{
|
||||
/** @var PostRepositoryInterface&MockObject */
|
||||
private PostRepositoryInterface $repository;
|
||||
|
||||
/** @var HtmlSanitizerInterface&MockObject */
|
||||
private HtmlSanitizerInterface $sanitizer;
|
||||
|
||||
private PostService $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->repository = $this->createMock(PostRepositoryInterface::class);
|
||||
$this->sanitizer = $this->createMock(HtmlSanitizerInterface::class);
|
||||
$this->service = new PostService($this->repository, $this->sanitizer);
|
||||
}
|
||||
|
||||
public function testGetAllPostsPassesCategoryIdToRepository(): void
|
||||
{
|
||||
$posts = [$this->makePost(1, 'Titre', 'titre')];
|
||||
$this->repository->expects($this->once())->method('findAll')->with(9)->willReturn($posts);
|
||||
|
||||
self::assertSame($posts, $this->service->getAllPosts(9));
|
||||
}
|
||||
|
||||
public function testGetRecentPostsPassesLimitToRepository(): void
|
||||
{
|
||||
$posts = [$this->makePost(2, 'Titre', 'titre-2')];
|
||||
$this->repository->expects($this->once())->method('findRecent')->with(7)->willReturn($posts);
|
||||
|
||||
self::assertSame($posts, $this->service->getRecentPosts(7));
|
||||
}
|
||||
|
||||
public function testCreatePostAddsNumericSuffixWhenBaseSlugAlreadyExists(): void
|
||||
{
|
||||
$this->sanitizer->expects($this->once())->method('sanitize')->with('<p>Brut</p>')->willReturn('<p>Sur</p>');
|
||||
$this->repository->expects($this->exactly(2))
|
||||
->method('slugExists')
|
||||
->withAnyParameters()
|
||||
->willReturnOnConsecutiveCalls(true, false);
|
||||
$this->repository->expects($this->once())
|
||||
->method('create')
|
||||
->with($this->isInstanceOf(Post::class), 'mon-titre-1', 4, 8)
|
||||
->willReturn(99);
|
||||
|
||||
self::assertSame(99, $this->service->createPost('Mon titre', '<p>Brut</p>', 4, 8));
|
||||
}
|
||||
|
||||
private function makePost(int $id, string $title, string $slug, string $content = '<p>Contenu</p>'): Post
|
||||
{
|
||||
return new Post($id, $title, $content, $slug);
|
||||
}
|
||||
}
|
||||
35
tests/Shared/ClientIpResolverCoverageTest.php
Normal file
35
tests/Shared/ClientIpResolverCoverageTest.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Http\ClientIpResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Slim\Psr7\Factory\ServerRequestFactory;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class ClientIpResolverCoverageTest extends TestCase
|
||||
{
|
||||
public function testResolveReturnsRemoteAddrWhenTrustedProxyHasNoForwardedHeader(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())->createServerRequest('GET', '/', [
|
||||
'REMOTE_ADDR' => '127.0.0.1',
|
||||
]);
|
||||
|
||||
$resolver = new ClientIpResolver(['127.0.0.1']);
|
||||
|
||||
self::assertSame('127.0.0.1', $resolver->resolve($request));
|
||||
}
|
||||
|
||||
public function testResolveTrimsForwardedIpWhenProxyWildcardIsTrusted(): void
|
||||
{
|
||||
$request = (new ServerRequestFactory())->createServerRequest('GET', '/', [
|
||||
'REMOTE_ADDR' => '10.0.0.1',
|
||||
'HTTP_X_FORWARDED_FOR' => ' 203.0.113.77 , 198.51.100.12',
|
||||
]);
|
||||
|
||||
$resolver = new ClientIpResolver(['*']);
|
||||
|
||||
self::assertSame('203.0.113.77', $resolver->resolve($request));
|
||||
}
|
||||
}
|
||||
26
tests/Shared/FlashServiceCoverageTest.php
Normal file
26
tests/Shared/FlashServiceCoverageTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Http\FlashService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class FlashServiceCoverageTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
$_SESSION = [];
|
||||
}
|
||||
|
||||
public function testGetCastsFalseToEmptyString(): void
|
||||
{
|
||||
$_SESSION['flash']['flag'] = false;
|
||||
|
||||
$flash = new FlashService();
|
||||
|
||||
self::assertSame('', $flash->get('flag'));
|
||||
self::assertArrayNotHasKey('flag', $_SESSION['flash']);
|
||||
}
|
||||
}
|
||||
32
tests/Shared/SessionManagerCoverageTest.php
Normal file
32
tests/Shared/SessionManagerCoverageTest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Shared;
|
||||
|
||||
use App\Shared\Http\SessionManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
|
||||
final class SessionManagerCoverageTest extends TestCase
|
||||
{
|
||||
private SessionManager $manager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$_SESSION = [];
|
||||
$this->manager = new SessionManager();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$_SESSION = [];
|
||||
}
|
||||
|
||||
public function testGetUserIdCastsNumericStringToInteger(): void
|
||||
{
|
||||
$_SESSION['user_id'] = '42';
|
||||
|
||||
self::assertSame(42, $this->manager->getUserId());
|
||||
self::assertTrue($this->manager->isAuthenticated());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user