mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
DX: Add a DQL AGE
function to calculate age between two dates
This commit is contained in:
79
src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/AgeTest.php
Normal file
79
src/Bundle/ChillMainBundle/Tests/Doctrine/DQL/AgeTest.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?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 Chill\MainBundle\Tests\Doctrine\DQL;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class AgeTest extends KernelTestCase
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->entityManager = self::$container->get(EntityManagerInterface::class);
|
||||
}
|
||||
|
||||
public function generateQueries(): iterable
|
||||
{
|
||||
yield [
|
||||
'SELECT AGE(a.validFrom, a.validTo) FROM ' . Address::class . ' a',
|
||||
[],
|
||||
];
|
||||
|
||||
yield [
|
||||
'SELECT AGE(:date0, :date1) FROM ' . Address::class . ' a',
|
||||
[
|
||||
'date0' => new DateTimeImmutable('now'),
|
||||
'date1' => new DateTimeImmutable('2020-01-01'),
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
'SELECT AGE(a.validFrom, :date1) FROM ' . Address::class . ' a',
|
||||
[
|
||||
'date1' => new DateTimeImmutable('now'),
|
||||
],
|
||||
];
|
||||
|
||||
yield [
|
||||
'SELECT AGE(:date0, a.validFrom) FROM ' . Address::class . ' a',
|
||||
[
|
||||
'date0' => new DateTimeImmutable('now'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider generateQueries
|
||||
*/
|
||||
public function testWorking(string $dql, array $args)
|
||||
{
|
||||
$dql = $this->entityManager->createQuery($dql)->setMaxResults(3);
|
||||
|
||||
foreach ($args as $key => $value) {
|
||||
$dql->setParameter($key, $value);
|
||||
}
|
||||
|
||||
$results = $dql->getResult();
|
||||
|
||||
$this->assertIsArray($results);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user