Skip to content

Commit aca7f53

Browse files
authored
Merge pull request #4 from stefna/fix-404-return-types/main
Fix returns when 404 occurs
2 parents e21dd0a + d224042 commit aca7f53

File tree

21 files changed

+134
-51
lines changed

21 files changed

+134
-51
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,28 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
php-version:
19-
- "7.4"
2019
- "8.0"
20+
- "8.1"
21+
- "8.2"
2122
experimental:
2223
- false
2324
include:
24-
- php-version: "8.1"
25+
- php-version: "8.3"
2526
composer-options: "--ignore-platform-reqs"
2627
experimental: true
2728

2829
steps:
2930
- name: "Checkout"
30-
uses: "actions/checkout@v2"
31+
uses: "actions/checkout@v4"
3132

3233
- name: "Install PHP"
3334
uses: "shivammathur/setup-php@v2"
3435
with:
3536
coverage: "none"
3637
php-version: "${{ matrix.php-version }}"
3738

38-
- name: Get composer cache directory
39-
id: composercache
40-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
41-
42-
- name: Cache dependencies
43-
uses: actions/cache@v2
44-
with:
45-
path: ${{ steps.composercache.outputs.dir }}
46-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
47-
restore-keys: ${{ runner.os }}-composer-
48-
4939
- name: "Install latest dependencies"
5040
run: "composer update ${{ env.COMPOSER_FLAGS }} ${{ matrix.composer-options }}"
5141

5242
- name: "Run tests"
53-
run: ./vendor/bin/phpunit -c phpunit.xml.dist
43+
run: ./vendor/bin/phpunit -c phpunit.xml.dist

.github/workflows/phpstan.yml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,20 @@ jobs:
1616
strategy:
1717
matrix:
1818
php-version:
19-
- "7.4"
19+
- "8.0"
2020

2121
steps:
2222
- name: "Checkout"
23-
uses: "actions/checkout@v2"
23+
uses: "actions/checkout@v4"
2424

2525
- name: "Install PHP"
2626
uses: "shivammathur/setup-php@v2"
2727
with:
2828
coverage: "none"
2929
php-version: "${{ matrix.php-version }}"
3030

31-
- name: Get composer cache directory
32-
id: composercache
33-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
34-
35-
- name: Cache dependencies
36-
uses: actions/cache@v2
37-
with:
38-
path: ${{ steps.composercache.outputs.dir }}
39-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
40-
restore-keys: ${{ runner.os }}-composer-
41-
4231
- name: "Install latest dependencies"
4332
run: "composer update ${{ env.COMPOSER_FLAGS }}"
4433

4534
- name: Run PHPStan
46-
run: vendor/bin/phpstan analyse
35+
run: vendor/bin/phpstan analyse

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"php-http/curl-client": "^1.0",
1616
"php-http/message": "^1.0",
1717
"guzzlehttp/psr7": "^1.0",
18-
"monolog/monolog": "^1.0",
18+
"monolog/monolog": "^2.0",
1919
"roave/security-advisories": "dev-latest",
20-
"phpunit/phpunit": "^9.0",
20+
"phpunit/phpunit": "^9.0 || ^10.0",
2121
"phpstan/phpstan": "^1.0",
2222
"stefna/codestyle": "^1.0"
2323
},
@@ -37,5 +37,10 @@
3737
"stan": "./vendor/bin/phpstan",
3838
"codestyle": "./vendor/bin/phpcs -n --standard=vendor/stefna/codestyle/library.xml src/",
3939
"codestyle-fix": "./vendor/bin/phpcbf --colors --standard=vendor/stefna/codestyle/library.xml src/"
40+
},
41+
"config": {
42+
"allow-plugins": {
43+
"php-http/discovery": false
44+
}
4045
}
4146
}

src/Api/RestApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected function doUpdate(string $id, $data, string $className): AbstractData
120120
$path = $this->getPath(self::ACTION_UPDATE, [$id]);
121121
$retData = $this->client->patch($path, $data);
122122

123-
/** @var T $className */
123+
/** @var T */
124124
return new $className($retData);
125125
}
126126

src/Client.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Stefna\Mailchimp\Api\Campaigns\Campaigns as CampaignsApi;
1414
use Stefna\Mailchimp\Api\Lists\Lists as ListsApi;
1515
use Stefna\Mailchimp\Api\Templates\Templates;
16+
use Stefna\Mailchimp\Exceptions\NotFoundException;
1617
use function GuzzleHttp\Psr7\str;
1718

1819
class Client
@@ -80,7 +81,7 @@ public function get(string $path, array $args = []): array
8081
'GET',
8182
$this->createUrl($path, $args),
8283
$this->getDefaultHeaders()
83-
));
84+
)) ?? [];
8485
}
8586

