From dd834f859e0da5024af9a07367fc9986e1e6a85d Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Fri, 30 Sep 2022 17:08:12 +0200 Subject: [PATCH] test: add a test for json_schema_context --- .../Document/JsonSchemaContextDummy.php | 62 ++++++++++++++++++ .../Entity/JsonSchemaContextDummy.php | 64 +++++++++++++++++++ tests/Symfony/Bundle/Test/ApiTestCaseTest.php | 15 +++++ 3 files changed, 141 insertions(+) create mode 100644 tests/Fixtures/TestBundle/Document/JsonSchemaContextDummy.php create mode 100644 tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php diff --git a/tests/Fixtures/TestBundle/Document/JsonSchemaContextDummy.php b/tests/Fixtures/TestBundle/Document/JsonSchemaContextDummy.php new file mode 100644 index 00000000000..085c49d5524 --- /dev/null +++ b/tests/Fixtures/TestBundle/Document/JsonSchemaContextDummy.php @@ -0,0 +1,62 @@ + + * + * 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\Fixtures\TestBundle\Document; + +use ApiPlatform\Core\Annotation\ApiProperty; +use ApiPlatform\Core\Annotation\ApiResource; +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; + +/** + * JSON Schema Context Dummy. + * + * @ApiResource + * + * @ODM\Document + */ +class JsonSchemaContextDummy +{ + /** + * @var int The id + * + * @ApiProperty(identifier=true) + * @ODM\Id(strategy="INCREMENT", type="int") + */ + private $id; + + /** + * @var array + * + * @ApiProperty( + * attributes={ + * "json_schema_context"={ + * "type"="array", + * "items"={"type"="string"}, + * "minItems"=2, + * "maxItems"=2 + * } + * }, + * ) + */ + private $things = ['pool', 'bag']; + + public function getId() + { + return $this->id; + } + + public function getThings() + { + return $this->things; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php b/tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php new file mode 100644 index 00000000000..c81b3166c2c --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/JsonSchemaContextDummy.php @@ -0,0 +1,64 @@ + + * + * 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\Fixtures\TestBundle\Entity; + +use ApiPlatform\Core\Annotation\ApiProperty; +use ApiPlatform\Core\Annotation\ApiResource; +use Doctrine\ORM\Mapping as ORM; + +/** + * JSON Schema Context Dummy. + * + * @ApiResource + * + * @ORM\Entity + */ +class JsonSchemaContextDummy +{ + /** + * @var int The id + * + * @ApiProperty(identifier=true) + * @ORM\Column(type="integer") + * @ORM\Id + * @ORM\GeneratedValue(strategy="AUTO") + */ + private $id; + + /** + * @var array + * + * @ApiProperty( + * attributes={ + * "json_schema_context"={ + * "type"="array", + * "items"={"type"="string"}, + * "minItems"=2, + * "maxItems"=2 + * } + * }, + * ) + */ + private $things = ['pool', 'bag']; + + public function getId() + { + return $this->id; + } + + public function getThings() + { + return $this->things; + } +} diff --git a/tests/Symfony/Bundle/Test/ApiTestCaseTest.php b/tests/Symfony/Bundle/Test/ApiTestCaseTest.php index 5bda0a07223..8c1689c86fe 100644 --- a/tests/Symfony/Bundle/Test/ApiTestCaseTest.php +++ b/tests/Symfony/Bundle/Test/ApiTestCaseTest.php @@ -17,6 +17,7 @@ use ApiPlatform\Tests\Fixtures\TestBundle\Document\Dummy as DummyDocument; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy; use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput; +use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaContextDummy; use ApiPlatform\Tests\Fixtures\TestBundle\Model\ResourceInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Tools\SchemaTool; @@ -141,6 +142,20 @@ public function testAssertMatchesResourceItemJsonSchema(): void $this->assertMatchesResourceItemJsonSchema(ResourceInterface::class); } + public function testAssertMatchesResourceItemJsonSchemaWithCustomJson(): void + { + $this->recreateSchema(); + + /** @var EntityManagerInterface $manager */ + $manager = (method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container)->get('doctrine')->getManager(); + $jsonSchemaContextDummy = new JsonSchemaContextDummy(); + $manager->persist($jsonSchemaContextDummy); + $manager->flush(); + + self::createClient()->request('GET', '/json_schema_context_dummies/1'); + $this->assertMatchesResourceItemJsonSchema(JsonSchemaContextDummy::class); + } + public function testAssertMatchesResourceItemJsonSchemaOutput(): void { $this->recreateSchema();