Skip to content

Commit 48e6e52

Browse files
authored
Refactor: phpunit >= 10 (#57)
* fix: clone payload * feat: rename psalm.xml * refactor: phpunit >= 10 * fix: remove php 8.4 * fix: phpcs * fix: LogTrait * fix: LogTrait * fix: LogTrait * refactor: response * fix: response
1 parent d5d9cc2 commit 48e6e52

15 files changed

+341
-348
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
/tests/files/api.log
77
/coverage/*
88
/index.html
9+
/phpcs.xml
910
/phpstan.neon
1011
/phpunit.xml
12+
/psalm.xml
1113
/provision/*
1214
/vendor/*
1315
!/vendor/.gitkeep

composer.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"woohoolabs/yang": "^3.0"
2626
},
2727
"require-dev": {
28-
"cakephp/cakephp-codesniffer": "^3.0",
28+
"cakephp/cakephp-codesniffer": "^5.0",
2929
"josegonzalez/dotenv": "2.*",
30-
"phpstan/phpstan": "^1.10",
31-
"phpunit/phpunit": "^9.0",
30+
"phpstan/phpstan": "^1.12",
31+
"phpunit/phpunit": "^10.5.5 || ^11.1.3",
3232
"psy/psysh": "@stable",
3333
"vimeo/psalm": "^5.18"
3434
},
@@ -64,6 +64,9 @@
6464
},
6565
"prefer-stable": true,
6666
"config": {
67-
"sort-packages": true
67+
"sort-packages": true,
68+
"allow-plugins": {
69+
"dealerdirect/phpcodesniffer-composer-installer": true
70+
}
6871
}
6972
}

phpunit.xml.dist

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="./tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage>
3+
<source>
44
<include>
55
<directory suffix=".php">./src/</directory>
66
</include>
7-
</coverage>
7+
</source>
88
<php>
99
<ini name="memory_limit" value="-1"/>
1010
<ini name="apc.enable_cli" value="1"/>

psalm.xml.dist

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="8"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
findUnusedBaselineEntry="true"
9+
findUnusedCode="false"
10+
>
11+
<issueHandlers>
12+
<ImplicitToStringCast errorLevel="suppress" />
13+
<UndefinedConstant errorLevel="suppress" />
14+
</issueHandlers>
15+
<projectFiles>
16+
<directory name="src" />
17+
<ignoreFiles>
18+
<directory name="vendor" />
19+
</ignoreFiles>
20+
</projectFiles>
21+
</psalm>

src/BEditaClient.php

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
declare(strict_types=1);
3+
34
/**
45
* BEdita, API-first content management framework
56
* Copyright 2023 Atlas Srl, ChannelWeb Srl, Chialab Srl
@@ -11,6 +12,8 @@
1112

1213
namespace BEdita\SDK;
1314

15+
use Exception;
16+
1417
/**
1518
* BEdita API Client class
1619
*/
@@ -31,8 +34,9 @@ public function authenticate(string $username, string $password): ?array
3134
unset($headers['Authorization']);
3235
$this->setDefaultHeaders($headers);
3336
}
37+
$body = (string)json_encode(compact('username', 'password') + ['grant_type' => 'password']);
3438

35-
return $this->post('/auth', json_encode(compact('username', 'password') + ['grant_type' => 'password']), ['Content-Type' => 'application/json']);
39+
return $this->post('/auth', $body, ['Content-Type' => 'application/json']);
3640
}
3741

3842
/**
@@ -51,73 +55,73 @@ public function getObjects(string $type = 'objects', ?array $query = null, ?arra
5155
/**
5256
* GET a single object of a given type
5357
*
54-
* @param int|string $id Object id
58+
* @param string|int $id Object id
5559
* @param string $type Object type name
5660
* @param array|null $query Optional query string
5761
* @param array|null $headers Custom request headers
5862
* @return array|null Response in array format
5963
*/
60-
public function getObject($id, string $type = 'objects', ?array $query = null, ?array $headers = null): ?array
64+
public function getObject(string|int $id, string $type = 'objects', ?array $query = null, ?array $headers = null): ?array
6165
{
6266
return $this->get(sprintf('/%s/%s', $type, $id), $query, $headers);
6367
}
6468

