Skip to content

Commit ceecb7f

Browse files
authored
chore: improve cleanup handling (#73)
* chore: improve cleanup handling * Fix styling * cleanup composer * add test * update tests * ignore coverage
1 parent 99378cf commit ceecb7f

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

.github/workflows/run-tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
php: [8.3, 8.2, 8.1]
15+
php: [8.3, 8.2, 8.1, 8.0]
1616
laravel: ["^11.0", "^10.0", "^9.0", "^8.0"]
1717
dependency-version: [prefer-lowest, prefer-stable]
1818
exclude:
1919
- laravel: "^11.0"
2020
php: 8.1
21+
- laravel: "^11.0"
22+
php: 8.0
23+
- laravel: "^10.0"
24+
php: 8.0
2125

2226
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}
2327

composer.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
}
3434
},
3535
"scripts": {
36-
"analyse": "vendor/bin/phpstan analyse",
37-
"baseline": "vendor/bin/phpstan analyse --generate-baseline",
3836
"test": "vendor/bin/pest"
3937
},
4038
"config": {

src/Client.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,24 @@ public function make($service)
9898

9999
$service = 'Google\\Service\\'.ucfirst(str_replace('_', '', $service));
100100

101-
if (class_exists($service)) {
102-
return new $service($this->client);
101+
try {
102+
if (class_exists($service)) {
103+
return new $service($this->client);
104+
}
105+
// catch any errors thrown when fetching the service
106+
// this can be caused when the service was removed
107+
// but the reference still exists in the auto loader
108+
// @codeCoverageIgnoreStart
109+
} catch (\ErrorException $e) {
110+
if (str_contains($e->getMessage(), 'No such file or directory')) {
111+
UnknownServiceException::throwForService($service, 0, $e);
112+
}
113+
114+
throw $e;
115+
// @codeCoverageIgnoreEnd
103116
}
104117

105-
throw new UnknownServiceException($service);
118+
UnknownServiceException::throwForService($service);
106119
}
107120

108121
/**

src/Exceptions/UnknownServiceException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
namespace PulkitJalan\Google\Exceptions;
44

5+
use Throwable;
6+
57
class UnknownServiceException extends \Exception
68
{
7-
//
9+
public static function throwForService(string $service, int $code = 0, ?Throwable $previous = null)
10+
{
11+
throw new static("Unknown service [$service].", $code, $previous);
12+
}
813
}

tests/Unit/ClientTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@
2929
],
3030
]);
3131

32-
expect('test')->toEqual($client->getClient()->getConfig('subject'));
32+
expect($client->getClient()->getConfig('subject'))->toEqual('test');
33+
});
34+
35+
test('client fallback to use application default credentials', function () {
36+
$client = new Client([
37+
'service' => [
38+
'enable' => true,
39+
],
40+
]);
41+
42+
expect($client->getClient()->isUsingApplicationDefaultCredentials())->toBeTrue();
3343
});
3444

3545
test('client sets prompt or approval_prompt', function () {
@@ -39,10 +49,10 @@
3949
]);
4050

4151
// default value should be empty
42-
expect('auto')->toEqual($client->getClient()->getConfig('prompt'));
52+
expect($client->getClient()->getConfig('prompt'))->toEqual('auto');
4353

4454
// default value should be auto, since prompt is set, it should be ignored
45-
expect('auto')->toEqual($client->getClient()->getConfig('approval_prompt'));
55+
expect($client->getClient()->getConfig('approval_prompt'))->toEqual('auto');
4656
});
4757

4858
test('client sets approval_prompt', function () {
@@ -51,10 +61,10 @@
5161
]);
5262

5363
// default value should be empty
54-
expect('')->toEqual($client->getClient()->getConfig('prompt'));
64+
expect($client->getClient()->getConfig('prompt'))->toBeEmpty();
5565

5666
// default value should be auto
57-
expect('consent')->toEqual($client->getClient()->getConfig('approval_prompt'));
67+
expect($client->getClient()->getConfig('approval_prompt'))->toEqual('consent');
5868
});
5969

6070
test('service make', function () {
@@ -68,18 +78,14 @@
6878
test('service make exception', function () {
6979
$client = new Client();
7080

71-
$this->expectException(UnknownServiceException::class);
72-
7381
$client->make('storag');
74-
});
82+
})->throws(UnknownServiceException::class);
7583

7684
test('magic method exception', function () {
7785
$client = new Client();
7886

79-
$this->expectException(BadMethodCallException::class);
80-
8187
$client->getAuthTest();
82-
});
88+
})->throws(BadMethodCallException::class);
8389

8490
test('no credentials', function () {
8591
$client = new Client();

0 commit comments

Comments
 (0)