From 726f71c8f13865df8ca24c5f9bf386a45fd03f3a Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Mon, 18 Dec 2023 15:34:43 +0100 Subject: [PATCH] Correct the NewsItemsApiController test : remove commented code and extend properly --- .../Controller/NewsItemApiControllerTest.php | 44 ++----------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php index 55c06d18a..5616cf311 100644 --- a/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Controller/NewsItemApiControllerTest.php @@ -8,16 +8,17 @@ use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\NewsItemRepository; use Chill\MainBundle\Test\PrepareClientTrait; use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Serializer\SerializerInterface; -class NewsItemApiControllerTest extends TestCase +class NewsItemApiControllerTest extends WebTestCase { use PrepareClientTrait; -/* public function testListCurrentNewsItems() + public function testListCurrentNewsItems() { $client = $this->getClientAuthenticated(); @@ -25,47 +26,10 @@ class NewsItemApiControllerTest extends TestCase $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']); - } } }