From 321c94325bde1813107f6cdf5737d07211d3ab15 Mon Sep 17 00:00:00 2001 From: Roland Kovacsics Date: Fri, 17 Jul 2020 16:37:31 +0200 Subject: [PATCH] Implement asset creation changes, the item post and update post methods will return the created assets list --- README.md | 7 ++-- .../Unit/GatherContentClientItemTest.php | 34 ++++++++++++++----- src-dev/Tests/Unit/GcBaseTestCase.php | 13 +++++++ src/DataTypes/Meta.php | 32 +++++++++++++++++ src/GatherContentClient.php | 14 +++++++- src/GatherContentClientInterface.php | 4 ++- 6 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 src/DataTypes/Meta.php diff --git a/README.md b/README.md index 7cc4942..e35eb2e 100644 --- a/README.md +++ b/README.md @@ -192,8 +192,9 @@ catch (\Exception $e) { exit(1); } -echo "Content's name = {$item->name}".PHP_EOL; -echo "Item ID = {$item->id}".PHP_EOL; +echo "Content's name = {$item['data']->name}".PHP_EOL; +echo "Item ID = {$item['data']->id}".PHP_EOL; +echo "Created assets array = {$item['meta']->assets}".PHP_EOL; ``` To update an item with assets, you can do the following: @@ -224,4 +225,6 @@ catch (\Exception $e) { exit(1); } + +echo "Created assets array = {$item->assets}".PHP_EOL; ``` diff --git a/src-dev/Tests/Unit/GatherContentClientItemTest.php b/src-dev/Tests/Unit/GatherContentClientItemTest.php index ed069d8..850b72e 100644 --- a/src-dev/Tests/Unit/GatherContentClientItemTest.php +++ b/src-dev/Tests/Unit/GatherContentClientItemTest.php @@ -384,24 +384,28 @@ public function casesItemPost() $itemEmpty, 131313, $itemEmpty->id, + [], ], 'custom' => [ $itemCustom, $itemCustom, 131313, $itemCustom->id, + [], ], 'multiple-elements' => [ $itemMultipleElements, $itemMultipleElements, 131313, $itemMultipleElements->id, + [], ], 'with-assets' => [ $itemAssets, $sentItem, 131313, $itemAssets->id, + static::getUniqueResponseMeta(), ], ]; } @@ -409,7 +413,7 @@ public function casesItemPost() /** * @dataProvider casesItemPost */ - public function testItemPost(Item $expected, Item $item, $projectId, $resultItemId) + public function testItemPost(Item $expected, Item $item, $projectId, $resultItemId, $meta) { $tester = $this->getBasicHttpClientTester([ new Response( @@ -417,7 +421,7 @@ public function testItemPost(Item $expected, Item $item, $projectId, $resultItem [ 'Content-Type' => 'application/json', ], - \GuzzleHttp\json_encode(['data' => $expected]) + \GuzzleHttp\json_encode(['data' => $expected, 'meta' => $meta]) ), ]); $client = $tester['client']; @@ -427,15 +431,19 @@ public function testItemPost(Item $expected, Item $item, $projectId, $resultItem ->setOptions($this->gcClientOptions) ->itemPost($projectId, $item); - $actual->setSkipEmptyProperties(true); + $actual['data']->setSkipEmptyProperties(true); $expected->setSkipEmptyProperties(true); - static::assertEquals($resultItemId, $actual->id); + static::assertEquals($resultItemId, $actual['data']->id); - static::assertTrue($actual instanceof Item, 'Data type of the return is Item'); + if (!empty($meta)) { + static::assertEquals($actual['meta']->assets, $meta['assets']); + } + + static::assertTrue($actual['data'] instanceof Item, 'Data type of the return is Item'); static::assertEquals( \GuzzleHttp\json_encode($expected, JSON_PRETTY_PRINT), - \GuzzleHttp\json_encode($actual, JSON_PRETTY_PRINT) + \GuzzleHttp\json_encode($actual['data'], JSON_PRETTY_PRINT) ); /** @var Request $request */ @@ -545,21 +553,25 @@ public function casesItemUpdatePost() 13, $itemEmpty->content, [], + [], ], 'custom' => [ 13, $itemCustom->content, [], + [], ], 'multiple-elements' => [ 13, $itemMultipleElements->content, [], + [], ], 'with-assets' => [ 13, $itemAssets->content, ['field-uuid' => [__DIR__.'/files/test.txt']], + static::getUniqueResponseMeta(), ], ]; } @@ -567,26 +579,30 @@ public function casesItemUpdatePost() /** * @dataProvider casesItemUpdatePost */ - public function testItemUpdatePost($itemId, array $content, array $assets) + public function testItemUpdatePost($itemId, array $content, array $assets, array $meta) { $tester = $this->getBasicHttpClientTester([ new Response( 202, [ 'Content-Type' => 'application/json', - ] + ], + \GuzzleHttp\json_encode(['meta' => $meta]) ), ]); $client = $tester['client']; $container = &$tester['container']; - (new GatherContentClient($client)) + $item = (new GatherContentClient($client)) ->setOptions($this->gcClientOptions) ->itemUpdatePost($itemId, $content, $assets); /** @var Request $request */ $request = $container[0]['request']; + if (!empty($meta)) { + static::assertEquals($item->assets, $meta['assets']); + } static::assertEquals(1, count($container)); static::assertEquals('POST', $request->getMethod()); static::assertEquals(['application/vnd.gathercontent.v2+json'], $request->getHeader('Accept')); diff --git a/src-dev/Tests/Unit/GcBaseTestCase.php b/src-dev/Tests/Unit/GcBaseTestCase.php index 46a9b7c..b27085f 100644 --- a/src-dev/Tests/Unit/GcBaseTestCase.php +++ b/src-dev/Tests/Unit/GcBaseTestCase.php @@ -376,6 +376,19 @@ public static function getUniqueResponseItem(array $elementTypes = [], array $st return $item; } + public static function getUniqueResponseMeta() + { + $amount = rand(1, 5); + $meta = [ + 'assets' => [], + ]; + for ($i = 0; $i < $amount; $i++) { + $meta['assets'][] = static::getUniqueString('file'); + } + + return $meta; + } + public static function getUniqueResponseTemplate() { return [ diff --git a/src/DataTypes/Meta.php b/src/DataTypes/Meta.php new file mode 100644 index 0000000..a0ed636 --- /dev/null +++ b/src/DataTypes/Meta.php @@ -0,0 +1,32 @@ +propertyMapping = array_replace( + $this->propertyMapping, + [ + 'assets' => 'assets', + ] + ); + + return $this; + } +} diff --git a/src/GatherContentClient.php b/src/GatherContentClient.php index a75eb43..10bac77 100644 --- a/src/GatherContentClient.php +++ b/src/GatherContentClient.php @@ -367,7 +367,14 @@ public function itemPost($projectId, Item $item) $this->validatePostResponse(201); $body = $this->parseResponse(); - return empty($body['data']) ? null : $this->parseResponseDataItem($body['data'], DataTypes\Item::class); + $response['data'] = empty($body['data']) + ? null + : $this->parseResponseDataItem($body['data'], DataTypes\Item::class); + $response['meta'] = empty($body['meta']) + ? null + : $this->parseResponseDataItem($body['meta'], DataTypes\Meta::class); + + return $response; } /** @@ -390,6 +397,11 @@ public function itemUpdatePost($itemId, array $content = [], array $assets = []) } $this->sendPost("items/$itemId/content", $request); + + $this->validatePostResponse(202); + $body = $this->parseResponse(); + + return empty($body['meta']) ? null : $this->parseResponseDataItem($body['meta'], DataTypes\Meta::class); } /** diff --git a/src/GatherContentClientInterface.php b/src/GatherContentClientInterface.php index dbc3f26..b1490e7 100644 --- a/src/GatherContentClientInterface.php +++ b/src/GatherContentClientInterface.php @@ -157,7 +157,7 @@ public function itemGet($itemId); /** * @see https://docs.gathercontent.com/reference#createitem * - * @return \Cheppers\GatherContent\DataTypes\Item|null + * @return array|null */ public function itemPost( $projectId, @@ -166,6 +166,8 @@ public function itemPost( /** * @see https://docs.gathercontent.com/reference#updateitemcontent + * + * @return \Cheppers\GatherContent\DataTypes\Meta|null */ public function itemUpdatePost( $itemId,