6569
/**
6670
* Get a list of related resources or objects
6771
*
68-
* @param int|string $id Resource id or object uname/id
72+
* @param string|int $id Resource id or object uname/id
6973
* @param string $type Type name
7074
* @param string $relation Relation name
7175
* @param array|null $query Optional query string
7276
* @param array|null $headers Custom request headers
7377
* @return array|null Response in array format
7478
*/
75-
public function getRelated($id, string $type, string $relation, ?array $query = null, ?array $headers = null): ?array
79+
public function getRelated(string|int $id, string $type, string $relation, ?array $query = null, ?array $headers = null): ?array
7680
{
7781
return $this->get(sprintf('/%s/%s/%s', $type, $id, $relation), $query, $headers);
7882
}
7983

8084
/**
8185
* Add a list of related resources or objects
8286
*
83-
* @param int|string $id Resource id or object uname/id
87+
* @param string|int $id Resource id or object uname/id
8488
* @param string $type Type name
8589
* @param string $relation Relation name
8690
* @param array $data Related resources or objects to add, MUST contain id and type
8791
* @param array|null $headers Custom request headers
8892
* @return array|null Response in array format
8993
*/
90-
public function addRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
94+
public function addRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
9195
{
9296
return $this->post(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
9397
}
9498

9599
/**
96100
* Remove a list of related resources or objects
97101
*
98-
* @param int|string $id Resource id or object uname/id
102+
* @param string|int $id Resource id or object uname/id
99103
* @param string $type Type name
100104
* @param string $relation Relation name
101105
* @param array $data Related resources or objects to remove from relation
102106
* @param array|null $headers Custom request headers
103107
* @return array|null Response in array format
104108
*/
105-
public function removeRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
109+
public function removeRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
106110
{
107111
return $this->delete(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
108112
}
109113

110114
/**
111115
* Replace a list of related resources or objects: previuosly related are removed and replaced with these.
112116
*
113-
* @param int|string $id Object id
117+
* @param string|int $id Object id
114118
* @param string $type Object type name
115119
* @param string $relation Relation name
116120
* @param array $data Related resources or objects to insert
117121
* @param array|null $headers Custom request headers
118122
* @return array|null Response in array format
119123
*/
120-
public function replaceRelated($id, string $type, string $relation, array $data, ?array $headers = null): ?array
124+
public function replaceRelated(string|int $id, string $type, string $relation, array $data, ?array $headers = null): ?array
121125
{
122126
return $this->patch(sprintf('/%s/%s/relationships/%s', $type, $id, $relation), json_encode(compact('data')), $headers);
123127
}
@@ -170,11 +174,11 @@ public function saveObject(string $type, array $data, ?array $headers = null): ?
170174
/**
171175
* Delete an object (DELETE) => move to trashcan.
172176
*
173-
* @param int|string $id Object id
177+
* @param string|int $id Object id
174178
* @param string $type Object type name
175179
* @return array|null Response in array format
176180
*/
177-
public function deleteObject($id, string $type): ?array
181+
public function deleteObject(string|int $id, string $type): ?array
178182
{
179183
return $this->delete(sprintf('/%s/%s', $type, $id));
180184
}
@@ -191,7 +195,7 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
191195
$response = null;
192196
try {
193197
$response = $this->delete(sprintf('/%s?ids=%s', $type, implode(',', $ids)));
194-
} catch (\Exception $e) {
198+
} catch (Exception $e) {
195199
// fallback to delete one by one, to be retrocompatible
196200
foreach ($ids as $id) {
197201
$response = !empty($response) ? $response : $this->deleteObject($id, $type);
@@ -204,10 +208,10 @@ public function deleteObjects(array $ids, string $type = 'objects'): ?array
204208
/**
205209
* Remove an object => permanently remove object from trashcan.
206210
*
207-
* @param int|string $id Object id
211+
* @param string|int $id Object id
208212
* @return array|null Response in array format
209213
*/
210-
public function remove($id): ?array
214+
public function remove(string|int $id): ?array
211215
{
212216
return $this->delete(sprintf('/trash/%s', $id));
213217
}
@@ -223,7 +227,7 @@ public function removeObjects(array $ids): ?array
223227
$response = null;
224228
try {
225229
$response = $this->delete(sprintf('/trash?ids=%s', implode(',', $ids)));
226-
} catch (\Exception $e) {
230+
} catch (Exception $e) {
227231
// fallback to delete one by one, to be retrocompatible
228232
foreach ($ids as $id) {
229233
$response = !empty($response) ? $response : $this->remove($id);
@@ -270,7 +274,7 @@ public function upload(string $filename, string $filepath, ?array $headers = nul
270274
* @return array|null Response in array format
271275
* @throws \BEdita\SDK\BEditaClientException
272276
*/
273-
public function createMediaFromStream($streamId, string $type, array $body): ?array
277+
public function createMediaFromStream(string $streamId, string $type, array $body): ?array
274278
{
275279
$id = $this->createMedia($type, $body);
276280
$this->addStreamToMedia($streamId, $id, $type);
@@ -335,7 +339,7 @@ public function addStreamToMedia(string $streamId, string $id, string $type): vo
335339
* @param array $query The query params for thumbs call.
336340
* @return array|null Response in array format
337341
*/
338-
public function thumbs($id = null, $query = []): ?array
342+
public function thumbs(?int $id = null, array $query = []): ?array
339343
{
340344
if (empty($id) && empty($query['ids'])) {
341345
throw new BEditaClientException('Invalid empty id|ids for thumbs');
@@ -377,11 +381,11 @@ public function relationData(string $name): ?array
377381
/**
378382
* Restore object from trash
379383
*
380-
* @param int|string $id Object id
384+
* @param string|int $id Object id
381385
* @param string $type Object type name
382386
* @return array|null Response in array format
383387
*/
384-
public function restoreObject($id, string $type): ?array
388+
public function restoreObject(string|int $id, string $type): ?array
385389
{
386390
return $this->patch(
387391
sprintf('/trash/%s', $id),

src/BEditaClientException.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
/**
35
* BEdita, API-first content management framework
46
* Copyright 2018 ChannelWeb Srl, Chialab Srl
@@ -10,45 +12,48 @@
1012

1113
namespace BEdita\SDK;
1214

15+
use Exception;
16+
use RuntimeException;
17+
1318
/**
1419
* Network exception thrown by BEdita API Client.
1520
*/
16-
class BEditaClientException extends \RuntimeException
21+
class BEditaClientException extends RuntimeException
1722
{
1823
/**
1924
* Array of attributes that are passed in from the constructor, and
2025
* made available in the view when a development error is displayed.
2126
*
2227
* @var array
2328
*/
24-
protected $attributes = [];
29+
protected array $attributes = [];
2530

2631
/**
2732
* Template string that has attributes sprintf()'ed into it.
2833
*
2934
* @var string
3035
*/
31-
protected $messageTemplate = '[%s] %s';
36+
protected string $messageTemplate = '[%s] %s';
3237

3338
/**
3439
* Default exception code
3540
*
3641
* @var int
3742
*/
38-
protected $defaultCode = 503;
43+
protected int $defaultCode = 503;
3944

4045
/**
4146
* Constructor.
4247
*
4348
* Allows you to create exceptions that are treated as framework errors and disabled
4449
* when debug = 0.
4550
*
46-
* @param string|array $message Either the string of the error message, or an array of attributes
51+
* @param array|string $message Either the string of the error message, or an array of attributes
4752
* that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
4853
* @param int|null $code The code of the error, is also the HTTP status code for the error.
4954
* @param \Exception|null $previous the previous exception.
5055
*/
51-
public function __construct($message = '', ?int $code = null, \Exception $previous = null)
56+
public function __construct(array|string $message = '', ?int $code = null, ?Exception $previous = null)
5257
{
5358
if ($code === null) {
5459
$code = $this->defaultCode;

0 commit comments

Comments
 (0)