feat(mcp) : add CRUD tools for Sites, Constructeurs, Products
- 5 tools each: list, get, create, update, delete - McpToolHelper extracted to AbstractApiTestCase for reuse - DashboardStatsToolTest simplified to use base helpers - 22 MCP tests pass Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,7 @@ use App\Entity\Profile;
|
||||
use App\Entity\Site;
|
||||
use App\Enum\ModelCategory;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use stdClass;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
abstract class AbstractApiTestCase extends ApiTestCase
|
||||
@@ -85,6 +86,93 @@ abstract class AbstractApiTestCase extends ApiTestCase
|
||||
return static::createClient();
|
||||
}
|
||||
|
||||
// ── MCP helpers ──────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* @return array{client: Client, sessionId: string}
|
||||
*/
|
||||
protected function createMcpClient(string $role = 'ROLE_VIEWER'): array
|
||||
{
|
||||
$profile = $this->createProfile(roles: [$role], password: self::DEFAULT_PASSWORD);
|
||||
|
||||
return $this->initMcpSession($profile->getId(), self::DEFAULT_PASSWORD);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{client: Client, sessionId: string}
|
||||
*/
|
||||
protected function initMcpSession(string $profileId, string $password): array
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
$response = $client->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profileId,
|
||||
'X-Profile-Password' => $password,
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'initialize',
|
||||
'params' => [
|
||||
'protocolVersion' => '2025-03-26',
|
||||
'capabilities' => new stdClass(),
|
||||
'clientInfo' => ['name' => 'test', 'version' => '1.0'],
|
||||
],
|
||||
'id' => 1,
|
||||
]),
|
||||
]);
|
||||
|
||||
$sessionId = $response->getHeaders()['mcp-session-id'][0] ?? '';
|
||||
|
||||
$client->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profileId,
|
||||
'X-Profile-Password' => $password,
|
||||
'Mcp-Session-Id' => $sessionId,
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'notifications/initialized',
|
||||
]),
|
||||
]);
|
||||
|
||||
return ['client' => $client, 'sessionId' => $sessionId, 'profileId' => $profileId, 'password' => $password];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function callMcpTool(array $session, string $toolName, array $arguments = []): array
|
||||
{
|
||||
$response = $session['client']->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $session['profileId'],
|
||||
'X-Profile-Password' => $session['password'],
|
||||
'Mcp-Session-Id' => $session['sessionId'],
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'tools/call',
|
||||
'params' => [
|
||||
'name' => $toolName,
|
||||
'arguments' => empty($arguments) ? new stdClass() : $arguments,
|
||||
],
|
||||
'id' => random_int(10, 9999),
|
||||
]),
|
||||
]);
|
||||
|
||||
$data = $response->toArray(false);
|
||||
|
||||
if (isset($data['result']['content'][0]['text'])) {
|
||||
$data['_parsed'] = json_decode($data['result']['content'][0]['text'], true);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
// ── Factory helpers ─────────────────────────────────────────────
|
||||
|
||||
protected function createProfile(
|
||||
|
||||
85
tests/Mcp/Tool/Constructeur/ConstructeursCrudToolTest.php
Normal file
85
tests/Mcp/Tool/Constructeur/ConstructeursCrudToolTest.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\Constructeur;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ConstructeursCrudToolTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListConstructeurs(): void
|
||||
{
|
||||
$this->createConstructeur(name: 'Constructeur Alpha');
|
||||
$this->createConstructeur(name: 'Constructeur Beta');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_constructeurs');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data, 'MCP response: '.json_encode($data));
|
||||
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
|
||||
}
|
||||
|
||||
public function testGetConstructeur(): void
|
||||
{
|
||||
$constructeur = $this->createConstructeur(name: 'Constructeur Gamma');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_constructeur', ['constructeurId' => $constructeur->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Constructeur Gamma', $data['_parsed']['name']);
|
||||
}
|
||||
|
||||
public function testCreateConstructeur(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_constructeur', [
|
||||
'name' => 'Constructeur Nouveau',
|
||||
'email' => 'contact@nouveau.com',
|
||||
'phone' => '+33123456789',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Constructeur Nouveau', $data['_parsed']['name']);
|
||||
$this->assertNotEmpty($data['_parsed']['id']);
|
||||
}
|
||||
|
||||
public function testCreateConstructeurRequiresGestionnaire(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_constructeur', ['name' => 'Forbidden']);
|
||||
|
||||
$this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
|
||||
}
|
||||
|
||||
public function testUpdateConstructeur(): void
|
||||
{
|
||||
$constructeur = $this->createConstructeur(name: 'Old Name');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'update_constructeur', [
|
||||
'constructeurId' => $constructeur->getId(),
|
||||
'name' => 'New Name',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('New Name', $data['_parsed']['name']);
|
||||
}
|
||||
|
||||
public function testDeleteConstructeur(): void
|
||||
{
|
||||
$constructeur = $this->createConstructeur(name: 'To Delete');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_constructeur', ['constructeurId' => $constructeur->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertTrue($data['_parsed']['deleted']);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace App\Tests\Mcp\Tool;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@@ -18,98 +17,13 @@ class DashboardStatsToolTest extends AbstractApiTestCase
|
||||
$this->createMachine(name: 'Machine Stats 1', site: $site);
|
||||
$this->createMachine(name: 'Machine Stats 2', site: $site);
|
||||
|
||||
$profile = $this->createProfile(roles: ['ROLE_VIEWER'], password: 'test123');
|
||||
$session = $this->initMcpSession($profile->getId(), 'test123');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
$data = $this->callMcpTool($session, 'get_dashboard_stats');
|
||||
|
||||
$response = $session['client']->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profile->getId(),
|
||||
'X-Profile-Password' => 'test123',
|
||||
'Mcp-Session-Id' => $session['sessionId'],
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'tools/call',
|
||||
'params' => [
|
||||
'name' => 'get_dashboard_stats',
|
||||
'arguments' => new stdClass(),
|
||||
],
|
||||
'id' => 2,
|
||||
]),
|
||||
]);
|
||||
|
||||
// First list tools to see what's registered
|
||||
$listResponse = $session['client']->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profile->getId(),
|
||||
'X-Profile-Password' => 'test123',
|
||||
'Mcp-Session-Id' => $session['sessionId'],
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'tools/list',
|
||||
'params' => new stdClass(),
|
||||
'id' => 3,
|
||||
]),
|
||||
]);
|
||||
$toolsList = $listResponse->toArray(false);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$data = $response->toArray(false);
|
||||
$this->assertArrayHasKey('result', $data, 'Tools list: '.json_encode($toolsList).' | Call response: '.json_encode($data));
|
||||
|
||||
// Parse the text content from the MCP response
|
||||
$content = $data['result']['content'][0]['text'] ?? '';
|
||||
$stats = json_decode($content, true);
|
||||
$this->assertIsArray($stats);
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$stats = $data['_parsed'];
|
||||
$this->assertGreaterThanOrEqual(2, $stats['machines']);
|
||||
$this->assertArrayHasKey('sites', $stats);
|
||||
$this->assertArrayHasKey('unresolvedComments', $stats);
|
||||
}
|
||||
|
||||
private function initMcpSession(string $profileId, string $password): array
|
||||
{
|
||||
$client = static::createClient();
|
||||
|
||||
// Step 1: Initialize MCP session
|
||||
$response = $client->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profileId,
|
||||
'X-Profile-Password' => $password,
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'initialize',
|
||||
'params' => [
|
||||
'protocolVersion' => '2025-03-26',
|
||||
'capabilities' => new stdClass(),
|
||||
'clientInfo' => ['name' => 'test', 'version' => '1.0'],
|
||||
],
|
||||
'id' => 1,
|
||||
]),
|
||||
]);
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$sessionId = $response->getHeaders()['mcp-session-id'][0] ?? '';
|
||||
$this->assertNotEmpty($sessionId, 'MCP session ID should be returned');
|
||||
|
||||
// Step 2: Send initialized notification
|
||||
$client->request('POST', '/_mcp', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'X-Profile-Id' => $profileId,
|
||||
'X-Profile-Password' => $password,
|
||||
'Mcp-Session-Id' => $sessionId,
|
||||
],
|
||||
'body' => json_encode([
|
||||
'jsonrpc' => '2.0',
|
||||
'method' => 'notifications/initialized',
|
||||
]),
|
||||
]);
|
||||
|
||||
return ['client' => $client, 'sessionId' => $sessionId];
|
||||
}
|
||||
}
|
||||
|
||||
99
tests/Mcp/Tool/Product/ProductsCrudToolTest.php
Normal file
99
tests/Mcp/Tool/Product/ProductsCrudToolTest.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\Product;
|
||||
|
||||
use App\Enum\ModelCategory;
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ProductsCrudToolTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListProducts(): void
|
||||
{
|
||||
$this->createProduct(name: 'Product Alpha');
|
||||
$this->createProduct(name: 'Product Beta');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_products');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
|
||||
}
|
||||
|
||||
public function testGetProduct(): void
|
||||
{
|
||||
$constructeur = $this->createConstructeur(name: 'Fournisseur A');
|
||||
$modelType = $this->createModelType(name: 'Type Produit', code: 'TP-001', category: ModelCategory::PRODUCT);
|
||||
$product = $this->createProduct(name: 'Product Gamma', reference: 'REF-001', type: $modelType);
|
||||
|
||||
// Add constructeur to product
|
||||
$product->addConstructeur($constructeur);
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_product', ['productId' => $product->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Product Gamma', $data['_parsed']['name']);
|
||||
$this->assertSame('REF-001', $data['_parsed']['reference']);
|
||||
$this->assertNotNull($data['_parsed']['typeProduct']);
|
||||
$this->assertSame('Type Produit', $data['_parsed']['typeProduct']['name']);
|
||||
$this->assertCount(1, $data['_parsed']['constructeurs']);
|
||||
$this->assertSame('Fournisseur A', $data['_parsed']['constructeurs'][0]['name']);
|
||||
}
|
||||
|
||||
public function testCreateProduct(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_product', [
|
||||
'name' => 'Product Nouveau',
|
||||
'reference' => 'REF-NEW',
|
||||
'supplierPrice' => '42.99',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Product Nouveau', $data['_parsed']['name']);
|
||||
$this->assertNotEmpty($data['_parsed']['id']);
|
||||
}
|
||||
|
||||
public function testCreateProductRequiresGestionnaire(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_product', ['name' => 'Forbidden']);
|
||||
|
||||
$this->assertArrayHasKey('error', $data, 'Should fail with VIEWER role');
|
||||
}
|
||||
|
||||
public function testUpdateProduct(): void
|
||||
{
|
||||
$product = $this->createProduct(name: 'Old Product');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'update_product', [
|
||||
'productId' => $product->getId(),
|
||||
'name' => 'Updated Product',
|
||||
'supplierPrice' => '99.00',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Updated Product', $data['_parsed']['name']);
|
||||
}
|
||||
|
||||
public function testDeleteProduct(): void
|
||||
{
|
||||
$product = $this->createProduct(name: 'To Delete');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_product', ['productId' => $product->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertTrue($data['_parsed']['deleted']);
|
||||
}
|
||||
}
|
||||
84
tests/Mcp/Tool/Site/SitesCrudToolTest.php
Normal file
84
tests/Mcp/Tool/Site/SitesCrudToolTest.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Mcp\Tool\Site;
|
||||
|
||||
use App\Tests\AbstractApiTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class SitesCrudToolTest extends AbstractApiTestCase
|
||||
{
|
||||
public function testListSites(): void
|
||||
{
|
||||
$this->createSite(name: 'Site Alpha');
|
||||
$this->createSite(name: 'Site Beta');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'list_sites');
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertGreaterThanOrEqual(2, $data['_parsed']['total']);
|
||||
}
|
||||
|
||||
public function testGetSite(): void
|
||||
{
|
||||
$site = $this->createSite(name: 'Site Gamma');
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'get_site', ['siteId' => $site->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Site Gamma', $data['_parsed']['name']);
|
||||
}
|
||||
|
||||
public function testCreateSite(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_site', [
|
||||
'name' => 'Site Nouveau',
|
||||
'contactCity' => 'Paris',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('Site Nouveau', $data['_parsed']['name']);
|
||||
$this->assertNotEmpty($data['_parsed']['id']);
|
||||
}
|
||||
|
||||
public function testCreateSiteRequiresGestionnaire(): void
|
||||
{
|
||||
$session = $this->createMcpClient('ROLE_VIEWER');
|
||||
|
||||
$data = $this->callMcpTool($session, 'create_site', ['name' => 'Forbidden']);
|
||||
|
||||
$this->assertArrayHasKey('error', $data);
|
||||
}
|
||||
|
||||
public function testUpdateSite(): void
|
||||
{
|
||||
$site = $this->createSite(name: 'Old Name');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'update_site', [
|
||||
'siteId' => $site->getId(),
|
||||
'name' => 'New Name',
|
||||
]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertSame('New Name', $data['_parsed']['name']);
|
||||
}
|
||||
|
||||
public function testDeleteSite(): void
|
||||
{
|
||||
$site = $this->createSite(name: 'To Delete');
|
||||
$session = $this->createMcpClient('ROLE_GESTIONNAIRE');
|
||||
|
||||
$data = $this->callMcpTool($session, 'delete_site', ['siteId' => $site->getId()]);
|
||||
|
||||
$this->assertArrayHasKey('_parsed', $data);
|
||||
$this->assertTrue($data['_parsed']['deleted']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user