-
-
Notifications
You must be signed in to change notification settings - Fork 962
Expand file tree
/
Copy pathStateOptionTest.php
More file actions
130 lines (105 loc) · 4.96 KB
/
StateOptionTest.php
File metadata and controls
130 lines (105 loc) · 4.96 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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\Doctrine;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6039\UserApi;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7689\Issue7689CategoryDto;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7689\Issue7689ProductDto;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7801\Issue7801CategoryDto;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7801\Issue7801ProductDto;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6039\Issue6039EntityUser;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7689\Issue7689Category;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7689\Issue7689Product;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7801\Issue7801Category;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue7801\Issue7801Product;
use ApiPlatform\Tests\RecreateSchemaTrait;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Symfony\Component\ObjectMapper\Metadata\ReverseClassObjectMapperMetadataFactory;
final class StateOptionTest extends ApiTestCase
{
use RecreateSchemaTrait;
use SetupClassResourcesTrait;
protected static ?bool $alwaysBootKernel = false;
/**
* @return class-string[]
*/
public static function getResources(): array
{
return [UserApi::class, Issue7689ProductDto::class, Issue7689CategoryDto::class, Issue7801ProductDto::class, Issue7801CategoryDto::class];
}
public function testDtoWithEntityClassOptionCollection(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('This test is not for MongoDB.');
}
$this->recreateSchema([Issue6039EntityUser::class]);
$manager = static::getContainer()->get('doctrine')->getManager();
$user = new Issue6039EntityUser();
$user->name = 'name';
$user->bar = 'bar';
$manager->persist($user);
$manager->flush();
$response = static::createClient()->request('GET', '/issue6039_user_apis', ['headers' => ['Accept' => 'application/ld+json']]);
$this->assertResponseStatusCodeSame(200);
$this->assertArrayNotHasKey('bar', $response->toArray()['hydra:member'][0]);
}
public function testPostWithEntityClassOption(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}
// This test requires symfony/object-mapper >= 8.1 for bidirectional mapping support
if (!class_exists(ReverseClassObjectMapperMetadataFactory::class)) {
$this->markTestSkipped('This test requires symfony/object-mapper >= 8.1');
}
$this->recreateSchema([Issue7689Product::class, Issue7689Category::class]);
$manager = static::getContainer()->get('doctrine')->getManager();
$c = new Issue7689Category();
$c->name = 'category';
$manager->persist($c);
$manager->flush();
$iri = '/issue7689_categories/'.$c->getId();
$response = static::createClient()->request('POST', '/issue7689_products', ['json' => [
'name' => 'product',
'category' => $iri,
]]);
$this->assertResponseStatusCodeSame(201);
$this->assertCount(1, $manager->getRepository(Issue7689Product::class)->findAll());
$product = $manager->getRepository(Issue7689Product::class)->findOneBy(['name' => 'product']);
$this->assertNotNull($product->category);
$this->assertEquals(1, $product->category->getId());
}
public function testGetWithNestedDtoMapping(): void
{
if ($this->isMongoDB()) {
$this->markTestSkipped('MongoDB not tested.');
}
if (!class_exists(ReverseClassObjectMapperMetadataFactory::class)) {
$this->markTestSkipped('This test requires symfony/object-mapper >= 8.1');
}
$this->recreateSchema([Issue7801Product::class, Issue7801Category::class]);
$manager = static::getContainer()->get('doctrine')->getManager();
$category = new Issue7801Category();
$category->name = 'electronics';
$manager->persist($category);
$product = new Issue7801Product();
$product->name = 'laptop';
$product->category = $category;
$manager->persist($product);
$manager->flush();
$response = static::createClient()->request('GET', '/issue7801_products/'.$product->getId(), ['headers' => ['Accept' => 'application/ld+json']]);
$this->assertResponseStatusCodeSame(200);
$json = $response->toArray();
$this->assertSame('laptop', $json['name']);
$this->assertArrayHasKey('category', $json);
$this->assertSame('electronics', $json['category']['name']);
}
}