Skip to content

Commit bf21e0b

Browse files
committed
test(core): InvalidUriVariableException status code (e400)
1 parent 119fed1 commit bf21e0b

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\PullRequest7135;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\Uid\Uuid;
20+
21+
#[ORM\Entity()]
22+
#[ApiResource(
23+
shortName: 'BarPr7135',
24+
operations: [
25+
new Get(
26+
uriTemplate: '/pull-request-7135/bar/{id}',
27+
)
28+
]
29+
)]
30+
#[ORM\Table(name: 'bar6466')]
31+
class Bar
32+
{
33+
#[ORM\Id]
34+
#[ORM\Column(type: 'symfony_uuid', unique: true)]
35+
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
36+
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
37+
public Uuid $id;
38+
39+
#[ORM\Column]
40+
public string $title = '';
41+
42+
public function __construct(?Uuid $id = null)
43+
{
44+
$this->id = $id ?: Uuid::v7();
45+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\PullRequest7135;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Post;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\Uid\Uuid;
20+
21+
#[ORM\Entity()]
22+
#[ApiResource(
23+
shortName: 'FooPr7135',
24+
operations: [
25+
new Post(
26+
uriTemplate: '/pull-request-7135/foo/',
27+
)
28+
],
29+
normalizationContext: ['iri_only' => true],
30+
)]
31+
#[ORM\Table(name: 'foo6466')]
32+
class Foo
33+
{
34+
#[ORM\Id]
35+
#[ORM\Column(type: 'symfony_uuid', unique: true)]
36+
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
37+
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
38+
public Uuid $id;
39+
40+
#[ORM\ManyToOne(targetEntity: Bar::class)]
41+
#[ORM\JoinColumn(referencedColumnName: 'id')]
42+
public Bar $bar;
43+
44+
public function __construct()
45+
{
46+
$this->id = Uuid::v7();
47+
}
48+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiPlatform\Tests\Functional\PullRequests;
6+
7+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
8+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PullRequest7135\Bar;
9+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\PullRequest7135\Foo;
10+
use ApiPlatform\Tests\RecreateSchemaTrait;
11+
use ApiPlatform\Tests\SetupClassResourcesTrait;
12+
use Doctrine\ORM\EntityManagerInterface;
13+
use Doctrine\ORM\Tools\SchemaTool;
14+
use Symfony\Component\Uid\Uuid;
15+
16+
class PullRequest7135Test extends ApiTestCase
17+
{
18+
use RecreateSchemaTrait;
19+
use SetupClassResourcesTrait;
20+
21+
protected static ?bool $alwaysBootKernel = false;
22+
23+
/**
24+
* @return class-string[]
25+
*/
26+
public static function getResources(): array
27+
{
28+
return [Bar::class, Foo::class];
29+
}
30+
31+
public function testValidPostRequestWithIriWhenIdentifierIsUuid(): void
32+
{
33+
$this->recreateSchema(self::getResources());
34+
$bar = $this->loadFixtures();
35+
36+
$response = self::createClient()->request('POST', '/pull-request-7135/foo/', [
37+
'json' => [
38+
'bar' => 'pull-request-7135/bar/' . $bar->id
39+
]
40+
]);
41+
42+
self::assertEquals(201, $response->getStatusCode());
43+
}
44+
45+
public function testInvalidPostRequestWithIriWhenIdentifierIsUuid(): void
46+
{
47+
$response = self::createClient()->request('POST', '/pull-request-7135/foo/', [
48+
'json' => [
49+
'bar' => 'pull-request-7135/bar/invalid-uuid'
50+
]
51+
]);
52+
53+
self::assertEquals(400, $response->getStatusCode());
54+
self::assertJsonContains(['detail' => 'Identifier "id" could not be transformed.']);
55+
}
56+
57+
public function testInvalidGetRequestWhenIdentifierIsUuid(): void
58+
{
59+
$response = self::createClient()->request('GET', '/pull-request-7135/bar/invalid-uuid');
60+
61+
self::assertEquals(404, $response->getStatusCode());
62+
self::assertJsonContains(['detail' => 'Invalid uri variables.']);
63+
}
64+
65+
protected function loadFixtures(): Bar
66+
{
67+
$container = static::getContainer();
68+
$registry = $container->get('doctrine');
69+
$manager = $registry->getManager();
70+
71+
$bar = new Bar(Uuid::fromString('0196b66f-66bd-780b-95fe-0ce987a32357'));
72+
$bar->title = 'Bar one';
73+
$manager->persist($bar);
74+
75+
$manager->flush();
76+
77+
return $bar;
78+
}
79+
80+
protected function tearDown(): void
81+
{
82+
$container = static::getContainer();
83+
$registry = $container->get('doctrine');
84+
$manager = $registry->getManager();
85+
if (!$manager instanceof EntityManagerInterface) {
86+
return;
87+
}
88+
89+
$classes = [];
90+
foreach (self::getResources() as $entityClass) {
91+
$classes[] = $manager->getClassMetadata($entityClass);
92+
}
93+
94+
$schemaTool = new SchemaTool($manager);
95+
@$schemaTool->dropSchema($classes);
96+
parent::tearDown();
97+
}
98+
}

0 commit comments

Comments
 (0)