Skip to content

Recreate baseline sqlite db once per test suite run #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2020
Merged
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
48 changes: 0 additions & 48 deletions tests/AlwaysRunFirstTest.php

This file was deleted.

13 changes: 6 additions & 7 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
trait CreatesApplication
{
use EnvironmentSetup;
use MigrateBaselineDatabase;

protected $cache;
protected $testingSqlitePath;
Expand All @@ -22,18 +23,16 @@ protected function cache()

public function setUp() : void
{
parent::setUp();
$this->setUpBaseLineSqlLiteDatabase();

$databasePath = __DIR__ . "/database";
$this->testingSqlitePath = "{$databasePath}/";
$baselinePath = "{$databasePath}/baseline.sqlite";
$testingPath = "{$databasePath}/testing.sqlite";

if (file_exists($testingPath)) {
unlink($testingPath);
}

shell_exec("cp {$baselinePath} {$testingPath}");

parent::setUp();
! file_exists($testingPath) ?: unlink($testingPath);
copy($baselinePath, $testingPath);

require(__DIR__ . '/routes/web.php');

Expand Down
42 changes: 42 additions & 0 deletions tests/MigrateBaselineDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace GeneaLabs\LaravelModelCaching\Tests;

use Illuminate\Support\Facades\Artisan;

trait MigrateBaselineDatabase
{
private static $baseLineDatabaseMigrated = false;

public function setUpBaseLineSqlLiteDatabase()
{
if (self::$baseLineDatabaseMigrated) {
return;
}

self::$baseLineDatabaseMigrated = true;
$file = __DIR__ . '/database/baseline.sqlite';
$this->app['config']->set('database.default', 'baseline');
$this->app['config']->set('database.connections.baseline', [
'driver' => 'sqlite',
"url" => null,
'database' => $file,
'prefix' => '',
"foreign_key_constraints" => false,
]);

! file_exists($file) ?: unlink($file);
touch($file);

$this->withFactories(__DIR__ . '/database/factories');
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');

Artisan::call('db:seed', [
'--class' => 'DatabaseSeeder',
'--database' => 'baseline',
]);

// Reset default connection to testing
$this->app['config']->set('database.default', 'testing');
}
}
1 change: 1 addition & 0 deletions tests/database/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sqlite
Binary file removed tests/database/baseline.sqlite
Binary file not shown.
Binary file removed tests/database/testing.sqlite
Binary file not shown.