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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illuminate\Foundation\Testing\Concerns;

trait ComposesTemplateMethods
{
/**
* Cache of template methods for given test cases.
*
* @var array
*/
protected static $traitTemplateMethods = [];

/**
* Get a list of template methods from traits.
*
* @param string $template
* @return array
*/
protected static function getTraitTemplateMethods($template)
{
$class = static::class;

if (! isset(static::$traitTemplateMethods[$class])) {
$templateMethods = ['setUp', 'tearDown'];
static::$traitTemplateMethods[$class] = array_fill_keys($templateMethods, []);

foreach (class_uses_recursive($class) as $trait) {
foreach ($templateMethods as $templateMethod) {
if (method_exists($class, $method = $templateMethod.class_basename($trait))) {
static::$traitTemplateMethods[$class][$templateMethod][] = $method;
}
}
}
}

return static::$traitTemplateMethods[$class][$template];
}

/**
* Call composed template methods for all used traits.
*
* @param string $template
* @return void
*/
protected function callTraitTemplateMethods($template)
{
foreach (static::getTraitTemplateMethods($template) as $method) {
$this->$method();
}
}
}
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/DatabaseMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait DatabaseMigrations
*
* @return void
*/
public function runDatabaseMigrations()
public function setUpDatabaseMigrations()
{
$this->artisan('migrate:fresh');

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/DatabaseTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait DatabaseTransactions
*
* @return void
*/
public function beginDatabaseTransaction()
public function setUpDatabaseTransactions()
{
$database = $this->app->make('db');

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/RefreshDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait RefreshDatabase
*
* @return void
*/
public function refreshDatabase()
public function setUpRefreshDatabase()
{
$this->usingInMemoryDatabase()
? $this->refreshInMemoryDatabase()
Expand Down
39 changes: 5 additions & 34 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ abstract class TestCase extends BaseTestCase
Concerns\InteractsWithDatabase,
Concerns\InteractsWithExceptionHandling,
Concerns\InteractsWithSession,
Concerns\MocksApplicationServices;
Concerns\MocksApplicationServices,
Concerns\ComposesTemplateMethods;

/**
* The Illuminate application instance.
Expand Down Expand Up @@ -68,7 +69,7 @@ protected function setUp()
$this->refreshApplication();
}

$this->setUpTraits();
$this->callTraitTemplateMethods('setUp');

foreach ($this->afterApplicationCreatedCallbacks as $callback) {
call_user_func($callback);
Expand All @@ -91,45 +92,15 @@ protected function refreshApplication()
$this->app = $this->createApplication();
}

/**
* Boot the testing helper traits.
*
* @return array
*/
protected function setUpTraits()
{
$uses = array_flip(class_uses_recursive(static::class));

if (isset($uses[RefreshDatabase::class])) {
$this->refreshDatabase();
}

if (isset($uses[DatabaseMigrations::class])) {
$this->runDatabaseMigrations();
}

if (isset($uses[DatabaseTransactions::class])) {
$this->beginDatabaseTransaction();
}

if (isset($uses[WithoutMiddleware::class])) {
$this->disableMiddlewareForAllTests();
}

if (isset($uses[WithoutEvents::class])) {
$this->disableEventsForAllTests();
}

return $uses;
}

/**
* Clean up the testing environment before the next test.
*
* @return void
*/
protected function tearDown()
{
$this->callTraitTemplateMethods('tearDown');

if ($this->app) {
foreach ($this->beforeApplicationDestroyedCallbacks as $callback) {
call_user_func($callback);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/WithoutEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait WithoutEvents
*
* @throws \Exception
*/
public function disableEventsForAllTests()
public function setUpWithoutEvents()
{
if (method_exists($this, 'withoutEvents')) {
$this->withoutEvents();
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/WithoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait WithoutMiddleware
*
* @throws \Exception
*/
public function disableMiddlewareForAllTests()
public function setUpWithoutMiddleware()
{
if (method_exists($this, 'withoutMiddleware')) {
$this->withoutMiddleware();
Expand Down
57 changes: 57 additions & 0 deletions tests/Foundation/FoundationComposesTemplateMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Illuminate\Tests\Foundation;

use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Testing\Concerns\ComposesTemplateMethods;

class FoundationComposesTemplateMethodsTest extends TestCase
{
use ComposesTemplateMethods, TestTraitOne, TestTraitTwo;

public function testCallsComposedSetUpMethods()
{
$this->callTraitTemplateMethods('setUp');

$this->assertTrue($this->testTraitOneSetUp);
$this->assertTrue($this->testTraitTwoSetUp);
}

public function testCallsComposedTearDownMethods()
{
$this->callTraitTemplateMethods('tearDown');

$this->assertFalse($this->testTraitOneSetUp);
$this->assertFalse($this->testTraitTwoSetUp);
}
}

trait TestTraitOne
{
public $testTraitOneSetUp;

public function setUpTestTraitOne()
{
$this->testTraitOneSetUp = true;
}

public function tearDownTestTraitOne()
{
$this->testTraitOneSetUp = false;
}
}

trait TestTraitTwo
{
public $testTraitTwoSetUp;

public function setUpTestTraitTwo()
{
$this->testTraitTwoSetUp = true;
}

public function tearDownTestTraitTwo()
{
$this->testTraitTwoSetUp = false;
}
}