Skip to content

Commit ffe793e

Browse files
authored
Merge pull request #6 from atymic/fix/exc-res
fix: exception handling + wrap guzzle
2 parents 77c3108 + e10d3a8 commit ffe793e

File tree

8 files changed

+148
-17
lines changed

8 files changed

+148
-17
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
composer.phar
22
composer.lock
33
vendor
4-
.DS_Store
4+
.DS_Store
5+
.phpunit.result.cache
6+
build

composer.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
}
1111
],
1212
"require": {
13-
"php": ">=7.3",
13+
"php": ">=7.4",
1414
"illuminate/support": "^6|^7|^8|^9|^10",
15-
"guzzlehttp/guzzle": "^7.4.4"
15+
"guzzlehttp/guzzle": "^6.3.1|^7.0.1"
1616
},
1717
"require-dev": {
18-
"orchestra/testbench": "^5.0",
19-
"phpunit/phpunit": "^8.5|^9.0"
18+
"phpunit/phpunit": "^8.5|^9.0",
19+
"nunomaduro/collision": "^5.3",
20+
"orchestra/testbench": "^6.15",
21+
"pestphp/pest": "^1.18",
22+
"pestphp/pest-plugin-laravel": "^1.1"
2023
},
2124
"autoload": {
2225
"psr-4": {
@@ -28,6 +31,10 @@
2831
"CodeGreenCreative\\Freshworks\\Tests\\": "tests"
2932
}
3033
},
34+
"scripts": {
35+
"test": "./vendor/bin/pest --no-coverage",
36+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
37+
},
3138
"extra": {
3239
"branch-alias": {
3340
"dev-master": "1.x-dev"

phpunit.xml.dist

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
bootstrap="vendor/autoload.php"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
executionOrder="random"
15+
failOnWarning="true"
16+
failOnRisky="true"
17+
failOnEmptyTestSuite="true"
18+
beStrictAboutOutputDuringTests="true"
19+
verbose="true"
20+
>
21+
<testsuites>
22+
<testsuite name="Laravel Freshworks Test Suite">
23+
<directory>tests</directory>
24+
</testsuite>
25+
</testsuites>
26+
<coverage>
27+
<include>
28+
<directory suffix=".php">./src</directory>
29+
</include>
30+
<report>
31+
<html outputDirectory="build/coverage"/>
32+
<text outputFile="build/coverage.txt"/>
33+
<clover outputFile="build/logs/clover.xml"/>
34+
</report>
35+
</coverage>
36+
<logging>
37+
<junit outputFile="build/report.junit.xml"/>
38+
</logging>
39+
</phpunit>

src/Client.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
namespace CodeGreenCreative\Freshworks;
44

5-
use Psr\Http\Message\ResponseInterface;
5+
use CodeGreenCreative\Freshworks\Exceptions\FreshworksException;
6+
use Exception;
7+
use GuzzleHttp\Client as GuzzleClient;
8+
use GuzzleHttp\Exception\RequestException;
9+
use GuzzleHttp\Psr7\Response;
610

7-
class Client extends \GuzzleHttp\Client
11+
class Client
812
{
13+
/** @var GuzzleClient */
14+
protected $client;
15+
/** @var Response */
916
private $response;
1017

1118
/**
@@ -17,28 +24,34 @@ public function __construct($type = 'api')
1724
'Accept' => 'application/json',
1825
'Content-Type' => 'application/json',
1926
];
20-
if ($type == 'api') {
27+
28+
if ($type === 'api') {
2129
$headers['Authorization'] = "Token token=" . config('freshworks.api_key');
2230
}
23-
parent::__construct([
31+
32+
$this->client = new GuzzleClient([
2433
'base_uri' => sprintf('https://%s.myfreshworks.com/crm/sales/%s/', config('freshworks.domain'), $type),
25-
'headers' => $headers
34+
'headers' => $headers,
2635
]);
2736
}
2837

2938
/**
3039
* Go perform the request
31-
* @param string $method
32-
* @param string $uri
33-
* @param array $options
40+
*
41+
* @param string $method
42+
* @param string $uri
43+
* @param array $options
44+
*
3445
* @return \CodeGreenCreative\Freshworks\Client
3546
*/
36-
public function go(string $method, $uri = '', array $options = []): Object
47+
public function go(string $method, string $uri = '', array $options = []): object
3748
{
3849
try {
39-
$this->response = parent::request($method, $uri, $options);
40-
} catch (\Exception $e) {
41-
throw new Exceptions\FreshworksException($e->getResponse()->getBody()->getContents(), 1);
50+
$this->response = $this->client->request($method, $uri, $options);
51+
} catch (RequestException $e) {
52+
throw FreshworksException::fromGuzzleException($e);
53+
} catch (Exception $e) {
54+
throw new Exceptions\FreshworksException($e->getMessage(), $e->getCode());
4255
}
4356

4457
return $this->toObject();

src/Exceptions/FreshworksException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
namespace CodeGreenCreative\Freshworks\Exceptions;
44

5+
use GuzzleHttp\Exception\RequestException;
6+
use GuzzleHttp\Psr7\Response;
7+
58
class FreshworksException extends \Exception
69
{
10+
/** @var ?Response */
11+
protected ?Response $response;
12+
13+
public static function fromGuzzleException(RequestException $requestException): self
14+
{
15+
$response = $requestException->getResponse();
16+
17+
if (!$response) {
18+
return new self($requestException->getMessage(), $requestException->getCode());
19+
}
20+
21+
$exception = new self(trim(sprintf("%s\n\%s", $requestException->getMessage(), (string) $response->getBody())), $response->getStatusCode());
22+
$exception->response = $response;
23+
24+
return $exception;
25+
}
26+
27+
public function getResponse(): ?Response
28+
{
29+
return $this->response;
30+
}
731
}

tests/ExceptionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
it('throws non guzzle exception as freshworks exception', function () {
4+
\CodeGreenCreative\Freshworks\FreshworksFacade::contacts()->all(1);
5+
})->throws(\CodeGreenCreative\Freshworks\Exceptions\FreshworksException::class);
6+
7+
it('throws guzzle exception as freshworks exception with 404 response', function () {
8+
config()->set('freshworks.domain', 'testing');
9+
\CodeGreenCreative\Freshworks\FreshworksFacade::contacts()->all(1);
10+
})->throws(\CodeGreenCreative\Freshworks\Exceptions\FreshworksException::class, 'Client error: `GET https://testing.myfreshworks.com/crm/sales/api/contacts/view/1` resulted in a `404 Not Found` response');
11+
12+
it('throws guzzle exception as freshworks exception with 401 response', function () {
13+
config()->set('freshworks.domain', 'edgility-au');
14+
\CodeGreenCreative\Freshworks\FreshworksFacade::contacts()->all(1);
15+
})->throws(\CodeGreenCreative\Freshworks\Exceptions\FreshworksException::class, "Client error: `GET https://edgility-au.myfreshworks.com/crm/sales/api/contacts/view/1` resulted in a `401 Unauthorized` response:\n{\"login\":\"failed\",\"message\":null}");

tests/Pest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use CodeGreenCreative\Freshworks\Tests\TestCase;
4+
5+
uses(TestCase::class)->in(__DIR__);

tests/TestCase.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CodeGreenCreative\Freshworks\Tests;
4+
5+
use CodeGreenCreative\Freshworks\FreshworksServiceProvider;
6+
use Orchestra\Testbench\TestCase as Orchestra;
7+
8+
class TestCase extends Orchestra
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
}
14+
15+
protected function getPackageProviders($app)
16+
{
17+
return [
18+
FreshworksServiceProvider::class,
19+
];
20+
}
21+
22+
public function getEnvironmentSetUp($app)
23+
{
24+
config()->set('database.default', 'testing');
25+
}
26+
}

0 commit comments

Comments
 (0)