Skip to content

Commit b8c0614

Browse files
authored
test: add a test for json_schema_context (#5043)
1 parent ae40d35 commit b8c0614

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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\Document;
15+
16+
use ApiPlatform\Core\Annotation\ApiProperty;
17+
use ApiPlatform\Core\Annotation\ApiResource;
18+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
19+
20+
/**
21+
* JSON Schema Context Dummy.
22+
*
23+
* @ApiResource
24+
*
25+
* @ODM\Document
26+
*/
27+
class JsonSchemaContextDummy
28+
{
29+
/**
30+
* @var int The id
31+
*
32+
* @ApiProperty(identifier=true)
33+
* @ODM\Id(strategy="INCREMENT", type="int")
34+
*/
35+
private $id;
36+
37+
/**
38+
* @var array
39+
*
40+
* @ApiProperty(
41+
* attributes={
42+
* "json_schema_context"={
43+
* "type"="array",
44+
* "items"={"type"="string"},
45+
* "minItems"=2,
46+
* "maxItems"=2
47+
* }
48+
* },
49+
* )
50+
*/
51+
private $things = ['pool', 'bag'];
52+
53+
public function getId()
54+
{
55+
return $this->id;
56+
}
57+
58+
public function getThings()
59+
{
60+
return $this->things;
61+
}
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 ApiPlatform\Core\Annotation\ApiProperty;
17+
use ApiPlatform\Core\Annotation\ApiResource;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
/**
21+
* JSON Schema Context Dummy.
22+
*
23+
* @ApiResource
24+
*
25+
* @ORM\Entity
26+
*/
27+
class JsonSchemaContextDummy
28+
{
29+
/**
30+
* @var int The id
31+
*
32+
* @ApiProperty(identifier=true)
33+
* @ORM\Column(type="integer")
34+
* @ORM\Id
35+
* @ORM\GeneratedValue(strategy="AUTO")
36+
*/
37+
private $id;
38+
39+
/**
40+
* @var array
41+
*
42+
* @ApiProperty(
43+
* attributes={
44+
* "json_schema_context"={
45+
* "type"="array",
46+
* "items"={"type"="string"},
47+
* "minItems"=2,
48+
* "maxItems"=2
49+
* }
50+
* },
51+
* )
52+
*/
53+
private $things = ['pool', 'bag'];
54+
55+
public function getId()
56+
{
57+
return $this->id;
58+
}
59+
60+
public function getThings()
61+
{
62+
return $this->things;
63+
}
64+
}

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
1919
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaContextDummy;
2021
use ApiPlatform\Tests\Fixtures\TestBundle\Model\ResourceInterface;
2122
use Doctrine\ORM\EntityManagerInterface;
2223
use Doctrine\ORM\Tools\SchemaTool;
@@ -141,6 +142,20 @@ public function testAssertMatchesResourceItemJsonSchema(): void
141142
$this->assertMatchesResourceItemJsonSchema(ResourceInterface::class);
142143
}
143144

145+
public function testAssertMatchesResourceItemJsonSchemaWithCustomJson(): void
146+
{
147+
$this->recreateSchema();
148+
149+
/** @var EntityManagerInterface $manager */
150+
$manager = (method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container)->get('doctrine')->getManager();
151+
$jsonSchemaContextDummy = new JsonSchemaContextDummy();
152+
$manager->persist($jsonSchemaContextDummy);
153+
$manager->flush();
154+
155+
self::createClient()->request('GET', '/json_schema_context_dummies/1');
156+
$this->assertMatchesResourceItemJsonSchema(JsonSchemaContextDummy::class);
157+
}
158+
144159
public function testAssertMatchesResourceItemJsonSchemaOutput(): void
145160
{
146161
$this->recreateSchema();

0 commit comments

Comments
 (0)