7
7
use Illuminate \Foundation \Application ;
8
8
use Illuminate \Foundation \Bootstrap \HandleExceptions ;
9
9
use Illuminate \Log \LogManager ;
10
+ use Illuminate \Support \Env ;
10
11
use Mockery as m ;
11
12
use Monolog \Handler \NullHandler ;
12
13
use PHPUnit \Framework \TestCase ;
13
14
use ReflectionClass ;
15
+ use RuntimeException ;
14
16
15
17
class HandleExceptionsTest extends TestCase
16
18
{
17
19
protected $ app ;
18
20
protected $ config ;
19
- protected $ handleExceptions ;
20
21
21
22
protected function setUp (): void
22
23
{
@@ -27,40 +28,38 @@ protected function setUp(): void
27
28
$ this ->app ->singleton ('config ' , function () {
28
29
return $ this ->config ;
29
30
});
31
+ }
30
32
31
- $ this ->handleExceptions = new HandleExceptions ();
32
-
33
- with (new ReflectionClass ($ this ->handleExceptions ), function ($ reflection ) {
34
- $ property = $ reflection ->getProperty ('app ' );
35
-
36
- $ property ->setValue (
37
- $ this ->handleExceptions ,
38
- tap ($ this ->app , function ($ app ) {
39
- $ app ->shouldReceive ('runningUnitTests ' )->andReturn (false );
40
- $ app ->shouldReceive ('hasBeenBootstrapped ' )->andReturn (true );
41
- })
42
- );
33
+ protected function handleExceptions ()
34
+ {
35
+ return tap (new HandleExceptions (), function ($ instance ) {
36
+ with (new ReflectionClass ($ instance ), function ($ reflection ) use ($ instance ) {
37
+ $ reflection ->getProperty ('app ' )->setValue ($ instance , $ this ->app );
38
+ });
43
39
});
44
40
}
45
41
46
42
protected function tearDown (): void
47
43
{
48
44
Application::setInstance (null );
45
+ m::close ();
49
46
}
50
47
51
48
public function testPhpDeprecations ()
52
49
{
53
50
$ logger = m::mock (LogManager::class);
54
51
$ this ->app ->instance (LogManager::class, $ logger );
52
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
53
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
55
54
56
- $ logger ->shouldReceive ('channel ' )->with ('deprecations ' )->andReturnSelf ();
57
- $ logger ->shouldReceive ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
55
+ $ logger ->expects ('channel ' )->with ('deprecations ' )->andReturnSelf ();
56
+ $ logger ->expects ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
58
57
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
59
58
'/home/user/laravel/routes/web.php ' ,
60
59
17
61
60
));
62
61
63
- $ this ->handleExceptions ->handleError (
62
+ $ this ->handleExceptions () ->handleError (
64
63
E_DEPRECATED ,
65
64
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
66
65
'/home/user/laravel/routes/web.php ' ,
@@ -72,14 +71,16 @@ public function testPhpDeprecationsWithStackTraces()
72
71
{
73
72
$ logger = m::mock (LogManager::class);
74
73
$ this ->app ->instance (LogManager::class, $ logger );
74
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
75
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
75
76
76
77
$ this ->config ->set ('logging.deprecations ' , [
77
78
'channel ' => 'null ' ,
78
79
'trace ' => true ,
79
80
]);
80
81
81
- $ logger ->shouldReceive ('channel ' )->with ('deprecations ' )->andReturnSelf ();
82
- $ logger ->shouldReceive ('warning ' )->with (
82
+ $ logger ->expects ('channel ' )->with ('deprecations ' )->andReturnSelf ();
83
+ $ logger ->expects ('warning ' )->with (
83
84
m::on (fn (string $ message ) => (bool ) preg_match (
84
85
<<<REGEXP
85
86
#ErrorException: str_contains\(\): Passing null to parameter \#2 \(\\ \$needle\) of type string is deprecated in /home/user/laravel/routes/web\.php:17
@@ -94,7 +95,7 @@ public function testPhpDeprecationsWithStackTraces()
94
95
))
95
96
);
96
97
97
- $ this ->handleExceptions ->handleError (
98
+ $ this ->handleExceptions () ->handleError (
98
99
E_DEPRECATED ,
99
100
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
100
101
'/home/user/laravel/routes/web.php ' ,
@@ -106,20 +107,22 @@ public function testNullValueAsChannelUsesNullDriver()
106
107
{
107
108
$ logger = m::mock (LogManager::class);
108
109
$ this ->app ->instance (LogManager::class, $ logger );
110
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
111
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
109
112
110
113
$ this ->config ->set ('logging.deprecations ' , [
111
114
'channel ' => null ,
112
115
'trace ' => false ,
113
116
]);
114
117
115
- $ logger ->shouldReceive ('channel ' )->with ('deprecations ' )->andReturnSelf ();
116
- $ logger ->shouldReceive ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
118
+ $ logger ->expects ('channel ' )->with ('deprecations ' )->andReturnSelf ();
119
+ $ logger ->expects ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
117
120
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
118
121
'/home/user/laravel/routes/web.php ' ,
119
122
17
120
123
));
121
124
122
- $ this ->handleExceptions ->handleError (
125
+ $ this ->handleExceptions () ->handleError (
123
126
E_DEPRECATED ,
124
127
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
125
128
'/home/user/laravel/routes/web.php ' ,
@@ -136,15 +139,17 @@ public function testUserDeprecations()
136
139
{
137
140
$ logger = m::mock (LogManager::class);
138
141
$ this ->app ->instance (LogManager::class, $ logger );
142
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
143
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
139
144
140
- $ logger ->shouldReceive ('channel ' )->with ('deprecations ' )->andReturnSelf ();
141
- $ logger ->shouldReceive ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
145
+ $ logger ->expects ('channel ' )->with ('deprecations ' )->andReturnSelf ();
146
+ $ logger ->expects ('warning ' )->with (sprintf ('%s in %s on line %s ' ,
142
147
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
143
148
'/home/user/laravel/routes/web.php ' ,
144
149
17
145
150
));
146
151
147
- $ this ->handleExceptions ->handleError (
152
+ $ this ->handleExceptions () ->handleError (
148
153
E_USER_DEPRECATED ,
149
154
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
150
155
'/home/user/laravel/routes/web.php ' ,
@@ -156,14 +161,16 @@ public function testUserDeprecationsWithStackTraces()
156
161
{
157
162
$ logger = m::mock (LogManager::class);
158
163
$ this ->app ->instance (LogManager::class, $ logger );
164
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
165
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
159
166
160
167
$ this ->config ->set ('logging.deprecations ' , [
161
168
'channel ' => 'null ' ,
162
169
'trace ' => true ,
163
170
]);
164
171
165
- $ logger ->shouldReceive ('channel ' )->with ('deprecations ' )->andReturnSelf ();
166
- $ logger ->shouldReceive ('warning ' )->with (
172
+ $ logger ->expects ('channel ' )->with ('deprecations ' )->andReturnSelf ();
173
+ $ logger ->expects ('warning ' )->with (
167
174
m::on (fn (string $ message ) => (bool ) preg_match (
168
175
<<<REGEXP
169
176
#ErrorException: str_contains\(\): Passing null to parameter \#2 \(\\ \$needle\) of type string is deprecated in /home/user/laravel/routes/web\.php:17
@@ -178,7 +185,7 @@ public function testUserDeprecationsWithStackTraces()
178
185
))
179
186
);
180
187
181
- $ this ->handleExceptions ->handleError (
188
+ $ this ->handleExceptions () ->handleError (
182
189
E_USER_DEPRECATED ,
183
190
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
184
191
'/home/user/laravel/routes/web.php ' ,
@@ -197,7 +204,7 @@ public function testErrors()
197
204
$ this ->expectException (ErrorException::class);
198
205
$ this ->expectExceptionMessage ('Something went wrong ' );
199
206
200
- $ this ->handleExceptions ->handleError (
207
+ $ this ->handleExceptions () ->handleError (
201
208
E_ERROR ,
202
209
'Something went wrong ' ,
203
210
'/home/user/laravel/src/Providers/AppServiceProvider.php ' ,
@@ -209,9 +216,11 @@ public function testEnsuresDeprecationsDriver()
209
216
{
210
217
$ logger = m::mock (LogManager::class);
211
218
$ this ->app ->instance (LogManager::class, $ logger );
219
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
220
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
212
221
213
- $ logger ->shouldReceive ('channel ' )->andReturnSelf ();
214
- $ logger ->shouldReceive ('warning ' );
222
+ $ logger ->expects ('channel ' )->andReturnSelf ();
223
+ $ logger ->expects ('warning ' );
215
224
216
225
$ this ->config ->set ('logging.channels.stack ' , [
217
226
'driver ' => 'stack ' ,
@@ -220,7 +229,7 @@ public function testEnsuresDeprecationsDriver()
220
229
]);
221
230
$ this ->config ->set ('logging.deprecations ' , 'stack ' );
222
231
223
- $ this ->handleExceptions ->handleError (
232
+ $ this ->handleExceptions () ->handleError (
224
233
E_USER_DEPRECATED ,
225
234
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
226
235
'/home/user/laravel/routes/web.php ' ,
@@ -241,11 +250,13 @@ public function testEnsuresNullDeprecationsDriver()
241
250
{
242
251
$ logger = m::mock (LogManager::class);
243
252
$ this ->app ->instance (LogManager::class, $ logger );
253
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
254
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
244
255
245
- $ logger ->shouldReceive ('channel ' )->andReturnSelf ();
246
- $ logger ->shouldReceive ('warning ' );
256
+ $ logger ->expects ('channel ' )->andReturnSelf ();
257
+ $ logger ->expects ('warning ' );
247
258
248
- $ this ->handleExceptions ->handleError (
259
+ $ this ->handleExceptions () ->handleError (
249
260
E_USER_DEPRECATED ,
250
261
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
251
262
'/home/user/laravel/routes/web.php ' ,
@@ -262,11 +273,13 @@ public function testEnsuresNullLogDriver()
262
273
{
263
274
$ logger = m::mock (LogManager::class);
264
275
$ this ->app ->instance (LogManager::class, $ logger );
276
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
277
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
265
278
266
- $ logger ->shouldReceive ('channel ' )->andReturnSelf ();
267
- $ logger ->shouldReceive ('warning ' );
279
+ $ logger ->expects ('channel ' )->andReturnSelf ();
280
+ $ logger ->expects ('warning ' );
268
281
269
- $ this ->handleExceptions ->handleError (
282
+ $ this ->handleExceptions () ->handleError (
270
283
E_USER_DEPRECATED ,
271
284
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
272
285
'/home/user/laravel/routes/web.php ' ,
@@ -283,16 +296,18 @@ public function testDoNotOverrideExistingNullLogDriver()
283
296
{
284
297
$ logger = m::mock (LogManager::class);
285
298
$ this ->app ->instance (LogManager::class, $ logger );
299
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
300
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
286
301
287
- $ logger ->shouldReceive ('channel ' )->andReturnSelf ();
288
- $ logger ->shouldReceive ('warning ' );
302
+ $ logger ->expects ('channel ' )->andReturnSelf ();
303
+ $ logger ->expects ('warning ' );
289
304
290
305
$ this ->config ->set ('logging.channels.null ' , [
291
306
'driver ' => 'monolog ' ,
292
307
'handler ' => CustomNullHandler::class,
293
308
]);
294
309
295
- $ this ->handleExceptions ->handleError (
310
+ $ this ->handleExceptions () ->handleError (
296
311
E_USER_DEPRECATED ,
297
312
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
298
313
'/home/user/laravel/routes/web.php ' ,
@@ -313,7 +328,50 @@ public function testNoDeprecationsDriverIfNoDeprecationsHereSend()
313
328
314
329
public function testIgnoreDeprecationIfLoggerUnresolvable ()
315
330
{
316
- $ this ->handleExceptions ->handleError (
331
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (false );
332
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
333
+
334
+ $ this ->handleExceptions ()->handleError (
335
+ E_DEPRECATED ,
336
+ 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
337
+ '/home/user/laravel/routes/web.php ' ,
338
+ 17
339
+ );
340
+ }
341
+
342
+ public function testItIgnoreDeprecationLoggingWhenRunningUnitTests ()
343
+ {
344
+ $ resolved = false ;
345
+ $ this ->app ->bind (LogManager::class, function () use (&$ resolved ) {
346
+ $ resolved = true ;
347
+
348
+ throw new RuntimeException ();
349
+ });
350
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (true );
351
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
352
+
353
+ $ this ->handleExceptions ()->handleError (
354
+ E_DEPRECATED ,
355
+ 'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
356
+ '/home/user/laravel/routes/web.php ' ,
357
+ 17
358
+ );
359
+
360
+ $ this ->assertFalse ($ resolved );
361
+ }
362
+
363
+ public function testItCanForceViaConfigDeprecationLoggingWhenRunningUnitTests ()
364
+ {
365
+ $ logger = m::mock (LogManager::class);
366
+ $ logger ->expects ('channel ' )->andReturnSelf ();
367
+ $ logger ->expects ('warning ' );
368
+ $ this ->app ->instance (LogManager::class, $ logger );
369
+ $ this ->app ->expects ('runningUnitTests ' )->andReturn (true );
370
+ $ this ->app ->expects ('hasBeenBootstrapped ' )->andReturn (true );
371
+
372
+ Env::getRepository ()->set ('LOG_DEPRECATIONS_WHILE_TESTING ' , true );
373
+
374
+ $ this ->handleExceptions ()->handleError (
317
375
E_DEPRECATED ,
318
376
'str_contains(): Passing null to parameter #2 ($needle) of type string is deprecated ' ,
319
377
'/home/user/laravel/routes/web.php ' ,
@@ -323,31 +381,35 @@ public function testIgnoreDeprecationIfLoggerUnresolvable()
323
381
324
382
public function testForgetApp ()
325
383
{
326
- $ appResolver = fn () => with (new ReflectionClass ($ this ->handleExceptions ), function ($ reflection ) {
384
+ $ instance = $ this ->handleExceptions ();
385
+
386
+ $ appResolver = fn () => with (new ReflectionClass ($ instance ), function ($ reflection ) use ($ instance ) {
327
387
$ property = $ reflection ->getProperty ('app ' );
328
388
329
- return $ property ->getValue ($ this -> handleExceptions );
389
+ return $ property ->getValue ($ instance );
330
390
});
331
391
332
392
$ this ->assertNotNull ($ appResolver ());
333
393
334
- handleExceptions ::forgetApp ();
394
+ HandleExceptions ::forgetApp ();
335
395
336
396
$ this ->assertNull ($ appResolver ());
337
397
}
338
398
339
399
public function testHandlerForgetsPreviousApp ()
340
400
{
341
- $ appResolver = fn () => with (new ReflectionClass ($ this ->handleExceptions ), function ($ reflection ) {
401
+ $ instance = $ this ->handleExceptions ();
402
+
403
+ $ appResolver = fn () => with (new ReflectionClass ($ instance ), function ($ reflection ) use ($ instance ) {
342
404
$ property = $ reflection ->getProperty ('app ' );
343
405
344
- return $ property ->getValue ($ this -> handleExceptions );
406
+ return $ property ->getValue ($ instance );
345
407
});
346
408
347
409
$ this ->assertSame ($ this ->app , $ appResolver ());
348
410
349
- $ this -> handleExceptions ->bootstrap ($ newApp = tap (m::mock (Application::class), function ($ app ) {
350
- $ app ->shouldReceive ('environment ' )-> once ( )->andReturn (true );
411
+ $ instance ->bootstrap ($ newApp = tap (m::mock (Application::class), function ($ app ) {
412
+ $ app ->expects ('environment ' )->andReturn (true );
351
413
}));
352
414
353
415
$ this ->assertNotSame ($ this ->app , $ appResolver ());
0 commit comments