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:
80
src/Mcp/Tool/Composant/CreateComposantTool.php
Normal file
80
src/Mcp/Tool/Composant/CreateComposantTool.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Composant;
|
||||
|
||||
use App\Entity\Composant;
|
||||
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_composant',
|
||||
description: 'Create a new composant. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class CreateComposantTool
|
||||
{
|
||||
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 $description = '',
|
||||
string $prix = '',
|
||||
string $modelTypeId = '',
|
||||
array $constructeurIds = [],
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$composant = new Composant();
|
||||
$composant->setName($name);
|
||||
|
||||
if ('' !== $reference) {
|
||||
$composant->setReference($reference);
|
||||
}
|
||||
if ('' !== $description) {
|
||||
$composant->setDescription($description);
|
||||
}
|
||||
if ('' !== $prix) {
|
||||
$composant->setPrix($prix);
|
||||
}
|
||||
|
||||
if ('' !== $modelTypeId) {
|
||||
$modelType = $this->modelTypes->find($modelTypeId);
|
||||
if (!$modelType) {
|
||||
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
||||
}
|
||||
$composant->setTypeComposant($modelType);
|
||||
}
|
||||
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$composant->addConstructeur($c);
|
||||
}
|
||||
|
||||
$this->em->persist($composant);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $composant->getId(),
|
||||
'name' => $composant->getName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
src/Mcp/Tool/Composant/DeleteComposantTool.php
Normal file
42
src/Mcp/Tool/Composant/DeleteComposantTool.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Composant;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ComposantRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'delete_composant',
|
||||
description: 'Delete a composant by ID. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class DeleteComposantTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly ComposantRepository $composants,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $composantId): array
|
||||
{
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$composant = $this->composants->find($composantId);
|
||||
|
||||
if (!$composant) {
|
||||
$this->mcpError('not_found', "Composant not found: {$composantId}");
|
||||
}
|
||||
|
||||
$this->em->remove($composant);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['deleted' => true, 'id' => $composantId]);
|
||||
}
|
||||
}
|
||||
59
src/Mcp/Tool/Composant/GetComposantTool.php
Normal file
59
src/Mcp/Tool/Composant/GetComposantTool.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Composant;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ComposantRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'get_composant',
|
||||
description: 'Get a single composant by ID with all its details, including typeComposant and constructeurs.',
|
||||
)]
|
||||
class GetComposantTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly ComposantRepository $composants,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $composantId): array
|
||||
{
|
||||
$composant = $this->composants->find($composantId);
|
||||
|
||||
if (!$composant) {
|
||||
$this->mcpError('not_found', "Composant not found: {$composantId}");
|
||||
}
|
||||
|
||||
$constructeurs = [];
|
||||
foreach ($composant->getConstructeurs() as $c) {
|
||||
$constructeurs[] = [
|
||||
'id' => $c->getId(),
|
||||
'name' => $c->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$typeComposant = null;
|
||||
if ($composant->getTypeComposant()) {
|
||||
$typeComposant = [
|
||||
'id' => $composant->getTypeComposant()->getId(),
|
||||
'name' => $composant->getTypeComposant()->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $composant->getId(),
|
||||
'name' => $composant->getName(),
|
||||
'reference' => $composant->getReference(),
|
||||
'description' => $composant->getDescription(),
|
||||
'prix' => $composant->getPrix(),
|
||||
'typeComposant' => $typeComposant,
|
||||
'constructeurs' => $constructeurs,
|
||||
'createdAt' => $composant->getCreatedAt()->format('Y-m-d H:i:s'),
|
||||
'updatedAt' => $composant->getUpdatedAt()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
src/Mcp/Tool/Composant/ListComposantsTool.php
Normal file
55
src/Mcp/Tool/Composant/ListComposantsTool.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Composant;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ComposantRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'list_composants',
|
||||
description: 'List composants with pagination. Filterable by name or reference.',
|
||||
)]
|
||||
class ListComposantsTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly ComposantRepository $composants,
|
||||
) {}
|
||||
|
||||
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
|
||||
{
|
||||
$p = $this->paginationParams($page, $limit);
|
||||
|
||||
$countQb = $this->composants->createQueryBuilder('c')
|
||||
->select('COUNT(c.id)')
|
||||
;
|
||||
|
||||
$qb = $this->composants->createQueryBuilder('c')
|
||||
->select('c.id', 'c.name', 'c.reference', 'c.prix')
|
||||
->orderBy('c.name', 'ASC')
|
||||
;
|
||||
|
||||
if ('' !== $search) {
|
||||
$countQb->andWhere('LOWER(c.name) LIKE LOWER(:search) OR LOWER(c.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
$qb->andWhere('LOWER(c.name) LIKE LOWER(:search) OR LOWER(c.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']);
|
||||
}
|
||||
}
|
||||
93
src/Mcp/Tool/Composant/UpdateComposantTool.php
Normal file
93
src/Mcp/Tool/Composant/UpdateComposantTool.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Composant;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ComposantRepository;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\ModelTypeRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'update_composant',
|
||||
description: 'Update an existing composant. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class UpdateComposantTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly ComposantRepository $composants,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly ModelTypeRepository $modelTypes,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param null|string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $composantId,
|
||||
?string $name = null,
|
||||
?string $reference = null,
|
||||
?string $description = null,
|
||||
?string $prix = null,
|
||||
?string $modelTypeId = null,
|
||||
?array $constructeurIds = null,
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$composant = $this->composants->find($composantId);
|
||||
|
||||
if (!$composant) {
|
||||
$this->mcpError('not_found', "Composant not found: {$composantId}");
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$composant->setName($name);
|
||||
}
|
||||
if (null !== $reference) {
|
||||
$composant->setReference($reference);
|
||||
}
|
||||
if (null !== $description) {
|
||||
$composant->setDescription($description);
|
||||
}
|
||||
if (null !== $prix) {
|
||||
$composant->setPrix($prix);
|
||||
}
|
||||
|
||||
if (null !== $modelTypeId) {
|
||||
if ('' === $modelTypeId) {
|
||||
$composant->setTypeComposant(null);
|
||||
} else {
|
||||
$modelType = $this->modelTypes->find($modelTypeId);
|
||||
if (!$modelType) {
|
||||
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
||||
}
|
||||
$composant->setTypeComposant($modelType);
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $constructeurIds) {
|
||||
foreach ($composant->getConstructeurs()->toArray() as $existing) {
|
||||
$composant->removeConstructeur($existing);
|
||||
}
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$composant->addConstructeur($c);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['id' => $composant->getId(), 'name' => $composant->getName()]);
|
||||
}
|
||||
}
|
||||
74
src/Mcp/Tool/Machine/CreateMachineTool.php
Normal file
74
src/Mcp/Tool/Machine/CreateMachineTool.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Entity\Machine;
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\SiteRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'create_machine',
|
||||
description: 'Create a new machine. siteId is required. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class CreateMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly SiteRepository $sites,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $name,
|
||||
string $siteId,
|
||||
string $reference = '',
|
||||
string $prix = '',
|
||||
array $constructeurIds = [],
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$site = $this->sites->find($siteId);
|
||||
if (!$site) {
|
||||
$this->mcpError('not_found', "Site not found: {$siteId}");
|
||||
}
|
||||
|
||||
$machine = new Machine();
|
||||
$machine->setName($name);
|
||||
$machine->setSite($site);
|
||||
|
||||
if ('' !== $reference) {
|
||||
$machine->setReference($reference);
|
||||
}
|
||||
if ('' !== $prix) {
|
||||
$machine->setPrix($prix);
|
||||
}
|
||||
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$machine->addConstructeur($c);
|
||||
}
|
||||
|
||||
$this->em->persist($machine);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $machine->getId(),
|
||||
'name' => $machine->getName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
src/Mcp/Tool/Machine/DeleteMachineTool.php
Normal file
42
src/Mcp/Tool/Machine/DeleteMachineTool.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'delete_machine',
|
||||
description: 'Delete a machine by ID. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class DeleteMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $machineId): array
|
||||
{
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
$this->em->remove($machine);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['deleted' => true, 'id' => $machineId]);
|
||||
}
|
||||
}
|
||||
58
src/Mcp/Tool/Machine/GetMachineTool.php
Normal file
58
src/Mcp/Tool/Machine/GetMachineTool.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'get_machine',
|
||||
description: 'Get a single machine by ID with all its details, including site and constructeurs.',
|
||||
)]
|
||||
class GetMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $machineId): array
|
||||
{
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
$constructeurs = [];
|
||||
foreach ($machine->getConstructeurs() as $c) {
|
||||
$constructeurs[] = [
|
||||
'id' => $c->getId(),
|
||||
'name' => $c->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$site = null;
|
||||
if ($machine->getSite()) {
|
||||
$site = [
|
||||
'id' => $machine->getSite()->getId(),
|
||||
'name' => $machine->getSite()->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $machine->getId(),
|
||||
'name' => $machine->getName(),
|
||||
'reference' => $machine->getReference(),
|
||||
'prix' => $machine->getPrix(),
|
||||
'site' => $site,
|
||||
'constructeurs' => $constructeurs,
|
||||
'createdAt' => $machine->getCreatedAt()->format('Y-m-d H:i:s'),
|
||||
'updatedAt' => $machine->getUpdatedAt()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
src/Mcp/Tool/Machine/ListMachinesTool.php
Normal file
55
src/Mcp/Tool/Machine/ListMachinesTool.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\MachineRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'list_machines',
|
||||
description: 'List machines with pagination. Filterable by name or reference.',
|
||||
)]
|
||||
class ListMachinesTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
) {}
|
||||
|
||||
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
|
||||
{
|
||||
$p = $this->paginationParams($page, $limit);
|
||||
|
||||
$countQb = $this->machines->createQueryBuilder('m')
|
||||
->select('COUNT(m.id)')
|
||||
;
|
||||
|
||||
$qb = $this->machines->createQueryBuilder('m')
|
||||
->select('m.id', 'm.name', 'm.reference', 'm.prix')
|
||||
->orderBy('m.name', 'ASC')
|
||||
;
|
||||
|
||||
if ('' !== $search) {
|
||||
$countQb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
$qb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.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']);
|
||||
}
|
||||
}
|
||||
85
src/Mcp/Tool/Machine/UpdateMachineTool.php
Normal file
85
src/Mcp/Tool/Machine/UpdateMachineTool.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Machine;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\MachineRepository;
|
||||
use App\Repository\SiteRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'update_machine',
|
||||
description: 'Update an existing machine. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class UpdateMachineTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly MachineRepository $machines,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly SiteRepository $sites,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param null|string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $machineId,
|
||||
?string $name = null,
|
||||
?string $reference = null,
|
||||
?string $prix = null,
|
||||
?string $siteId = null,
|
||||
?array $constructeurIds = null,
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$machine = $this->machines->find($machineId);
|
||||
|
||||
if (!$machine) {
|
||||
$this->mcpError('not_found', "Machine not found: {$machineId}");
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$machine->setName($name);
|
||||
}
|
||||
if (null !== $reference) {
|
||||
$machine->setReference($reference);
|
||||
}
|
||||
if (null !== $prix) {
|
||||
$machine->setPrix($prix);
|
||||
}
|
||||
|
||||
if (null !== $siteId) {
|
||||
$site = $this->sites->find($siteId);
|
||||
if (!$site) {
|
||||
$this->mcpError('not_found', "Site not found: {$siteId}");
|
||||
}
|
||||
$machine->setSite($site);
|
||||
}
|
||||
|
||||
if (null !== $constructeurIds) {
|
||||
foreach ($machine->getConstructeurs()->toArray() as $existing) {
|
||||
$machine->removeConstructeur($existing);
|
||||
}
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$machine->addConstructeur($c);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['id' => $machine->getId(), 'name' => $machine->getName()]);
|
||||
}
|
||||
}
|
||||
80
src/Mcp/Tool/Piece/CreatePieceTool.php
Normal file
80
src/Mcp/Tool/Piece/CreatePieceTool.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Entity\Piece;
|
||||
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_piece',
|
||||
description: 'Create a new piece. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class CreatePieceTool
|
||||
{
|
||||
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 $description = '',
|
||||
string $prix = '',
|
||||
string $modelTypeId = '',
|
||||
array $constructeurIds = [],
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$piece = new Piece();
|
||||
$piece->setName($name);
|
||||
|
||||
if ('' !== $reference) {
|
||||
$piece->setReference($reference);
|
||||
}
|
||||
if ('' !== $description) {
|
||||
$piece->setDescription($description);
|
||||
}
|
||||
if ('' !== $prix) {
|
||||
$piece->setPrix($prix);
|
||||
}
|
||||
|
||||
if ('' !== $modelTypeId) {
|
||||
$modelType = $this->modelTypes->find($modelTypeId);
|
||||
if (!$modelType) {
|
||||
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
||||
}
|
||||
$piece->setTypePiece($modelType);
|
||||
}
|
||||
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$piece->addConstructeur($c);
|
||||
}
|
||||
|
||||
$this->em->persist($piece);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $piece->getId(),
|
||||
'name' => $piece->getName(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
src/Mcp/Tool/Piece/DeletePieceTool.php
Normal file
42
src/Mcp/Tool/Piece/DeletePieceTool.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'delete_piece',
|
||||
description: 'Delete a piece by ID. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class DeletePieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $pieceId): array
|
||||
{
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$piece = $this->pieces->find($pieceId);
|
||||
|
||||
if (!$piece) {
|
||||
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
||||
}
|
||||
|
||||
$this->em->remove($piece);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['deleted' => true, 'id' => $pieceId]);
|
||||
}
|
||||
}
|
||||
59
src/Mcp/Tool/Piece/GetPieceTool.php
Normal file
59
src/Mcp/Tool/Piece/GetPieceTool.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'get_piece',
|
||||
description: 'Get a single piece by ID with all its details, including typePiece and constructeurs.',
|
||||
)]
|
||||
class GetPieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
) {}
|
||||
|
||||
public function __invoke(string $pieceId): array
|
||||
{
|
||||
$piece = $this->pieces->find($pieceId);
|
||||
|
||||
if (!$piece) {
|
||||
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
||||
}
|
||||
|
||||
$constructeurs = [];
|
||||
foreach ($piece->getConstructeurs() as $c) {
|
||||
$constructeurs[] = [
|
||||
'id' => $c->getId(),
|
||||
'name' => $c->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
$typePiece = null;
|
||||
if ($piece->getTypePiece()) {
|
||||
$typePiece = [
|
||||
'id' => $piece->getTypePiece()->getId(),
|
||||
'name' => $piece->getTypePiece()->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->jsonResponse([
|
||||
'id' => $piece->getId(),
|
||||
'name' => $piece->getName(),
|
||||
'reference' => $piece->getReference(),
|
||||
'description' => $piece->getDescription(),
|
||||
'prix' => $piece->getPrix(),
|
||||
'typePiece' => $typePiece,
|
||||
'constructeurs' => $constructeurs,
|
||||
'createdAt' => $piece->getCreatedAt()->format('Y-m-d H:i:s'),
|
||||
'updatedAt' => $piece->getUpdatedAt()->format('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
55
src/Mcp/Tool/Piece/ListPiecesTool.php
Normal file
55
src/Mcp/Tool/Piece/ListPiecesTool.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\PieceRepository;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
|
||||
#[McpTool(
|
||||
name: 'list_pieces',
|
||||
description: 'List pieces with pagination. Filterable by name or reference.',
|
||||
)]
|
||||
class ListPiecesTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
) {}
|
||||
|
||||
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
|
||||
{
|
||||
$p = $this->paginationParams($page, $limit);
|
||||
|
||||
$countQb = $this->pieces->createQueryBuilder('pi')
|
||||
->select('COUNT(pi.id)')
|
||||
;
|
||||
|
||||
$qb = $this->pieces->createQueryBuilder('pi')
|
||||
->select('pi.id', 'pi.name', 'pi.reference', 'pi.prix')
|
||||
->orderBy('pi.name', 'ASC')
|
||||
;
|
||||
|
||||
if ('' !== $search) {
|
||||
$countQb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.reference) LIKE LOWER(:search)')
|
||||
->setParameter('search', "%{$search}%")
|
||||
;
|
||||
$qb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.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']);
|
||||
}
|
||||
}
|
||||
93
src/Mcp/Tool/Piece/UpdatePieceTool.php
Normal file
93
src/Mcp/Tool/Piece/UpdatePieceTool.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Mcp\Tool\Piece;
|
||||
|
||||
use App\Mcp\Tool\McpToolHelper;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
use App\Repository\ModelTypeRepository;
|
||||
use App\Repository\PieceRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mcp\Capability\Attribute\McpTool;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
#[McpTool(
|
||||
name: 'update_piece',
|
||||
description: 'Update an existing piece. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
|
||||
)]
|
||||
class UpdatePieceTool
|
||||
{
|
||||
use McpToolHelper;
|
||||
|
||||
public function __construct(
|
||||
private readonly PieceRepository $pieces,
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly Security $security,
|
||||
private readonly ModelTypeRepository $modelTypes,
|
||||
private readonly ConstructeurRepository $constructeurs,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param null|string[] $constructeurIds
|
||||
*/
|
||||
public function __invoke(
|
||||
string $pieceId,
|
||||
?string $name = null,
|
||||
?string $reference = null,
|
||||
?string $description = null,
|
||||
?string $prix = null,
|
||||
?string $modelTypeId = null,
|
||||
?array $constructeurIds = null,
|
||||
): array {
|
||||
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
|
||||
|
||||
$piece = $this->pieces->find($pieceId);
|
||||
|
||||
if (!$piece) {
|
||||
$this->mcpError('not_found', "Piece not found: {$pieceId}");
|
||||
}
|
||||
|
||||
if (null !== $name) {
|
||||
$piece->setName($name);
|
||||
}
|
||||
if (null !== $reference) {
|
||||
$piece->setReference($reference);
|
||||
}
|
||||
if (null !== $description) {
|
||||
$piece->setDescription($description);
|
||||
}
|
||||
if (null !== $prix) {
|
||||
$piece->setPrix($prix);
|
||||
}
|
||||
|
||||
if (null !== $modelTypeId) {
|
||||
if ('' === $modelTypeId) {
|
||||
$piece->setTypePiece(null);
|
||||
} else {
|
||||
$modelType = $this->modelTypes->find($modelTypeId);
|
||||
if (!$modelType) {
|
||||
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
|
||||
}
|
||||
$piece->setTypePiece($modelType);
|
||||
}
|
||||
}
|
||||
|
||||
if (null !== $constructeurIds) {
|
||||
foreach ($piece->getConstructeurs()->toArray() as $existing) {
|
||||
$piece->removeConstructeur($existing);
|
||||
}
|
||||
foreach ($constructeurIds as $cId) {
|
||||
$c = $this->constructeurs->find($cId);
|
||||
if (!$c) {
|
||||
$this->mcpError('not_found', "Constructeur not found: {$cId}");
|
||||
}
|
||||
$piece->addConstructeur($c);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->jsonResponse(['id' => $piece->getId(), 'name' => $piece->getName()]);
|
||||
}
|
||||
}
|
||||
99
tests/Mcp/Tool/Composant/ComposantsCrudToolTest.php
Normal file
99
tests/Mcp/Tool/Composant/ComposantsCrudToolTest.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
107
tests/Mcp/Tool/Machine/MachinesCrudToolTest.php
Normal file
107
tests/Mcp/Tool/Machine/MachinesCrudToolTest.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
99
tests/Mcp/Tool/Piece/PiecesCrudToolTest.php
Normal file
99
tests/Mcp/Tool/Piece/PiecesCrudToolTest.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user