Skip to content

Commit

Permalink
Implement asset creation changes, the item post and update post metho…
Browse files Browse the repository at this point in the history
…ds will return the created assets list
  • Loading branch information
Roland Kovacsics committed Jul 17, 2020
1 parent 2ca1938 commit 321c943
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -224,4 +225,6 @@ catch (\Exception $e) {

exit(1);
}

echo "Created assets array = {$item->assets}".PHP_EOL;
```
34 changes: 25 additions & 9 deletions src-dev/Tests/Unit/GatherContentClientItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,40 +384,44 @@ 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(),
],
];
}

/**
* @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(
201,
[
'Content-Type' => 'application/json',
],
\GuzzleHttp\json_encode(['data' => $expected])
\GuzzleHttp\json_encode(['data' => $expected, 'meta' => $meta])
),
]);
$client = $tester['client'];
Expand All @@ -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 */
Expand Down Expand Up @@ -545,48 +553,56 @@ 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(),
],
];
}

/**
* @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'));
Expand Down
13 changes: 13 additions & 0 deletions src-dev/Tests/Unit/GcBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
32 changes: 32 additions & 0 deletions src/DataTypes/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cheppers\GatherContent\DataTypes;

class Meta extends Base
{
/**
* @var array
*/
public $assets = [];

/**
* {@inheritdoc}
*/
protected $unusedProperties = ['id'];

/**
* {@inheritdoc}
*/
protected function initPropertyMapping()
{
parent::initPropertyMapping();
$this->propertyMapping = array_replace(
$this->propertyMapping,
[
'assets' => 'assets',
]
);

return $this;
}
}
14 changes: 13 additions & 1 deletion src/GatherContentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/GatherContentClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -166,6 +166,8 @@ public function itemPost(

/**
* @see https://docs.gathercontent.com/reference#updateitemcontent
*
* @return \Cheppers\GatherContent\DataTypes\Meta|null
*/
public function itemUpdatePost(
$itemId,
Expand Down

0 comments on commit 321c943

Please sign in to comment.