Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 5 missing API endpoints #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions src/Bynder/Api/Impl/AssetBankManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,60 @@ public function getMetapropertyOptions($query)
);
}

/**
* Creates an option for a metaproperty.
*
* @param string $metaPropId
* @param array $data
* @return \GuzzleHttp\Promise\Promise
* @throws \Exception
*/
public function createMetaPropertyOption($metaPropId, $data)
{
return $this->requestHandler->sendRequestAsync(
'POST',
sprintf('api/v4/metaproperties/%s/options/', $metaPropId),
['form_params' => ['data' => json_encode($data)]]
);
}

/**
* Modifies existing metaproperty option.
*
* @param string $metaPropId
* @param string $optionId
* @param array $data
*
* @return \GuzzleHttp\Promise\Promise
* @throws \Exception
*/
public function modifyMetaPropertyOption($metaPropId, $optionId, $data)
{
return $this->requestHandler->sendRequestAsync(
'POST',
sprintf('api/v4/metaproperties/%s/options/%s/', $metaPropId, $optionId),
['form_params' => ['data' => json_encode($data)]]
);
}

/**
* Get a single MetaPropertyOption
*
* @param string $propertyId
* @param array $query
*
* @return \GuzzleHttp\Promise\Promise
* @throws \Exception
*/
public function getSingleMetaPropertyOptions($propertyId, $query)
{
return $this->requestHandler->sendRequestAsync(
'GET',
sprintf('api/v4/metaproperties/%s/options/', $propertyId),
['query' => $query]
);
}

/**
* Gets a list of all meta property option dependencies (globally).
*
Expand Down Expand Up @@ -346,7 +400,7 @@ public function getUsage($query)
*
* @param $query
* @return \GuzzleHttp\Promise\Promise
* @throws \GuzzleHttp\Exception\RequestException
* @throws \GuzzleHttp\Exception\RequestException
*/
public function deleteUsage($query)
{
Expand All @@ -355,6 +409,21 @@ public function deleteUsage($query)
);
}

/**
* Synchronizes asset usages
*
* @param array $data
*
* @return mixed
* @throws \Exception
*/
public function syncAssetUsage($data)
{
return $this->requestHandler->sendRequestAsync('POST', 'api/media/usage/sync',
['json' => $data]
);
}

/**
* Gets all collections based on optional query parameters.
*
Expand All @@ -380,4 +449,15 @@ public function getCollectionAssets($collectionId)
{
return $this->requestHandler->sendRequestAsync('GET', "api/v4/collections/$collectionId/media/");
}
}

/**
* Gets account information
*
* @return \GuzzleHttp\Promise\PromiseInterface
* @throws \Exception
*/
public function getAccount()
{
return $this->requestHandler->sendRequestAsync('GET', "api/v4/account/");
}
}
7 changes: 6 additions & 1 deletion src/Bynder/Api/Impl/PermanentTokens/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public function __construct($configuration)

