-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed throwing errors in exists-check functions (#54)
Suppresses warning triggered by `stat`/`lstat` when file does not exit. By injecting another error handler which resets error reporting the suppressed warning gets caught and does not appear in error logs.
- Loading branch information
Showing
2 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/** | ||
* @see https://github.com/dg/bypass-finals/issues/52 | ||
* @see https://www.php.net/manual/en/language.operators.errorcontrol.php | ||
* | ||
* As certain testing frameworks establish error handlers to pick up | ||
* on suppressed errors, exceptions and warnings, the warning generated | ||
* by `stat`/`lstat` when provided an unknown path is recorded. | ||
* | ||
* `nette/tester` does _not_ detect suppressed warnings as the error handler | ||
* is set right before executing the closure. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
Tester\Environment::setup(); | ||
|
||
DG\BypassFinals::enable(); | ||
|
||
function thoroughErrorHandler(int $number, string $message, string $file, int $line): bool | ||
{ | ||
/** | ||
* NOTE: The use of the `@` operator sets the error level to | ||
* `E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE` | ||
* or 0 prior to PHP 8.0.0. | ||
* Resetting the error level to `E_ALL | E_STRICT` lets the previous error handler detect | ||
* suppressed errors, exceptions or warnings. | ||
*/ | ||
error_reporting(E_ALL | E_STRICT); | ||
|
||
/** | ||
* NOTE: Acquire the previous error handler from the stack and forward the error. | ||
* Restore the error handler stack afterwards. | ||
*/ | ||
restore_error_handler(); | ||
$errorHandler = set_error_handler(null); | ||
$errorHandler($number, $message, $file, $line); | ||
|
||
return true; | ||
} | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
file_exists('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_writable('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_readable('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_executable('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_file('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_dir('unknown'); | ||
|
||
restore_error_handler(); | ||
}); | ||
|
||
Assert::noError(function () { | ||
set_error_handler('thoroughErrorHandler'); | ||
|
||
is_link('unknown'); | ||
|
||
restore_error_handler(); | ||
}); |