Files
Inventory/tests/Api/Entity/MachineProductLinkTest.php
r-dev efc6ec5691 test(api) : add comprehensive API test suite (161 tests)
- Add AbstractApiTestCase with auth helpers and entity factories
- Add tests for all entities: Machine, Piece, Composant, Product, Site,
  ModelType, Constructeur, CustomField, CustomFieldValue, Document,
  MachineComponentLink, MachinePieceLink, MachineProductLink, Profile
- Add controller tests: CommentController, EntityHistory
- Add HealthCheck, Filter, Pagination, Validation, Session tests
- Test auth (401), authorization (403), CRUD, and edge cases

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 13:42:56 +01:00

122 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class MachineProductLinkTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$machine = $this->createMachine('Machine A');
$product = $this->createProduct('Produit A');
$this->createMachineProductLink($machine, $product);
$client = $this->createViewerClient();
$client->request('GET', '/api/machine_product_links');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testPost(): void
{
$machine = $this->createMachine('Machine A');
$product = $this->createProduct('Produit A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_product_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'product' => self::iri('products', $product->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPostWithParentComponentLink(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe');
$product = $this->createProduct('Produit A');
$compLink = $this->createMachineComponentLink($machine, $composant);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_product_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'product' => self::iri('products', $product->getId()),
'parentComponentLink' => self::iri('machine_component_links', $compLink->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPostWithParentPieceLink(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint');
$product = $this->createProduct('Produit A');
$plLink = $this->createMachinePieceLink($machine, $piece);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_product_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'product' => self::iri('products', $product->getId()),
'parentPieceLink' => self::iri('machine_piece_links', $plLink->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testDelete(): void
{
$machine = $this->createMachine('Machine A');
$product = $this->createProduct('Produit A');
$link = $this->createMachineProductLink($machine, $product);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('machine_product_links', $link->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/machine_product_links');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$machine = $this->createMachine('Machine A');
$product = $this->createProduct('Produit A');
$client = $this->createViewerClient();
$client->request('POST', '/api/machine_product_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'product' => self::iri('products', $product->getId()),
],
]);
$this->assertResponseStatusCodeSame(403);
}
}