Skip to content

Commit 120a34a

Browse files
feat: add tests of filters with stateOptions
1 parent d762704 commit 120a34a

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
17+
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
18+
use ApiPlatform\Doctrine\Orm\State\Options;
19+
use ApiPlatform\Metadata\ApiResource;
20+
use ApiPlatform\Metadata\GetCollection;
21+
use ApiPlatform\Metadata\QueryParameter;
22+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterWithStateOptionsAndNoApiFilterEntity;
23+
24+
#[ApiResource(
25+
stateOptions: new Options(entityClass: FilterWithStateOptionsAndNoApiFilterEntity::class),
26+
operations: [
27+
new GetCollection(
28+
uriTemplate: 'filter_with_state_options_and_no_api_filters',
29+
parameters: [
30+
'search[:property]' => new QueryParameter(
31+
properties: ['dummyDate', 'name'],
32+
filter: new PartialSearchFilter(),
33+
),
34+
],
35+
provider: CollectionProvider::class
36+
),
37+
]
38+
)]
39+
final class FilterWithStateOptionsAndNoApiFilter
40+
{
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\Tests\Fixtures\TestBundle\Entity;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Entity]
19+
class FilterWithStateOptionsAndNoApiFilterEntity
20+
{
21+
public function __construct(
22+
#[ORM\Column(type: 'integer')]
23+
#[ORM\Id]
24+
#[ORM\GeneratedValue(strategy: 'AUTO')]
25+
public ?int $id = null,
26+
#[ORM\Column(type: 'date_immutable', nullable: true)]
27+
public ?\DateTimeImmutable $dummyDate = null,
28+
#[ORM\Column(type: 'string', nullable: true)]
29+
public ?string $name = null,
30+
) {
31+
}
32+
}

tests/Functional/Parameters/DoctrineTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
1717
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FilterWithStateOptions;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FilterWithStateOptionsAndNoApiFilter;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\Document\SearchFilterParameter as SearchFilterParameterDocument;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterWithStateOptionsAndNoApiFilterEntity;
1921
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\FilterWithStateOptionsEntity;
2022
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\ProductWithQueryParameter;
2123
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SearchFilterParameter;
@@ -35,7 +37,7 @@ final class DoctrineTest extends ApiTestCase
3537
*/
3638
public static function getResources(): array
3739
{
38-
return [SearchFilterParameter::class, FilterWithStateOptions::class, ProductWithQueryParameter::class];
40+
return [SearchFilterParameter::class, FilterWithStateOptions::class, FilterWithStateOptionsAndNoApiFilter::class, ProductWithQueryParameter::class];
3941
}
4042

4143
public function testDoctrineEntitySearchFilter(): void
@@ -147,6 +149,38 @@ public function testStateOptions(): void
147149
$this->assertEquals('after', $a['hydra:member'][0]['name']);
148150
}
149151

152+
public function testStateOptionsAndNoApiFilter(): void
153+
{
154+
if ($this->isMongoDB()) {
155+
$this->markTestSkipped('Not tested with mongodb.');
156+
}
157+
158+
static::bootKernel();
159+
$container = static::$kernel->getContainer();
160+
$this->recreateSchema([FilterWithStateOptionsAndNoApiFilterEntity::class]);
161+
$manager = $container->get('doctrine')->getManager();
162+
163+
$d = new \DateTimeImmutable();
164+
$manager->persist(new FilterWithStateOptionsAndNoApiFilterEntity(dummyDate: $d, name: 'current'));
165+
$manager->persist(new FilterWithStateOptionsAndNoApiFilterEntity(name: 'null'));
166+
$manager->persist(new FilterWithStateOptionsAndNoApiFilterEntity(dummyDate: $d->add(\DateInterval::createFromDateString('1 day')), name: 'after'));
167+
$manager->flush();
168+
169+
$response = self::createClient()->request('GET', '/filter_with_state_options_and_no_api_filters');
170+
$this->assertResponseIsSuccessful();
171+
$a = $response->toArray();
172+
$this->assertSame('hydra:Collection', $a['@type']);
173+
$this->assertSame(3, $a['hydra:totalItems']);
174+
$this->assertCount(3, $a['hydra:member']);
175+
176+
$response = self::createClient()->request('GET', '/filter_with_state_options_and_no_api_filters?search[name]=aft');
177+
$this->assertResponseIsSuccessful();
178+
$a = $response->toArray();
179+
$this->assertSame('hydra:Collection', $a['@type']);
180+
$this->assertSame(1, $a['hydra:totalItems']);
181+
$this->assertCount(1, $a['hydra:member']);
182+
}
183+
150184
#[DataProvider('partialFilterParameterProviderForSearchFilterParameter')]
151185
public function testPartialSearchFilterWithSearchFilterParameter(string $url, int $expectedCount, array $expectedFoos): void
152186
{

0 commit comments

Comments
 (0)