Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Testing/InitializeTraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Contracts\Testing;

interface InitializeTraits
{
//
}
23 changes: 23 additions & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Console\Application as Artisan;
use Illuminate\Contracts\Testing\InitializeTraits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\Queue;
use Illuminate\Support\Facades\Facade;
Expand Down Expand Up @@ -94,6 +95,10 @@ protected function setUp(): void
$callback();
}

if ($this instanceof InitializeTraits) {
$this->initializeFirstLevelTraits();
}

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

$this->setUpHasRun = true;
Expand Down Expand Up @@ -145,6 +150,24 @@ protected function setUpTraits()
return $uses;
}

/**
* Initialize first-level testing traits and register their tear-down callbacks.
*
* @return void
*/
protected function initializeFirstLevelTraits()
{
foreach (class_uses(static::class) as $trait) {
if (method_exists($this, $setUpMethod = 'setUp'.class_basename($trait))) {
$this->{$setUpMethod}();
}

if (method_exists($this, $tearDownMethod = 'tearDown'.class_basename($trait))) {
$this->beforeApplicationDestroyed([$this, $tearDownMethod]);
}
}
}

/**
* Clean up the testing environment before the next test.
*
Expand Down