work on tests

This commit is contained in:
2023-12-11 17:32:21 +01:00
parent a97a22d464
commit 2402050f5f
6 changed files with 268 additions and 103 deletions

View File

@@ -0,0 +1,71 @@
<?php
namespace Controller;
use Chill\MainBundle\Controller\NewsItemApiController;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\NewsItemRepository;
use Chill\MainBundle\Test\PrepareClientTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
class NewsItemApiControllerTest extends TestCase
{
use PrepareClientTrait;
/* public function testListCurrentNewsItems()
{
$client = $this->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']);
}
}
}