Files
Inventory/tests/Api/Entity/ComposantTest.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

139 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ComposantTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createComposant('Pompe A');
$this->createComposant('Pompe B');
$client = $this->createViewerClient();
$client->request('GET', '/api/composants');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$c = $this->createComposant('Pompe A');
$client = $this->createViewerClient();
$client->request('GET', self::iri('composants', $c->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Pompe A']);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/composants', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Nouveau composant',
'reference' => 'REF-COMP-001',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Nouveau composant']);
}
public function testPostWithType(): void
{
$mt = $this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/composants', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Composant typé',
'typeComposant' => self::iri('model_types', $mt->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPut(): void
{
$c = $this->createComposant('Pompe A');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('composants', $c->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Pompe A Renommée'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Pompe A Renommée']);
}
public function testPatch(): void
{
$c = $this->createComposant('Pompe A');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('composants', $c->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['prix' => '250.00'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['prix' => '250.00']);
}
public function testDelete(): void
{
$c = $this->createComposant('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('composants', $c->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/composants');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/composants', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Blocked'],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testSearchFilter(): void
{
$this->createComposant('Pompe hydraulique');
$this->createComposant('Vanne de contrôle');
$client = $this->createViewerClient();
$client->request('GET', '/api/composants?name=pompe');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['totalItems' => 1]);
}
}