-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoopsClientTest.php
More file actions
111 lines (94 loc) · 2.94 KB
/
Copy pathLoopsClientTest.php
File metadata and controls
111 lines (94 loc) · 2.94 KB
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
<?php
namespace Loops\Tests;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Loops\LoopsClient;
use Loops\Exceptions\APIError;
use Loops\Exceptions\RateLimitExceededError;
use PHPUnit\Framework\TestCase;
class LoopsClientTest extends TestCase
{
private LoopsClient $client;
private Client $mockHttpClient;
protected function setUp(): void
{
// Create a mock HTTP client
$this->mockHttpClient = $this->createMock(Client::class);
// Create the LoopsClient with the mock
$this->client = new LoopsClient('test_api_key');
$this->client->setHttpClient($this->mockHttpClient);
}
public function testSuccessfulApiCall(): void
{
// Mock a successful API response
$expectedResponse = ['success' => true, 'id' => '1234567890'];
$responseBody = json_encode($expectedResponse);
$this->mockHttpClient
->expects($this->once())
->method('post')
->with(
'v1/contacts/create',
$this->callback(function ($options) {
// The json option is already an array, no need to decode
$payload = $options['json'];
&& $payload['name'] === 'Test User';
})
)
->willReturn(new Response(
status: 200,
headers: ['Content-Type' => 'application/json'],
body: $responseBody
));
// Make the API call
$result = $this->client->contacts->create(
email: '[email protected]',
properties: ['name' => 'Test User']
);
// Assert the response
$this->assertEquals($expectedResponse, $result);
}
public function testApiErrorHandling(): void
{
// Mock an API error response
$errorResponse = ['error' => 'Invalid request', 'message' => 'Bad request'];
$responseBody = json_encode($errorResponse);
$this->mockHttpClient
->expects($this->once())
->method('post')
->willReturn(new Response(
status: 400,
headers: ['Content-Type' => 'application/json'],
body: $responseBody
));
// Assert that the API error is thrown
$this->expectException(APIError::class);
$this->expectExceptionCode(400);
$this->client->contacts->create(
email: '[email protected]',
properties: ['name' => 'Test User']
);
}
public function testRateLimitHandling(): void
{
// Mock a rate limit response
$this->mockHttpClient
->expects($this->once())
->method('post')
->willReturn(new Response(
status: 429,
headers: [
'Content-Type' => 'application/json',
'x-ratelimit-limit' => ['10'],
'x-ratelimit-remaining' => ['0']
],
body: json_encode(['error' => 'Rate limit exceeded'])
));
// Assert that the rate limit error is thrown
$this->expectException(RateLimitExceededError::class);
$this->client->contacts->create(
email: '[email protected]',
properties: ['name' => 'Test User']
);
}
}