Skip to content

Commit 11ccdc9

Browse files
authored
Support PHP 8.5 (#340)
* Remove Prophecy We prefer to use PHPUnit's built-in mocking support. * Only call setAccessible() for PHP < 8.1 setAccessible() is only required for PHP versions less than 8.1 and is deprecated from 8.5. * Add PHP 8.5 to the CI matrix
1 parent cce9c0d commit 11ccdc9

10 files changed

Lines changed: 138 additions & 109 deletions

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
php: [8.0, 8.1, 8.2, 8.3, 8.4]
16+
php: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5]
1717
experimental: [false]
1818
composer-options: ['']
1919
include:

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
"adriansuter/php-autoload-override": "^1.5|| ^2.0",
4040
"http-interop/http-factory-tests": "^1.0 || ^2.0",
4141
"php-http/psr7-integration-tests": "^1.4",
42-
"phpspec/prophecy": "^1.22",
43-
"phpspec/prophecy-phpunit": "^2.2",
4442
"phpstan/phpstan": "^2.1",
4543
"phpunit/phpunit": "^9.6 || ^10",
4644
"squizlabs/php_codesniffer": "^3.13"
@@ -61,9 +59,9 @@
6159
},
6260
"scripts": {
6361
"test": [
64-
"@phpunit",
6562
"@phpcs",
66-
"@phpstan"
63+
"@phpstan",
64+
"@phpunit"
6765
],
6866
"phpunit": "@php phpunit",
6967
"phpcs": "@php phpcs",

tests/BodyTest.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ protected function tearDown(): void
4747
}
4848
}
4949