8687
/**
@@ -116,13 +117,17 @@ public function post(string $path, array $data = [])
116117
*/
117118
public function put(string $path, array $data = []): array
118119
{
119-
/** @var array<string, mixed> */
120-
return $this->request($this->messageFactory->createRequest(
120+
$ret = $this->request($this->messageFactory->createRequest(
121121
'PUT',
122122
$this->createUrl($path),
123123
$this->getDefaultHeaders(),
124124
(string)json_encode($data)
125125
));
126+
if ($ret === null) {
127+
throw new NotFoundException('Put item not found: ' . $path);
128+
}
129+
/** @var array<string, mixed> */
130+
return $ret;
126131
}
127132

128133
/**
@@ -132,12 +137,16 @@ public function put(string $path, array $data = []): array
132137
*/
133138
public function patch(string $path, array $data = [])
134139
{
135-
return $this->request($this->messageFactory->createRequest(
140+
$ret = $this->request($this->messageFactory->createRequest(
136141
'PATCH',
137142
$this->createUrl($path),
138143
$this->getDefaultHeaders(),
139144
(string)json_encode($data)
140145
));
146+
if ($ret === null) {
147+
throw new NotFoundException('Patch item not found: ' . $path);
148+
}
149+
return $ret;
141150
}
142151

143152
/**
@@ -207,22 +216,21 @@ public function response(ResponseInterface $response)
207216
'status' => $status,
208217
]);
209218
}
219+
if ($status === 404) {
220+
return null;
221+
}
210222

211223
if ($status > 299 && is_array($ret)) {
212224
$errorMsg = $this->formatError($ret);
213225
if ($this->logger) {
214226
$this->logger->alert($errorMsg);
215227
}
216228

217-
if ($status !== 404) {
218-
$msg = 'Error from API';
219-
if ($errorMsg && $errorMsg[0] !== '{') {
220-
$msg .= ": $errorMsg";
221-
}
222-
throw new RuntimeException($msg, $status);
229+
$msg = 'Error from API';
230+
if ($errorMsg && $errorMsg[0] !== '{') {
231+
$msg .= ": $errorMsg";
223232
}
224-
// todo replace with a not found exception
225-
return null;
233+
throw new RuntimeException($msg, $status);
226234
}
227235
if (!$jsonOk) {
228236
return $contents;

src/Exceptions/NotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Stefna\Mailchimp\Exceptions;
4+
5+
final class NotFoundException extends \InvalidArgumentException
6+
{
7+
}

tests/Api/Campaigns/CampaignsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function getBadCreateParam2(): string
7272
return 'badType';
7373
}
7474

75-
protected function getBadDeleteId(): string
75+
protected function getNotFoundId(): string
7676
{
7777
return 'nonExisting';
7878
}

tests/Api/CollectionTestCase.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ public function testDeleteWorks(): void
4646

4747
public function testDeleteNotFound(): void
4848
{
49-
$this->checkBadDelete($this->getBadDeleteId());
49+
$this->checkBadDelete($this->getNotFoundId());
50+
}
51+
52+
public function testGetNotFound(): void
53+
{
54+
$this->checkGetNotFound($this->getNotFoundId());
55+
}
56+
57+
public function testUpdateNotFound(): void
58+
{
59+
$this->checkPatchNotFound($this->getNotFoundId());
5060
}
5161
}

tests/Api/Lists/ListsMembersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function getBadCreateParam2(): string
109109
return 'badStatus';
110110
}
111111

112-
protected function getBadDeleteId(): string
112+
protected function getNotFoundId(): string
113113
{
114114
115115
}

tests/Api/Lists/ListsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function getBadCreateParam2(): ?string
104104
return null;
105105
}
106106

107-
protected function getBadDeleteId(): string
107+
protected function getNotFoundId(): string
108108
{
109109
return 'nonExisting';
110110
}

tests/Api/Templates/TemplatesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function getBadCreateParam2(): string
109109
return 'someBadFolderId';
110110
}
111111

112-
protected function getBadDeleteId(): string
112+
protected function getNotFoundId(): string
113113
{
114114
return 'noTemplate';
115115
}

tests/Api/TestCase.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Stefna\Mailchimp\Api\CollectionRestApi;
66
use Stefna\Mailchimp\Api\Request\AllInterface;
77
use Stefna\Mailchimp\Api\RestApi;
8+
use Stefna\Mailchimp\Exceptions\NotFoundException;
89
use Stefna\Mailchimp\Other\AbstractData;
910
use Stefna\Mailchimp\Other\AbstractRequest;
1011
use Tests\Stefna\Mailchimp\AbstractTestCase;
@@ -57,7 +58,7 @@ abstract protected function getBadCreateParam1(): string;
5758

5859
abstract protected function getBadCreateParam2(): ?string;
5960

60-
abstract protected function getBadDeleteId(): string;
61+
abstract protected function getNotFoundId(): string;
6162

6263
/**
6364
* @return array|AbstractData
@@ -156,6 +157,20 @@ protected function checkBadDelete($id): void
156157
$this->assertFalse($ret);
157158
}
158159

160+
protected function checkGetNotFound($id): void
161+
{
162+
$api = $this->getApi();
163+
$ret = $api->get($id);
164+
$this->assertNull($ret);
165+
}
166+
167+
protected function checkPatchNotFound($id): void
168+
{
169+
$api = $this->getApi();
170+
$this->expectException(NotFoundException::class);
171+
$api->update($id, []);
172+
}
173+
159174
protected function checkEntityDefault($entity): void
160175
{
161176

tests/ArrayIntersectAssocRecursiveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function test($a, $b, $expected): void
1818
$this->assertEquals($expected, TestedTestCase::array_intersect_assoc_recursive($a, $b));
1919
}
2020

21-
public function provide(): array
21+
public static function provide(): array
2222
{
2323
return [
2424
'php.net' => [
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return new \GuzzleHttp\Psr7\Response(
4+
404,
5+
['IsMock' => true],
6+
''
7+
);

0 commit comments

Comments
 (0)