From 3230eb937414bea281d848046a8a31348f0acacc Mon Sep 17 00:00:00 2001 From: Jasper Smet Date: Sat, 14 Nov 2026 15:11:40 +0100 Subject: [PATCH] Use CakePHP HTTP streams on CakePHP 6 --- src/Middleware/GlideMiddleware.php | 2 +- src/Response/PsrResponseFactory.php | 2 +- tests/TestCase/Middleware/GlideMiddlewareTest.php | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Middleware/GlideMiddleware.php b/src/Middleware/GlideMiddleware.php index 1c22964..d7df722 100644 --- a/src/Middleware/GlideMiddleware.php +++ b/src/Middleware/GlideMiddleware.php @@ -12,9 +12,9 @@ use Cake\Event\EventDispatcherTrait; use Cake\Event\EventManagerInterface; use Cake\Http\Response; +use Cake\Http\Stream; use Cake\Utility\Security; use Exception; -use Laminas\Diactoros\Stream; use League\Glide\Api\Encoder; use League\Glide\Server; use League\Glide\ServerFactory; diff --git a/src/Response/PsrResponseFactory.php b/src/Response/PsrResponseFactory.php index 4bf698a..b14b9b8 100644 --- a/src/Response/PsrResponseFactory.php +++ b/src/Response/PsrResponseFactory.php @@ -5,7 +5,7 @@ use ADmad\Glide\Exception\ResponseException; use Cake\Http\Response; -use Laminas\Diactoros\Stream; +use Cake\Http\Stream; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; use League\Glide\Responses\ResponseFactoryInterface; diff --git a/tests/TestCase/Middleware/GlideMiddlewareTest.php b/tests/TestCase/Middleware/GlideMiddlewareTest.php index d723165..2bbad23 100644 --- a/tests/TestCase/Middleware/GlideMiddlewareTest.php +++ b/tests/TestCase/Middleware/GlideMiddlewareTest.php @@ -12,9 +12,9 @@ use Cake\Http\Response; use Cake\Http\ServerRequest; use Cake\Http\ServerRequestFactory; +use Cake\Http\Stream; use Cake\TestSuite\TestCase; use Cake\Utility\Security; -use Laminas\Diactoros\Stream; use League\Flysystem\UnableToRetrieveMetadata; use League\Glide\ServerFactory; use League\Glide\Signatures\Signature; @@ -59,6 +59,13 @@ public function testNormalResponse() $headers = $response->getHeaders(); $this->assertTrue(isset($headers['Content-Length'])); + $this->assertInstanceOf(Stream::class, $response->getBody()); + $this->assertSame('image/png', $response->getHeaderLine('Content-Type')); + $body = (string)$response->getBody(); + $this->assertSame(strlen($body), (int)$response->getHeaderLine('Content-Length')); + $imageSize = getimagesizefromstring($body); + $this->assertNotFalse($imageSize); + $this->assertSame(100, $imageSize[0]); } public function testDefaultConfig(): void @@ -134,6 +141,12 @@ public function testOriginalPassThrough() $headers = $response->getHeaders(); $this->assertSame($fileSize, (int)$headers['Content-Length'][0]); + $this->assertInstanceOf(Stream::class, $response->getBody()); + $this->assertSame('image/png', $response->getHeaderLine('Content-Type')); + $this->assertSame( + file_get_contents(PLUGIN_ROOT . '/test_app/webroot/upload/cake-logo.png'), + (string)$response->getBody(), + ); } /**