50+
protected function setAccessible(ReflectionProperty $property, bool $accessible = true): void
51+
{
52+
// only if PHP version < 8.1
53+
if (PHP_VERSION_ID > 80100) {
54+
return;
55+
}
56+
$property->setAccessible($accessible);
57+
}
58+
5059
/**
5160
* @param string $mode
5261
*
@@ -66,7 +75,7 @@ public function testConstructorAttachesStream()
6675
$this->stream = $this->resourceFactory();
6776
$body = new Stream($this->stream);
6877
$bodyStream = new ReflectionProperty($body, 'stream');
69-
$bodyStream->setAccessible(true);
78+
$this->setAccessible($bodyStream);
7079

7180
$this->assertSame($this->stream, $bodyStream->getValue($body));
7281
}
@@ -109,19 +118,19 @@ public function testDetach()
109118
$body = new Stream($this->stream);
110119

111120
$bodyStream = new ReflectionProperty($body, 'stream');
112-
$bodyStream->setAccessible(true);
121+
$this->setAccessible($bodyStream);
113122

114123
$bodyMetadata = new ReflectionProperty($body, 'meta');
115-
$bodyMetadata->setAccessible(true);
124+
$this->setAccessible($bodyMetadata);
116125

117126
$bodyReadable = new ReflectionProperty($body, 'readable');
118-
$bodyReadable->setAccessible(true);
127+
$this->setAccessible($bodyReadable);
119128

120129
$bodyWritable = new ReflectionProperty($body, 'writable');
121-
$bodyWritable->setAccessible(true);
130+
$this->setAccessible($bodyWritable);
122131

123132
$bodySeekable = new ReflectionProperty($body, 'seekable');
124-
$bodySeekable->setAccessible(true);
133+
$this->setAccessible($bodySeekable);
125134

126135
$result = $body->detach();
127136

@@ -156,7 +165,7 @@ public function testToStringDetached()
156165
$this->stream = $this->resourceFactory();
157166
$body = new Stream($this->stream);
158167
$bodyStream = new ReflectionProperty($body, 'stream');
159-
$bodyStream->setAccessible(true);
168+
$this->setAccessible($bodyStream);
160169
$bodyStream->setValue($body, null);
161170

162171
$this->assertEquals('', (string) $body);
@@ -169,7 +178,7 @@ public function testClose()
169178
$body->close();
170179

171180
$bodyStream = new ReflectionProperty($body, 'stream');
172-
$bodyStream->setAccessible(true);
181+
$this->setAccessible($bodyStream);
173182

174183
$this->assertNull($bodyStream->getValue($body));
175184
}
@@ -187,7 +196,7 @@ public function testGetSizeDetached()
187196
$this->stream = $this->resourceFactory();
188197
$body = new Stream($this->stream);
189198
$bodyStream = new ReflectionProperty($body, 'stream');
190-
$bodyStream->setAccessible(true);
199+
$this->setAccessible($bodyStream);
191200
$bodyStream->setValue($body, null);
192201

193202
$this->assertNull($body->getSize());
@@ -209,7 +218,7 @@ public function testTellDetachedThrowsRuntimeException()
209218
$this->stream = $this->resourceFactory();
210219
$body = new Stream($this->stream);
211220
$bodyStream = new ReflectionProperty($body, 'stream');
212-
$bodyStream->setAccessible(true);
221+
$this->setAccessible($bodyStream);
213222
$bodyStream->setValue($body, null);
214223

215224
$body->tell();
@@ -240,7 +249,7 @@ public function testEofDetached()
240249
$this->stream = $this->resourceFactory();
241250
$body = new Stream($this->stream);
242251
$bodyStream = new ReflectionProperty($body, 'stream');
243-
$bodyStream->setAccessible(true);
252+
$this->setAccessible($bodyStream);
244253
$bodyStream->setValue($body, null);
245254

246255
$this->assertTrue($body->eof());

tests/CookiesTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use InvalidArgumentException;
1414
use PHPUnit\Framework\TestCase;
1515
use ReflectionClass;
16+
use ReflectionMethod;
1617
use ReflectionProperty;
1718
use Slim\Psr7\Cookies;
1819
use stdClass;
@@ -30,11 +31,20 @@ public function testConstructor()
3031
'test' => 'Works',
3132
]);
3233
$prop = new ReflectionProperty($cookies, 'requestCookies');
33-
$prop->setAccessible(true);
34+
$this->setAccessible($prop);
3435
$this->assertNotEmpty($prop->getValue($cookies)['test']);
3536
$this->assertEquals('Works', $prop->getValue($cookies)['test']);
3637
}
3738

39+
protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
40+
{
41+
// only if PHP version < 8.1
42+
if (PHP_VERSION_ID > 80100) {
43+
return;
44+
}
45+
$property->setAccessible($accessible);
46+
}
47+
3848
public function testSetDefaults()
3949
{
4050
$defaults = [
@@ -51,7 +61,7 @@ public function testSetDefaults()
5161
$cookies = new Cookies();
5262

5363
$prop = new ReflectionProperty($cookies, 'defaults');
54-
$prop->setAccessible(true);
64+
$this->setAccessible($prop);
5565

5666
$origDefaults = $prop->getValue($cookies);
5767

@@ -67,7 +77,7 @@ public function testSetCookieValues()
6777
$cookies->set('foo', 'bar');
6878

6979
$prop = new ReflectionProperty($cookies, 'responseCookies');
70-
$prop->setAccessible(true);
80+
$this->setAccessible($prop);
7181

7282
$expectedValue = [
7383
'foo' => [
@@ -103,7 +113,7 @@ public function testSetCookieValuesContainDefaults()
103113
$cookies->set('foo', 'bar');
104114

105115
$prop = new ReflectionProperty($cookies, 'responseCookies');
106-
$prop->setAccessible(true);
116+
$this->setAccessible($prop);
107117

108118
$expectedValue = [
109119
'foo' => [
@@ -139,7 +149,7 @@ public function testSetCookieValuesCanOverrideDefaults()
139149
$cookies->set('foo', ['value' => 'bar', 'secure' => false, 'samesite' => 'strict']);
140150

141151
$prop = new ReflectionProperty($cookies, 'responseCookies');
142-
$prop->setAccessible(true);
152+
$this->setAccessible($prop);
143153

144154
$expectedValue = [
145155
'foo' => [
@@ -169,7 +179,7 @@ public function testSetSameSiteCookieValuesAreCaseInsensitive()
169179
$cookies->set('breakfast', ['samesite' => 'StricT']);
170180

171181
$prop = new ReflectionProperty($cookies, 'responseCookies');
172-
$prop->setAccessible(true);
182+
$this->setAccessible($prop);
173183

174184
$expectedValue = [
175185
'breakfast' => [
@@ -224,7 +234,7 @@ public function testToHeader()
224234
$cookies = new Cookies();
225235
$class = new ReflectionClass($cookies);
226236
$method = $class->getMethod('toHeader');
227-
$method->setAccessible(true);
237+
$this->setAccessible($method);
228238
$properties = [
229239
'name' => 'test',
230240
'properties' => [

tests/Factory/ServerRequestFactoryTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use InvalidArgumentException;
1515
use Psr\Http\Message\UriInterface;
1616
use ReflectionClass;
17+
use ReflectionMethod;
18+
use ReflectionProperty;
1719
use Slim\Psr7\Environment;
1820
use Slim\Psr7\Factory\ServerRequestFactory;
1921
use Slim\Psr7\Factory\UriFactory;
@@ -25,6 +27,15 @@
2527

2628
class ServerRequestFactoryTest extends ServerRequestFactoryTestCase
2729
{
30+
protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
31+
{
32+
// only if PHP version < 8.1
33+
if (PHP_VERSION_ID > 80100) {
34+
return;
35+
}
36+
$property->setAccessible($accessible);
37+
}
38+
2839
protected function createServerRequestFactory(): ServerRequestFactory
2940
{
3041
return new ServerRequestFactory();
@@ -123,7 +134,7 @@ public function testCreateFromGlobalsSetsACache()
123134
$stream = $request->getBody();
124135
$class = new ReflectionClass($stream);
125136
$property = $class->getProperty('cache');
126-
$property->setAccessible(true);
137+
$this->setAccessible($property);
127138
$cacheStreamValue = $property->getValue($stream);
128139
$this->assertNotNull($cacheStreamValue);
129140
}

tests/Factory/UploadedFileFactoryTest.php

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Interop\Http\Factory\UploadedFileFactoryTestCase;
1414
use InvalidArgumentException;
15-
use Prophecy\PhpUnit\ProphecyTrait;
1615
use Psr\Http\Message\StreamInterface;
1716
use Slim\Psr7\Factory\StreamFactory;
1817
use Slim\Psr7\Factory\UploadedFileFactory;
@@ -25,8 +24,6 @@
2524

2625
class UploadedFileFactoryTest extends UploadedFileFactoryTestCase
2726
{
28-
use ProphecyTrait;
29-
3027
protected function createUploadedFileFactory(): UploadedFileFactory
3128
{
3229
return new UploadedFileFactory();
@@ -43,25 +40,20 @@ protected function createStream($content): StreamInterface
4340
}
4441

4542
/**
46-
* Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy.
43+
* Create a `\Psr\Http\Message\StreamInterface` mock with a `getMetadata` method expectation.
4744
*
48-
* @param string $argKey Argument for the method prophecy.
45+
* @param string $argKey Argument for the method expectation.
4946
* @param mixed $returnValue Return value of the `getMetadata` method.
5047
*
5148
* @return StreamInterface
5249
*/
5350
protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface
5451
{
55-
$streamProphecy = $this->prophesize(StreamInterface::class);
56-
57-
/** @noinspection PhpUndefinedMethodInspection */
58-
$streamProphecy
59-
->getMetadata($argKey)
60-
->willReturn($returnValue)
61-
->shouldBeCalled();
62-
63-
/** @var StreamInterface $stream */
64-
$stream = $streamProphecy->reveal();
52+
$stream = $this->createMock(StreamInterface::class);
53+
$stream->expects($this->once())
54+
->method('getMetadata')
55+
->with($argKey)
56+
->willReturn($returnValue);
6557

6658
return $stream;
6759
}
@@ -71,17 +63,11 @@ public function testCreateUploadedFileWithInvalidUri()
7163
$this->expectException(InvalidArgumentException::class);
7264
$this->expectExceptionMessage('File is not readable.');
7365

