Merge remote-tracking branch 'origin/master' into upgrade-sf5

This commit is contained in:
2024-04-04 18:45:01 +02:00
162 changed files with 3849 additions and 713 deletions

View File

@@ -0,0 +1,39 @@
<?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\Test\PrepareClientTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
*
* @coversNothing
*/
class NewsItemApiControllerTest extends WebTestCase
{
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, 512, JSON_THROW_ON_ERROR);
if (!empty($responseContent['data'][0])) {
$this->assertArrayHasKey('title', $responseContent['data'][0]);
}
}
}

View File

@@ -0,0 +1,96 @@
<?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.
*
* @internal
*
* @coversNothing
*/
class NewsItemControllerTest extends WebTestCase
{
use PrepareClientTrait;
/**
* @var list<array{0: class-string, 1: int}>
*/
private static array $entitiesToDelete = [];
private readonly EntityManagerInterface $em;
protected function tearDown(): void
{
self::ensureKernelShutdown();
}
public static function tearDownAfterClass(): void
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
foreach (self::$entitiesToDelete as [$class, $id]) {
$entity = $em->find($class, $id);
if (null !== $entity) {
$em->remove($entity);
}
}
$em->flush();
}
public static function generateNewsItemIds(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$newsItem = new NewsItem();
$newsItem->setTitle('Lorem Ipsum');
$newsItem->setContent('some text');
$newsItem->setStartDate(new \DateTimeImmutable('now'));
$em->persist($newsItem);
$em->flush();
self::$entitiesToDelete[] = [NewsItem::class, $newsItem];
self::ensureKernelShutdown();
yield [$newsItem];
}
public function testList()
{
$client = $this->getClientAuthenticated('admin', 'password');
$client->request('GET', '/fr/admin/news_item');
self::assertResponseIsSuccessful('News item admin page shows');
}
/**
* @dataProvider generateNewsItemIds
*/
public function testShowSingleItem(NewsItem $newsItem)
{
$client = $this->getClientAuthenticated('admin', 'password');
$client->request('GET', "/fr/admin/news_item/{$newsItem->getId()}/view");
self::assertResponseIsSuccessful('Single news item admin page loads successfully');
}
}

View File

@@ -0,0 +1,97 @@
<?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;
/**
* @internal
*
* @coversNothing
*/
class NewsItemsHistoryControllerTest extends WebTestCase
{
use PrepareClientTrait;
/**
* @var list<array{0: class-string, 1: NewsItem}>
*/
private static array $toDelete = [];
public static function tearDownAfterClass(): void
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
foreach (static::$toDelete as [$class, $entity]) {
$query = $em->createQuery(sprintf('DELETE FROM %s e WHERE e.id = :id', $class))
->setParameter('id', $entity->getId());
$query->execute();
}
static::$toDelete = [];
self::ensureKernelShutdown();
}
protected function tearDown(): void
{
self::ensureKernelShutdown();
}
public static function generateNewsItemIds(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$news = new NewsItem();
$news->setContent('test content');
$news->setTitle('Title');
$news->setStartDate(new \DateTimeImmutable('yesterday'));
$em->persist($news);
$em->flush();
static::$toDelete[] = [NewsItem::class, $news];
self::ensureKernelShutdown();
yield [$news->getId()];
}
public function testList()
{
self::ensureKernelShutdown();
$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)
{
self::ensureKernelShutdown();
$client = $this->getClientAuthenticated();
$client->request('GET', "/fr/news-items/{$newsItemId}");
$this->assertResponseIsSuccessful('test that single news item page loads successfully');
}
}

View File

@@ -91,7 +91,7 @@ class JobWithReturn implements CronJobInterface
return 'with-data';
}
public function run(array $lastExecutionData): array|null
public function run(array $lastExecutionData): ?array
{
return ['data' => 'test'];
}

View File

@@ -173,7 +173,7 @@ class JobCanRun implements CronJobInterface
return $this->key;
}
public function run(array $lastExecutionData): array|null
public function run(array $lastExecutionData): ?array
{
return null;
}
@@ -191,7 +191,7 @@ class JobCannotRun implements CronJobInterface
return 'job-b';
}
public function run(array $lastExecutionData): array|null
public function run(array $lastExecutionData): ?array
{
return null;
}

View File

@@ -0,0 +1,108 @@
<?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;
use Symfony\Component\Clock\MockClock;
/**
* @internal
*
* @coversNothing
*/
class NewsItemRepositoryTest extends KernelTestCase
{
private EntityManagerInterface $entityManager;
/**
* @var list<array{0: class-string, 1: NewsItem}>
*/
private array $toDelete = [];
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
protected function tearDown(): void
{
foreach ($this->toDelete as [$class, $entity]) {
$query = $this->entityManager->createQuery(sprintf('DELETE FROM %s e WHERE e.id = :id', $class))
->setParameter('id', $entity->getId());
$query->execute();
}
$this->toDelete = [];
}
private function getNewsItemsRepository(ClockInterface $clock): NewsItemRepository
{
return new NewsItemRepository($this->entityManager, $clock);
}
public function testFindCurrentNews()
{
$clock = new MockClock($now = new \DateTimeImmutable('2023-01-10'));
$repository = $this->getNewsItemsRepository($clock);
$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-01-01'));
$newsItem2->setEndDate($now->add(new \DateInterval('P1D')));
$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);
$newsItem4 = new NewsItem();
$newsItem4->setTitle('This is a mock news item');
$newsItem4->setContent('We are testing that the repository returns the correct news items');
$newsItem4->setStartDate(new \DateTimeImmutable('2023-01-03'));
$newsItem4->setEndDate(null);
$this->entityManager->persist($newsItem1);
$this->entityManager->persist($newsItem2);
$this->entityManager->persist($newsItem3);
$this->entityManager->persist($newsItem4);
$this->entityManager->flush();
$this->toDelete = [
[NewsItem::class, $newsItem1],
[NewsItem::class, $newsItem2],
[NewsItem::class, $newsItem3],
[NewsItem::class, $newsItem4],
];
// 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($newsItem4, $result);
}
}

View File

@@ -117,7 +117,7 @@ final class UserNormalizerTest extends TestCase
*
* @throws ExceptionInterface
*/
public function testNormalize(User|null $user, mixed $format, mixed $context, mixed $expected)
public function testNormalize(?User $user, mixed $format, mixed $context, mixed $expected)
{
$userRender = $this->prophesize(UserRender::class);
$userRender->renderString(Argument::type(User::class), Argument::type('array'))->willReturn($user ? $user->getLabel() : '');