Skip to content

Commit

Permalink
Add Orchestra\Testing\Concerns\TestingHooks
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone committed Oct 26, 2023
1 parent ba625e8 commit 46a5df4
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 198 deletions.
220 changes: 22 additions & 198 deletions src/Concerns/Testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@

namespace Orchestra\Testbench\Concerns;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Console\Application as Artisan;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Bootstrap\HandleExceptions;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\DatabaseTruncation;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\WithoutEvents;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Queue\Queue;
use Illuminate\Support\Facades\ParallelTesting;
use Illuminate\Support\LazyCollection;
use Illuminate\View\Component;
use Mockery;
use PHPUnit\Framework\TestCase;
use Throwable;

trait Testing
{
Expand All @@ -30,43 +19,9 @@ trait Testing
use HandlesDatabases;
use HandlesRoutes;
use InteractsWithMigrations;
use TestingHooks;
use WithFactories;

/**
* The Illuminate application instance.
*
* @var \Illuminate\Foundation\Application|null
*/
protected $app;

/**
* The callbacks that should be run after the application is created.
*
* @var array<int, callable():void>
*/
protected $afterApplicationCreatedCallbacks = [];

/**
* The callbacks that should be run after the application is refreshed.
*
* @var array<int, callable():void>
*/
protected $afterApplicationRefreshedCallbacks = [];

/**
* The callbacks that should be run before the application is destroyed.
*
* @var array<int, callable():void>
*/
protected $beforeApplicationDestroyedCallbacks = [];

/**
* The exception thrown while running an application destruction callback.
*
* @var \Throwable|null
*/
protected $callbackException;

/**
* Indicates if we have made it through the base setUp function.
*
Expand All @@ -85,26 +40,15 @@ final protected function setUpTheTestEnvironment(): void
{
if (! $this->app) {
$this->refreshApplication();

$this->setUpParallelTestingCallbacks();
}

/** @var \Illuminate\Foundation\Application $app */
$app = $this->app;

foreach ($this->afterApplicationRefreshedCallbacks as $callback) {
\call_user_func($callback);
}

$this->setUpTraits();

foreach ($this->afterApplicationCreatedCallbacks as $callback) {
\call_user_func($callback);
}

Model::setEventDispatcher($app['events']);

$this->setUpHasRun = true;
$this->setUpTheTestingHooks($app, function () {
$this->setUpTraits();
$this->setUpHasRun = true;
});
}

/**
Expand All @@ -116,61 +60,29 @@ final protected function setUpTheTestEnvironment(): void
*/
final protected function tearDownTheTestEnvironment(): void
{
if ($this->app) {
$this->callBeforeApplicationDestroyedCallbacks();

$this->tearDownParallelTestingCallbacks();

$this->app?->flush();

$this->app = null;
}

$this->setUpHasRun = false;

if (property_exists($this, 'serverVariables')) {
$this->serverVariables = [];
}

if (property_exists($this, 'defaultHeaders')) {
$this->defaultHeaders = [];
}

if (class_exists(Mockery::class)) {
if ($container = Mockery::getContainer()) {
$this->addToAssertionCount($container->mockery_getExpectationCount());
$this->tearDownTheTestingHooks($this->app, function () {
if (! \is_null($this->app)) {
$this->app = null;
}

Mockery::close();
}

Carbon::setTestNow();
$this->setUpHasRun = false;

if (class_exists(CarbonImmutable::class)) {
CarbonImmutable::setTestNow();
}

$this->afterApplicationCreatedCallbacks = [];
$this->beforeApplicationDestroyedCallbacks = [];

if (property_exists($this, 'originalExceptionHandler')) {
$this->originalExceptionHandler = null;
}
if (property_exists($this, 'serverVariables')) {
$this->serverVariables = [];
}

if (property_exists($this, 'originalDeprecationHandler')) {
$this->originalDeprecationHandler = null;
}
if (property_exists($this, 'defaultHeaders')) {
$this->defaultHeaders = [];
}

Artisan::forgetBootstrappers();
Component::flushCache();
Component::forgetComponentsResolver();
Component::forgetFactory();
Queue::createPayloadUsing(null);
HandleExceptions::forgetApp();
if (property_exists($this, 'originalExceptionHandler')) {
$this->originalExceptionHandler = null;
}

if ($this->callbackException) {
throw $this->callbackException;
}
if (property_exists($this, 'originalDeprecationHandler')) {
$this->originalDeprecationHandler = null;
}
});
}

/**
Expand Down Expand Up @@ -263,87 +175,6 @@ protected function setUpTheTestEnvironmentTraitToBeIgnored(string $use): bool
return false;
}

/**
* Setup parallel testing callback.
*/
protected function setUpParallelTestingCallbacks(): void
{
if (class_exists(ParallelTesting::class) && $this instanceof TestCase) {
/** @phpstan-ignore-next-line */
ParallelTesting::callSetUpTestCaseCallbacks($this);
}
}

/**
* Teardown parallel testing callback.
*/
protected function tearDownParallelTestingCallbacks(): void
{
if (class_exists(ParallelTesting::class) && $this instanceof TestCase) {
/** @phpstan-ignore-next-line */
ParallelTesting::callTearDownTestCaseCallbacks($this);
}
}

/**
* Register a callback to be run after the application is refreshed.
*
* @param callable():void $callback
* @return void
*/
protected function afterApplicationRefreshed(callable $callback): void
{
$this->afterApplicationRefreshedCallbacks[] = $callback;

if ($this->setUpHasRun) {
\call_user_func($callback);
}
}

/**
* Register a callback to be run after the application is created.
*
* @param callable():void $callback
* @return void
*/
protected function afterApplicationCreated(callable $callback): void
{
$this->afterApplicationCreatedCallbacks[] = $callback;

if ($this->setUpHasRun) {
\call_user_func($callback);
}
}

/**
* Register a callback to be run before the application is destroyed.
*
* @param callable():void $callback
* @return void
*/
protected function beforeApplicationDestroyed(callable $callback): void
{
array_unshift($this->beforeApplicationDestroyedCallbacks, $callback);
}

/**
* Execute the application's pre-destruction callbacks.
*
* @return void
*/
protected function callBeforeApplicationDestroyedCallbacks()
{
foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
try {
\call_user_func($callback);
} catch (Throwable $e) {
if (! $this->callbackException) {
$this->callbackException = $e;
}
}
}
}

/**
* Reload the application instance with cached routes.
*/
Expand All @@ -359,11 +190,4 @@ protected function reloadApplication(): void
* @return array<class-string, class-string>
*/
abstract protected function setUpTraits();

/**
* Refresh the application instance.
*
* @return void
*/
abstract protected function refreshApplication();
}
Loading

0 comments on commit 46a5df4

Please sign in to comment.