• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / testbench-core / 8367713678

21 Mar 2024 12:24AM UTC coverage: 90.502% (-1.6%) from 92.093%
8367713678

Pull #201

github

web-flow
Merge 9215eaf9f into 4950c247f
Pull Request #201: [8.x] Flush Statics Improvements

6 of 6 new or added lines in 1 file covered. (100.0%)

21 existing lines in 1 file now uncovered.

1172 of 1295 relevant lines covered (90.5%)

52.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

41.3
/src/Bootstrap/HandleExceptions.php
1
<?php
2

3
namespace Orchestra\Testbench\Bootstrap;
4

5
use Illuminate\Log\LogManager;
6
use Orchestra\Testbench\Exceptions\DeprecatedException;
7
use Orchestra\Testbench\Foundation\Env;
8
use PHPUnit\Runner\ErrorHandler;
9

10
use function Illuminate\Filesystem\join_paths;
11

12
/**
13
 * @internal
14
 */
15
final class HandleExceptions extends \Illuminate\Foundation\Bootstrap\HandleExceptions
16
{
17
    /**
18
     * Reports a deprecation to the "deprecations" logger.
19
     *
20
     * @param  string  $message
21
     * @param  string  $file
22
     * @param  int  $line
23
     * @param  int  $level
24
     * @return void
25
     *
26
     * @throws \Orchestra\Testbench\Exceptions\DeprecatedException
27
     */
28
    #[\Override]
29
    public function handleDeprecationError($message, $file, $line, $level = E_DEPRECATED)
30
    {
31
        parent::handleDeprecationError($message, $file, $line, $level);
2✔
32

33
        $testbenchConvertDeprecationsToExceptions = Env::get('TESTBENCH_CONVERT_DEPRECATIONS_TO_EXCEPTIONS', false);
2✔
34

35
        if ($testbenchConvertDeprecationsToExceptions === true) {
2✔
36
            throw new DeprecatedException($message, $level, $file, $line);
1✔
37
        }
38
    }
39

40
    /**
41
     * Ensure the "deprecations" logger is configured.
42
     *
43
     * @return void
44
     */
45
    #[\Override]
46
    protected function ensureDeprecationLoggerIsConfigured()
47
    {
48
        with(self::$app->make('config'), static function ($config) {
2✔
49
            /** @var \Illuminate\Contracts\Config\Repository $config */
50
            if ($config->get('logging.channels.deprecations')) {
2✔
51
                return;
×
52
            }
53

54
            /** @var array{channel?: string, trace?: bool}|string|null $options */
55
            $options = $config->get('logging.deprecations');
2✔
56

57
            if (\is_array($options)) {
2✔
58
                $driver = $options['channel'] ?? 'null';
2✔
59
            } else {
60
                $driver = $options ?? 'null';
×
61
            }
62

63
            if ($driver === 'single') {
2✔
64
                $config->set('logging.channels.deprecations', array_merge($config->get('logging.channels.single'), [
×
65
                    'path' => self::$app->storagePath(join_paths('logs', 'deprecations.log')),
×
66
                ]));
×
67
            } else {
68
                $config->set('logging.channels.deprecations', $config->get("logging.channels.{$driver}"));
2✔
69
            }
70

71
            $config->set('logging.deprecations', [
2✔
72
                'channel' => 'deprecations',
2✔
73
                'trace' => true,
2✔
74
            ]);
2✔
75
        });
2✔
76
    }
77

78
    /**
79
     * Determine if deprecation error should be ignored.
80
     *
81
     * @return bool
82
     */
83
    #[\Override]
84
    protected function shouldIgnoreDeprecationErrors()
85
    {
86
        return ! class_exists(LogManager::class)
2✔
87
            || ! self::$app->hasBeenBootstrapped()
2✔
88
            || ! Env::get('LOG_DEPRECATIONS_WHILE_TESTING', true);
2✔
89
    }
90

91
    /**
92
     * Clear the local application instance from memory.
93
     *
94
     * @return void
95
     *
96
     * @deprecated This method will be removed in a future Laravel version.
97
     */
98
    #[\Override]
99
    public static function forgetApp()
100
    {
UNCOV
101
        if (\is_null(self::$app)) {
×
102
            return;
×
103
        }
104

UNCOV
105
        self::flushHandlersState();
×
106

UNCOV
107
        self::$app = null;
×
108

UNCOV
109
        self::$reservedMemory = null;
×
110
    }
111

112
    /**
113
     * Flush the bootstrapper's global handlers state.
114
     *
115
     * @return void
116
     */
117
    public static function flushHandlersState()
118
    {
UNCOV
119
        while (true) {
×
UNCOV
120
            $previousHandler = set_exception_handler(static fn () => null);
×
UNCOV
121
            restore_exception_handler();
×
122

UNCOV
123
            if ($previousHandler === null) {
×
UNCOV
124
                break;
×
125
            }
126

UNCOV
127
            restore_exception_handler();
×
128
        }
129

UNCOV
130
        while (true) {
×
UNCOV
131
            $previousHandler = set_error_handler(static fn () => null);
×
UNCOV
132
            restore_error_handler();
×
133

UNCOV
134
            if ($previousHandler === null) {
×
UNCOV
135
                break;
×
136
            }
137

UNCOV
138
            restore_error_handler();
×
139
        }
140

UNCOV
141
        if (class_exists(ErrorHandler::class)) {
×
UNCOV
142
            $instance = ErrorHandler::instance();
×
143

UNCOV
144
            if ((fn () => $this->enabled ?? false)->call($instance)) {
×
UNCOV
145
                $instance->disable();
×
UNCOV
146
                $instance->enable();
×
147
            }
148
        }
149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc