Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHP 8 #169

Merged
merged 3 commits into from
Nov 28, 2020
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
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@
}
],
"require": {
"php": "^7.2",
"php": "^7.2 || ^8.0",
"fig/http-message-util": "^1.1.4",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
"ralouphie/getallheaders": "^3"
"ralouphie/getallheaders": "^3",
"symfony/polyfill-php80": "^1.18"
},
"require-dev": {
"ext-json": "*",
"adriansuter/php-autoload-override": "^1.2",
"http-interop/http-factory-tests": "^0.7.0",
"php-http/psr7-integration-tests": "dev-master",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
"phpunit/phpunit": "^8.5 || ^9.3",
"squizlabs/php_codesniffer": "^3.5",
"weirdan/prophecy-shim": "^1.0 || ^2.0.2"
},
"provide": {
"psr/http-message-implementation": "1.0",
Expand Down
24 changes: 20 additions & 4 deletions src/Factory/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Slim\Psr7\Stream;
use ValueError;

use function fopen;
use function fwrite;
Expand Down Expand Up @@ -53,20 +54,35 @@ public function createStreamFromFile(
string $mode = 'r',
StreamInterface $cache = null
): StreamInterface {
// When fopen fails, PHP normally raises a warning. Add an error
// When fopen fails, PHP 7 normally raises a warning. Add an error
// handler to check for errors and throw an exception instead.
// On PHP 8, exceptions are thrown.
$exc = null;

set_error_handler(function (int $errno, string $errstr) use ($filename, $mode, &$exc) {
// Would not be initialized if fopen throws on PHP >= 8.0
$resource = null;

$errorHandler = function (string $errorMessage) use ($filename, $mode, &$exc) {
$exc = new RuntimeException(sprintf(
'Unable to open %s using mode %s: %s',
$filename,
$mode,
$errstr
$errorMessage
));
};

set_error_handler(function (int $errno, string $errstr) use ($errorHandler) {
$errorHandler($errstr);
});

$resource = fopen($filename, $mode);
try {
$resource = fopen($filename, $mode);
// @codeCoverageIgnoreStart
// (Can only be executed in PHP >= 8.0)
} catch (ValueError $exception) {
$errorHandler($exception->getMessage());
}
// @codeCoverageIgnoreEnd
restore_error_handler();

if ($exc) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Factory/UploadedFileFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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 @@ -24,6 +25,8 @@

class UploadedFileFactoryTest extends UploadedFileFactoryTestCase
{
use ProphecyTrait;

/**
* @return UploadedFileFactory
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Slim\Tests\Psr7;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
Expand All @@ -24,6 +25,8 @@

class StreamTest extends TestCase
{
use ProphecyTrait;

/**
* @var resource pipe stream file handle
*/
Expand Down
7 changes: 5 additions & 2 deletions tests/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use ReflectionProperty;
Expand Down Expand Up @@ -42,6 +43,8 @@

class UploadedFileTest extends TestCase
{
use ProphecyTrait;

private static $filename = './phpUxcOty';

private static $tmpFiles = ['./phpUxcOty'];
Expand Down Expand Up @@ -245,7 +248,7 @@ public function testMoveTo(UploadedFile $uploadedFile)
public function testMoveToRenameFailure()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp('/^Error moving uploaded file .* to .*$/');
$this->expectExceptionMessageMatches('/^Error moving uploaded file .* to .*$/');

$uploadedFile = $this->generateNewTmpFile();

Expand Down Expand Up @@ -280,7 +283,7 @@ public function testMoveToSapiNonUploadedFile(UploadedFile $uploadedFile)
public function testMoveToSapiMoveUploadedFileFails(UploadedFile $uploadedFile)
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageRegExp('~Error moving uploaded file.*~');
$this->expectExceptionMessageMatches('~Error moving uploaded file.*~');

$GLOBALS['is_uploaded_file_return'] = true;

Expand Down