Skip to content

Commit 419ce72

Browse files
[11.x] Test Improvements (#56630)
* Test Improvements - Fix integration with PHPUnit 12.3.4 - Fix PHPStan Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]>
1 parent 57bf892 commit 419ce72

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"league/flysystem-read-only": "^3.25.1",
112112
"league/flysystem-sftp-v3": "^3.25.1",
113113
"mockery/mockery": "^1.6.10",
114-
"orchestra/testbench-core": "^9.13.2",
114+
"orchestra/testbench-core": "9.x-dev#78a641cf",
115115
"pda/pheanstalk": "^5.0.6",
116116
"php-http/discovery": "^1.15",
117117
"phpstan/phpstan": "^2.0",

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Illuminate\Log\LogManager;
1010
use Illuminate\Support\Env;
1111
use Monolog\Handler\NullHandler;
12+
use PHPUnit\Framework\TestCase;
1213
use PHPUnit\Runner\ErrorHandler;
14+
use PHPUnit\Runner\Version;
1315
use Symfony\Component\Console\Output\ConsoleOutput;
1416
use Symfony\Component\ErrorHandler\Error\FatalError;
1517
use Throwable;
@@ -304,15 +306,16 @@ public static function forgetApp()
304306
/**
305307
* Flush the bootstrapper's global state.
306308
*
309+
* @param \PHPUnit\Framework\TestCase|null $testCase
307310
* @return void
308311
*/
309-
public static function flushState()
312+
public static function flushState(?TestCase $testCase = null)
310313
{
311314
if (is_null(static::$app)) {
312315
return;
313316
}
314317

315-
static::flushHandlersState();
318+
static::flushHandlersState($testCase);
316319

317320
static::$app = null;
318321

@@ -322,9 +325,10 @@ public static function flushState()
322325
/**
323326
* Flush the bootstrapper's global handlers state.
324327
*
328+
* @param \PHPUnit\Framework\TestCase|null $testCase
325329
* @return void
326330
*/
327-
public static function flushHandlersState()
331+
public static function flushHandlersState(?TestCase $testCase = null)
328332
{
329333
while (true) {
330334
$previousHandler = set_exception_handler(static fn () => null);
@@ -355,7 +359,12 @@ public static function flushHandlersState()
355359

356360
if ((fn () => $this->enabled ?? false)->call($instance)) {
357361
$instance->disable();
358-
$instance->enable();
362+
363+
if (version_compare(Version::id(), '12.3.4', '>=')) {
364+
$instance->enable($testCase);
365+
} else {
366+
$instance->enable();
367+
}
359368
}
360369
}
361370
}

src/Illuminate/Foundation/Testing/Concerns/InteractsWithRedis.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public function setUpRedis()
105105
*/
106106
public function tearDownRedis()
107107
{
108+
if (static::$connectionFailedOnceWithDefaultsSkip === true) {
109+
return;
110+
}
111+
108112
if (isset($this->redis['phpredis'])) {
109113
$this->redis['phpredis']->connection()->flushdb();
110114
}

src/Illuminate/Foundation/Testing/Concerns/InteractsWithTestCaseLifecycle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ protected function tearDownTheTestEnvironment(): void
175175
Factory::flushState();
176176
EncodedHtmlString::flushState();
177177
EncryptCookies::flushState();
178-
HandleExceptions::flushState();
178+
HandleExceptions::flushState($this);
179179
Markdown::flushState();
180180
Migrator::withoutMigrations([]);
181181
Once::flush();

tests/Foundation/Bootstrap/HandleExceptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function handleExceptions()
3838
protected function tearDown(): void
3939
{
4040
Application::setInstance(null);
41-
HandleExceptions::flushState();
41+
HandleExceptions::flushState($this);
4242

4343
m::close();
4444
}

types/Support/Helpers.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343

4444
function testThrowIf(float|int $foo, ?DateTime $bar = null): void
4545
{
46-
assertType('never', throw_if(true, Exception::class));
47-
assertType('false', throw_if(false, Exception::class));
46+
rescue(fn () => assertType('never', throw_if(true, Exception::class)));
47+
assertType('false', throw_if(false, Exception::class)); // @phpstan-ignore deadCode.unreachable
4848
assertType('false', throw_if(empty($foo)));
4949
throw_if(is_float($foo));
5050
assertType('int', $foo);
@@ -62,8 +62,8 @@ function testThrowIf(float|int $foo, ?DateTime $bar = null): void
6262
function testThrowUnless(float|int $foo, ?DateTime $bar = null): void
6363
{
6464
assertType('true', throw_unless(true, Exception::class));
65-
assertType('never', throw_unless(false, Exception::class));
66-
assertType('true', throw_unless(empty($foo)));
65+
rescue(fn () => assertType('never', throw_unless(false, Exception::class)));
66+
assertType('true', throw_unless(empty($foo))); // @phpstan-ignore deadCode.unreachable
6767
throw_unless(is_int($foo));
6868
assertType('int', $foo);
6969
throw_unless($foo == false);

0 commit comments

Comments
 (0)