-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
133 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
use BrightFlair\SpektrixAPI\Client; | ||
use BrightFlair\SpektrixAPI\CustomerNotFoundException; | ||
use BrightFlair\SpektrixAPI\Endpoint; | ||
use BrightFlair\SpektrixAPI\SpektrixAPIException; | ||
use BrightFlair\SpektrixAPI\TagNotFoundException; | ||
use Gt\Fetch\Http; | ||
use Gt\Http\Response; | ||
use Gt\Json\JsonObject; | ||
|
@@ -57,30 +59,6 @@ public function testGetCustomer_byId():void { | |
self::assertSame("07123456789", $customer->mobile); | ||
} | ||
|
||
public function testGetAllTags():void { | ||
$testCustomerId = "test-id-123"; | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::getAllTags->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
$expectedUri = str_replace("{id}", $testCustomerId, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
["id" => "id-1", "name" => "name-1"], | ||
["id" => "id-2", "name" => "name-2"], | ||
["id" => "id-3", "name" => "name-3"], | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $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); | ||
} | ||
} | ||
|
||
public function testCreateCustomer():void { | ||
$newCustomerId = uniqid("I-"); | ||
$newCustomerEmail = "[email protected]"; | ||
|
@@ -193,6 +171,136 @@ public function testGetCustomerByEmail_notFound():void { | |
$sut->getCustomer(email: $customerEmail); | ||
} | ||
|
||
public function testGetAllTags():void { | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::getAllTags->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
["id" => "id-1", "name" => "name-1"], | ||
["id" => "id-2", "name" => "name-2"], | ||
["id" => "id-3", "name" => "name-3"], | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $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); | ||
} | ||
} | ||
|
||
public function testGetTagById():void { | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::getAllTags->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
["id" => "id-1", "name" => "name-1"], | ||
["id" => "id-2", "name" => "name-2"], | ||
["id" => "id-3", "name" => "name-3"], | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
$tag = $sut->getTag(id: "id-2"); | ||
|
||
self::assertSame("name-2", $tag->name); | ||
} | ||
|
||
public function testGetTagById_notFound():void { | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::getAllTags->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
["id" => "id-1", "name" => "name-1"], | ||
["id" => "id-2", "name" => "name-2"], | ||
["id" => "id-3", "name" => "name-3"], | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
self::expectException(TagNotFoundException::class); | ||
$sut->getTag(id: "id-4"); | ||
} | ||
|
||
public function testGetTagsForCustomer():void { | ||
$customerId = uniqid("I-"); | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::getCustomerTags->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
$expectedUri = str_replace("{id}", $customerId, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
["id" => "id-1", "name" => "name-1"], | ||
["id" => "id-2", "name" => "name-2"], | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
$allTags = $sut->getTagsForCustomer($customerId); | ||
|
||
self::assertCount(2, $allTags); | ||
foreach($allTags as $i => $tag) { | ||
$num = $i + 1; | ||
self::assertSame("id-$num", $tag->id); | ||
self::assertSame("name-$num", $tag->name); | ||
} | ||
} | ||
|
||
public function testAddTagToCustomer():void { | ||
$customerId = uniqid("I-"); | ||
$tagId = "tag-id-1"; | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::addTagToCustomer->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
$expectedUri = str_replace("{id}", $customerId, $expectedUri); | ||
$expectedUri = str_replace("{tagId}", $tagId, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 200, [ | ||
"id" => $tagId, | ||
"name" => "Example tag name", | ||
]); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
$tag = $sut->addTagToCustomer($tagId, $customerId); | ||
self::assertSame($tagId, $tag->id); | ||
self::assertSame("Example tag name", $tag->name); | ||
} | ||
|
||
public function testAddTagToCustomer_invalid():void { | ||
$customerId = uniqid("I-"); | ||
$tagId = "tag-id-1-does-not-exist"; | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::addTagToCustomer->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
$expectedUri = str_replace("{id}", $customerId, $expectedUri); | ||
$expectedUri = str_replace("{tagId}", $tagId, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 404); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
self::expectException(SpektrixAPIException::class); | ||
self::expectExceptionMessage("Error adding tag ID $tagId to customer $customerId"); | ||
$sut->addTagToCustomer($tagId, $customerId); | ||
} | ||
|
||
public function testRemoveTagFromCustomer():void { | ||
$customerId = uniqid("I-"); | ||
$tagId = "tag-id-1"; | ||
$expectedUri = Client::BASE_URI . "/"; | ||
$expectedUri .= explode(" ", Endpoint::removeTagFromCustomer->value)[1]; | ||
$expectedUri = str_replace("{client}", self::TEST_CLIENT, $expectedUri); | ||
$expectedUri = str_replace("{id}", $customerId, $expectedUri); | ||
$expectedUri = str_replace("{tagId}", $tagId, $expectedUri); | ||
|
||
$fetchClient = self::getFetchClient($expectedUri, 204); | ||
|
||
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient); | ||
$sut->removeTagFromCustomer($tagId, $customerId); | ||
} | ||
|
||
private function getFetchClient( | ||
string $uri, | ||
int $status, | ||
|