74-
// Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy.
75-
$streamProphecy = $this->prophesize(StreamInterface::class);
76-
77-
/** @noinspection PhpUndefinedMethodInspection */
78-
$streamProphecy
79-
->getMetadata('uri')
80-
->willReturn(null)
81-
->shouldBeCalled();
82-
83-
/** @var StreamInterface $stream */
84-
$stream = $streamProphecy->reveal();
66+
$stream = $this->createMock(StreamInterface::class);
67+
$stream->expects($this->once())
68+
->method('getMetadata')
69+
->with('uri')
70+
->willReturn(null);
8571

8672
$this->factory->createUploadedFile($stream);
8773
}
@@ -91,23 +77,14 @@ public function testCreateUploadedFileWithNonReadableFile()
9177
$this->expectException(InvalidArgumentException::class);
9278
$this->expectExceptionMessage('File is not readable.');
9379

94-
// Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` and `isReadable` method prophecies.
95-
$streamProphecy = $this->prophesize(StreamInterface::class);
96-
97-
/** @noinspection PhpUndefinedMethodInspection */
98-
$streamProphecy
99-
->getMetadata('uri')
100-
->willReturn('non-readable')
101-
->shouldBeCalled();
102-
103-
/** @noinspection PhpUndefinedMethodInspection */
104-
$streamProphecy
105-
->isReadable()
106-
->willReturn(false)
107-
->shouldBeCalled();
108-
109-
/** @var StreamInterface $stream */
110-
$stream = $streamProphecy->reveal();
80+
$stream = $this->createMock(StreamInterface::class);
81+
$stream->expects($this->once())
82+
->method('getMetadata')
83+
->with('uri')
84+
->willReturn('non-readable');
85+
$stream->expects($this->once())
86+
->method('isReadable')
87+
->willReturn(false);
11188

11289
$this->factory->createUploadedFile($stream);
11390
}

0 commit comments

Comments
 (0)