Skip to content

Commit 4c0013b

Browse files
authored
Merge pull request #337 from dmason30/baseline-test-migration
Recreate baseline sqlite db once per test suite run
2 parents be138bc + fb16522 commit 4c0013b

6 files changed

+49
-55
lines changed

tests/AlwaysRunFirstTest.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

tests/CreatesApplication.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
trait CreatesApplication
66
{
77
use EnvironmentSetup;
8+
use MigrateBaselineDatabase;
89

910
protected $cache;
1011
protected $testingSqlitePath;
@@ -22,18 +23,16 @@ protected function cache()
2223

2324
public function setUp() : void
2425
{
26+
parent::setUp();
27+
$this->setUpBaseLineSqlLiteDatabase();
28+
2529
$databasePath = __DIR__ . "/database";
2630
$this->testingSqlitePath = "{$databasePath}/";
2731
$baselinePath = "{$databasePath}/baseline.sqlite";
2832
$testingPath = "{$databasePath}/testing.sqlite";
2933

30-
if (file_exists($testingPath)) {
31-
unlink($testingPath);
32-
}
33-
34-
shell_exec("cp {$baselinePath} {$testingPath}");
35-
36-
parent::setUp();
34+
! file_exists($testingPath) ?: unlink($testingPath);
35+
copy($baselinePath, $testingPath);
3736

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

tests/MigrateBaselineDatabase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace GeneaLabs\LaravelModelCaching\Tests;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
7+
trait MigrateBaselineDatabase
8+
{
9+
private static $baseLineDatabaseMigrated = false;
10+
11+
public function setUpBaseLineSqlLiteDatabase()
12+
{
13+
if (self::$baseLineDatabaseMigrated) {
14+
return;
15+
}
16+
17+
self::$baseLineDatabaseMigrated = true;
18+
$file = __DIR__ . '/database/baseline.sqlite';
19+
$this->app['config']->set('database.default', 'baseline');
20+
$this->app['config']->set('database.connections.baseline', [
21+
'driver' => 'sqlite',
22+
"url" => null,
23+
'database' => $file,
24+
'prefix' => '',
25+
"foreign_key_constraints" => false,
26+
]);
27+
28+
! file_exists($file) ?: unlink($file);
29+
touch($file);
30+
31+
$this->withFactories(__DIR__ . '/database/factories');
32+
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
33+
34+
Artisan::call('db:seed', [
35+
'--class' => 'DatabaseSeeder',
36+
'--database' => 'baseline',
37+
]);
38+
39+
// Reset default connection to testing
40+
$this->app['config']->set('database.default', 'testing');
41+
}
42+
}

tests/database/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sqlite

tests/database/baseline.sqlite

-216 KB
Binary file not shown.

tests/database/testing.sqlite

-216 KB
Binary file not shown.

0 commit comments

Comments
 (0)