Repository navigation
Expand file tree
/
Copy pathIssue7655FileDeclaredParameterTest.php
More file actions
99 lines (81 loc) · 3.22 KB
/
Copy pathIssue7655FileDeclaredParameterTest.php
File metadata and controls
99 lines (81 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Functional\Parameters;
use ApiPlatform\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7655\Author;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7655\Book;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;
/**
* Parameters declared in resource configuration files (YAML here) must get the same metadata as
* parameters declared with attributes: property defaults, filter schema and OpenAPI parameters,
* and Doctrine nested property information.
*
* @see https://github.com/api-platform/core/issues/7655
*
* @group issue-7655
*/
final class Issue7655FileDeclaredParameterTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;
protected static ?bool $alwaysBootKernel = false;
/**
* @return class-string[]
*/
public static function getResources(): array
{
return [Book::class];
}
protected function setUp(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('The Book resource is declared in the ORM resource configuration only.');
}
$this->recreateSchema([Book::class, Author::class]);
$this->loadFixtures();
}
public function testParameterPropertyDefaultsToTheParameterKey(): void
{
$response = self::createClient()->request('GET', '/issue7655_books?title=Dune');
$this->assertResponseIsSuccessful();
$titles = array_map(static fn (array $book): string => $book['title'], $response->toArray()['hydra:member']);
$this->assertSame(['Dune'], $titles);
}
public function testFilterOnNestedProperty(): void
{
$response = self::createClient()->request('GET', '/issue7655_books?authorName=Ursula');
$this->assertResponseIsSuccessful();
$titles = array_map(static fn (array $book): string => $book['title'], $response->toArray()['hydra:member']);
$this->assertSame(['The Dispossessed'], $titles);
}
public function testOpenApiDocumentsTheFilterParameters(): void
{
$response = self::createClient()->request('GET', '/docs', [
'headers' => ['Accept' => 'application/vnd.openapi+json'],
]);
$this->assertResponseIsSuccessful();
$parameterNames = array_column($response->toArray()['paths']['/issue7655_books']['get']['parameters'], 'name');
$this->assertContains('authorName', $parameterNames);
$this->assertContains('authorName[]', $parameterNames);
}
private function loadFixtures(): void
{
$manager = $this->getManager();
$frank = (new Author())->setName('Frank');
$ursula = (new Author())->setName('Ursula');
$manager->persist($frank);
$manager->persist($ursula);
$manager->persist((new Book())->setTitle('Dune')->setAuthor($frank));
$manager->persist((new Book())->setTitle('The Dispossessed')->setAuthor($ursula));
$manager->flush();
}
}