mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-06 07:49:53 +00:00
75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?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 Chill\MainBundle\Entity\NewsItem;
|
|
use Chill\MainBundle\Repository\NewsItemRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|
use Symfony\Component\Clock\ClockInterface;
|
|
|
|
/**
|
|
* @internal
|
|
*
|
|
* @coversNothing
|
|
*/
|
|
class NewsItemRepositoryTest extends KernelTestCase
|
|
{
|
|
private function getNewsItemsRepository(): NewsItemRepository
|
|
{
|
|
return self::$container->get(NewsItemRepository::class);
|
|
}
|
|
|
|
public function testFindCurrentNews()
|
|
{
|
|
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->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($mockClock->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);
|
|
|
|
$em->persist($newsItem1);
|
|
$em->persist($newsItem2);
|
|
$em->persist($newsItem3);
|
|
$em->flush();
|
|
|
|
// Call the method to test
|
|
$result = $repository->findCurrentNews();
|
|
|
|
// Assertions
|
|
$this->assertCount(2, $result);
|
|
$this->assertInstanceOf(NewsItem::class, $result[0]);
|
|
$this->assertContains($newsItem2, $result);
|
|
$this->assertContains($newsItem3, $result);
|
|
}
|
|
}
|