-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClientTest.php
117 lines (102 loc) · 3.38 KB
/
ClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace BrightFlair\SpektrixAPI\Test;
use BrightFlair\SpektrixAPI\Client;
use BrightFlair\SpektrixAPI\Endpoint;
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;
class ClientTest extends TestCase {
public function testGetCustomer_byId():void {
$username = "test-user";
$client = "test-client";
$secretKey = "super-secret-key";
$testCustomerId = "test-id-123";
$expectedUri = Client::BASE_URI . "/";
$expectedUri .= explode(" ", Endpoint::getCustomerById->value)[1];
$expectedUri = str_replace("{client}", $client, $expectedUri);
$expectedUri = str_replace("{id}", $testCustomerId, $expectedUri);
$json = self::createMock(JsonObject::class);
$json->method("getString")->willReturnMap([
["id", "customer-id"],
["email", "[email protected]"],
["firstName", "Test"],
["lastName", "Tester"],
["mobile", "07123456789"],
]);
$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);
$customer = $sut->getCustomer(id: $testCustomerId);
self::assertSame("customer-id", $customer->id);
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);
}
}
}