Correct the NewsItemsApiController test : remove commented code and extend properly

This commit is contained in:
Julie Lenaerts 2023-12-18 15:34:43 +01:00
parent f03ae2cabc
commit 726f71c8f1

View File

@ -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']);
}
}
}