repository = $this->createMock(PostRepositoryInterface::class); $this->sanitizer = $this->createMock(HtmlSanitizerInterface::class); $this->service = new PostApplicationService($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('

Brut

')->willReturn('

Sur

'); $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', '

Brut

', 4, 8)); } private function makePost(int $id, string $title, string $slug, string $content = '

Contenu

'): Post { return new Post($id, $title, $content, $slug); } }