|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Foundation\Application; |
| 6 | +use Illuminate\Support\Facades\Facade; |
| 7 | +use Laravel\Boost\Mcp\Tools\Tinker; |
| 8 | +use Laravel\Mcp\Request; |
| 9 | +use Laravel\Tinker\TinkerServiceProvider; |
| 10 | +use Orchestra\Testbench\Foundation\Application as Testbench; |
| 11 | + |
| 12 | +use function Orchestra\Testbench\package_path; |
| 13 | + |
| 14 | +beforeEach(function (): void { |
| 15 | + $result = Testbench::createVendorSymlink(base_path(), package_path('vendor')); |
| 16 | + $this->vendorSymlinkCreated = $result['TESTBENCH_VENDOR_SYMLINK'] ?? false; |
| 17 | + |
| 18 | + Facade::clearResolvedInstances(); |
| 19 | + Facade::setFacadeApplication($this->app); |
| 20 | + Application::setInstance($this->app); |
| 21 | + |
| 22 | + $this->app->register(TinkerServiceProvider::class); |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(function (): void { |
| 26 | + if ($this->vendorSymlinkCreated ?? false) { |
| 27 | + Testbench::deleteVendorSymlink(base_path()); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +test('executes code and returns output', function (): void { |
| 32 | + $tool = new Tinker; |
| 33 | + $response = $tool->handle(new Request(['code' => 'echo "Hello World";'])); |
| 34 | + |
| 35 | + expect($response)->isToolResult() |
| 36 | + ->toolHasNoError() |
| 37 | + ->toolTextContains('Hello World'); |
| 38 | +}); |
| 39 | + |
| 40 | +test('handles errors gracefully', function (): void { |
| 41 | + $tool = new Tinker; |
| 42 | + $response = $tool->handle(new Request(['code' => 'invalid syntax here'])); |
| 43 | + |
| 44 | + expect((string) $response->content())->toContain('Syntax error'); |
| 45 | +}); |
| 46 | + |
| 47 | +test('strips php tags from code', function (): void { |
| 48 | + $tool = new Tinker; |
| 49 | + $response = $tool->handle(new Request(['code' => '<?php echo "stripped"; ?>'])); |
| 50 | + |
| 51 | + expect($response)->isToolResult() |
| 52 | + ->toolHasNoError() |
| 53 | + ->toolTextContains('stripped'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('executes code without semicolon', function (): void { |
| 57 | + $tool = new Tinker; |
| 58 | + $response = $tool->handle(new Request(['code' => 'echo 1'])); |
| 59 | + |
| 60 | + expect($response)->isToolResult() |
| 61 | + ->toolHasNoError() |
| 62 | + ->toolTextContains('1'); |
| 63 | +}); |
| 64 | + |
| 65 | +test('is not registered by default', function (): void { |
| 66 | + $tool = new Tinker; |
| 67 | + |
| 68 | + expect($tool->shouldRegister())->toBeFalse(); |
| 69 | +}); |
| 70 | + |
| 71 | +test('is registered when tinker_tool_enabled config is true', function (): void { |
| 72 | + config()->set('boost.tinker_tool_enabled', true); |
| 73 | + |
| 74 | + $tool = new Tinker; |
| 75 | + |
| 76 | + expect($tool->shouldRegister())->toBeTrue(); |
| 77 | +}); |
0 commit comments