Last active
December 5, 2016 01:47
-
-
Save jrm2k6/5ec449ddb3e396ed862d23e665d845fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Testing\Traits; | |
trait DatabaseTruncate | |
{ | |
/** | |
* Define hooks to truncate the database before each test. | |
* | |
* @return void | |
*/ | |
public function runTruncateDatabase() | |
{ | |
$this->artisan('testing:truncate-db'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Console\Commands\Testing; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
class DeleteContentTestingDatabase extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'testing:truncate-db'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Running before each test, making sure the content of the testing db is reset'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
DB::statement("SET foreign_key_checks=0"); | |
$tables = DB::select("SELECT TABLE_NAME as name FROM INFORMATION_SCHEMA.TABLES where table_schema in ('i-heart-reading-testing')"); | |
collect($tables)->each(function($item) { | |
DB::statement("TRUNCATE ".$item->name.";"); | |
}); | |
DB::statement("SET foreign_key_checks=1"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Testing\Traits\DatabaseTruncate; | |
class TestCase extends Illuminate\Foundation\Testing\TestCase | |
{ | |
/** | |
* The base URL to use while testing the application. | |
* | |
* @var string | |
*/ | |
protected $baseUrl = 'http://localhost'; | |
/** | |
* Creates the application. | |
* | |
* @return \Illuminate\Foundation\Application | |
*/ | |
public function createApplication() | |
{ | |
$app = require __DIR__.'/../bootstrap/app.php'; | |
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); | |
return $app; | |
} | |
public function setUpTraits() | |
{ | |
parent::setUpTraits(); | |
$uses = array_flip(class_uses_recursive(static::class)); | |
if (isset($uses[DatabaseTruncate::class])) { | |
$this->runTruncateDatabase(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment