diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php new file mode 100644 index 000000000..55c06d18a --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php @@ -0,0 +1,71 @@ +getClientAuthenticated(); + + $client->request('GET', '/api/1.0/main/news/current.json'); + $this->assertResponseIsSuccessful('Testing whether the GET request to the news item Api endpoint was successful'); + + $responseContent = json_decode($client->getResponse()->getContent(), true); + if (!empty($responseContent['data'][0])) { + $this->assertArrayHasKey('title', $responseContent['data'][0]); + } + }*/ + + public function testListCurrentNewsItems() + { + // Mock dependencies + $newsItemRepository = $this->createMock(NewsItemRepository::class); + $serializer = $this->createMock(SerializerInterface::class); + $paginatorFactory = $this->createMock(PaginatorFactory::class); + + $mockNewsItem = $this->createMock(NewsItem::class); + $mockNewsItem->method('getTitle')->willReturn('Mock News Title'); + $mockNewsItem->method('getContent')->willReturn('Mock News Content'); + + $newsItemRepository->expects($this->once()) + ->method('countCurrentNews') + ->willReturn(1); + + $newsItemRepository->expects($this->once()) + ->method('findCurrentNews') + ->willReturn([$mockNewsItem]); + + $serializer->expects($this->once()) + ->method('serialize') + ->willReturn('{"data":[{"title":"Mock News Title"}]}'); + + $paginatorFactory->expects($this->once()) + ->method('create') + ->willReturn(/* Your Paginator object here */); + + $controller = new NewsItemApiController($newsItemRepository, $serializer, $paginatorFactory); + + $response = $controller->listCurrentNewsItems(); + $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); + + $responseContent = json_decode($response->getContent(), true); + if (!empty($responseContent['data'][0])) { + $this->assertEquals('Mock News Title', $responseContent['data'][0]['title']); + } + } + +} diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemControllerTest.php new file mode 100644 index 000000000..aa6aa821d --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemControllerTest.php @@ -0,0 +1,68 @@ +get(EntityManagerInterface::class); + + $qb = $em->createQueryBuilder(); + $newsItems = $qb->select('n')->from(NewsItem::class, 'n') + ->setMaxResults(2) + ->getQuery() + ->getResult(); + + foreach ($newsItems as $n) { + yield [$n->getId()]; + } + + self::ensureKernelShutdown(); + } + + public function testList() + { + $client = $this->getClientAuthenticated(); + $client->request('GET', "/fr/admin/news_item"); + + self::assertResponseIsSuccessful('Test that news item admin page shows'); + } + + /** + * @dataProvider generateNewsItemIds + */ + public function testShowSingleItem(int $newsItemId) + { + $client = $this->getClientAuthenticated(); + $client->request('GET', "/fr/amdin/news_item/{$newsItemId}/view"); + + $this->assertResponseIsSuccessful('test that single news item admin page loads successfully'); + } +} diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemsHistoryControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemsHistoryControllerTest.php new file mode 100644 index 000000000..77c471380 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemsHistoryControllerTest.php @@ -0,0 +1,58 @@ +get(EntityManagerInterface::class); + + $qb = $em->createQueryBuilder(); + $newsItems = $qb->select('n')->from(NewsItem::class, 'n') + ->setMaxResults(2) + ->getQuery() + ->getResult(); + + foreach ($newsItems as $n) { + yield [$n->getId()]; + } + + self::ensureKernelShutdown(); + } + + public function testList() + { + $client = $this->getClientAuthenticated(); + + $client->request('GET', "/fr/news-items/history"); + + self::assertResponseIsSuccessful('Test that /fr/news-items history shows'); + } + + /** + * @dataProvider generateNewsItemIds + */ + public function testShowSingleItem(int $newsItemId) + { + $client = $this->getClientAuthenticated(); + + $client->request('GET', "/fr/news-items/{$newsItemId}"); + + $this->assertResponseIsSuccessful('test that single news item page loads successfully'); + } + +} diff --git a/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemControllerTest.php deleted file mode 100644 index bbbd354e6..000000000 --- a/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemControllerTest.php +++ /dev/null @@ -1,44 +0,0 @@ -getClientAuthenticated('admin', 'admin'); - - $crawler = $client->request('GET', '/fr/admin/news_item'); - self::assertResponseIsSuccessful('Testing /fr/admin/news_item'); - - $btnEdit = $crawler->filter('.btn-edit')?->first(); - - self::assertNotNull($btnEdit, 'check that there is at least one btn-edit on news item page'); - - $client->click($btnEdit->link()); - - self::assertResponseIsSuccessful(); - } -} diff --git a/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemRepositoryTest.php b/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemRepositoryTest.php deleted file mode 100644 index e831e59ad..000000000 --- a/src/Bundle/ChillMainBundle/Tests/Dashboard/NewsItemRepositoryTest.php +++ /dev/null @@ -1,59 +0,0 @@ -newsItemRepository = self::$container->get(NewsItemRepository::class); - } - - /** - * @doesNotPerformAssertions - */ - public function testFindWithDateFilter(): void - { - $clock = new MockClock('2023-11-20'); - - $newsItemA = new NewsItem(); - $newsItemA->setTitle('With end date article'); - $newsItemA->setContent('blabla'); - $newsItemA->setStartDate(new \DateTime('2023-11-01')); - $newsItemA->setEndDate(new \DateTime('2023-11-15')); - - $newsItemB = new NewsItem(); - $newsItemB->setTitle('No end date article'); - $newsItemB->setContent('with blopblop'); - $newsItemB->setStartDate(new \DateTime('2023-11-02')); - - $newsItemC = new NewsItem(); - $newsItemC->setTitle('Null as end date article'); - $newsItemC->setContent('tralala'); - $newsItemC->setStartDate(new \DateTime('2023-11-03')); - $newsItemC->setEndDate(null); - } -} diff --git a/src/Bundle/ChillMainBundle/Tests/Repository/NewsItemRepositoryTest.php b/src/Bundle/ChillMainBundle/Tests/Repository/NewsItemRepositoryTest.php new file mode 100644 index 000000000..f9aee89a9 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Repository/NewsItemRepositoryTest.php @@ -0,0 +1,71 @@ +entityManager = $this->createMock(EntityManagerInterface::class); + $this->clock = $this->createMock(ClockInterface::class); + + $this->repository = new NewsItemRepository( + $this->entityManager, + $this->clock + ); + } + + public function testFindCurrentNews() + { + $this->clock->expects($this->once())->method('now')->willReturn(new \DateTime('2023-01-10')); + + $newsItem1 = new NewsItem(); + $newsItem1->setTitle('This is a mock news item'); + $newsItem1->setContent('We are testing that the repository returns the correct news items'); + $newsItem1->setStartDate(new \DateTimeImmutable('2023-01-01')); + $newsItem1->setEndDate(new \DateTimeImmutable('2023-01-05')); + + $newsItem2 = new NewsItem(); + $newsItem2->setTitle('This is a mock news item'); + $newsItem2->setContent('We are testing that the repository returns the correct news items'); + $newsItem2->setStartDate(new \DateTimeImmutable('2023-12-15')); + $newsItem2->setEndDate($this->clock->now()); + + $newsItem3 = new NewsItem(); + $newsItem3->setTitle('This is a mock news item'); + $newsItem3->setContent('We are testing that the repository returns the correct news items'); + $newsItem3->setStartDate(new \DateTimeImmutable('2033-11-03')); + $newsItem3->setEndDate(null); + + // Call the method to test + $result = $this->repository->findCurrentNews(); + + // Assertions + $this->assertCount(2, $result); + $this->assertInstanceOf(NewsItem::class, $result[0]); + $this->assertContains($newsItem2, $result); + $this->assertContains($newsItem3, $result); + + } +}