Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.0, 8.1, 8.2, 8.3, 8.4]
php: [8.0, 8.1, 8.2, 8.3, 8.4, 8.5]
experimental: [false]
composer-options: ['']
include:
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
"adriansuter/php-autoload-override": "^1.5|| ^2.0",
"http-interop/http-factory-tests": "^1.0 || ^2.0",
"php-http/psr7-integration-tests": "^1.4",
"phpspec/prophecy": "^1.22",
"phpspec/prophecy-phpunit": "^2.2",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^9.6 || ^10",
"squizlabs/php_codesniffer": "^3.13"
Expand All @@ -61,9 +59,9 @@
},
"scripts": {
"test": [
"@phpunit",
"@phpcs",
"@phpstan"
"@phpstan",
"@phpunit"
],
"phpunit": "@php phpunit",
"phpcs": "@php phpcs",
Expand Down
31 changes: 20 additions & 11 deletions tests/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ protected function tearDown(): void
}
}

protected function setAccessible(ReflectionProperty $property, bool $accessible = true): void
{
// only if PHP version < 8.1
if (PHP_VERSION_ID > 80100) {
return;
}
$property->setAccessible($accessible);
}

/**
* @param string $mode
*
Expand All @@ -66,7 +75,7 @@ public function testConstructorAttachesStream()
$this->stream = $this->resourceFactory();
$body = new Stream($this->stream);
$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);

$this->assertSame($this->stream, $bodyStream->getValue($body));
}
Expand Down Expand Up @@ -109,19 +118,19 @@ public function testDetach()
$body = new Stream($this->stream);

$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);

$bodyMetadata = new ReflectionProperty($body, 'meta');
$bodyMetadata->setAccessible(true);
$this->setAccessible($bodyMetadata);

$bodyReadable = new ReflectionProperty($body, 'readable');
$bodyReadable->setAccessible(true);
$this->setAccessible($bodyReadable);

$bodyWritable = new ReflectionProperty($body, 'writable');
$bodyWritable->setAccessible(true);
$this->setAccessible($bodyWritable);

$bodySeekable = new ReflectionProperty($body, 'seekable');
$bodySeekable->setAccessible(true);
$this->setAccessible($bodySeekable);

$result = $body->detach();

Expand Down Expand Up @@ -156,7 +165,7 @@ public function testToStringDetached()
$this->stream = $this->resourceFactory();
$body = new Stream($this->stream);
$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);
$bodyStream->setValue($body, null);

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

$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);

$this->assertNull($bodyStream->getValue($body));
}
Expand All @@ -187,7 +196,7 @@ public function testGetSizeDetached()
$this->stream = $this->resourceFactory();
$body = new Stream($this->stream);
$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);
$bodyStream->setValue($body, null);

$this->assertNull($body->getSize());
Expand All @@ -209,7 +218,7 @@ public function testTellDetachedThrowsRuntimeException()
$this->stream = $this->resourceFactory();
$body = new Stream($this->stream);
$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);
$bodyStream->setValue($body, null);

$body->tell();
Expand Down Expand Up @@ -240,7 +249,7 @@ public function testEofDetached()
$this->stream = $this->resourceFactory();
$body = new Stream($this->stream);
$bodyStream = new ReflectionProperty($body, 'stream');
$bodyStream->setAccessible(true);
$this->setAccessible($bodyStream);
$bodyStream->setValue($body, null);

$this->assertTrue($body->eof());
Expand Down
24 changes: 17 additions & 7 deletions tests/CookiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Slim\Psr7\Cookies;
use stdClass;
Expand All @@ -30,11 +31,20 @@ public function testConstructor()
'test' => 'Works',
]);
$prop = new ReflectionProperty($cookies, 'requestCookies');
$prop->setAccessible(true);
$this->setAccessible($prop);
$this->assertNotEmpty($prop->getValue($cookies)['test']);
$this->assertEquals('Works', $prop->getValue($cookies)['test']);
}

protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
{
// only if PHP version < 8.1
if (PHP_VERSION_ID > 80100) {
return;
}
$property->setAccessible($accessible);
}

public function testSetDefaults()
{
$defaults = [
Expand All @@ -51,7 +61,7 @@ public function testSetDefaults()
$cookies = new Cookies();

$prop = new ReflectionProperty($cookies, 'defaults');
$prop->setAccessible(true);
$this->setAccessible($prop);

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

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

$prop = new ReflectionProperty($cookies, 'responseCookies');
$prop->setAccessible(true);
$this->setAccessible($prop);

$expectedValue = [
'foo' => [
Expand Down Expand Up @@ -103,7 +113,7 @@ public function testSetCookieValuesContainDefaults()
$cookies->set('foo', 'bar');

$prop = new ReflectionProperty($cookies, 'responseCookies');
$prop->setAccessible(true);
$this->setAccessible($prop);

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

$prop = new ReflectionProperty($cookies, 'responseCookies');
$prop->setAccessible(true);
$this->setAccessible($prop);

$expectedValue = [
'foo' => [
Expand Down Expand Up @@ -169,7 +179,7 @@ public function testSetSameSiteCookieValuesAreCaseInsensitive()
$cookies->set('breakfast', ['samesite' => 'StricT']);

$prop = new ReflectionProperty($cookies, 'responseCookies');
$prop->setAccessible(true);
$this->setAccessible($prop);

$expectedValue = [
'breakfast' => [
Expand Down Expand Up @@ -224,7 +234,7 @@ public function testToHeader()
$cookies = new Cookies();
$class = new ReflectionClass($cookies);
$method = $class->getMethod('toHeader');
$method->setAccessible(true);
$this->setAccessible($method);
$properties = [
'name' => 'test',
'properties' => [
Expand Down
13 changes: 12 additions & 1 deletion tests/Factory/ServerRequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Slim\Psr7\Environment;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Factory\UriFactory;
Expand All @@ -25,6 +27,15 @@

class ServerRequestFactoryTest extends ServerRequestFactoryTestCase
{
protected function setAccessible(ReflectionProperty|ReflectionMethod $property, bool $accessible = true): void
{
// only if PHP version < 8.1
if (PHP_VERSION_ID > 80100) {
return;
}
$property->setAccessible($accessible);
}

protected function createServerRequestFactory(): ServerRequestFactory
{
return new ServerRequestFactory();
Expand Down Expand Up @@ -123,7 +134,7 @@ public function testCreateFromGlobalsSetsACache()
$stream = $request->getBody();
$class = new ReflectionClass($stream);
$property = $class->getProperty('cache');
$property->setAccessible(true);
$this->setAccessible($property);
$cacheStreamValue = $property->getValue($stream);
$this->assertNotNull($cacheStreamValue);
}
Expand Down
63 changes: 20 additions & 43 deletions tests/Factory/UploadedFileFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Interop\Http\Factory\UploadedFileFactoryTestCase;
use InvalidArgumentException;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\StreamInterface;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Factory\UploadedFileFactory;
Expand All @@ -25,8 +24,6 @@

class UploadedFileFactoryTest extends UploadedFileFactoryTestCase
{
use ProphecyTrait;

protected function createUploadedFileFactory(): UploadedFileFactory
{
return new UploadedFileFactory();
Expand All @@ -43,25 +40,20 @@ protected function createStream($content): StreamInterface
}

/**
* Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy.
* Create a `\Psr\Http\Message\StreamInterface` mock with a `getMetadata` method expectation.
*
* @param string $argKey Argument for the method prophecy.
* @param string $argKey Argument for the method expectation.
* @param mixed $returnValue Return value of the `getMetadata` method.
*
* @return StreamInterface
*/
protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface
{
$streamProphecy = $this->prophesize(StreamInterface::class);

/** @noinspection PhpUndefinedMethodInspection */
$streamProphecy
->getMetadata($argKey)
->willReturn($returnValue)
->shouldBeCalled();

/** @var StreamInterface $stream */
$stream = $streamProphecy->reveal();
$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())
->method('getMetadata')
->with($argKey)
->willReturn($returnValue);

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

// Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy.
$streamProphecy = $this->prophesize(StreamInterface::class);

/** @noinspection PhpUndefinedMethodInspection */
$streamProphecy
->getMetadata('uri')
->willReturn(null)
->shouldBeCalled();

/** @var StreamInterface $stream */
$stream = $streamProphecy->reveal();
$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())
->method('getMetadata')
->with('uri')
->willReturn(null);

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

// Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` and `isReadable` method prophecies.
$streamProphecy = $this->prophesize(StreamInterface::class);

/** @noinspection PhpUndefinedMethodInspection */
$streamProphecy
->getMetadata('uri')
->willReturn('non-readable')
->shouldBeCalled();

/** @noinspection PhpUndefinedMethodInspection */
$streamProphecy
->isReadable()
->willReturn(false)
->shouldBeCalled();

/** @var StreamInterface $stream */
$stream = $streamProphecy->reveal();
$stream = $this->createMock(StreamInterface::class);
$stream->expects($this->once())
->method('getMetadata')
->with('uri')
->willReturn('non-readable');
$stream->expects($this->once())
->method('isReadable')
->willReturn(false);

$this->factory->createUploadedFile($stream);
}
Expand Down
Loading