Skip to content

Commit

Permalink
Add mongodb date_immutable filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Pascal Devierne committed Jan 4, 2021
1 parent 4f05477 commit ac0befd
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 2 deletions.
1 change: 0 additions & 1 deletion features/doctrine/date_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ Feature: Date filter on collections
And the JSON node "hydra:member[0].dateIncludeNullBeforeAndAfter" should be equal to "2015-04-02T00:00:00+00:00"
And the JSON node "hydra:member[1].dateIncludeNullBeforeAndAfter" should be null

@!mongodb
@createSchema
Scenario: Get collection filtered by date that is an immutable date variant
Given there are 30 dummyimmutabledate objects with dummyDate
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Doctrine/MongoDbOdm/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DateFilter extends AbstractFilter implements DateFilterInterface

public const DOCTRINE_DATE_TYPES = [
MongoDbType::DATE => true,
MongoDbType::DATE_IMMUTABLE => true,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function getTypes($class, $property, array $context = [])
switch ($typeOfField) {
case MongoDbType::DATE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')];
case MongoDbType::DATE_IMMUTABLE:
return [new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTimeImmutable')];
case MongoDbType::HASH:
return [new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)];
case MongoDbType::COLLECTION:
Expand Down
12 changes: 11 additions & 1 deletion tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyCustomMutation as DummyCustomMutationDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyCustomQuery as DummyCustomQueryDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyDate as DummyDateDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyImmutableDate as DummyDateImmutableDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyDifferentGraphQlSerializationGroup as DummyDifferentGraphQlSerializationGroupDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyDtoCustom as DummyDtoCustomDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyDtoNoInput as DummyDtoNoInputDocument;
Expand Down Expand Up @@ -1209,7 +1210,7 @@ public function thereAreDummyImmutableDateObjectsWithDummyDate(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTimeImmutable(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
$dummy = new DummyImmutableDate();
$dummy = $this->buildDummyImmutableDate();
$dummy->dummyDate = $date;

$this->manager->persist($dummy);
Expand Down Expand Up @@ -1618,6 +1619,15 @@ private function buildDummyDate()
return $this->isOrm() ? new DummyDate() : new DummyDateDocument();
}

/**
* @return DummyDate|DummyDateDocument
*/
private function buildDummyImmutableDate()
{
return $this->isOrm() ? new DummyImmutableDate() : new DummyDateImmutableDocument();
}


/**
* @return DummyDifferentGraphQlSerializationGroup|DummyDifferentGraphQlSerializationGroupDocument
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testGetProperties(): void
'binUuidRfc4122',
'timestamp',
'date',
'dateImmutable',
'float',
'bool',
'boolean',
Expand Down Expand Up @@ -141,6 +142,7 @@ public function typesProvider(): array
['binUuidRfc4122', [new Type(Type::BUILTIN_TYPE_STRING)]],
['timestamp', [new Type(Type::BUILTIN_TYPE_STRING)]],
['date', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')]],
['dateImmutable', [new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTimeImmutable')]],
['float', [new Type(Type::BUILTIN_TYPE_FLOAT)]],
['bool', [new Type(Type::BUILTIN_TYPE_BOOL)]],
['boolean', [new Type(Type::BUILTIN_TYPE_BOOL)]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class DoctrineDummy
*/
private $date;

/**
* @Field(type="date_immutable")
*/
private $dateImmutable;

/**
* @Field(type="float")
*/
Expand Down
57 changes: 57 additions & 0 deletions tests/Fixtures/TestBundle/Document/DummyImmutableDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\DateFilter;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* Dummy Date Immutable.
*
*
* @ApiResource(attributes={
* "filters"={"my_dummy_immutable_date.mongodb.date"}
* })
*
* @ODM\Document
*/
class DummyImmutableDate
{
/**
* @var int The id
*
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
private $id;

/**
* @var \DateTimeImmutable The dummy date
*
* @ODM\Field(type="date_immutable")
*/
public $dummyDate;

/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
}
4 changes: 4 additions & 0 deletions tests/Fixtures/app/config/config_mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ services:
parent: 'api_platform.doctrine_mongodb.odm.date_filter'
arguments: [ { 'dummyDate': ~ } ]
tags: [ { name: 'api_platform.filter', id: 'my_dummy_date.mongodb.date' } ]
app.my_dummy_immutable_date_resource.mongodb.date_filter:
parent: 'api_platform.doctrine_mongodb.odm.date_filter'
arguments: [ { 'dummyDate': ~ } ]
tags: [ { name: 'api_platform.filter', id: 'my_dummy_immutable_date.mongodb.date' } ]
app.related_dummy_to_friend_resource.mongodb.search_filter:
parent: 'api_platform.doctrine_mongodb.odm.search_filter'
arguments: [ { 'name': 'ipartial', 'description': 'ipartial' } ]
Expand Down

0 comments on commit ac0befd

Please sign in to comment.