diff --git a/test/phpunit/ClientTest.php b/test/phpunit/ClientTest.php index 0d49822..4cb287d 100644 --- a/test/phpunit/ClientTest.php +++ b/test/phpunit/ClientTest.php @@ -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; @@ -52,4 +53,65 @@ public function testGetCustomer_byId():void { self::assertSame("customer@example.com", $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); + } + } }