feat(mcp) : add business tools — search, history, comments, custom fields, documents, model types
- search_inventory: global search across all 6 entity types - get_entity_history + get_activity_log: audit trail access - 4 comment tools: list, create, resolve, unresolved count - 3 custom field tools: list values, upsert, delete - 2 document tools: list, delete (upload via REST only) - 6 model type tools: list, get, create, update, delete, sync - 69 MCP tests pass total Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
98
tests/Mcp/Tool/Comment/CommentsToolTest.php
Normal file
98
tests/Mcp/Tool/Comment/CommentsToolTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\Comment;
|
||||
|
||||
use App\Entity\Comment;
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CommentsToolTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListComments(): void
|
||||
{
|
||||
$entityId = 'entity-'.uniqid();
|
||||
$this->createComment('First comment', 'machine', $entityId);
|
||||
$this->createComment('Second comment', 'machine', $entityId);
|
||||
$this->createComment('Other entity', 'machine', 'other-id');
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_comments', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $entityId,
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame(2, $data['_parsed']['total']);
|
||||
$this->assertCount(2, $data['_parsed']['items']);
|
||||
}
|
||||
|
||||
public function testCreateComment(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_comment', [
|
||||
'content' => 'A new comment',
|
||||
'entityType' => 'machine',
|
||||
'entityId' => 'some-machine-id',
|
||||
'entityName' => 'Machine Alpha',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertNotEmpty($data['_parsed']['id']);
|
||||
$this->assertSame('A new comment', $data['_parsed']['content']);
|
||||
$this->assertSame('machine', $data['_parsed']['entityType']);
|
||||
$this->assertSame('open', $data['_parsed']['status']);
|
||||
$this->assertSame('Machine Alpha', $data['_parsed']['entityName']);
|
||||
}
|
||||
|
||||
public function testResolveComment(): void
|
||||
{
|
||||
$comment = $this->createComment('To resolve', 'piece', 'piece-123');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'resolve_comment', [
|
||||
'commentId' => $comment->getId(),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('resolved', $data['_parsed']['status']);
|
||||
$this->assertNotEmpty($data['_parsed']['resolvedByName']);
|
||||
}
|
||||
|
||||
public function testUnresolvedCount(): void
|
||||
{
|
||||
$entityId = 'entity-'.uniqid();
|
||||
$this->createComment('Open 1', 'machine', $entityId, 'open');
|
||||
$this->createComment('Open 2', 'machine', $entityId, 'open');
|
||||
$this->createComment('Resolved', 'machine', $entityId, 'resolved');
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_unresolved_comments_count');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertGreaterThanOrEqual(2, $data['_parsed']['count']);
|
||||
}
|
||||
|
||||
private function createComment(string $content, string $entityType, string $entityId, string $status = 'open'): Comment
|
||||
{
|
||||
$comment = new Comment();
|
||||
$comment->setContent($content);
|
||||
$comment->setEntityType($entityType);
|
||||
$comment->setEntityId($entityId);
|
||||
$comment->setAuthorId('test-author-id');
|
||||
$comment->setAuthorName('Test Author');
|
||||
$comment->setStatus($status);
|
||||
|
||||
$em = $this->getEntityManager();
|
||||
$em->persist($comment);
|
||||
$em->flush();
|
||||
|
||||
return $comment;
|
||||
}
|
||||
}
|
||||
101
tests/Mcp/Tool/CustomField/CustomFieldToolsTest.php
Normal file
101
tests/Mcp/Tool/CustomField/CustomFieldToolsTest.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\CustomField;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class CustomFieldToolsTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListCustomFieldValues(): void
|
||||
{
|
||||
$machine = $this->createMachine(name: 'Machine CF');
|
||||
$customField = $this->createCustomField(name: 'Serial Number', type: 'text', machine: $machine);
|
||||
$this->createCustomFieldValue(customField: $customField, value: 'SN-12345', machine: $machine);
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_custom_field_values', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $machine->getId(),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$parsed = $data['_parsed'];
|
||||
$this->assertSame('machine', $parsed['entityType']);
|
||||
$this->assertSame($machine->getId(), $parsed['entityId']);
|
||||
$this->assertSame(1, $parsed['total']);
|
||||
$this->assertSame('SN-12345', $parsed['values'][0]['value']);
|
||||
$this->assertSame('Serial Number', $parsed['values'][0]['customFieldName']);
|
||||
$this->assertSame('text', $parsed['values'][0]['customFieldType']);
|
||||
}
|
||||
|
||||
public function testUpsertCustomFieldValues(): void
|
||||
{
|
||||
$machine = $this->createMachine(name: 'Machine Upsert');
|
||||
$customField = $this->createCustomField(name: 'Voltage', type: 'text', machine: $machine);
|
||||
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
// Create
|
||||
$data = $this->callMcpTool($session, 'upsert_custom_field_values', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $machine->getId(),
|
||||
'fields' => [
|
||||
['customFieldId' => $customField->getId(), 'value' => '220V'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$parsed = $data['_parsed'];
|
||||
$this->assertSame(1, $parsed['total']);
|
||||
$this->assertSame('created', $parsed['results'][0]['action']);
|
||||
$this->assertSame('220V', $parsed['results'][0]['value']);
|
||||
|
||||
$createdId = $parsed['results'][0]['id'];
|
||||
|
||||
// Update (upsert same field)
|
||||
$data = $this->callMcpTool($session, 'upsert_custom_field_values', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $machine->getId(),
|
||||
'fields' => [
|
||||
['customFieldId' => $customField->getId(), 'value' => '380V'],
|
||||
],
|
||||
]);
|
||||
|
||||
$parsed = $data['_parsed'];
|
||||
$this->assertSame('updated', $parsed['results'][0]['action']);
|
||||
$this->assertSame('380V', $parsed['results'][0]['value']);
|
||||
$this->assertSame($createdId, $parsed['results'][0]['id']);
|
||||
}
|
||||
|
||||
public function testDeleteCustomFieldValue(): void
|
||||
{
|
||||
$machine = $this->createMachine(name: 'Machine Delete CF');
|
||||
$customField = $this->createCustomField(name: 'Weight', type: 'text', machine: $machine);
|
||||
$cfv = $this->createCustomFieldValue(customField: $customField, value: '150kg', machine: $machine);
|
||||
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_custom_field_value', [
|
||||
'customFieldValueId' => $cfv->getId(),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$parsed = $data['_parsed'];
|
||||
$this->assertTrue($parsed['deleted']);
|
||||
$this->assertSame($cfv->getId(), $parsed['id']);
|
||||
|
||||
// Verify it's gone
|
||||
$listData = $this->callMcpTool($session, 'list_custom_field_values', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $machine->getId(),
|
||||
]);
|
||||
|
||||
$this->assertSame(0, $listData['_parsed']['total']);
|
||||
}
|
||||
}
|
||||
41
tests/Mcp/Tool/Document/DocumentToolsTest.php
Normal file
41
tests/Mcp/Tool/Document/DocumentToolsTest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\Document;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class DocumentToolsTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListDocuments(): void
|
||||
{
|
||||
$site = $this->createSite(name: 'Doc Site');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_documents', [
|
||||
'entityType' => 'site',
|
||||
'entityId' => $site->getId(),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('site', $data['_parsed']['entityType']);
|
||||
$this->assertSame($site->getId(), $data['_parsed']['entityId']);
|
||||
$this->assertIsArray($data['_parsed']['items']);
|
||||
$this->assertSame(0, $data['_parsed']['total']);
|
||||
}
|
||||
|
||||
public function testDeleteDocumentRequiresGestionnaire(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_document', [
|
||||
'documentId' => 'nonexistent-id',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('error', $data);
|
||||
}
|
||||
}
|
||||
44
tests/Mcp/Tool/HistoryToolsTest.php
Normal file
44
tests/Mcp/Tool/HistoryToolsTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class HistoryToolsTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testGetActivityLog(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_activity_log');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertArrayHasKey('items', $data['_parsed']);
|
||||
$this->assertArrayHasKey('total', $data['_parsed']);
|
||||
$this->assertArrayHasKey('page', $data['_parsed']);
|
||||
$this->assertArrayHasKey('limit', $data['_parsed']);
|
||||
$this->assertArrayHasKey('pageCount', $data['_parsed']);
|
||||
$this->assertIsArray($data['_parsed']['items']);
|
||||
}
|
||||
|
||||
public function testGetEntityHistory(): void
|
||||
{
|
||||
$machine = $this->createMachine(name: 'History Machine');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_entity_history', [
|
||||
'entityType' => 'machine',
|
||||
'entityId' => $machine->getId(),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertArrayHasKey('items', $data['_parsed']);
|
||||
$this->assertArrayHasKey('total', $data['_parsed']);
|
||||
$this->assertIsArray($data['_parsed']['items']);
|
||||
}
|
||||
}
|
||||
66
tests/Mcp/Tool/ModelType/ModelTypeToolsTest.php
Normal file
66
tests/Mcp/Tool/ModelType/ModelTypeToolsTest.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\ModelType;
|
||||
|
||||
use App\Enum\ModelCategory;
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ModelTypeToolsTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListModelTypes(): void
|
||||
{
|
||||
$this->createModelType(name: 'MT Alpha', code: 'MTA-'.bin2hex(random_bytes(3)), category: ModelCategory::COMPONENT);
|
||||
$this->createModelType(name: 'MT Beta', code: 'MTB-'.bin2hex(random_bytes(3)), category: ModelCategory::PIECE);
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_model_types');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
|
||||
}
|
||||
|
||||
public function testGetModelType(): void
|
||||
{
|
||||
$mt = $this->createModelType(name: 'MT Detail', code: 'MTD-'.bin2hex(random_bytes(3)));
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_model_type', ['modelTypeId' => $mt->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('MT Detail', $data['_parsed']['name']);
|
||||
$this->assertSame('COMPONENT', $data['_parsed']['category']);
|
||||
$this->assertIsArray($data['_parsed']['skeletonPieceRequirements']);
|
||||
}
|
||||
|
||||
public function testCreateModelType(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_model_type', [
|
||||
'name' => 'MT Nouveau',
|
||||
'category' => 'composant',
|
||||
'code' => 'MTN-'.bin2hex(random_bytes(3)),
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('MT Nouveau', $data['_parsed']['name']);
|
||||
$this->assertSame('COMPONENT', $data['_parsed']['category']);
|
||||
$this->assertNotEmpty($data['_parsed']['id']);
|
||||
}
|
||||
|
||||
public function testDeleteModelType(): void
|
||||
{
|
||||
$mt = $this->createModelType(name: 'MT To Delete', code: 'MTDEL-'.bin2hex(random_bytes(3)));
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_model_type', ['modelTypeId' => $mt->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertTrue($data['_parsed']['deleted']);
|
||||
}
|
||||
}
|
||||
63
tests/Mcp/Tool/SearchInventoryToolTest.php
Normal file
63
tests/Mcp/Tool/SearchInventoryToolTest.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SearchInventoryToolTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testSearchFindsAcrossEntities(): void
|
||||
{
|
||||
$this->createMachine(name: 'Alpha Machine');
|
||||
$this->createPiece(name: 'Alpha Piece');
|
||||
$this->createSite(name: 'Alpha Site');
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'search_inventory', ['query' => 'Alpha']);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$results = $data['_parsed'];
|
||||
$this->assertIsArray($results);
|
||||
|
||||
$types = array_unique(array_column($results, 'type'));
|
||||
$this->assertContains('machine', $types);
|
||||
$this->assertContains('piece', $types);
|
||||
$this->assertContains('site', $types);
|
||||
|
||||
foreach ($results as $result) {
|
||||
$this->assertArrayHasKey('type', $result);
|
||||
$this->assertArrayHasKey('id', $result);
|
||||
$this->assertArrayHasKey('name', $result);
|
||||
$this->assertArrayHasKey('reference', $result);
|
||||
$this->assertStringContainsStringIgnoringCase('Alpha', $result['name']);
|
||||
}
|
||||
}
|
||||
|
||||
public function testSearchFiltersByType(): void
|
||||
{
|
||||
$this->createMachine(name: 'Beta Machine');
|
||||
$this->createPiece(name: 'Beta Piece');
|
||||
$this->createSite(name: 'Beta Site');
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'search_inventory', [
|
||||
'query' => 'Beta',
|
||||
'types' => 'machine',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$results = $data['_parsed'];
|
||||
$this->assertIsArray($results);
|
||||
$this->assertNotEmpty($results);
|
||||
|
||||
$types = array_unique(array_column($results, 'type'));
|
||||
$this->assertSame(['machine'], $types);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user