Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mongodb date_immutable filter support #3940

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,6 @@ jobs:
- name: Update project dependencies
run: |
composer update --no-interaction --no-progress --ansi
composer why doctrine/reflection
# - name: Require Symfony Uid
# run: composer require symfony/uid --dev --no-interaction --no-progress --ansi
- name: Install PHPUnit
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.7.0

* MongoDB: `date_immutable` support (#3940)

## 2.6.1

* Fix defaults when using attributes (#3978)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"doctrine/common": "^2.11 || ^3.0",
"doctrine/data-fixtures": "^1.2.2",
"doctrine/doctrine-bundle": "^1.12 || ^2.0",
"doctrine/mongodb-odm": "^2.0",
"doctrine/mongodb-odm": "^2.1",
"doctrine/mongodb-odm-bundle": "^4.0",
"doctrine/orm": "^2.6.4 || ^3.0",
"elasticsearch/elasticsearch": "^6.0 || ^7.0",
Expand Down Expand Up @@ -86,7 +86,7 @@
},
"conflict": {
"doctrine/common": "<2.7",
"doctrine/mongodb-odm": "<2.0",
"doctrine/mongodb-odm": "<2.1",
"doctrine/persistence": "<1.3"
},
"suggest": {
Expand Down
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
1 change: 1 addition & 0 deletions src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ protected function getType(string $doctrineType): string
case MongoDbType::BOOLEAN:
return 'bool';
case MongoDbType::DATE:
case MongoDbType::DATE_IMMUTABLE:
return \DateTimeInterface::class;
case MongoDbType::FLOAT:
return 'float';
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
11 changes: 10 additions & 1 deletion tests/Behat/DoctrineContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyDtoOutputSameClass as DummyDtoOutputSameClassDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyFriend as DummyFriendDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyGroup as DummyGroupDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyImmutableDate as DummyImmutableDateDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyMercure as DummyMercureDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyOffer as DummyOfferDocument;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\DummyProduct as DummyProductDocument;
Expand Down Expand Up @@ -1270,7 +1271,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 @@ -1801,6 +1802,14 @@ private function buildDummyDate()
return $this->isOrm() ? new DummyDate() : new DummyDateDocument();
}

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

/**
* @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
53 changes: 53 additions & 0 deletions tests/Fixtures/TestBundle/Document/DummyImmutableDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\ApiResource;
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