Skip to content

Commit 436ceff

Browse files
committed
Fix DateFilter for datetime_immutable
1 parent 2d3f143 commit 436ceff

File tree

7 files changed

+96
-6
lines changed

7 files changed

+96
-6
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDate;
2424
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
2525
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyGroup;
26+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyImmutableDate;
2627
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
2728
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyProduct;
2829
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyProperty;
@@ -773,4 +774,20 @@ public function thereIsDummyDateObjectsWithDummyDate(int $nb)
773774

774775
$this->manager->flush();
775776
}
777+
778+
/**
779+
* @Given there are :nb dummyimmutabledate objects with dummyDate
780+
*/
781+
public function thereIsDummyImmutableDateObjectsWithDummyDate(int $nb)
782+
{
783+
for ($i = 1; $i <= $nb; ++$i) {
784+
$date = new \DateTimeImmutable(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
785+
$dummy = new DummyImmutableDate();
786+
$dummy->dummyDate = $date;
787+
788+
$this->manager->persist($dummy);
789+
}
790+
791+
$this->manager->flush();
792+
}
776793
}

features/doctrine/date_filter.feature

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,16 @@ Feature: Date filter on collections
660660
Given there are 30 dummydate objects with dummyDate
661661
When I send a "GET" request to "/dummy_dates?dummyDate[after]=2015-04-28"
662662
Then the response status code should be 200
663-
And the response should be in JSON
663+
And the JSON node "hydra:totalItems" should be equal to 3
664+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
665+
666+
@dropSchema
667+
@createSchema
668+
Scenario: Get collection filtered by date that is an immutable date variant
669+
Given there are 30 dummyimmutabledate objects with dummyDate
670+
When I send a "GET" request to "/dummy_immutable_dates?dummyDate[after]=2015-04-28"
671+
Then the response status code should be 200
672+
And the JSON node "hydra:totalItems" should be equal to 3
664673
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
665674

666675
@dropSchema

src/Bridge/Doctrine/Orm/Filter/DateFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
167167
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value, string $nullManagement = null, $type = null)
168168
{
169169
try {
170-
$value = new \DateTime($value);
170+
$value = false !== strpos('_immutable', $type) ? new \DateTime($value) : new \DateTimeImmutable($value);
171171
} catch (\Exception $e) {
172172
// Silently ignore this filter if it can not be transformed to a \DateTime
173173
$this->logger->notice('Invalid filter ignored', [

tests/Fixtures/TestBundle/Entity/DummyDate.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
*
2222
* @author Antoine Bluchet <[email protected]>
2323
*
24-
* @ApiResource
24+
*
2525
* @ApiResource(attributes={
26-
* "filters"={
27-
* "my_dummy_date.date",
28-
* }
26+
* "filters"={"my_dummy_date.date"}
2927
* })
3028
* @ORM\Entity
3129
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* Dummy Immutable Date.
21+
*
22+
* @ApiResource(attributes={
23+
* "filters"={"my_dummy_immutable_date.date"}
24+
* })
25+
* @ORM\Entity
26+
*/
27+
class DummyImmutableDate
28+
{
29+
/**
30+
* @var int The id
31+
*
32+
* @ORM\Column(type="integer")
33+
* @ORM\Id
34+
* @ORM\GeneratedValue(strategy="AUTO")
35+
*/
36+
private $id;
37+
38+
/**
39+
* @var \DateTimeImmutable The dummy date
40+
*
41+
* @ORM\Column(type="date_immutable")
42+
*/
43+
public $dummyDate;
44+
45+
/**
46+
* Get id.
47+
*
48+
* @return int
49+
*/
50+
public function getId()
51+
{
52+
return $this->id;
53+
}
54+
}

tests/Fixtures/app/bootstrap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
declare(strict_types=1);
1313

1414
use Doctrine\Common\Annotations\AnnotationRegistry;
15+
use Doctrine\DBAL\Types\DateType;
16+
use Doctrine\DBAL\Types\Type;
1517

1618
date_default_timezone_set('UTC');
1719

@@ -20,4 +22,9 @@
2022

2123
AnnotationRegistry::registerLoader('class_exists');
2224

25+
if (!array_key_exists('date_immutable', Type::getTypesMap())) {
26+
// Hack to avoid Unknown column type "date_immutable" requested with doctrine < 2.6 when loading DummyImmutableDate
27+
Type::addType('date_immutable', DateType::class);
28+
}
29+
2330
return $loader;

tests/Fixtures/app/config/config_test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ services:
119119
arguments: [ { 'dummyDate': ~ } ]
120120
tags: [ { name: 'api_platform.filter', id: 'my_dummy_date.date' } ]
121121

122+
my_dummy_immutable_date.date:
123+
parent: 'api_platform.doctrine.orm.date_filter'
124+
arguments: [ { 'dummyDate': ~ } ]
125+
tags: [ { name: 'api_platform.filter' } ]
126+
122127
app.my_dummy_resource.range_filter:
123128
parent: 'api_platform.doctrine.orm.range_filter'
124129
arguments: [ { 'dummyFloat': ~, 'dummyPrice': ~ } ]

0 commit comments

Comments
 (0)