diff --git a/features/main/object_primary_key.feature b/features/main/object_primary_key.feature new file mode 100644 index 00000000000..03445389982 --- /dev/null +++ b/features/main/object_primary_key.feature @@ -0,0 +1,24 @@ +Feature: Planning handling + In order to use an object as primary key + As a developer + I should be able serialize types with object primary key. + + @createSchema + @dropSchema + Scenario: Get a resource containing a raw object + When I send a "GET" request to "/object_primary_keys/2017-01-30" + Then print last JSON response + Scenario: Get a resource + When I send a "GET" request to "/object_primary_keys/2017-01-30" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" + And the JSON should be equal to: + """ + { + "@context": "/contexts/ObjectPrimaryKey", + "@id": "/object_primary_keys/2017-01-30", + "@type": "ObjectPrimaryKey", + "date": "2017-01-30" + } + """ diff --git a/tests/Fixtures/TestBundle/DataProvider/ObjectPrimaryKeyItemDataProvider.php b/tests/Fixtures/TestBundle/DataProvider/ObjectPrimaryKeyItemDataProvider.php new file mode 100644 index 00000000000..f8d3660eed9 --- /dev/null +++ b/tests/Fixtures/TestBundle/DataProvider/ObjectPrimaryKeyItemDataProvider.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider; + +use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; +use ApiPlatform\Core\Exception\ResourceClassNotSupportedException; +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ObjectPrimaryKey; +use DateTime; + +/** + * @author Kévin Dunglas + */ +class ObjectPrimaryKeyItemDataProvider implements ItemDataProviderInterface +{ + /** + * {@inheritdoc} + */ + public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) + { + if (ObjectPrimaryKey::class !== $resourceClass) { + throw new ResourceClassNotSupportedException(); + } + + $planning = new ObjectPrimaryKey(); + $planning->setDate(new DateTime($id)); + + return $planning; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/ObjectPrimaryKey.php b/tests/Fixtures/TestBundle/Entity/ObjectPrimaryKey.php new file mode 100644 index 00000000000..192772385cb --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/ObjectPrimaryKey.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; + +use ApiPlatform\Core\Annotation\ApiResource; +use DateTime; +use Doctrine\ORM\Mapping as ORM; + +/** + * Planning. + * + * @author Kévin Dunglas + * + * @ApiResource + * @ORM\Entity + */ +class ObjectPrimaryKey +{ + /** + * @var DateTime + * + * @ORM\Column(type="date") + * @ORM\Id + */ + private $date; + + public function getDate(): DateTime + { + return $this->date; + } + + public function setDate(DateTime $date) + { + $this->date = $date; + } +} diff --git a/tests/Fixtures/app/config/config.yml b/tests/Fixtures/app/config/config.yml index b29382a6164..712d99639cd 100644 --- a/tests/Fixtures/app/config/config.yml +++ b/tests/Fixtures/app/config/config.yml @@ -69,6 +69,10 @@ nelmio_api_doc: json: 'application/json' services: + object_primary_key.item_data_provider: + class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ObjectPrimaryKeyItemDataProvider' + tags: + - { name: 'api_platform.item_data_provider' } app.user_manager: class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Manager\UserManager' arguments: