Skip to content

Commit 0c3fa0a

Browse files
committed
refactor: streamline ToolResult assertions in tests for improved readability
1 parent 1d3fbfe commit 0c3fa0a

11 files changed

+205
-232
lines changed

tests/Feature/Mcp/Tools/ApplicationInfoTest.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Laravel\Boost\Install\GuidelineAssist;
66
use Laravel\Boost\Mcp\Tools\ApplicationInfo;
7-
use Laravel\Mcp\Server\Tools\ToolResult;
87
use Laravel\Roster\Enums\Packages;
98
use Laravel\Roster\Package;
109
use Laravel\Roster\PackageCollection;
@@ -28,26 +27,24 @@
2827
$tool = new ApplicationInfo($roster, $guidelineAssist);
2928
$result = $tool->handle([]);
3029

31-
expect($result)->toBeInstanceOf(ToolResult::class);
32-
33-
$data = $result->toArray();
34-
expect($data['isError'])->toBeFalse();
35-
36-
$content = json_decode($data['content'][0]['text'], true);
37-
expect($content['php_version'])->toBe(PHP_VERSION);
38-
expect($content['laravel_version'])->toBe(app()->version());
39-
expect($content['database_engine'])->toBe(config('database.default'));
40-
expect($content['packages'])->toHaveCount(2);
41-
expect($content['packages'][0]['roster_name'])->toBe('LARAVEL');
42-
expect($content['packages'][0]['package_name'])->toBe('laravel/framework');
43-
expect($content['packages'][0]['version'])->toBe('11.0.0');
44-
expect($content['packages'][1]['roster_name'])->toBe('PEST');
45-
expect($content['packages'][1]['package_name'])->toBe('pestphp/pest');
46-
expect($content['packages'][1]['version'])->toBe('2.0.0');
47-
expect($content['models'])->toBeArray();
48-
expect($content['models'])->toHaveCount(2);
49-
expect($content['models'])->toContain('App\\Models\\User');
50-
expect($content['models'])->toContain('App\\Models\\Post');
30+
expect($result)->isToolResult()
31+
->toolHasNoError()
32+
->toolJsonContent(function ($content) {
33+
expect($content['php_version'])->toBe(PHP_VERSION)
34+
->and($content['laravel_version'])->toBe(app()->version())
35+
->and($content['database_engine'])->toBe(config('database.default'))
36+
->and($content['packages'])->toHaveCount(2)
37+
->and($content['packages'][0]['roster_name'])->toBe('LARAVEL')
38+
->and($content['packages'][0]['package_name'])->toBe('laravel/framework')
39+
->and($content['packages'][0]['version'])->toBe('11.0.0')
40+
->and($content['packages'][1]['roster_name'])->toBe('PEST')
41+
->and($content['packages'][1]['package_name'])->toBe('pestphp/pest')
42+
->and($content['packages'][1]['version'])->toBe('2.0.0')
43+
->and($content['models'])->toBeArray()
44+
->and($content['models'])->toHaveCount(2)
45+
->and($content['models'])->toContain('App\\Models\\User')
46+
->and($content['models'])->toContain('App\\Models\\Post');
47+
});
5148
});
5249

5350
test('it returns application info with no packages', function () {
@@ -60,17 +57,14 @@
6057
$tool = new ApplicationInfo($roster, $guidelineAssist);
6158
$result = $tool->handle([]);
6259

63-
expect($result)->toBeInstanceOf(ToolResult::class);
64-
expect($result)->toBeInstanceOf(ToolResult::class);
65-
66-
$data = $result->toArray();
67-
expect($data['isError'])->toBeFalse();
68-
69-
$content = json_decode($data['content'][0]['text'], true);
70-
expect($content['php_version'])->toBe(PHP_VERSION);
71-
expect($content['laravel_version'])->toBe(app()->version());
72-
expect($content['database_engine'])->toBe(config('database.default'));
73-
expect($content['packages'])->toHaveCount(0);
74-
expect($content['models'])->toBeArray();
75-
expect($content['models'])->toHaveCount(0);
60+
expect($result)->isToolResult()
61+
->toolHasNoError()
62+
->toolJsonContent(function ($content) {
63+
expect($content['php_version'])->toBe(PHP_VERSION)
64+
->and($content['laravel_version'])->toBe(app()->version())
65+
->and($content['database_engine'])->toBe(config('database.default'))
66+
->and($content['packages'])->toHaveCount(0)
67+
->and($content['models'])->toBeArray()
68+
->and($content['models'])->toHaveCount(0);
69+
});
7670
});

tests/Feature/Mcp/Tools/BrowserLogsTest.php

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Laravel\Boost\Mcp\Tools\BrowserLogs;
1111
use Laravel\Boost\Middleware\InjectBoost;
1212
use Laravel\Boost\Services\BrowserLogger;
13-
use Laravel\Mcp\Server\Tools\ToolResult;
1413

1514
beforeEach(function () {
1615
// Clean up any existing browser.log file before each test
@@ -36,47 +35,38 @@
3635
$tool = new BrowserLogs;
3736
$result = $tool->handle(['entries' => 2]);
3837

39-
expect($result)->toBeInstanceOf(ToolResult::class);
38+
expect($result)->isToolResult()
39+
->toolHasNoError()
40+
->toolTextContains('browser.WARNING: Warning message', 'browser.ERROR: JavaScript error occurred')
41+
->toolTextDoesNotContain('browser.DEBUG: console log message');
4042

4143
$data = $result->toArray();
42-
expect($data['isError'])->toBeFalse();
4344
expect($data['content'][0]['type'])->toBe('text');
44-
45-
$text = $data['content'][0]['text'];
46-
expect($text)->toContain('browser.WARNING: Warning message');
47-
expect($text)->toContain('browser.ERROR: JavaScript error occurred');
48-
expect($text)->not->toContain('browser.DEBUG: console log message');
4945
});
5046

5147
test('it returns error when entries argument is invalid', function () {
5248
$tool = new BrowserLogs;
5349

5450
// Test with zero
5551
$result = $tool->handle(['entries' => 0]);
56-
expect($result)->toBeInstanceOf(ToolResult::class);
57-
58-
$data = $result->toArray();
59-
expect($data['isError'])->toBeTrue();
60-
expect($data['content'][0]['text'])->toBe('The "entries" argument must be greater than 0.');
52+
expect($result)->isToolResult()
53+
->toolHasError()
54+
->toolTextContains('The "entries" argument must be greater than 0.');
6155

6256
// Test with negative
6357
$result = $tool->handle(['entries' => -5]);
64-
expect($result)->toBeInstanceOf(ToolResult::class);
65-
66-
$data = $result->toArray();
67-
expect($data['isError'])->toBeTrue();
68-
expect($data['content'][0]['text'])->toBe('The "entries" argument must be greater than 0.');
58+
expect($result)->isToolResult()
59+
->toolHasError()
60+
->toolTextContains('The "entries" argument must be greater than 0.');
6961
});
7062

7163
test('it returns error when log file does not exist', function () {
7264
$tool = new BrowserLogs;
7365
$result = $tool->handle(['entries' => 10]);
7466

75-
expect($result)->toBeInstanceOf(ToolResult::class);
76-
77-
$data = $result->toArray();
78-
expect($data['isError'])->toBeTrue();
79-
expect($data['content'][0]['text'])->toBe('No log file found, probably means no logs yet.');
67+
expect($result)->isToolResult()
68+
->toolHasError()
69+
->toolTextContains('No log file found, probably means no logs yet.');
8070
});
8171

8272
test('it returns error when log file is empty', function () {
@@ -88,11 +78,9 @@
8878
$tool = new BrowserLogs;
8979
$result = $tool->handle(['entries' => 5]);
9080

91-
expect($result)->toBeInstanceOf(ToolResult::class);
92-
93-
$data = $result->toArray();
94-
expect($data['isError'])->toBeFalse();
95-
expect($data['content'][0]['text'])->toBe('Unable to retrieve log entries, or no logs');
81+
expect($result)->isToolResult()
82+
->toolHasNoError()
83+
->toolTextContains('Unable to retrieve log entries, or no logs');
9684
});
9785

9886
test('@boostJs blade directive renders browser logger script', function () {
@@ -106,11 +94,11 @@
10694

10795
// Test that the script contains expected content
10896
$script = BrowserLogger::getScript();
109-
expect($script)->toContain('browser-logger-active');
110-
expect($script)->toContain('/_boost/browser-logs');
111-
expect($script)->toContain('console.log');
112-
expect($script)->toContain('console.error');
113-
expect($script)->toContain('window.onerror');
97+
expect($script)->toContain('browser-logger-active')
98+
->and($script)->toContain('/_boost/browser-logs')
99+
->and($script)->toContain('console.log')
100+
->and($script)->toContain('console.error')
101+
->and($script)->toContain('window.onerror');
114102
});
115103

116104
test('browser logs endpoint processes logs correctly', function () {
@@ -215,9 +203,10 @@
215203
});
216204

217205
$content = $result->getContent();
218-
expect($content)->toContain('browser-logger-active');
219-
expect($content)->toContain('</head>');
220-
expect(substr_count($content, 'browser-logger-active'))->toBe(1); // Should not inject twice
206+
expect($content)->toContain('browser-logger-active')
207+
->and($content)->toContain('</head>')
208+
->and(substr_count($content, 'browser-logger-active'))->toBe(1);
209+
// Should not inject twice
221210
});
222211

223212
test('InjectBoost middleware does not inject into non-HTML responses', function () {
@@ -233,8 +222,8 @@
233222
});
234223

235224
$content = $result->getContent();
236-
expect($content)->toBe($json);
237-
expect($content)->not->toContain('browser-logger-active');
225+
expect($content)->toBe($json)
226+
->and($content)->not->toContain('browser-logger-active');
238227
});
239228

240229
test('InjectBoost middleware does not inject script twice', function () {
@@ -284,6 +273,6 @@
284273
});
285274

286275
$content = $result->getContent();
287-
expect($content)->toContain('browser-logger-active');
288-
expect($content)->toMatch('/<script[^>]*browser-logger-active[^>]*>.*<\/script>\s*<\/body>/s');
276+
expect($content)->toContain('browser-logger-active')
277+
->and($content)->toMatch('/<script[^>]*browser-logger-active[^>]*>.*<\/script>\s*<\/body>/s');
289278
});

tests/Feature/Mcp/Tools/DatabaseConnectionsTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
use Laravel\Boost\Mcp\Tools\DatabaseConnections;
6-
use Laravel\Mcp\Server\Tools\ToolResult;
76

87
beforeEach(function () {
98
config()->set('database.default', 'mysql');
@@ -18,16 +17,15 @@
1817
$tool = new DatabaseConnections;
1918
$result = $tool->handle([]);
2019

21-
expect($result)->toBeInstanceOf(ToolResult::class);
22-
$data = $result->toArray();
23-
expect($data['isError'])->toBe(false);
24-
25-
$content = json_decode($data['content'][0]['text'], true);
26-
expect($content['default_connection'])->toBe('mysql');
27-
expect($content['connections'])->toHaveCount(3);
28-
expect($content['connections'])->toContain('mysql');
29-
expect($content['connections'])->toContain('pgsql');
30-
expect($content['connections'])->toContain('sqlite');
20+
expect($result)->isToolResult()
21+
->toolHasNoError()
22+
->toolJsonContent(function ($content) {
23+
expect($content['default_connection'])->toBe('mysql')
24+
->and($content['connections'])->toHaveCount(3)
25+
->and($content['connections'])->toContain('mysql')
26+
->and($content['connections'])->toContain('pgsql')
27+
->and($content['connections'])->toContain('sqlite');
28+
});
3129
});
3230

