|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Facades\Http; |
| 4 | +use STS\HubSpot\Api\Client; |
| 5 | +use STS\HubSpot\Exceptions\HubSpotApiException; |
| 6 | +use STS\HubSpot\Exceptions\InvalidRequestException; |
| 7 | +use STS\HubSpot\Exceptions\NotFoundException; |
| 8 | +use STS\HubSpot\Exceptions\RateLimitException; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + config()->set('hubspot.http.timeout', 10); |
| 12 | + config()->set('hubspot.http.connect_timeout', 10); |
| 13 | +}); |
| 14 | + |
| 15 | +test('throws InvalidRequestException for 400 status', function () { |
| 16 | + Http::fake([ |
| 17 | + '*' => Http::response(['message' => 'Bad request'], 400), |
| 18 | + ]); |
| 19 | + |
| 20 | + $client = new Client('test-token'); |
| 21 | + |
| 22 | + expect(fn() => $client->get('/v3/objects/contacts')) |
| 23 | + ->toThrow(InvalidRequestException::class); |
| 24 | +}); |
| 25 | + |
| 26 | +test('throws NotFoundException for 404 status', function () { |
| 27 | + Http::fake([ |
| 28 | + '*' => Http::response(['message' => 'Not found'], 404), |
| 29 | + ]); |
| 30 | + |
| 31 | + $client = new Client('test-token'); |
| 32 | + |
| 33 | + expect(fn() => $client->get('/v3/objects/contacts/123')) |
| 34 | + ->toThrow(NotFoundException::class); |
| 35 | +}); |
| 36 | + |
| 37 | +test('throws InvalidRequestException for 409 status', function () { |
| 38 | + Http::fake([ |
| 39 | + '*' => Http::response(['message' => 'Conflict'], 409), |
| 40 | + ]); |
| 41 | + |
| 42 | + $client = new Client('test-token'); |
| 43 | + |
| 44 | + expect(fn() => $client->post('/v3/objects/contacts')) |
| 45 | + ->toThrow(InvalidRequestException::class); |
| 46 | +}); |
| 47 | + |
| 48 | +test('throws RateLimitException for rate limit response', function () { |
| 49 | + Http::fake([ |
| 50 | + '*' => Http::response(['category' => 'RATE_LIMITS', 'message' => 'Rate limit exceeded'], 429), |
| 51 | + ]); |
| 52 | + |
| 53 | + $client = new Client('test-token'); |
| 54 | + |
| 55 | + expect(fn() => $client->get('/v3/objects/contacts')) |
| 56 | + ->toThrow(RateLimitException::class); |
| 57 | +}); |
| 58 | + |
| 59 | +test('throws HubSpotApiException for 401 unauthorized status', function () { |
| 60 | + Http::fake([ |
| 61 | + '*' => Http::response(['message' => 'Unauthorized'], 401), |
| 62 | + ]); |
| 63 | + |
| 64 | + $client = new Client('test-token'); |
| 65 | + |
| 66 | + expect(fn() => $client->get('/v3/objects/contacts')) |
| 67 | + ->toThrow(HubSpotApiException::class); |
| 68 | +}); |
| 69 | + |
| 70 | +test('throws HubSpotApiException for 403 forbidden status', function () { |
| 71 | + Http::fake([ |
| 72 | + '*' => Http::response(['message' => 'Forbidden'], 403), |
| 73 | + ]); |
| 74 | + |
| 75 | + $client = new Client('test-token'); |
| 76 | + |
| 77 | + expect(fn() => $client->get('/v3/objects/contacts')) |
| 78 | + ->toThrow(HubSpotApiException::class); |
| 79 | +}); |
| 80 | + |
| 81 | +test('throws HubSpotApiException for 422 unprocessable entity status', function () { |
| 82 | + Http::fake([ |
| 83 | + '*' => Http::response(['message' => 'Unprocessable Entity'], 422), |
| 84 | + ]); |
| 85 | + |
| 86 | + $client = new Client('test-token'); |
| 87 | + |
| 88 | + expect(fn() => $client->post('/v3/objects/contacts')) |
| 89 | + ->toThrow(HubSpotApiException::class); |
| 90 | +}); |
| 91 | + |
| 92 | +test('throws HubSpotApiException for 500 server error status', function () { |
| 93 | + Http::fake([ |
| 94 | + '*' => Http::response(['message' => 'Internal Server Error'], 500), |
| 95 | + ]); |
| 96 | + |
| 97 | + $client = new Client('test-token'); |
| 98 | + |
| 99 | + expect(fn() => $client->get('/v3/objects/contacts')) |
| 100 | + ->toThrow(HubSpotApiException::class); |
| 101 | +}); |
| 102 | + |
| 103 | +test('HubSpotApiException contains response object', function () { |
| 104 | + Http::fake([ |
| 105 | + '*' => Http::response(['message' => 'Unauthorized', 'correlationId' => '123'], 401), |
| 106 | + ]); |
| 107 | + |
| 108 | + $client = new Client('test-token'); |
| 109 | + |
| 110 | + try { |
| 111 | + $client->get('/v3/objects/contacts'); |
| 112 | + $this->fail('Expected HubSpotApiException to be thrown'); |
| 113 | + } catch (HubSpotApiException $e) { |
| 114 | + expect($e->response)->not->toBeNull(); |
| 115 | + expect($e->response->status())->toBe(401); |
| 116 | + expect($e->response->json('correlationId'))->toBe('123'); |
| 117 | + } |
| 118 | +}); |
| 119 | + |
| 120 | +test('HubSpotApiException message includes status code when no message in response', function () { |
| 121 | + Http::fake([ |
| 122 | + '*' => Http::response([], 401), |
| 123 | + ]); |
| 124 | + |
| 125 | + $client = new Client('test-token'); |
| 126 | + |
| 127 | + try { |
| 128 | + $client->get('/v3/objects/contacts'); |
| 129 | + $this->fail('Expected HubSpotApiException to be thrown'); |
| 130 | + } catch (HubSpotApiException $e) { |
| 131 | + expect($e->getMessage())->toContain('401'); |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +test('HubSpotApiException uses response message when available', function () { |
| 136 | + Http::fake([ |
| 137 | + '*' => Http::response(['message' => 'Missing required scopes'], 403), |
| 138 | + ]); |
| 139 | + |
| 140 | + $client = new Client('test-token'); |
| 141 | + |
| 142 | + try { |
| 143 | + $client->get('/v3/objects/contacts'); |
| 144 | + $this->fail('Expected HubSpotApiException to be thrown'); |
| 145 | + } catch (HubSpotApiException $e) { |
| 146 | + expect($e->getMessage())->toBe('Missing required scopes'); |
| 147 | + } |
| 148 | +}); |
| 149 | + |
| 150 | +test('successful request does not throw exception', function () { |
| 151 | + Http::fake([ |
| 152 | + '*' => Http::response(['results' => []], 200), |
| 153 | + ]); |
| 154 | + |
| 155 | + $client = new Client('test-token'); |
| 156 | + $response = $client->get('/v3/objects/contacts'); |
| 157 | + |
| 158 | + expect($response->successful())->toBeTrue(); |
| 159 | + expect($response->json('results'))->toBe([]); |
| 160 | +}); |
0 commit comments