Tester\Environment::setupErrors PHP Method

setupErrors() public static method

Configures PHP error handling.
public static setupErrors ( ) : void
return void
    public static function setupErrors()
    {
        error_reporting(E_ALL | E_STRICT);
        ini_set('display_errors', TRUE);
        ini_set('html_errors', FALSE);
        ini_set('log_errors', FALSE);
        set_exception_handler([__CLASS__, 'handleException']);
        set_error_handler(function ($severity, $message, $file, $line) {
            if (in_array($severity, [E_RECOVERABLE_ERROR, E_USER_ERROR], TRUE) || ($severity & error_reporting()) === $severity) {
                self::handleException(new \ErrorException($message, 0, $severity, $file, $line));
            }
            return FALSE;
        });
        register_shutdown_function(function () {
            Assert::$onFailure = [__CLASS__, 'handleException'];
            // note that Runner is unable to catch this errors in CLI & PHP 5.4.0 - 5.4.6 due PHP bug #62725
            $error = error_get_last();
            register_shutdown_function(function () use($error) {
                if (in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], TRUE)) {
                    if (($error['type'] & error_reporting()) !== $error['type']) {
                        // show fatal errors hidden by @shutup
                        self::removeOutputBuffers();
                        echo "\nFatal error: {$error['message']} in {$error['file']} on line {$error['line']}\n";
                    }
                } elseif (self::$checkAssertions && !Assert::$counter) {
                    self::removeOutputBuffers();
                    echo "\nError: This test forgets to execute an assertion.\n";
                    exit(Runner\Job::CODE_FAIL);
                }
            });
        });
    }

Usage Example

示例#1
0
 /** @return int|NULL */
 public function run()
 {
     Environment::setupColors();
     Environment::setupErrors();
     ob_start();
     $cmd = $this->loadOptions();
     Environment::$debugMode = (bool) $this->options['--debug'];
     if (isset($this->options['--colors'])) {
         Environment::$useColors = (bool) $this->options['--colors'];
     } elseif (in_array($this->options['-o'], ['tap', 'junit'])) {
         Environment::$useColors = FALSE;
     }
     if ($cmd->isEmpty() || $this->options['--help']) {
         $cmd->help();
         return;
     }
     $this->createPhpInterpreter();
     if ($this->options['--info']) {
         $job = new Job(__DIR__ . '/info.php', $this->interpreter);
         $job->run();
         echo $job->getOutput();
         return;
     }
     if ($this->options['--coverage']) {
         $coverageFile = $this->prepareCodeCoverage();
     }
     $runner = $this->createRunner();
     $runner->setEnvironmentVariable(Environment::RUNNER, 1);
     $runner->setEnvironmentVariable(Environment::COLORS, (int) Environment::$useColors);
     if (isset($coverageFile)) {
         $runner->setEnvironmentVariable(Environment::COVERAGE, $coverageFile);
     }
     if ($this->options['-o'] !== NULL) {
         ob_clean();
     }
     ob_end_flush();
     if ($this->options['--watch']) {
         $this->watch($runner);
         return;
     }
     $result = $runner->run();
     if (isset($coverageFile) && preg_match('#\\.(?:html?|xml)\\z#', $coverageFile)) {
         $this->finishCodeCoverage($coverageFile);
     }
     return $result ? 0 : 1;
 }
All Usage Examples Of Tester\Environment::setupErrors