Skip to content

Commit a161262

Browse files
authored
[8.x] Adds withoutDeprecationHandling (#39261)
* Adds `withoutDeprecationHandling` * Updates test * Updates tests
1 parent 6db59af commit a161262

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing\Concerns;
4+
5+
use ErrorException;
6+
7+
trait InteractsWithDeprecationHandling
8+
{
9+
/**
10+
* The original deprecation handler.
11+
*
12+
* @var callable|null
13+
*/
14+
protected $originalDeprecationHandler;
15+
16+
/**
17+
* Restore deprecation handling.
18+
*
19+
* @return $this
20+
*/
21+
protected function withDeprecationHandling()
22+
{
23+
if ($this->originalDeprecationHandler) {
24+
set_error_handler(tap($this->originalDeprecationHandler, function () {
25+
$this->originalDeprecationHandler = null;
26+
}));
27+
}
28+
29+
return $this;
30+
}
31+
32+
/**
33+
* Disable deprecation handling for the test.
34+
*
35+
* @return $this
36+
*/
37+
protected function withoutDeprecationHandling()
38+
{
39+
if ($this->originalDeprecationHandler == null) {
40+
$this->originalDeprecationHandler = set_error_handler(function ($level, $message, $file = '', $line = 0) {
41+
if (error_reporting() & $level) {
42+
throw new ErrorException($message, 0, $level, $file, $line);
43+
}
44+
});
45+
}
46+
47+
return $this;
48+
}
49+
}

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class TestCase extends BaseTestCase
2222
Concerns\InteractsWithAuthentication,
2323
Concerns\InteractsWithConsole,
2424
Concerns\InteractsWithDatabase,
25+
Concerns\InteractsWithDeprecationHandling,
2526
Concerns\InteractsWithExceptionHandling,
2627
Concerns\InteractsWithSession,
2728
Concerns\InteractsWithTime,
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Testing\Concerns;
4+
5+
use ErrorException;
6+
use Illuminate\Foundation\Testing\Concerns\InteractsWithDeprecationHandling;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class InteractsWithDeprecationHandlingTest extends TestCase
10+
{
11+
use InteractsWithDeprecationHandling;
12+
13+
protected $original;
14+
15+
protected $deprecationsFound = false;
16+
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->original = set_error_handler(function () {
22+
$this->deprecationsFound = true;
23+
});
24+
}
25+
26+
public function testWithDeprecationHandling()
27+
{
28+
$this->withDeprecationHandling();
29+
30+
trigger_error('Something is deprecated', E_USER_DEPRECATED);
31+
32+
$this->assertTrue($this->deprecationsFound);
33+
}
34+
35+
public function testWithoutDeprecationHandling()
36+
{
37+
$this->withoutDeprecationHandling();
38+
39+
$this->expectException(ErrorException::class);
40+
$this->expectExceptionMessage('Something is deprecated');
41+
42+
trigger_error('Something is deprecated', E_USER_DEPRECATED);
43+
}
44+
45+
public function tearDown(): void
46+
{
47+
set_error_handler($this->original);
48+
49+
$this->originalDeprecationHandler = null;
50+
$this->deprecationsFound = false;
51+
52+
parent::tearDown();
53+
}
54+
}

0 commit comments

Comments
 (0)