Created
August 6, 2018 14:12
-
-
Save bdelespierre/8f300111f8a27f89fe48d0ea11467e5a 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 Tests; | |
use App\Console\Kernel; | |
use Illuminate\Foundation\Testing\RefreshDatabaseState; | |
use Illuminate\Support\Facades\DB; | |
use RuntimeException; | |
trait RefreshSqliteDatabase | |
{ | |
/** | |
* Define hooks to migrate the database before and after each test. | |
* | |
* @return void | |
*/ | |
public function refreshDatabase() | |
{ | |
if (!$this->usingSqliteDatabase()) { | |
throw new RuntimeException("You're not using an SQLite database"); | |
} | |
$this->usingInMemoryDatabase() | |
? $this->refreshInMemoryDatabase() | |
: $this->refreshTestDatabase(); | |
} | |
/** | |
* Determine if a SQLite database is being used. | |
* | |
* @return bool | |
*/ | |
protected function usingSqliteDatabase() | |
{ | |
$default = config('database.default'); | |
return config("database.connections.{$default}.driver") == 'sqlite'; | |
} | |
/** | |
* Determine if an in-memory database is being used. | |
* | |
* @return bool | |
*/ | |
protected function usingInMemoryDatabase() | |
{ | |
return $this->getDatabaseFile() == ':memory:'; | |
} | |
/** | |
* Refresh the in-memory database. | |
* | |
* @return void | |
*/ | |
protected function refreshInMemoryDatabase() | |
{ | |
// | |
} | |
/** | |
* Refresh a conventional test database. | |
* | |
* @return void | |
*/ | |
protected function refreshTestDatabase() | |
{ | |
if (!$this->exemplarExists()) { | |
$this->makeExemplar(); | |
} | |
if (!copy($this->getExemplarFile(), $this->getDatabaseFile())) { | |
throw new RuntimeException("Unable to copy exemplar over database"); | |
} | |
DB::reconnect(); | |
} | |
/** | |
* Is exemplar database available | |
*/ | |
protected function exemplarExists() | |
{ | |
return file_exists($this->getExemplarFile()); | |
} | |
/** | |
* Create a new exemplar database | |
*/ | |
protected function makeExemplar() | |
{ | |
$this->artisan('migrate:fresh', $this->shouldDropViews() ? ['--drop-views' => true] : []); | |
$this->app[Kernel::class]->setArtisan(null); | |
if (!copy($this->getDatabaseFile(), $this->getExemplarFile())) { | |
throw new RuntimeException("Unable to backup database in exemplar"); | |
} | |
} | |
/** | |
* Obtain the exemplar filename | |
* | |
* @return string | |
*/ | |
protected function getExemplarFile(): string | |
{ | |
$pid = getmypid(); | |
return dirname($this->getDatabaseFile()) . "/exemplar_{$pid}.sqlite"; | |
} | |
/** | |
* Obtain the database fiename | |
* | |
* @return string | |
*/ | |
protected function getDatabaseFile() | |
{ | |
$default = config('database.default'); | |
return config("database.connections.{$default}.database"); | |
} | |
/** | |
* Determine if views should be dropped when refreshing the database. | |
* | |
* @return bool | |
*/ | |
protected function shouldDropViews() | |
{ | |
return property_exists($this, 'dropViews') ? $this->dropViews : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment