Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
run setUp once for all test methods after app boot
<?php
namespace Tests\Traits\Common;
trait HasSetUpOnce
{
/**
* flag to save setUp status when run test methods.
*
* @var bool
*/
protected static $setUpOnce = false;
/**
* execute closure once per class from setUp method after app boot.
*
* @param \Closure $closure
*/
protected function setUpOnce(\Closure $closure)
{
// check if already run return early
if (self::$setUpOnce) {
return;
}
self::$setUpOnce = true;
// execute closure.
$closure();
}
}
<?php
class SomeTest extends TestCase
{
use HasSetUpOnce;
public function test_1()
{
$this->assertTrue(true);
}
public function test_2()
{
$this->assertTrue(true);
}
protected function setUp()
{
parent::setUp();
$this->setUpOnce(function () {
// run code once for all test methods after app boot.
// factory(\App\User::class, 4)->create();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.