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

Fix StreamFactoryTest error #191

Merged
merged 4 commits into from
Apr 27, 2021
Merged

Conversation

Stilch
Copy link
Contributor

@Stilch Stilch commented Apr 13, 2021

Fix phpstan error for php 8.0
Parameter #1 $callback of function set_error_handler expects (callable(int, string, string, int, array): bool)|null, Closure(int, string): void given.

@l0gicgate l0gicgate added this to the 1.4 milestone Apr 26, 2021
l0gicgate
l0gicgate previously approved these changes Apr 26, 2021
Comment on lines 57 to 68
// 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;

// 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,
$errorMessage
));
};

set_error_handler(function (int $errno, string $errstr) use ($errorHandler) {
$errorHandler($errstr);
});
set_error_handler(
static function (int $errno, string $errstr) use ($filename, $mode): void {
throw new RuntimeException(
"Unable to open $filename using mode $mode: $errstr",
$errno
);
}
);

try {
$resource = fopen($filename, $mode);
} catch (ValueError $exception) {
$errorHandler($exception->getMessage());
}
restore_error_handler();

if ($exc) {
/** @var RuntimeException $exc */
throw $exc;
} finally {
restore_error_handler();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how removing the catch fixes this? That makes the try statement obsolete since it'll never catch anything.

Copy link
Contributor Author

@Stilch Stilch Apr 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Yes, you are right. I forgot to add catch to throw the exception. Fixed it.

@l0gicgate l0gicgate dismissed their stale review April 26, 2021 03:34

Made a mistake reviewing

l0gicgate
l0gicgate previously approved these changes Apr 26, 2021
@l0gicgate l0gicgate dismissed their stale review April 26, 2021 16:45

PHP 8 Tests are failing

@Stilch
Copy link
Contributor Author

Stilch commented Apr 26, 2021

@l0gicgate the current version of slim/psr7 does not pass this test either. Because this test is incorrect. Expected exception "ValueError", but PSR-17 states that "createStreamFromFile" only throw "RuntimeException" or "InvalidArgumentException".

Error message:

1) Slim\Tests\Psr7\Factory\StreamFactoryTest::testCreateStreamFromInvalidFileName
Failed asserting that exception of type "RuntimeException" matches expected exception "ValueError". Message was: "Unable to open  using mode r: Path cannot be empty" at
/home/runner/work/Slim-Psr7/Slim-Psr7/src/Factory/StreamFactory.php:68
/home/runner/work/Slim-Psr7/Slim-Psr7/vendor/http-interop/http-factory-tests/test/StreamFactoryTestCase.php:96

I can fix this, but it is a PSR-17 violation.

@Stilch
Copy link
Contributor Author

Stilch commented Apr 26, 2021

This discrepancy was fixed in http-interop/http-factory-tests v0. 9. 0
I update http-interop/http-factory-tests requirement from ^0.8.0 to ^0.9.0

@l0gicgate l0gicgate changed the title Refactoring Fix StreamFactoryTest error Apr 27, 2021
@l0gicgate l0gicgate merged commit b8a8bb9 into slimphp:master Apr 27, 2021
@Stilch Stilch deleted the feature-refactoring branch April 30, 2021 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants