Skip to content

Commit 1fe1329

Browse files
committed
test DatabaseSchema tool
1 parent 5c0bbde commit 1fe1329

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Mcp/Tools/DatabaseSchema.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Laravel\Mcp\Tools\ToolResponse;
66
use Laravel\Mcp\Tools\ToolInputSchema;
77
use Laravel\Mcp\Tools\Tool;
8-
use Illuminate\Database\Connection;
98
use Illuminate\Support\Facades\Artisan;
109
use Illuminate\Support\Str;
1110

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)