protected function sendAuthenticatedRequest($requestMethod, $uri, $options = [])
{
$request = new \GuzzleHttp\Psr7\Request($requestMethod, $uri, $options);
$headers = array_filter($options, function ($key) {
return $key !== "json";
}, ARRAY_FILTER_USE_KEY);

$request = new \GuzzleHttp\Psr7\Request($requestMethod, $uri, $headers);

return $this->httpClient->sendAsync(
$request,
array_merge(
Expand Down
181 changes: 181 additions & 0 deletions tests/AssetBank/AssetBankManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,101 @@ public function testGetMetapropertyOptions()
self::assertEquals($result, ['query']);
}

/**
* Tests the createMetaPropertyOption function.
*
* @covers \Bynder\Api\Impl\AssetBankManager::createMetaPropertyOption()
* @throws \Exception
*/
public function testCreateMetaPropertyOption()
{
$stub = $this->getMockBuilder('Bynder\Api\Impl\OAuth2\RequestHandler')
->disableOriginalConstructor()
->getMock();

$queryData = [
'name' => 'TEST_NAME',
'externalReference' => 'TEST_EXTERNAL_REFERENCE',
'label' => 'TEST_EXTERNAL_LABEL',
];

$stub->method('sendRequestAsync')
->with('POST', 'api/v4/metaproperties/TEST_METAPROPERTY_ID/options/', [
'form_params' => ['data' => json_encode($queryData)],
])
->willReturn([]);

$assetBankManager = new AssetBankManager($stub);
$result = $assetBankManager->createMetaPropertyOption('TEST_METAPROPERTY_ID', $queryData);

self::assertNotNull($result);
self::assertEquals($result, []);
}

/**
* Tests the modifyMetaPropertyOption function.
*
* @covers \Bynder\Api\Impl\AssetBankManager::createMetaPropertyOption()
* @throws \Exception
*/
public function testModifyMetaPropertyOption()
{
$stub = $this->getMockBuilder('Bynder\Api\Impl\OAuth2\RequestHandler')
->disableOriginalConstructor()
->getMock();

$queryData = [
'name' => 'TEST_NAME',
'externalReference' => 'TEST_EXTERNAL_REFERENCE',
'label' => 'TEST_EXTERNAL_LABEL',
];

$stub->method('sendRequestAsync')
->with('POST', 'api/v4/metaproperties/TEST_METAPROPERTY_ID/options/TEST_OPTION_ID/', [
'form_params' => ['data' => json_encode($queryData)],
])
->willReturn([]);

$assetBankManager = new AssetBankManager($stub);
$result = $assetBankManager->modifyMetaPropertyOption(
'TEST_METAPROPERTY_ID',
'TEST_OPTION_ID',
$queryData
);

self::assertNotNull($result);
self::assertEquals($result, []);
}

/**
* Test if we call getSingleMetaPropertyOptions it will use the correct params for the request and returns
* successfully.
*
* @covers \Bynder\Api\Impl\AssetBankManager::getMetapropertyOptions()
* @throws \Exception
*/
public function testGetSingleMetaPropertyOptions()
{
$stub = $this->getMockBuilder('Bynder\Api\Impl\OAuth2\RequestHandler')
->disableOriginalConstructor()
->getMock();

$optionId = '00000000-0000-0000-0000000000000000';
$query = ['page' => 1];

$stub->method('sendRequestAsync')
->with('GET', 'api/v4/metaproperties/' . $optionId . '/options/', [
'query' => $query,
])
->willReturn(['query']);

$assetBankManager = new AssetBankManager($stub);
$result = $assetBankManager->getSingleMetaPropertyOptions($optionId, $query);

self::assertNotNull($result);
self::assertEquals($result, ['query']);
}

/**
* Test if we call getMetapropetryGlobalOptionDependencies it will use the correct params for the request and
* returns successfully.
Expand Down Expand Up @@ -635,6 +730,50 @@ public function testDeleteAssetUsage()
self::assertEquals($result, array());
}

/**
* Tests the syncAssetUsage function.
*
* @covers \Bynder\Api\Impl\AssetBankManager::deleteUsage()
* @throws \Exception
*/
public function testSyncAssetUsage()
{
$stub = $this->getMockBuilder('Bynder\Api\Impl\OAuth2\RequestHandler')
->disableOriginalConstructor()
->getMock();

$queryData = [
'integration_id' => 'TEST_INTEGRATION_ID',
'uris' => ['TEST_URI_1', 'TEST_URI_2', 'TEST_URI_TO_DELETE_1'],
'usages' => [
'integration_id' => 'TEST_INTEGRATION_ID',
'asset_id' => 'TEST_ASSET_ID_1',
'timestamp' => (new \Datetime())->format('Y-m-d\TH:i:s\Z'),
'additional' => 'TEST_ADDITIONAL_DATA_1',
'uri' => 'TEST_URI_1',
],
[
'integration_id' => 'TEST_INTEGRATION_ID',
'asset_id' => 'TEST_ASSET_ID_2',
'timestamp' => (new \Datetime())->format('Y-m-d\TH:i:s\Z'),
'additional' => 'TEST_ADDITIONAL_DATA_2',
'uri' => 'TEST_URI_2',
],
];

$stub->method('sendRequestAsync')
->with('POST', 'api/media/usage/sync', [
'json' => $queryData,
])
->willReturn([]);

$assetBankManager = new AssetBankManager($stub);
$result = $assetBankManager->syncAssetUsage($queryData);

self::assertNotNull($result);
self::assertEquals($result, []);
}

/**
* Test if we call getCollections it will use the correct params for the request and returns successfully.
*
Expand Down Expand Up @@ -705,4 +844,46 @@ public function testGetCollection()
self::assertNotNull($collectionList);
self::assertEquals($collectionList, $returnedCollections);
}

/**
* Test if we call getAccount it will use the correct params for the request and returns successfully.
*
* @group collections
*
* @covers \Bynder\Api\Impl\AssetBankManager::getCollections()
* @throws \Exception
*/
public function testGetAccount()
{
$returnedAccount = [
"availableLanguages" => [
"nl_NL",
"en_GB",
"en_US",
"fr_FR",
"de_DE",
"it_IT",
"es_ES",
"pl_PL",
],
"defaultLanguage" => "en_US",
"name" => "Bynder",
"timeZone" => "Europe/Amsterdam",
"isOpenImageBank" => false,
];

$stub = $this->getMockBuilder('Bynder\Api\Impl\OAuth2\RequestHandler')
->disableOriginalConstructor()
->getMock();

$stub->method('sendRequestAsync')
->with('GET', "api/v4/account/")
->willReturn($returnedAccount);

$assetBankManager = new AssetBankManager($stub);
$account = $assetBankManager->getAccount();

self::assertNotNull($account);
self::assertEquals($account, $returnedAccount);
}
}