Skip to content

Commit

Permalink
Tags (#6)
Browse files Browse the repository at this point in the history
* wip: getting list of tags

* wip: get list of tags on account

* feature: get all tags on account
closes #3
  • Loading branch information
g105b committed Jan 31, 2024
1 parent c867838 commit febd767
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
8 changes: 7 additions & 1 deletion example/02-get-list-of-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
$config["secret_key"],
);

foreach($client->getAllTags() as $tag) {
$tagList = $client->getAllTags();
echo "There are " . count($tagList) . " tags on the account:\n";

foreach($tagList as $i => $tag) {
echo $i + 1;
echo ": ";
echo $tag->name;
echo "\n";
}
26 changes: 26 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Gt\Fetch\Http;
use Gt\Http\Response;
use Gt\Json\JsonObject;
use Gt\Json\JsonPrimitive\JsonArrayPrimitive;

readonly class Client {
const USER_AGENT = "github.com/BrightFlair/SpektrixAPI";
Expand Down Expand Up @@ -50,6 +51,31 @@ public function getCustomer(
throw new CustomerNotFoundException($email ?? $id);
}

/** @return array<Tag> */
public function getAllTags():array {
$endpoint = Endpoint::getAllTags;
$authenticatedRequest = new AuthenticatedRequest(
$this->secretKey,
$endpoint,
$this->client
);

$tagList = [];
/** @var JsonArrayPrimitive $jsonArray */
$jsonArray = $this->json($authenticatedRequest);
foreach($jsonArray->getPrimitiveValue() as $item) {
array_push(
$tagList,
new Tag(
$item->getString("id"),
$item->getString("name"),
)
);
}

return $tagList;
}

private function json(AuthenticatedRequest $authenticatedRequest):?JsonObject {
$authorizationHeader = Signature::AUTH_PREFIX
. " "
Expand Down
5 changes: 4 additions & 1 deletion src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
namespace BrightFlair\SpektrixAPI;

class Tag {

public function __construct(
public readonly string $id,
public readonly string $name,
) {}
}
62 changes: 62 additions & 0 deletions test/phpunit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Gt\Fetch\Http;
use Gt\Http\Response;
use Gt\Json\JsonObject;
use Gt\Json\JsonPrimitive\JsonArrayPrimitive;
use Gt\Promise\Promise;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -52,4 +53,65 @@ public function testGetCustomer_byId():void {
self::assertSame("[email protected]", $customer->email);
self::assertSame("07123456789", $customer->mobile);
}

public function testGetAllTags():void {
$username = "test-user";
$client = "test-client";
$secretKey = "super-secret-key";

$testCustomerId = "test-id-123";
$expectedUri = Client::BASE_URI . "/";
$expectedUri .= explode(" ", Endpoint::getAllTags->value)[1];
$expectedUri = str_replace("{client}", $client, $expectedUri);
$expectedUri = str_replace("{id}", $testCustomerId, $expectedUri);

$jsonTag1 = self::createMock(JsonObject::class);
$jsonTag1->method("getString")->willReturnMap([
["id", "id-1"],
["name", "name-1"],
]);
$jsonTag2 = self::createMock(JsonObject::class);
$jsonTag2->method("getString")->willReturnMap([
["id", "id-2"],
["name", "name-2"],
]);
$jsonTag3 = self::createMock(JsonObject::class);
$jsonTag3->method("getString")->willReturnMap([
["id", "id-3"],
["name", "name-3"],
]);

$json = self::createMock(JsonArrayPrimitive::class);
$json->method("getPrimitiveValue")->willReturn([
$jsonTag1,
$jsonTag2,
$jsonTag3,
]);

$response = self::createMock(Response::class);
$response->method("__get")->willReturnMap([
["ok", true],
["status", 200],
]);
$response->method("getStatusCode")->willReturn(200);
$response->expects(self::once())
->method("awaitJson")
->willReturn($json);

$fetchClient = self::createMock(Http::class);
$fetchClient->expects(self::once())
->method("awaitFetch")
->with($expectedUri)
->willReturn($response);

$sut = new Client($username, $client, $secretKey, $fetchClient);
$allTags = $sut->getAllTags();

self::assertCount(3, $allTags);
foreach($allTags as $i => $tag) {
$num = $i + 1;
self::assertSame("id-$num", $tag->id);
self::assertSame("name-$num", $tag->name);
}
}
}

0 comments on commit febd767

Please sign in to comment.