feat(mcp) : add CRUD tools for Pieces, Composants, Machines

- 5 tools each: list, get, create, update, delete
- Piece: includes typePiece, constructeurs, prix (string)
- Composant: includes typeComposant, constructeurs, prix (string)
- Machine: includes site (required), constructeurs
- 40 MCP tests pass total

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-16 14:38:55 +01:00
parent 4f1e136dc5
commit 2f173e766d
18 changed files with 1277 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace App\Tests\Mcp\Tool\Composant;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ComposantsCrudToolTest extends AbstractApiTestCase
{
public function testListComposants(): void
{
$this->createComposant(name: 'Composant Alpha');
$this->createComposant(name: 'Composant Beta');
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'list_composants');
$this->assertArrayHasKey('_parsed', $data);
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
}
public function testGetComposant(): void
{
$constructeur = $this->createConstructeur(name: 'Fournisseur Comp');
$modelType = $this->createModelType(name: 'Type Composant', code: 'TC-001', category: ModelCategory::COMPONENT);
$composant = $this->createComposant(name: 'Composant Gamma', type: $modelType);
// Add constructeur to composant
$composant->addConstructeur($constructeur);
$this->getEntityManager()->flush();
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'get_composant', ['composantId' => $composant->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Composant Gamma', $data['_parsed']['name']);
$this->assertNotNull($data['_parsed']['typeComposant']);
$this->assertSame('Type Composant', $data['_parsed']['typeComposant']['name']);
$this->assertCount(1, $data['_parsed']['constructeurs']);
$this->assertSame('Fournisseur Comp', $data['_parsed']['constructeurs'][0]['name']);
}
public function testCreateComposant(): void
{
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'create_composant', [
'name' => 'Composant Nouveau',
'reference' => 'REF-COMP',
'description' => 'Un composant de test',
'prix' => '42.99',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Composant Nouveau', $data['_parsed']['name']);
$this->assertNotEmpty($data['_parsed']['id']);
}
public function testCreateComposantRequiresGestionnaire(): void
{
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'create_composant', ['name' => 'Forbidden']);
$this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
}
public function testUpdateComposant(): void
{
$composant = $this->createComposant(name: 'Old Composant');
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'update_composant', [
'composantId' => $composant->getId(),
'name' => 'Updated Composant',
'prix' => '99.00',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Updated Composant', $data['_parsed']['name']);
}
public function testDeleteComposant(): void
{
$composant = $this->createComposant(name: 'To Delete');
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'delete_composant', ['composantId' => $composant->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertTrue($data['_parsed']['deleted']);
}
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace App\Tests\Mcp\Tool\Machine;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class MachinesCrudToolTest extends AbstractApiTestCase
{
public function testListMachines(): void
{
$site = $this->createSite(name: 'Site Usine');
$this->createMachine(name: 'Machine Alpha', site: $site);
$this->createMachine(name: 'Machine Beta', site: $site);
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'list_machines');
$this->assertArrayHasKey('_parsed', $data);
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
}
public function testGetMachine(): void
{
$site = $this->createSite(name: 'Site Principal');
$constructeur = $this->createConstructeur(name: 'Fournisseur M');
$machine = $this->createMachine(name: 'Machine Gamma', site: $site, reference: 'REF-M001');
// Add constructeur to machine
$machine->addConstructeur($constructeur);
$this->getEntityManager()->flush();
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'get_machine', ['machineId' => $machine->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Machine Gamma', $data['_parsed']['name']);
$this->assertSame('REF-M001', $data['_parsed']['reference']);
$this->assertNotNull($data['_parsed']['site']);
$this->assertSame('Site Principal', $data['_parsed']['site']['name']);
$this->assertCount(1, $data['_parsed']['constructeurs']);
$this->assertSame('Fournisseur M', $data['_parsed']['constructeurs'][0]['name']);
}
public function testCreateMachine(): void
{
$site = $this->createSite(name: 'Site Création');
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'create_machine', [
'name' => 'Machine Nouvelle',
'siteId' => $site->getId(),
'reference' => 'REF-NEW',
'prix' => '42.99',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Machine Nouvelle', $data['_parsed']['name']);
$this->assertNotEmpty($data['_parsed']['id']);
}
public function testCreateMachineRequiresGestionnaire(): void
{
$site = $this->createSite(name: 'Site Forbidden');
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'create_machine', [
'name' => 'Forbidden',
'siteId' => $site->getId(),
]);
$this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
}
public function testUpdateMachine(): void
{
$site = $this->createSite(name: 'Site Update');
$machine = $this->createMachine(name: 'Old Machine', site: $site);
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'update_machine', [
'machineId' => $machine->getId(),
'name' => 'Updated Machine',
'prix' => '99.00',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Updated Machine', $data['_parsed']['name']);
}
public function testDeleteMachine(): void
{
$site = $this->createSite(name: 'Site Delete');
$machine = $this->createMachine(name: 'To Delete', site: $site);
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'delete_machine', ['machineId' => $machine->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertTrue($data['_parsed']['deleted']);
}
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
namespace App\Tests\Mcp\Tool\Piece;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class PiecesCrudToolTest extends AbstractApiTestCase
{
public function testListPieces(): void
{
$this->createPiece(name: 'Piece Alpha');
$this->createPiece(name: 'Piece Beta', reference: 'REF-BETA');
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'list_pieces');
$this->assertArrayHasKey('_parsed', $data);
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
}
public function testGetPiece(): void
{
$constructeur = $this->createConstructeur(name: 'Fournisseur Piece');
$modelType = $this->createModelType(name: 'Type Piece', code: 'TP-001', category: ModelCategory::PIECE);
$piece = $this->createPiece(name: 'Piece Gamma', reference: 'REF-001', type: $modelType);
// Add constructeur to piece
$piece->addConstructeur($constructeur);
$this->getEntityManager()->flush();
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'get_piece', ['pieceId' => $piece->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Piece Gamma', $data['_parsed']['name']);
$this->assertSame('REF-001', $data['_parsed']['reference']);
$this->assertNotNull($data['_parsed']['typePiece']);
$this->assertSame('Type Piece', $data['_parsed']['typePiece']['name']);
$this->assertCount(1, $data['_parsed']['constructeurs']);
$this->assertSame('Fournisseur Piece', $data['_parsed']['constructeurs'][0]['name']);
}
public function testCreatePiece(): void
{
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'create_piece', [
'name' => 'Piece Nouveau',
'reference' => 'REF-NEW',
'prix' => '42.99',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Piece Nouveau', $data['_parsed']['name']);
$this->assertNotEmpty($data['_parsed']['id']);
}
public function testCreatePieceRequiresGestionnaire(): void
{
$session = $this->createMcpClient('ROLE_VIEWER');
$data = $this->callMcpTool($session, 'create_piece', ['name' => 'Forbidden']);
$this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
}
public function testUpdatePiece(): void
{
$piece = $this->createPiece(name: 'Old Piece');
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'update_piece', [
'pieceId' => $piece->getId(),
'name' => 'Updated Piece',
'prix' => '99.00',
]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertSame('Updated Piece', $data['_parsed']['name']);
}
public function testDeletePiece(): void
{
$piece = $this->createPiece(name: 'To Delete');
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
$data = $this->callMcpTool($session, 'delete_piece', ['pieceId' => $piece->getId()]);
$this->assertArrayHasKey('_parsed', $data);
$this->assertTrue($data['_parsed']['deleted']);
}
}