Skip to content

Commit

Permalink
Reset exception handler for PHPUnit 10. (#180)
Browse files Browse the repository at this point in the history
* Reset exception handler for PHPUnit 10.

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]>

* 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]>
  • Loading branch information
crynobone authored Jan 10, 2024
1 parent a8a53ca commit 2e3b653
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/strict-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "~10.1.0"
- "~10.4.0"
- "~10.5.0"
- "10.5.4"
dependencies:
- "highest"
experimental:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- "~10.1.0"
- "~10.4.0"
- "~10.5.0"
- "10.5.4"
dependencies:
- "highest"
- "lowest"
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"laravel/framework": "<10.40 || >=11.0.0",
"nunomaduro/collision": "<6.4.0 || >=7.0.0 <7.4.0 || >=8.0.0",
"orchestra/workbench": "<1.0.0",
"phpunit/phpunit": "<9.6.0 || 10.5.4 || >=10.6.0"
"phpunit/phpunit": "<9.6.0 || >=10.6.0"
},
"suggest": {
"ext-pcntl": "Required to use all features of the console signal trapping.",
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ parameters:
count: 1
path: src/Bootstrap/ConfigureRay.php

-
message: "#^Parameter \\#1 \\$callback of function set_error_handler expects \\(callable\\(int, string, string, int\\)\\: bool\\)\\|null, Closure\\(\\)\\: null given\\.$#"
count: 1
path: src/Bootstrap/HandleExceptions.php

-
message: "#^Static property Illuminate\\\\Foundation\\\\Bootstrap\\\\HandleExceptions\\:\\:\\$app \\(Illuminate\\\\Contracts\\\\Foundation\\\\Application\\) does not accept null\\.$#"
count: 1
path: src/Bootstrap/HandleExceptions.php

-
message: "#^Call to an undefined method Illuminate\\\\Contracts\\\\Foundation\\\\Application\\:\\:environmentFile\\(\\)\\.$#"
count: 1
Expand Down
61 changes: 61 additions & 0 deletions src/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Log\LogManager;
use Orchestra\Testbench\Exceptions\DeprecatedException;
use Orchestra\Testbench\Foundation\Env;
use PHPUnit\Runner\ErrorHandler;

use function Illuminate\Filesystem\join_paths;

Expand Down Expand Up @@ -86,4 +87,64 @@ protected function shouldIgnoreDeprecationErrors()
|| ! self::$app->hasBeenBootstrapped()
|| ! Env::get('LOG_DEPRECATIONS_WHILE_TESTING', true);
}

/**
* Clear the local application instance from memory.
*
* @return void
*
* @deprecated This method will be removed in a future Laravel version.
*/
#[\Override]
public static function forgetApp()
{
if (\is_null(self::$app)) {
return;
}

self::flushHandlersState();

self::$app = null;

self::$reservedMemory = null;
}

/**
* Flush the bootstrapper's global handlers state.
*
* @return void
*/
public static function flushHandlersState()
{
while (true) {
$previousHandler = set_exception_handler(static fn () => null);
restore_exception_handler();

if ($previousHandler === null) {
break;
}

restore_exception_handler();
}

while (true) {
$previousHandler = set_error_handler(static fn () => null);
restore_error_handler();

if ($previousHandler === null) {
break;
}

restore_error_handler();
}

if (class_exists(ErrorHandler::class)) {
$instance = ErrorHandler::instance();

if ((fn () => $this->enabled ?? false)->call($instance)) {
$instance->disable();
$instance->enable();
}
}
}
}
20 changes: 2 additions & 18 deletions src/Concerns/ApplicationTestingHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Closure;
use Illuminate\Console\Application as Artisan;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bootstrap\HandleExceptions;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\TrimStrings;
use Illuminate\Queue\Queue;
use Illuminate\Support\Facades\ParallelTesting;
use Illuminate\Support\Sleep;
use Illuminate\View\Component;
use Mockery;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use Throwable;

Expand Down Expand Up @@ -144,16 +137,7 @@ final protected function tearDownTheApplicationTestingHooks(?Closure $callback =
$this->afterApplicationCreatedCallbacks = [];
$this->beforeApplicationDestroyedCallbacks = [];

Artisan::forgetBootstrappers();
Component::flushCache();
Component::forgetComponentsResolver();
Component::forgetFactory();
ConvertEmptyStringsToNull::flushState();
HandleExceptions::forgetApp();
Queue::createPayloadUsing(null);
Sleep::fake(false);
AboutCommand::flushState();
TrimStrings::flushState();
Testbench::flushState();

if ($this->callbackException) {
throw $this->callbackException;
Expand Down
1 change: 1 addition & 0 deletions src/Console/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function handle()
} finally {
$this->handleTerminatingConsole();
Workbench::flush();
Application::flushState();

$this->untrap();
}
Expand Down
25 changes: 25 additions & 0 deletions src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace Orchestra\Testbench\Foundation;

use Illuminate\Console\Application as Artisan;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\TrimStrings;
use Illuminate\Queue\Queue;
use Illuminate\Support\Arr;
use Illuminate\Support\Sleep;
use Illuminate\View\Component;
use Orchestra\Testbench\Bootstrap\HandleExceptions;
use Orchestra\Testbench\Concerns\CreatesApplication;
use Orchestra\Testbench\Contracts\Config as ConfigContract;
use Orchestra\Testbench\Workbench\Workbench;
Expand Down Expand Up @@ -159,6 +166,24 @@ public static function createFromConfig(ConfigContract $config, ?callable $resol
return static::makeFromConfig($config, $resolvingCallback, $options)->createApplication();
}

/**
* Flush the application states.
*
* @return void
*/
public static function flushState(): void
{
Artisan::forgetBootstrappers();
Component::flushCache();
Component::forgetComponentsResolver();
Component::forgetFactory();
ConvertEmptyStringsToNull::flushState();
HandleExceptions::forgetApp();
Queue::createPayloadUsing(null);
Sleep::fake(false);
TrimStrings::flushState();
}

/**
* Configure the application options.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/CreatesApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

use Illuminate\Foundation\Application;
use Orchestra\Testbench\Concerns\CreatesApplication;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;

class CreatesApplicationTest extends TestCase
{
use CreatesApplication;

/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
Testbench::flushState();
}

/** @test */
public function it_properly_loads_laravel_application()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/Foundation/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

class ApplicationTest extends TestCase
{
/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
Application::flushState();
}

/**
* @test
*
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Foundation\Application;
use Orchestra\Testbench\Contracts\TestCase as TestCaseContract;
use Orchestra\Testbench\Foundation\Application as Testbench;
use PHPUnit\Framework\TestCase;

use function Orchestra\Testbench\container;
use function Orchestra\Testbench\phpunit_version_compare;

class TestCaseTest extends TestCase
{
/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
Testbench::flushState();
}

/** @test */
public function it_can_create_the_testcase()
{
Expand Down

0 comments on commit 2e3b653

Please sign in to comment.