|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Schema\Blueprint; |
| 4 | +use Illuminate\Support\Facades\File; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | +use Laravel\AiAssistant\Mcp\Tools\DatabaseSchema; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + // Switch the default connection to a file-backed SQLite DB. |
| 10 | + config()->set('database.default', 'testing'); |
| 11 | + config()->set('database.connections.testing', [ |
| 12 | + 'driver' => 'sqlite', |
| 13 | + 'database' => database_path('testing.sqlite'), |
| 14 | + 'prefix' => '', |
| 15 | + ]); |
| 16 | + |
| 17 | + // Ensure the DB file *and* schema folder exist. |
| 18 | + if (! is_file($file = database_path('testing.sqlite'))) { |
| 19 | + touch($file); |
| 20 | + } |
| 21 | + |
| 22 | + if (! is_dir($path = database_path('schema'))) { |
| 23 | + mkdir($path, 0777, true); |
| 24 | + } |
| 25 | + |
| 26 | + // Build a throw-away table that we expect in the dump. |
| 27 | + Schema::create('examples', function (Blueprint $table) { |
| 28 | + $table->id(); |
| 29 | + $table->string('name'); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +afterEach(function () { |
| 34 | + $dbFile = database_path('testing.sqlite'); |
| 35 | + if (File::exists($dbFile)) { |
| 36 | + File::delete($dbFile); |
| 37 | + } |
| 38 | + |
| 39 | + $schemaDir = database_path('schema'); |
| 40 | + if (File::isDirectory($schemaDir)) { |
| 41 | + File::deleteDirectory($schemaDir); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +test('it dumps the schema and returns it in the tool response', function () { |
| 46 | + $tool = new DatabaseSchema; |
| 47 | + $response = $tool->handle([]); |
| 48 | + |
| 49 | + $sql = $response->toArray()['content'][0]['text']; |
| 50 | + |
| 51 | + expect($sql)->toContain( |
| 52 | + 'CREATE TABLE IF NOT EXISTS "examples"' |
| 53 | + ); |
| 54 | + |
| 55 | + $this->assertDirectoryIsReadable(database_path('schema')); |
| 56 | + expect(glob(database_path('schema/*.sql')))->toBeEmpty(); |
| 57 | +}); |
0 commit comments