work on tests

This commit is contained in:
Julie Lenaerts 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']);
}
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Controller;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Tests the admin pages for news items
*
*/
class NewsItemControllerTest extends WebTestCase
{
use PrepareClientTrait;
protected function tearDown(): void
{
self::ensureKernelShutdown();
}
public function generateNewsItemIds(): iterable
{
self::bootKernel();
$em = self::$container->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');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Controller;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class NewsItemsHistoryControllerTest extends WebTestCase
{
use PrepareClientTrait;
protected function tearDown(): void
{
self::ensureKernelShutdown();
}
public function generateNewsItemIds(): iterable
{
self::bootKernel();
$em = self::$container->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');
}
}

View File

@ -1,44 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Dashboard;
use Chill\MainBundle\Test\PrepareClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
*
* @coversNothing
*/
class NewsItemControllerTest extends WebTestCase
{
use PrepareClientTrait;
/**
* Test to ensure that the news item admin index page works.
*/
public function newsItemAdminSmokeTest(): void
{
$client = $this->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();
}
}

View File

@ -1,59 +0,0 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Dashboard;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Repository\NewsItemRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
/**
* @internal
*
* @coversNothing
*/
class NewsItemRepositoryTest extends KernelTestCase
{
private NewsItemRepository $newsItemRepository;
public function setUp(): void
{
parent::setUp();
self::bootKernel();
$this->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);
}
}

View File

@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Repository;
use PHPUnit\Framework\TestCase;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Repository\NewsItemRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Clock\ClockInterface;
class NewsItemRepositoryTest extends TestCase
{
private $entityManager;
private $clock;
private $repository;
protected function setUp(): void
{
$this->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);
}
}