|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Database; |
| 4 | + |
| 5 | +use Illuminate\Database\Schema\SqliteSchemaState; |
| 6 | +use Illuminate\Database\SQLiteConnection; |
| 7 | +use Illuminate\Filesystem\Filesystem; |
| 8 | +use Mockery as m; |
| 9 | +use PDO; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Process\Process; |
| 12 | + |
| 13 | +class DatabaseSqliteSchemaStateTest extends TestCase |
| 14 | +{ |
| 15 | + protected function tearDown(): void |
| 16 | + { |
| 17 | + parent::tearDown(); |
| 18 | + m::close(); |
| 19 | + } |
| 20 | + |
| 21 | + public function testLoadSchemaToDatabase(): void |
| 22 | + { |
| 23 | + $config = ['driver' => 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite']; |
| 24 | + $connection = m::mock(SQLiteConnection::class); |
| 25 | + $connection->shouldReceive('getConfig')->andReturn($config); |
| 26 | + $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); |
| 27 | + |
| 28 | + $process = m::spy(Process::class); |
| 29 | + $processFactory = m::spy(function () use ($process) { |
| 30 | + return $process; |
| 31 | + }); |
| 32 | + |
| 33 | + $schemaState = new SqliteSchemaState($connection, null, $processFactory); |
| 34 | + $schemaState->load('database/schema/sqlite-schema.dump'); |
| 35 | + |
| 36 | + $processFactory->shouldHaveBeenCalled()->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"'); |
| 37 | + |
| 38 | + $process->shouldHaveReceived('mustRun')->with(null, [ |
| 39 | + 'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite', |
| 40 | + 'LARAVEL_LOAD_PATH' => 'database/schema/sqlite-schema.dump', |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + public function testLoadSchemaToInMemory(): void |
| 45 | + { |
| 46 | + $config = ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite']; |
| 47 | + $connection = m::mock(SQLiteConnection::class); |
| 48 | + $connection->shouldReceive('getConfig')->andReturn($config); |
| 49 | + $connection->shouldReceive('getDatabaseName')->andReturn($config['database']); |
| 50 | + $connection->shouldReceive('getPdo')->andReturn($pdo = m::spy(PDO::class)); |
| 51 | + |
| 52 | + $files = m::mock(Filesystem::class); |
| 53 | + $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);'); |
| 54 | + |
| 55 | + $schemaState = new SqliteSchemaState($connection, $files); |
| 56 | + $schemaState->load('database/schema/sqlite-schema.dump'); |
| 57 | + |
| 58 | + $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);'); |
| 59 | + } |
| 60 | +} |
0 commit comments