From 61472b83de848078c4d715271486dcc63f89a3a5 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Wed, 16 Jul 2025 09:10:40 +0200 Subject: [PATCH 1/2] build: Add psr/http-message v2 as alternative to allow compatibility with packages requiring v2 Some major packages require v2 of psr/http-message, such as aws/aws-sdk-php and elasticsearch/elasticsearch. Since v2 only adds return types it is considered compatible --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3caf00f0..bb86495d 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "myclabs/php-enum": "^1.5", "php-http/discovery": "^1.5", "php-http/httplug": ">=1.1", - "psr/http-message": "^1.0", + "psr/http-message": "^1.0 || ^2.0", "psr/simple-cache": "^1.0", "symfony/cache": ">=5.0", "setasign/fpdf": "^1.8", From 012f9f8768d3c662ba7333ca029120757227afd4 Mon Sep 17 00:00:00 2001 From: Martin Boer Date: Wed, 16 Jul 2025 13:47:10 +0200 Subject: [PATCH 2/2] :recycle: define StreamInterface return types and properly mock getBody --- src/Resources/Proxy/FileStreamProxy.php | 18 +++++++++--------- src/Resources/Shipment.php | 8 ++++---- .../Authentication/ClientCredentialsTest.php | 8 ++++++-- .../Unit/Collection/RequestCollectionTest.php | 5 ++++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Resources/Proxy/FileStreamProxy.php b/src/Resources/Proxy/FileStreamProxy.php index 2e558f60..5c8bf729 100644 --- a/src/Resources/Proxy/FileStreamProxy.php +++ b/src/Resources/Proxy/FileStreamProxy.php @@ -79,7 +79,7 @@ public function detach() * * @return int|null Returns the size in bytes if known, or null if unknown. */ - public function getSize() + public function getSize(): ?int { return $this->getStream()->getSize(); } @@ -90,7 +90,7 @@ public function getSize() * @return int Position of the file pointer * @throws \RuntimeException on error. */ - public function tell() + public function tell(): int { return $this->getStream()->tell(); } @@ -123,9 +123,9 @@ public function isSeekable(): bool * SEEK_END: Set position to end-of-stream plus offset. * @throws \RuntimeException on failure. */ - public function seek($offset, $whence = SEEK_SET) + public function seek($offset, $whence = SEEK_SET): void { - return $this->getStream()->seek($offset, $whence); + $this->getStream()->seek($offset, $whence); } /** @@ -138,9 +138,9 @@ public function seek($offset, $whence = SEEK_SET) * @link http://www.php.net/manual/en/function.fseek.php * @see seek() */ - public function rewind() + public function rewind(): void { - return $this->getStream()->rewind(); + $this->getStream()->rewind(); } /** @@ -158,7 +158,7 @@ public function isWritable(): bool * @return int Returns the number of bytes written to the stream. * @throws \RuntimeException on failure. */ - public function write($string) + public function write($string): int { return $this->getStream()->write($string); } @@ -181,7 +181,7 @@ public function isReadable(): bool * if no bytes are available. * @throws \RuntimeException if an error occurs. */ - public function read($length) + public function read($length): string { return $this->getStream()->read($length); } @@ -193,7 +193,7 @@ public function read($length) * @throws \RuntimeException if unable to read or an error occurs while * reading. */ - public function getContents() + public function getContents(): string { return $this->getStream()->getContents(); } diff --git a/src/Resources/Shipment.php b/src/Resources/Shipment.php index 3e7506d3..7b70bea0 100644 --- a/src/Resources/Shipment.php +++ b/src/Resources/Shipment.php @@ -171,10 +171,10 @@ public function getData(): array $data = $this->jsonSerialize(); // Remove read-only relationships. - unset($data['relationships']['colli']); - unset($data['relationships']['files']); - unset($data['relationships']['shipment_status']); - unset($data['relationships']['shipment_surcharges']); + unset($data['relationships'][self::RELATIONSHIP_COLLI]); + unset($data['relationships'][self::RELATIONSHIP_FILES]); + unset($data['relationships'][self::RELATIONSHIP_STATUS]); + unset($data['relationships'][self::RELATIONSHIP_SHIPMENT_SURCHARGES]); return $data; } diff --git a/tests/Unit/Authentication/ClientCredentialsTest.php b/tests/Unit/Authentication/ClientCredentialsTest.php index 32c7816e..13a0b40f 100644 --- a/tests/Unit/Authentication/ClientCredentialsTest.php +++ b/tests/Unit/Authentication/ClientCredentialsTest.php @@ -13,6 +13,7 @@ use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; class ClientCredentialsTest extends TestCase { @@ -41,11 +42,14 @@ protected function setUp(): void ->getMock(); $this->response->method('getBody') ->willReturnCallback(function () { - return json_encode([ + $streamMock = $this->createMock(StreamInterface::class); + $streamMock->method('__toString')->willReturn(json_encode([ 'token_type' => 'Bearer', 'expires_in' => 86400, 'access_token' => 'an-access-token-for-the-myparcelcom-api-' . $this->tokenSuffix, - ]); + ])); + + return $streamMock; }); $this->response->method('getStatusCode') ->willReturn(200); diff --git a/tests/Unit/Collection/RequestCollectionTest.php b/tests/Unit/Collection/RequestCollectionTest.php index 3e6856d3..63fcb4e8 100644 --- a/tests/Unit/Collection/RequestCollectionTest.php +++ b/tests/Unit/Collection/RequestCollectionTest.php @@ -9,6 +9,7 @@ use MyParcelCom\ApiSdk\Resources\Interfaces\ResourceInterface; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; class RequestCollectionTest extends TestCase { @@ -29,8 +30,10 @@ protected function setUp(): void $this->pageNumber = $pageNumber; $this->pageSize = $pageSize; + $streamMock = $this->createMock(StreamInterface::class); + $streamMock->method('__toString')->willReturn('{"data": "something something", "meta": {"total_records": 123}}'); $response = $this->createMock(ResponseInterface::class); - $response->method('getBody')->willReturn('{"data": "something something", "meta": {"total_records": 123}}'); + $response->method('getBody')->willReturn($streamMock); return $response; };