mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-05 15:29:50 +00:00
Change test into an integration test rather than unit test : extend KernelTestCase and get real services from the container.
This commit is contained in:
parent
726f71c8f1
commit
49dbd09167
@ -11,34 +11,28 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Repository;
|
namespace Repository;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
use Chill\MainBundle\Entity\NewsItem;
|
use Chill\MainBundle\Entity\NewsItem;
|
||||||
use Chill\MainBundle\Repository\NewsItemRepository;
|
use Chill\MainBundle\Repository\NewsItemRepository;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\ORM\EntityRepository;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
|
||||||
use Symfony\Component\Clock\ClockInterface;
|
use Symfony\Component\Clock\ClockInterface;
|
||||||
|
|
||||||
class NewsItemRepositoryTest extends TestCase
|
class NewsItemRepositoryTest extends KernelTestCase
|
||||||
{
|
{
|
||||||
private $entityManager;
|
private function getNewsItemsRepository(): NewsItemRepository
|
||||||
private $clock;
|
|
||||||
private $repository;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
|
||||||
{
|
{
|
||||||
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
return self::$container->get(NewsItemRepository::class);
|
||||||
$this->clock = $this->createMock(ClockInterface::class);
|
|
||||||
|
|
||||||
$this->repository = new NewsItemRepository(
|
|
||||||
$this->entityManager,
|
|
||||||
$this->clock
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFindCurrentNews()
|
public function testFindCurrentNews()
|
||||||
{
|
{
|
||||||
$this->clock->expects($this->once())->method('now')->willReturn(new \DateTime('2023-01-10'));
|
self::bootKernel();
|
||||||
|
$em = self::$container->get(EntityManagerInterface::class);
|
||||||
|
$repository = $this->getNewsItemsRepository();
|
||||||
|
|
||||||
|
$mockClock = $this->createMock(ClockInterface::class);
|
||||||
|
|
||||||
|
$mockClock->expects($this->once())->method('now')->willReturn(new \DateTime('2023-01-10'));
|
||||||
|
|
||||||
$newsItem1 = new NewsItem();
|
$newsItem1 = new NewsItem();
|
||||||
$newsItem1->setTitle('This is a mock news item');
|
$newsItem1->setTitle('This is a mock news item');
|
||||||
@ -50,7 +44,7 @@ class NewsItemRepositoryTest extends TestCase
|
|||||||
$newsItem2->setTitle('This is a mock news item');
|
$newsItem2->setTitle('This is a mock news item');
|
||||||
$newsItem2->setContent('We are testing that the repository returns the correct news items');
|
$newsItem2->setContent('We are testing that the repository returns the correct news items');
|
||||||
$newsItem2->setStartDate(new \DateTimeImmutable('2023-12-15'));
|
$newsItem2->setStartDate(new \DateTimeImmutable('2023-12-15'));
|
||||||
$newsItem2->setEndDate($this->clock->now());
|
$newsItem2->setEndDate($mockClock->now());
|
||||||
|
|
||||||
$newsItem3 = new NewsItem();
|
$newsItem3 = new NewsItem();
|
||||||
$newsItem3->setTitle('This is a mock news item');
|
$newsItem3->setTitle('This is a mock news item');
|
||||||
@ -58,14 +52,18 @@ class NewsItemRepositoryTest extends TestCase
|
|||||||
$newsItem3->setStartDate(new \DateTimeImmutable('2033-11-03'));
|
$newsItem3->setStartDate(new \DateTimeImmutable('2033-11-03'));
|
||||||
$newsItem3->setEndDate(null);
|
$newsItem3->setEndDate(null);
|
||||||
|
|
||||||
|
$em->persist($newsItem1);
|
||||||
|
$em->persist($newsItem2);
|
||||||
|
$em->persist($newsItem3);
|
||||||
|
$em->flush();
|
||||||
|
|
||||||
// Call the method to test
|
// Call the method to test
|
||||||
$result = $this->repository->findCurrentNews();
|
$result = $repository->findCurrentNews();
|
||||||
|
|
||||||
// Assertions
|
// Assertions
|
||||||
$this->assertCount(2, $result);
|
$this->assertCount(2, $result);
|
||||||
$this->assertInstanceOf(NewsItem::class, $result[0]);
|
$this->assertInstanceOf(NewsItem::class, $result[0]);
|
||||||
$this->assertContains($newsItem2, $result);
|
$this->assertContains($newsItem2, $result);
|
||||||
$this->assertContains($newsItem3, $result);
|
$this->assertContains($newsItem3, $result);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user