From cb1ab0ed181e528bdc6726a92b5ea78afdb54ef4 Mon Sep 17 00:00:00 2001 From: lubiana Date: Thu, 19 Dec 2024 00:35:23 +0100 Subject: [PATCH] improve unit tests --- tests/Controller/FoodOrderControllerTest.php | 39 ++++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/tests/Controller/FoodOrderControllerTest.php b/tests/Controller/FoodOrderControllerTest.php index a7c222e..1e46b47 100644 --- a/tests/Controller/FoodOrderControllerTest.php +++ b/tests/Controller/FoodOrderControllerTest.php @@ -137,10 +137,10 @@ final class FoodOrderControllerTest extends DbWebTest } /** - * @testWith [1, 0, 1] - * [2, 1, 1] - * [3, 1, 1] - * [4, 1, 0, 5] + * @testWith [1, 0, 2] + * [2, 1, 3] + * [3, 2, 4] + * [4, 3, 0, 5] */ public function testPaginatedFirstPage(int $page, int $prevPage, int $nextPage, int $items = 10): void { @@ -154,22 +154,27 @@ final class FoodOrderControllerTest extends DbWebTest $items, 'nobody' ); - $this->assertElementContainsCount( - $crawler, - 'a', - $nextPage, - 'next page' - ); - $this->assertElementContainsCount( - $crawler, - 'a', - $prevPage, - 'previous page' - ); + if ($prevPage > 0) { + if ($prevPage === 1) { + $prevPage = ''; + } else { + $prevPage = "/{$prevPage}"; + } + + $node = $crawler->filter('a')->reduce(fn(Crawler $node, $i): bool => $node->text() === 'previous page')->first(); + $target = $node->attr('href'); + $this->assertTrue(str_ends_with($target, $prevPage)); + } + if ($prevPage > 3) { + $node = $crawler->filter('a')->reduce(fn(Crawler $node, $i): bool => $node->text() === 'next page')->first(); + $target = $node->attr('href'); + $this->assertTrue(str_ends_with($target, "/{$nextPage}")); + } } public function testNew(): void { + $this->client->getCookieJar()->set(new \Symfony\Component\BrowserKit\Cookie('username', 'Testing-1')); $this->client->request('GET', sprintf('%snew', $this->path)); self::assertResponseStatusCodeSame(200); @@ -180,6 +185,8 @@ final class FoodOrderControllerTest extends DbWebTest self::assertResponseRedirects("{$this->path}list"); self::assertSame(1, $this->repository->count([])); + $order = $this->repository->findOneBy(['createdBy' => 'Testing-1']); + assert($order instanceof FoodOrder); } public function testOpen(): void