|
9 | 9 | use GuzzleHttp\HandlerStack;
|
10 | 10 | use GuzzleHttp\Psr7\Response;
|
11 | 11 | use PHPUnit\Framework\TestCase;
|
| 12 | +use Picqer\Financials\Exact\ApiException; |
12 | 13 | use Picqer\Financials\Exact\Connection;
|
13 | 14 |
|
14 | 15 | class ConnectionTest extends TestCase
|
@@ -50,6 +51,44 @@ public function testGetIncludesDivisionInUrlForRegularEndpoint(): void
|
50 | 51 | $this->assertStringContainsString((string) $divisionNumber, $mockHandler->getLastRequest()->getUri()->__toString());
|
51 | 52 | }
|
52 | 53 |
|
| 54 | + public function testResponseIsLoggedIfResponseIsNotValidJson(): void |
| 55 | + { |
| 56 | + $divisionNumber = random_int(0, PHP_INT_MAX); |
| 57 | + $mockHandler = new MockHandler([ |
| 58 | + new Response(200, [], 'invalid json'), |
| 59 | + ]); |
| 60 | + $handlerStack = HandlerStack::create($mockHandler); |
| 61 | + $client = new Client(['handler' => $handlerStack]); |
| 62 | + $connection = new Connection(); |
| 63 | + $connection->setClient($client); |
| 64 | + $connection->setDivision($divisionNumber); |
| 65 | + $connection->setAccessToken('1234567890'); |
| 66 | + $connection->setTokenExpires(time() + 60); |
| 67 | + |
| 68 | + $this->expectException(ApiException::class); |
| 69 | + $this->expectExceptionMessage('Json decode failed. Got response: invalid json'); |
| 70 | + |
| 71 | + $connection->get('crm/Accounts'); |
| 72 | + } |
| 73 | + |
| 74 | + public function testResponseIsLoggedIfAcquiringTokensFailed(): void |
| 75 | + { |
| 76 | + $divisionNumber = random_int(0, PHP_INT_MAX); |
| 77 | + $mockHandler = new MockHandler([ |
| 78 | + new Response(200, [], 'invalid json'), |
| 79 | + ]); |
| 80 | + $handlerStack = HandlerStack::create($mockHandler); |
| 81 | + $client = new Client(['handler' => $handlerStack]); |
| 82 | + $connection = new Connection(); |
| 83 | + $connection->setClient($client); |
| 84 | + $connection->setDivision($divisionNumber); |
| 85 | + |
| 86 | + $this->expectException(ApiException::class); |
| 87 | + $this->expectExceptionMessage('Could not acquire tokens, json decode failed. Got response: invalid json'); |
| 88 | + |
| 89 | + $connection->get('crm/Accounts'); |
| 90 | + } |
| 91 | + |
53 | 92 | public function endpointsThatDontUseDivisionInUrl(): array
|
54 | 93 | {
|
55 | 94 | return [
|
|
0 commit comments