From 31ea473a2bba1cec7200acf2f82913b81c600905 Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 4 Apr 2019 10:19:22 +0200 Subject: [PATCH] Add a regression test for api-platform/api-platform#1085 --- features/jsonapi/non_resource.feature | 38 +++++++++++++ features/jsonld/non_resource.feature | 29 ++++++++++ .../TestBundle/Entity/PlainObjectDummy.php | 56 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 tests/Fixtures/TestBundle/Entity/PlainObjectDummy.php diff --git a/features/jsonapi/non_resource.feature b/features/jsonapi/non_resource.feature index b4654ac0f0e..493e83efcc0 100644 --- a/features/jsonapi/non_resource.feature +++ b/features/jsonapi/non_resource.feature @@ -86,3 +86,41 @@ Feature: JSON API non-resource handling } } """ + + @!mongodb + @createSchema + Scenario: Create a resource that contains a stdClass object. + When I send a "POST" request to "/plain_object_dummies" with body: + """ + { + "data": { + "type": "PlainObjectDummy", + "attributes": { + "content":"{\"fields\":{\"title\":{\"value\":\"\"},\"images\":[{\"id\":0,\"categoryId\":0,\"uri\":\"/api/pictures\",\"resource\":\"{}\",\"description\":\"\",\"alt\":\"\",\"type\":\"picture\",\"text\":\"\",\"src\":\"\"}],\"alternativeAudio\":{},\"caption\":\"\"},\"showCaption\":false,\"alternativeContent\":false,\"alternativeAudioContent\":false,\"blockLayout\":\"default\"}" + } + } + } + """ + Then the response status code should be 201 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/vnd.api+json; charset=utf-8" + And the JSON should be valid according to the JSON API schema + And the JSON should be a superset of: + """ + { + "data": { + "id": "/plain_object_dummies/1", + "type": "PlainObjectDummy", + "attributes": { + "_id": 1, + "data": { + "fields": [], + "showCaption": false, + "alternativeContent": false, + "alternativeAudioContent": false, + "blockLayout": "default" + } + } + } + } + """ diff --git a/features/jsonld/non_resource.feature b/features/jsonld/non_resource.feature index e967e44f3e3..c47d5a2b826 100644 --- a/features/jsonld/non_resource.feature +++ b/features/jsonld/non_resource.feature @@ -62,3 +62,32 @@ Feature: JSON-LD non-resource handling "id": 1 } """ + + @!mongodb + @createSchema + Scenario: Create a resource that contains a stdClass object. + When I send a "POST" request to "/plain_object_dummies" with body: + """ + { + "content": "{\"fields\":{\"title\":{\"value\":\"\"},\"images\":[{\"id\":0,\"categoryId\":0,\"uri\":\"/api/pictures\",\"resource\":\"{}\",\"description\":\"\",\"alt\":\"\",\"type\":\"picture\",\"text\":\"\",\"src\":\"\"}],\"alternativeAudio\":{},\"caption\":\"\"},\"showCaption\":false,\"alternativeContent\":false,\"alternativeAudioContent\":false,\"blockLayout\":\"default\"}" + } + """ + Then the response status code should be 201 + 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/PlainObjectDummy", + "@id": "/plain_object_dummies/1", + "@type": "PlainObjectDummy", + "data": { + "fields": [], + "showCaption": false, + "alternativeContent": false, + "alternativeAudioContent": false, + "blockLayout": "default" + }, + "id": 1 + } + """ diff --git a/tests/Fixtures/TestBundle/Entity/PlainObjectDummy.php b/tests/Fixtures/TestBundle/Entity/PlainObjectDummy.php new file mode 100644 index 00000000000..92c4767b567 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/PlainObjectDummy.php @@ -0,0 +1,56 @@ + + * + * 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\Core\Tests\Fixtures\TestBundle\Entity; + +use ApiPlatform\Core\Annotation\ApiResource; +use Doctrine\ORM\Mapping as ORM; + +/** + * Regression test for https://github.com/api-platform/api-platform/issues/1085. + * + * @author Antoine Bluchet + * + * @ApiResource + * @ORM\Entity + */ +class PlainObjectDummy +{ + /** + * @ORM\Id + * @ORM\Column(type="integer") + * @ORM\GeneratedValue(strategy="AUTO") + */ + private $id; + + /** + * @var string + */ + private $content; + + /** + * @var array + */ + public $data; + + public function setContent($content) + { + $this->content = $content; + $this->data = (array) json_decode($content); + } + + public function getId() + { + return $this->id; + } +}