From 561f629f267129bca0a0780d6ab26541efa85c7d Mon Sep 17 00:00:00 2001 From: recca0120 Date: Wed, 21 Dec 2022 11:37:34 +0800 Subject: [PATCH 1/4] add SqliteSchemaState test --- .../DatabaseSqliteSchemaStateTest.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tests/Database/DatabaseSqliteSchemaStateTest.php diff --git a/tests/Database/DatabaseSqliteSchemaStateTest.php b/tests/Database/DatabaseSqliteSchemaStateTest.php new file mode 100644 index 000000000000..404f3bb9023e --- /dev/null +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -0,0 +1,39 @@ + 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite']; + $connection = m::mock(SQLiteConnection::class); + $connection->shouldReceive('getConfig')->andReturn($config); + + $process = m::spy(Process::class); + $processFactory = function () use ($process) { + return $process; + }; + + $schemaState = new SqliteSchemaState($connection, null, $processFactory); + $schemaState->load('database/schema/sqlite-schema.dump'); + + $process->shouldHaveReceived('mustRun')->with(null, [ + 'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite', + 'LARAVEL_LOAD_PATH' => 'database/schema/sqlite-schema.dump', + ]); + } + +} From 71046d74930ed3c7a82b648851bfea94bdd36240 Mon Sep 17 00:00:00 2001 From: recca0120 Date: Wed, 21 Dec 2022 12:06:03 +0800 Subject: [PATCH 2/4] load schema to in-memory database --- .../Database/Schema/SqliteSchemaState.php | 6 ++++++ .../DatabaseSqliteSchemaStateTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index 9a98b6331cba..10efc7c0aba9 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -61,6 +61,12 @@ protected function appendMigrationData(string $path) */ public function load($path) { + if ($this->connection->getDatabaseName() === ':memory:') { + $this->connection->getPdo()->exec($this->files->get($path)); + + return; + } + $process = $this->makeProcess($this->baseCommand().' < "${:LARAVEL_LOAD_PATH}"'); $process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [ diff --git a/tests/Database/DatabaseSqliteSchemaStateTest.php b/tests/Database/DatabaseSqliteSchemaStateTest.php index 404f3bb9023e..1a3c47ff79e5 100644 --- a/tests/Database/DatabaseSqliteSchemaStateTest.php +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -4,7 +4,9 @@ use Illuminate\Database\Schema\SqliteSchemaState; use Illuminate\Database\SQLiteConnection; +use Illuminate\Filesystem\Filesystem; use Mockery as m; +use PDO; use PHPUnit\Framework\TestCase; use Symfony\Component\Process\Process; @@ -21,6 +23,7 @@ public function testLoadSchemaToDatabase(): void $config = ['driver' => 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite']; $connection = m::mock(SQLiteConnection::class); $connection->shouldReceive('getConfig')->andReturn($config); + $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); $process = m::spy(Process::class); $processFactory = function () use ($process) { @@ -36,4 +39,20 @@ public function testLoadSchemaToDatabase(): void ]); } + public function testLoadSchemaToInMemory(): void + { + $config = ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite']; + $connection = m::mock(SQLiteConnection::class); + $connection->shouldReceive('getConfig')->andReturn($config); + $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); + $connection->shouldReceive('getPdo')->andReturn($pdo = m::spy(PDO::class)); + + $files = m::mock(Filesystem::class); + $files->shouldReceive('get')->andReturn('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);'); + + $schemaState = new SqliteSchemaState($connection, $files); + $schemaState->load('database/schema/sqlite-schema.dump'); + + $pdo->shouldHaveReceived('exec')->with('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);'); + } } From 90d17e959930f4e3f5e7a3ddd5a5eac881f7333d Mon Sep 17 00:00:00 2001 From: recca0120 Date: Wed, 21 Dec 2022 15:01:18 +0800 Subject: [PATCH 3/4] check sqlite3 command --- .../DatabaseSqliteSchemaStateTest.php | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/Database/DatabaseSqliteSchemaStateTest.php b/tests/Database/DatabaseSqliteSchemaStateTest.php index 1a3c47ff79e5..928712674b5f 100644 --- a/tests/Database/DatabaseSqliteSchemaStateTest.php +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -26,13 +26,14 @@ public function testLoadSchemaToDatabase(): void $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); $process = m::spy(Process::class); - $processFactory = function () use ($process) { - return $process; - }; + $processFactory = m::mock(new ProcessFactory($process)); $schemaState = new SqliteSchemaState($connection, null, $processFactory); $schemaState->load('database/schema/sqlite-schema.dump'); + $processFactory->shouldHaveReceived('__invoke') + ->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'); + $process->shouldHaveReceived('mustRun')->with(null, [ 'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite', 'LARAVEL_LOAD_PATH' => 'database/schema/sqlite-schema.dump', @@ -56,3 +57,18 @@ public function testLoadSchemaToInMemory(): void $pdo->shouldHaveReceived('exec')->with('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);'); } } + +class ProcessFactory +{ + private $process; + + public function __construct($process) + { + $this->process = $process; + } + + public function __invoke() + { + return $this->process; + } +} From 749e07ad82e0223f1ae37cbfce61d58fef30909c Mon Sep 17 00:00:00 2001 From: recca0120 Date: Wed, 21 Dec 2022 22:42:23 +0800 Subject: [PATCH 4/4] spy closure --- .../DatabaseSqliteSchemaStateTest.php | 22 ++++--------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/tests/Database/DatabaseSqliteSchemaStateTest.php b/tests/Database/DatabaseSqliteSchemaStateTest.php index 928712674b5f..6154d42ff207 100644 --- a/tests/Database/DatabaseSqliteSchemaStateTest.php +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -26,13 +26,14 @@ public function testLoadSchemaToDatabase(): void $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); $process = m::spy(Process::class); - $processFactory = m::mock(new ProcessFactory($process)); + $processFactory = m::spy(function () use ($process) { + return $process; + }); $schemaState = new SqliteSchemaState($connection, null, $processFactory); $schemaState->load('database/schema/sqlite-schema.dump'); - $processFactory->shouldHaveReceived('__invoke') - ->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'); + $processFactory->shouldHaveBeenCalled()->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'); $process->shouldHaveReceived('mustRun')->with(null, [ 'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite', @@ -57,18 +58,3 @@ public function testLoadSchemaToInMemory(): void $pdo->shouldHaveReceived('exec')->with('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);'); } } - -class ProcessFactory -{ - private $process; - - public function __construct($process) - { - $this->process = $process; - } - - public function __invoke() - { - return $this->process; - } -}