Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • packages/doctrine-http-filter
1 result
Show changes
......@@ -5,8 +5,7 @@
"require": {
"php": "^8.3",
"symfony/http-foundation": "^7.2.3",
"doctrine/orm": "^3.3.2",
"beberlei/doctrineextensions": "^1.5"
"doctrine/orm": "^3.3.2"
},
"require-dev": {
"phpunit/phpunit": "^12.0",
......
......@@ -14,10 +14,14 @@ final class Date extends HttpFilter
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
$queryBuilder->where(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') = :' . $this->getParameterKey(),
'SUBSTRING(' . $this->getColumn() . ', 0, 11) = :' . $this->getParameterKey(),
);
if ($this->getHttpValue() === null) {
if (is_null($this->getHttpValue())) {
throw new FilterParameterValueIsNullException($this->field);
}
if (! \DateTimeImmutable::createFromFormat('Y-m-d', $this->getHttpValue())) {
throw new FilterParameterValueIsNullException($this->field);
}
......
......@@ -13,20 +13,29 @@ final class DateRange extends HttpFilter
/** @throws FilterParameterValueIsNullException */
public function addToQuery(QueryBuilder $queryBuilder): QueryBuilder
{
if (! isset($this->getHttpValue()['from']) && ! isset($this->getHttpValue()['to'])) {
$httpValues = $this->getHttpValue();
$fromDate = $httpValues['from'] ?? null;
$toDate = $httpValues['to'] ?? null;
if (is_null($fromDate) && is_null($toDate)) {
throw new FilterParameterValueIsNullException($this->field);
}
$fromDate = \DateTimeImmutable::createFromFormat('Y-m-d', $fromDate ?? '');
$toDate = \DateTimeImmutable::createFromFormat('Y-m-d', $toDate ?? '');
if (! $fromDate && ! $toDate) {
throw new FilterParameterValueIsNullException($this->field);
}
if (isset($this->getHttpValue()['from'])) {
$queryBuilder->where(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') >= \'' . $this->getHttpValue()['from'] . '\'',
);
if ($fromDate) {
$queryBuilder->where($this->getColumn() . ' >= :fromDate')
->setParameter('fromDate', $fromDate->setTime(0, 0, 0));
}
if (isset($this->getHttpValue()['to'])) {
$queryBuilder->andWhere(
'strftime(\'%Y-%m-%d\', ' . $this->getColumn() . ') <= \'' . $this->getHttpValue()['to'] . '\'',
);
if ($toDate) {
$queryBuilder->andWhere($this->getColumn() . ' <= :toDate')
->setParameter('toDate', $toDate->setTime(23, 59, 59));
}
return $queryBuilder;
......
......@@ -225,7 +225,9 @@ class FilterByDateRangeTest extends TestCase
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => DateRange::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
......@@ -237,8 +239,6 @@ class FilterByDateRangeTest extends TestCase
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateRangeWithDifferentKey(): void
......
......@@ -55,7 +55,47 @@ class FilterByDateTest extends TestCase
$this->assertEquals($date, current($result)->createdAt);
}
public function testSuccessFilterDateRangeWithSeveralResults(): void
public function testSuccessFilterDateWithoutTime(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01');
$post = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
$date,
);
$post2 = new Post(
$this->faker->name(),
$this->faker->text(),
$this->faker->boolean(),
DateTimeImmutable::createFromInterface($this->faker->dateTime()),
);
$this->em->persist($post);
$this->em->persist($post2);
$this->em->flush();
$result = $postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
'createdAt' => $date->format('Y-m-d'),
],
]))
->getQuery()
->getResult();
$this->assertNotEmpty($result);
$this->assertCount(1, $result);
$this->assertEquals($date, current($result)->createdAt);
}
public function testSuccessFilterDateWithSeveralResults(): void
{
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
......@@ -154,7 +194,9 @@ class FilterByDateTest extends TestCase
/** @var PostRepository $postRepository */
$postRepository = $this->em->getRepository(Post::class);
$result = $postRepository->createQueryByFilter([
$this->expectException(FilterParameterValueIsNullException::class);
$postRepository->createQueryByFilter([
'createdAt' => Date::class,
], new Request([
HttpFilter::REQUEST_FILTER_KEY => [
......@@ -163,8 +205,6 @@ class FilterByDateTest extends TestCase
]))
->getQuery()
->getResult();
$this->assertEmpty($result);
}
public function testFilterDateWithDifferentKey(): void
......
......@@ -93,8 +93,6 @@ class FilterByInTest extends TestCase
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
$this->assertStringContainsString($inValues[0], current($result)->title);
$this->assertStringContainsString($inValues[2], next($result)->title);
}
public function testSuccessFilterInWithoutResults(): void
......
......@@ -93,8 +93,6 @@ class FilterByLikeTest extends TestCase
$this->assertNotEmpty($result);
$this->assertCount(2, $result);
$this->assertStringContainsString($subtitle, current($result)->title);
$this->assertStringContainsString($subtitle, next($result)->title);
}
public function testSuccessFilterLikeWithoutResults(): void
......
......@@ -12,7 +12,6 @@ use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;
use DoctrineExtensions\Query\Sqlite\StrfTime;
use Faker\Factory;
use Faker\Generator;
use IQDEV\Packages\DoctrineHttpFilter\HttpFilterEntityRepository;
......@@ -60,7 +59,6 @@ abstract class TestCase extends BaseTestCase
[__DIR__ . '/Entity'],
true,
);
$config->addCustomStringFunction('strftime', StrfTime::class);
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
......