Skip to content

Commit

Permalink
Creates tag endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
typhonius committed Dec 24, 2019
1 parent 764c75b commit d267f8d
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/Endpoints/Applications.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use AcquiaCloudApi\Connector\ClientInterface;
use AcquiaCloudApi\Response\ApplicationResponse;
use AcquiaCloudApi\Response\ApplicationsResponse;
use AcquiaCloudApi\Response\TagResponse;
use AcquiaCloudApi\Response\TagsResponse;
use AcquiaCloudApi\Response\OperationResponse;

/**
Expand Down Expand Up @@ -77,4 +79,66 @@ public function rename($applicationUuid, $name)
)
);
}

/**
* Returns a list of application tags associated with this application.
*
* @param string $applicationUuid
* @return TagsResponse
*/
public function getAllTags($applicationUuid)
{

return new TagsResponse(
$this->client->request(
'get',
"/applications/${applicationUuid}/tags"
)
);
}

/**
* Creates a new application tag.
*
* @param string $applicationUuid
* @param string $name
* @param string $color
* @return OperationResponse
*/
public function createTag($applicationUuid, $name, $color)
{

$options = [
'form_params' => [
'name' => $name,
'color' => $color,
],
];

return new OperationResponse(
$this->client->request(
'post',
"/applications/${applicationUuid}/tags",
$options
)
);
}

/**
* Deletes an application tag.
*
* @param string $applicationUuid
* @param string $tagName
* @return OperationResponse
*/
public function deleteTag($applicationUuid, $tagName)
{

return new OperationResponse(
$this->client->request(
'delete',
"/applications/${applicationUuid}/tags/${tagName}"
)
);
}
}
2 changes: 1 addition & 1 deletion src/Exception/ApiErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __toString()

public function setError($object)
{
if (is_array($object->message)) {
if (is_array($object->message) || is_object($object->message)) {
$output = '';
foreach ($object->message as $message) {
$output .= $message . PHP_EOL;
Expand Down
28 changes: 28 additions & 0 deletions src/Response/TagResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AcquiaCloudApi\Response;

/**
* Class TagResponse
* @package AcquiaCloudApi\Response
*/
class TagResponse
{

public $name;
public $color;
public $context;
public $links;

/**
* TagResponse constructor.
* @param object $tag
*/
public function __construct($tag)
{
$this->name = $tag->name;
$this->color = $tag->color;
$this->context = $tag->context;
$this->links = $tag->_links;
}
}
22 changes: 22 additions & 0 deletions src/Response/TagsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace AcquiaCloudApi\Response;

/**
* Class TagsResponse
* @package AcquiaCloudApi\Response
*/
class TagsResponse extends \ArrayObject
{

/**
* TagsResponse constructor.
* @param array $tags
*/
public function __construct($tags)
{
parent::__construct(array_map(function ($tag) {
return new TagResponse($tag);
}, $tags), self::ARRAY_AS_PROPS);
}
}
56 changes: 56 additions & 0 deletions tests/Endpoints/ApplicationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class ApplicationsTest extends CloudApiTestCase
'links'
];

protected $tagProperties = [
'name',
'color',
'context',
'links'
];

public function testGetApplications()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/Applications/getAllApplications.json');
Expand Down Expand Up @@ -71,4 +78,53 @@ public function testRenameApplication()

$this->assertEquals('Application renamed.', $result->message);
}

public function testGetAllTags()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/Applications/getAllTags.json');
$client = $this->getMockClient($response);

/** @var \AcquiaCloudApi\CloudApi\ClientInterface $client */
$application = new Applications($client);
$result = $application->getAllTags('8ff6c046-ec64-4ce4-bea6-27845ec18600');

$this->assertInstanceOf('\ArrayObject', $result);
$this->assertInstanceOf('\AcquiaCloudApi\Response\TagsResponse', $result);

foreach ($result as $record) {
$this->assertInstanceOf('\AcquiaCloudApi\Response\TagResponse', $record);

foreach ($this->tagProperties as $property) {
$this->assertObjectHasAttribute($property, $record);
}
}
}

public function testCreateTag()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/Applications/createTag.json');
$client = $this->getMockClient($response);

/** @var \AcquiaCloudApi\CloudApi\ClientInterface $client */
$application = new Applications($client);
$result = $application->createTag('8ff6c046-ec64-4ce4-bea6-27845ec18600', "deloitte", "orange");

$this->assertInstanceOf('\AcquiaCloudApi\Response\OperationResponse', $result);

$this->assertEquals('The tag has been added to the application.', $result->message);
}

public function testDeleteTag()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/Applications/deleteTag.json');
$client = $this->getMockClient($response);

/** @var \AcquiaCloudApi\CloudApi\ClientInterface $client */
$application = new Applications($client);
$result = $application->deleteTag('8ff6c046-ec64-4ce4-bea6-27845ec18600', "deloitte");

$this->assertInstanceOf('\AcquiaCloudApi\Response\OperationResponse', $result);

$this->assertEquals('The tag has been removed from the application.', $result->message);
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/Endpoints/Applications/createTag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"message": "The tag has been added to the application.",
"_links": {
"self": {
"href": "https://cloud.acquia.com/api/applications/0d9fc48f-9871-41d9-8b41-0f815e6039eb/tags"
},
"parent": {
"href": "https://cloud.acquia.com/api/applications/0d9fc48f-9871-41d9-8b41-0f815e6039eb"
},
"notification": {
"href": "https://cloud.acquia.com/api/notifications/773fca86-9d87-45dc-9220-9ba38306bbb1"
}
}
}
14 changes: 14 additions & 0 deletions tests/Fixtures/Endpoints/Applications/deleteTag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"message": "The tag has been removed from the application.",
"_links": {
"self": {
"href": "https://cloud.acquia.com/api/applications/0d9fc48f-9871-41d9-8b41-0f815e6039eb/tags/test_tag"
},
"parent": {
"href": "https://cloud.acquia.com/api/applications/0d9fc48f-9871-41d9-8b41-0f815e6039eb/tags"
},
"notification": {
"href": "https://cloud.acquia.com/api/notifications/37cdf705-9774-4526-9bf0-9feb279630ee"
}
}
}
60 changes: 60 additions & 0 deletions tests/Fixtures/Endpoints/Applications/getAllTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"total": 1,
"pagination": {
"total": 1,
"limit": 1,
"offset": 0
},
"_links": {
"self": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a/tags"
},
"parent": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a"
},
"limit": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a/tags{?limit}",
"templated": true
},
"offset": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a/tags{?offset}",
"templated": true
},
"sort": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a/tags{?sort}",
"templated": true
},
"filter": {
"href": "https://cloud.acquia.com/api/applications/185f07c7-9c4f-407b-8968-67892ebcb38a/tags{?filter}",
"templated": true
}
},
"_embedded": {
"items": [
{
"name": "Dev",
"color": "orange",
"context": {
"organization": {
"uuids": [
"2375e327-3fff-11e3-9170-12313920a02c"
]
},
"application": {
"uuids": [
"185f07c7-9c4f-407b-8968-67892ebcb38a"
]
}
},
"_links": {
"self": {
"href": "https://cloud.acquia.com/api/applications/2375e327-3fff-11e3-9170-12313920a02c/tags/dev"
},
"parent": {
"href": "https://cloud.acquia.com/api/applications/2375e327-3fff-11e3-9170-12313920a02c/tags"
}
}
}
]
}
}

0 comments on commit d267f8d

Please sign in to comment.