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:
Matthieu
2026-03-16 14:31:15 +01:00
parent e335f4c24c
commit 4f1e136dc5
20 changed files with 1184 additions and 90 deletions

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Entity\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_constructeur',
description: 'Create a new constructeur (manufacturer/supplier). Requires ROLE_GESTIONNAIRE.',
)]
class CreateConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $name,
string $email = '',
string $phone = '',
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = new Constructeur();
$constructeur->setName($name);
$constructeur->setEmail('' !== $email ? $email : null);
$constructeur->setPhone('' !== $phone ? $phone : null);
$this->em->persist($constructeur);
$this->em->flush();
return $this->jsonResponse([
'id' => $constructeur->getId(),
'name' => $constructeur->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_constructeur',
description: 'Delete a constructeur by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $constructeurId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
$this->em->remove($constructeur);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $constructeurId]);
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_constructeur',
description: 'Get a single constructeur (manufacturer/supplier) by ID with all its details.',
)]
class GetConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
) {}
public function __invoke(string $constructeurId): array
{
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
return $this->jsonResponse([
'id' => $constructeur->getId(),
'name' => $constructeur->getName(),
'email' => $constructeur->getEmail(),
'phone' => $constructeur->getPhone(),
'createdAt' => $constructeur->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $constructeur->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_constructeurs',
description: 'List all constructeurs (manufacturers/suppliers) with pagination. Filterable by name.',
)]
class ListConstructeursTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->constructeurs->createQueryBuilder('c')
->select('COUNT(c.id)')
;
$qb = $this->constructeurs->createQueryBuilder('c')
->select('c.id', 'c.name', 'c.email', 'c.phone')
->orderBy('c.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(c.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(c.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Constructeur;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_constructeur',
description: 'Update an existing constructeur. Only provided fields are changed. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateConstructeurTool
{
use McpToolHelper;
public function __construct(
private readonly ConstructeurRepository $constructeurs,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $constructeurId,
?string $name = null,
?string $email = null,
?string $phone = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$constructeur = $this->constructeurs->find($constructeurId);
if (!$constructeur) {
$this->mcpError('not_found', "Constructeur not found: {$constructeurId}");
}
if (null !== $name) {
$constructeur->setName($name);
}
if (null !== $email) {
$constructeur->setEmail($email);
}
if (null !== $phone) {
$constructeur->setPhone($phone);
}
$this->em->flush();
return $this->jsonResponse(['id' => $constructeur->getId(), 'name' => $constructeur->getName()]);
}
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Entity\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_product',
description: 'Create a new product. supplierPrice must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
)]
class CreateProductTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param string[] $constructeurIds
*/
public function __invoke(
string $name,
string $reference = '',
string $supplierPrice = '',
string $modelTypeId = '',
array $constructeurIds = [],
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = new Product();
$product->setName($name);
if ('' !== $reference) {
$product->setReference($reference);
}
if ('' !== $supplierPrice) {
$product->setSupplierPrice($supplierPrice);
}
if ('' !== $modelTypeId) {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$product->setTypeProduct($modelType);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$product->addConstructeur($c);
}
$this->em->persist($product);
$this->em->flush();
return $this->jsonResponse([
'id' => $product->getId(),
'name' => $product->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_product',
description: 'Delete a product by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $productId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
$this->em->remove($product);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $productId]);
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_product',
description: 'Get a single product by ID with all its details, including typeProduct and constructeurs.',
)]
class GetProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
) {}
public function __invoke(string $productId): array
{
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
$constructeurs = [];
foreach ($product->getConstructeurs() as $c) {
$constructeurs[] = [
'id' => $c->getId(),
'name' => $c->getName(),
];
}
$typeProduct = null;
if ($product->getTypeProduct()) {
$typeProduct = [
'id' => $product->getTypeProduct()->getId(),
'name' => $product->getTypeProduct()->getName(),
];
}
return $this->jsonResponse([
'id' => $product->getId(),
'name' => $product->getName(),
'reference' => $product->getReference(),
'supplierPrice' => $product->getSupplierPrice(),
'typeProduct' => $typeProduct,
'constructeurs' => $constructeurs,
'createdAt' => $product->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $product->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ProductRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_products',
description: 'List products with pagination. Filterable by name or reference.',
)]
class ListProductsTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->products->createQueryBuilder('pr')
->select('COUNT(pr.id)')
;
$qb = $this->products->createQueryBuilder('pr')
->select('pr.id', 'pr.name', 'pr.reference', 'pr.supplierPrice')
->orderBy('pr.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(pr.name) LIKE LOWER(:search) OR LOWER(pr.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(pr.name) LIKE LOWER(:search) OR LOWER(pr.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Product;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use App\Repository\ProductRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_product',
description: 'Update an existing product. Only provided fields are changed. supplierPrice must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateProductTool
{
use McpToolHelper;
public function __construct(
private readonly ProductRepository $products,
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param null|string[] $constructeurIds
*/
public function __invoke(
string $productId,
?string $name = null,
?string $reference = null,
?string $supplierPrice = null,
?string $modelTypeId = null,
?array $constructeurIds = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$product = $this->products->find($productId);
if (!$product) {
$this->mcpError('not_found', "Product not found: {$productId}");
}
if (null !== $name) {
$product->setName($name);
}
if (null !== $reference) {
$product->setReference($reference);
}
if (null !== $supplierPrice) {
$product->setSupplierPrice($supplierPrice);
}
if (null !== $modelTypeId) {
if ('' === $modelTypeId) {
$product->setTypeProduct(null);
} else {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$product->setTypeProduct($modelType);
}
}
if (null !== $constructeurIds) {
foreach ($product->getConstructeurs()->toArray() as $existing) {
$product->removeConstructeur($existing);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$product->addConstructeur($c);
}
}
$this->em->flush();
return $this->jsonResponse(['id' => $product->getId(), 'name' => $product->getName()]);
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Entity\Site;
use App\Mcp\Tool\McpToolHelper;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_site',
description: 'Create a new industrial site. Requires ROLE_GESTIONNAIRE.',
)]
class CreateSiteTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $name,
string $contactName = '',
string $contactPhone = '',
string $contactAddress = '',
string $contactPostalCode = '',
string $contactCity = '',
string $color = '',
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = new Site();
$site->setName($name);
$site->setContactName($contactName);
$site->setContactPhone($contactPhone);
$site->setContactAddress($contactAddress);
$site->setContactPostalCode($contactPostalCode);
$site->setContactCity($contactCity);
$site->setColor($color);
$this->em->persist($site);
$this->em->flush();
return $this->jsonResponse([
'id' => $site->getId(),
'name' => $site->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_site',
description: 'Delete a site by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $siteId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
$this->em->remove($site);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $siteId]);
}
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_site',
description: 'Get a single site by ID with all its details (name, contact info, address).',
)]
class GetSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
) {}
public function __invoke(string $siteId): array
{
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
return $this->jsonResponse([
'id' => $site->getId(),
'name' => $site->getName(),
'contactName' => $site->getContactName(),
'contactPhone' => $site->getContactPhone(),
'contactAddress' => $site->getContactAddress(),
'contactPostalCode' => $site->getContactPostalCode(),
'contactCity' => $site->getContactCity(),
'color' => $site->getColor(),
'createdAt' => $site->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $site->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_sites',
description: 'List all industrial sites with pagination. Sites contain machines. Filterable by name.',
)]
class ListSitesTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->sites->createQueryBuilder('s')
->select('COUNT(s.id)')
;
$qb = $this->sites->createQueryBuilder('s')
->select('s.id', 's.name', 's.contactName', 's.contactCity', 's.contactPhone')
->orderBy('s.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(s.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(s.name) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Site;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_site',
description: 'Update an existing site. Only provided fields are changed. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateSiteTool
{
use McpToolHelper;
public function __construct(
private readonly SiteRepository $sites,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(
string $siteId,
?string $name = null,
?string $contactName = null,
?string $contactPhone = null,
?string $contactAddress = null,
?string $contactPostalCode = null,
?string $contactCity = null,
?string $color = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
if (null !== $name) {
$site->setName($name);
}
if (null !== $contactName) {
$site->setContactName($contactName);
}
if (null !== $contactPhone) {
$site->setContactPhone($contactPhone);
}
if (null !== $contactAddress) {
$site->setContactAddress($contactAddress);
}
if (null !== $contactPostalCode) {
$site->setContactPostalCode($contactPostalCode);
}
if (null !== $contactCity) {
$site->setContactCity($contactCity);
}
if (null !== $color) {
$site->setColor($color);
}
$this->em->flush();
return $this->jsonResponse(['id' => $site->getId(), 'name' => $site->getName()]);
}
}

View File

@@ -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(

View 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']);
}
}

View File

@@ -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];
}
}

View 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']);
}
}

View 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']);
}
}