Skip to content

Commit bf0cc0e

Browse files
committed
test
1 parent 149d6e8 commit bf0cc0e

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\State\Options;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
20+
use Symfony\Component\ObjectMapper\Attribute\Map;
21+
22+
#[ApiResource(
23+
shortName: 'First',
24+
operations: [new GetCollection()],
25+
stateOptions: new Options(entityClass: SameEntity::class)
26+
)]
27+
#[Map(target: SameEntity::class)]
28+
final class FirstResource
29+
{
30+
#[Map(if: false)]
31+
public ?int $id = null;
32+
33+
public string $name;
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\State\Options;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
20+
use Symfony\Component\ObjectMapper\Attribute\Map;
21+
22+
#[ApiResource(
23+
shortName: 'Second',
24+
operations: [new GetCollection()],
25+
stateOptions: new Options(entityClass: SameEntity::class)
26+
)]
27+
#[Map(target: SameEntity::class)]
28+
final class SecondResource
29+
{
30+
#[Map(if: false)]
31+
public ?int $id = null;
32+
33+
public string $name;
34+
35+
public string $extra = 'field';
36+
}
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 <dunglas@gmail.com>
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 ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Component\ObjectMapper\Attribute\Map;
20+
21+
#[ORM\Entity]
22+
#[Map(target: FirstResource::class)]
23+
#[Map(target: SecondResource::class)]
24+
class SameEntity
25+
{
26+
#[ORM\Column(type: 'integer')]
27+
#[ORM\Id]
28+
#[ORM\GeneratedValue(strategy: 'AUTO')]
29+
private ?int $id = null;
30+
31+
#[ORM\Column]
32+
private string $name;
33+
34+
public function getId(): ?int
35+
{
36+
return $this->id;
37+
}
38+
39+
public function setName(string $name): void
40+
{
41+
$this->name = $name;
42+
}
43+
44+
public function getName(): string
45+
{
46+
return $this->name;
47+
}
48+
}

tests/Functional/MappingTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
namespace ApiPlatform\Tests\Functional;
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\FirstResource;
1718
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResource;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\MappedResourceOdm;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\SecondResource;
1921
use ApiPlatform\Tests\Fixtures\TestBundle\Document\MappedDocument;
2022
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\MappedEntity;
23+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\SameEntity;
2124
use ApiPlatform\Tests\RecreateSchemaTrait;
2225
use ApiPlatform\Tests\SetupClassResourcesTrait;
2326
use Doctrine\ODM\MongoDB\DocumentManager;
@@ -33,7 +36,7 @@ final class MappingTest extends ApiTestCase
3336
*/
3437
public static function getResources(): array
3538
{
36-
return [MappedResource::class, MappedResourceOdm::class];
39+
return [MappedResource::class, MappedResourceOdm::class, FirstResource::class, SecondResource::class];
3740
}
3841

3942
public function testShouldMapBetweenResourceAndEntity(): void
@@ -68,6 +71,25 @@ public function testShouldMapBetweenResourceAndEntity(): void
6871
$this->assertJsonContains(['username' => 'ba zar']);
6972
}
7073

74+
public function testShouldMapToTheCorrectResource(): void
75+
{
76+
if (!$this->getContainer()->has('object_mapper')) {
77+
$this->markTestSkipped('ObjectMapper not installed');
78+
}
79+
80+
$this->recreateSchema([SameEntity::class]);
81+
$manager = $this->getManager();
82+
$e = new SameEntity();
83+
$e->setName('foo');
84+
$manager->persist($e);
85+
$manager->flush();
86+
87+
self::createClient()->request('GET', '/seconds');
88+
$this->assertJsonContains(['hydra:member' => [
89+
['name' => 'foo', 'extra' => 'field'],
90+
]]);
91+
}
92+
7193
private function loadFixtures(): void
7294
{
7395
$manager = $this->getManager();

0 commit comments

Comments
 (0)