3331
test('it returns empty connections when none configured', function () {
@@ -36,11 +34,10 @@
3634
$tool = new DatabaseConnections;
3735
$result = $tool->handle([]);
3836

39-
expect($result)->toBeInstanceOf(ToolResult::class);
40-
$data = $result->toArray();
41-
expect($data['isError'])->toBe(false);
42-
43-
$content = json_decode($data['content'][0]['text'], true);
44-
expect($content['default_connection'])->toBe('mysql');
45-
expect($content['connections'])->toHaveCount(0);
37+
expect($result)->isToolResult()
38+
->toolHasNoError()
39+
->toolJsonContent(function ($content) {
40+
expect($content['default_connection'])->toBe('mysql')
41+
->and($content['connections'])->toHaveCount(0);
42+
});
4643
});

tests/Feature/Mcp/Tools/DatabaseSchemaTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
$tool = new DatabaseSchema;
4040
$response = $tool->handle([]);
4141

42+
expect($response)->isToolResult()
43+
->toolHasNoError();
44+
4245
$responseArray = $response->toArray();
43-
expect($responseArray['isError'])->toBeFalse();
4446

4547
$schemaArray = json_decode($responseArray['content'][0]['text'], true);
4648

tests/Feature/Mcp/Tools/GetAbsoluteUrlTest.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Facades\Route;
66
use Laravel\Boost\Mcp\Tools\GetAbsoluteUrl;
7-
use Laravel\Mcp\Server\Tools\ToolResult;
87

98
beforeEach(function () {
109
config()->set('app.url', 'http://localhost');
@@ -17,48 +16,43 @@
1716
$tool = new GetAbsoluteUrl;
1817
$result = $tool->handle([]);
1918

20-
expect($result)->toBeInstanceOf(ToolResult::class);
21-
$data = $result->toArray();
22-
expect($data['isError'])->toBe(false);
23-
expect($data['content'][0]['text'])->toBe('http://localhost');
19+
expect($result)->isToolResult()
20+
->toolHasNoError()
21+
->toolTextContains('http://localhost');
2422
});
2523

2624
test('it returns absolute url for given path', function () {
2725
$tool = new GetAbsoluteUrl;
2826
$result = $tool->handle(['path' => '/dashboard']);
2927

30-
expect($result)->toBeInstanceOf(ToolResult::class);
31-
$data = $result->toArray();
32-
expect($data['isError'])->toBe(false);
33-
expect($data['content'][0]['text'])->toBe('http://localhost/dashboard');
28+
expect($result)->isToolResult()
29+
->toolHasNoError()
30+
->toolTextContains('http://localhost/dashboard');
3431
});
3532

3633
test('it returns absolute url for named route', function () {
3734
$tool = new GetAbsoluteUrl;
3835
$result = $tool->handle(['route' => 'test.route']);
3936

40-
expect($result)->toBeInstanceOf(ToolResult::class);
41-
$data = $result->toArray();
42-
expect($data['isError'])->toBe(false);
43-
expect($data['content'][0]['text'])->toBe('http://localhost/test');
37+
expect($result)->isToolResult()
38+
->toolHasNoError()
39+
->toolTextContains('http://localhost/test');
4440
});
4541

4642
test('it prioritizes path over route when both are provided', function () {
4743
$tool = new GetAbsoluteUrl;
4844
$result = $tool->handle(['path' => '/dashboard', 'route' => 'test.route']);
4945

50-
expect($result)->toBeInstanceOf(ToolResult::class);
51-
$data = $result->toArray();
52-
expect($data['isError'])->toBe(false);
53-
expect($data['content'][0]['text'])->toBe('http://localhost/dashboard');
46+
expect($result)->isToolResult()
47+
->toolHasNoError()
48+
->toolTextContains('http://localhost/dashboard');
5449
});
5550

5651
test('it handles empty path', function () {
5752
$tool = new GetAbsoluteUrl;
5853
$result = $tool->handle(['path' => '']);
5954

60-
expect($result)->toBeInstanceOf(ToolResult::class);
61-
$data = $result->toArray();
62-
expect($data['isError'])->toBe(false);
63-
expect($data['content'][0]['text'])->toBe('http://localhost');
55+
expect($result)->isToolResult()
56+
->toolHasNoError()
57+
->toolTextContains('http://localhost');
6458
});

0 commit comments

Comments
 (0)