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>
This commit is contained in:
2026-03-08 13:42:56 +01:00
parent b342d0e50a
commit efc6ec5691
22 changed files with 2843 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
<?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]);
}
}

View File

@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ConstructeurTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createConstructeur('Siemens');
$this->createConstructeur('ABB');
$client = $this->createViewerClient();
$client->request('GET', '/api/constructeurs');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$c = $this->createConstructeur('Siemens', 'contact@siemens.com', '+33123456789');
$client = $this->createViewerClient();
$client->request('GET', self::iri('constructeurs', $c->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Siemens',
'email' => 'contact@siemens.com',
'phone' => '+33123456789',
]);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/constructeurs', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Schneider',
'email' => 'info@schneider.com',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Schneider']);
}
public function testPut(): void
{
$c = $this->createConstructeur('Siemens');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('constructeurs', $c->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Siemens AG',
'email' => 'updated@siemens.com',
],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Siemens AG']);
}
public function testPatch(): void
{
$c = $this->createConstructeur('Siemens');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('constructeurs', $c->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['phone' => '+33987654321'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['phone' => '+33987654321']);
}
public function testDelete(): void
{
$c = $this->createConstructeur('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('constructeurs', $c->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/constructeurs');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/constructeurs', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Blocked'],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testUniqueNameConstraint(): void
{
$this->createConstructeur('Siemens');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/constructeurs', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Siemens'],
]);
$this->assertResponseStatusCodeSame(422);
}
}

View File

@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class CustomFieldTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$machine = $this->createMachine('Machine A');
$this->createCustomField('Tension', 'number', $machine);
$this->createCustomField('Couleur', 'text', $machine);
$client = $this->createViewerClient();
$client->request('GET', '/api/custom_fields');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$client = $this->createViewerClient();
$client->request('GET', self::iri('custom_fields', $cf->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Tension',
'type' => 'number',
]);
}
public function testPost(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/custom_fields', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Pression',
'type' => 'number',
'machine' => self::iri('machines', $machine->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Pression']);
}
public function testPatch(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('custom_fields', $cf->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['required' => true],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['required' => true]);
}
public function testDelete(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('ToDelete', 'text', $machine);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('custom_fields', $cf->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/custom_fields');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/custom_fields', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Blocked',
'type' => 'text',
],
]);
$this->assertResponseStatusCodeSame(403);
}
}

View File

@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class CustomFieldValueTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$this->createCustomFieldValue($cf, '220', $machine);
$client = $this->createViewerClient();
$client->request('GET', '/api/custom_field_values');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testGetItem(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$cfv = $this->createCustomFieldValue($cf, '220', $machine);
$client = $this->createViewerClient();
$client->request('GET', self::iri('custom_field_values', $cfv->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['value' => '220']);
}
public function testPost(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/custom_field_values', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'value' => '380',
'customField' => self::iri('custom_fields', $cf->getId()),
'machine' => self::iri('machines', $machine->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['value' => '380']);
}
public function testPatch(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Tension', 'number', $machine);
$cfv = $this->createCustomFieldValue($cf, '220', $machine);
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('custom_field_values', $cfv->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['value' => '400'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['value' => '400']);
}
public function testDelete(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('ToDelete', 'text', $machine);
$cfv = $this->createCustomFieldValue($cf, 'val', $machine);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('custom_field_values', $cfv->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/custom_field_values');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$machine = $this->createMachine('Machine A');
$cf = $this->createCustomField('Blocked', 'text', $machine);
$client = $this->createViewerClient();
$client->request('POST', '/api/custom_field_values', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'value' => 'nope',
'customField' => self::iri('custom_fields', $cf->getId()),
],
]);
$this->assertResponseStatusCodeSame(403);
}
}

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Entity\Document;
use App\Entity\Machine;
use App\Entity\Site;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class DocumentTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createDocumentInDb();
$client = $this->createViewerClient();
$client->request('GET', '/api/documents');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testGetItem(): void
{
$doc = $this->createDocumentInDb();
$client = $this->createViewerClient();
$client->request('GET', self::iri('documents', $doc->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'test-doc',
'filename' => 'test.pdf',
'mimeType' => 'application/pdf',
'size' => 1024,
]);
}
public function testPut(): void
{
$doc = $this->createDocumentInDb();
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('documents', $doc->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'renamed-doc',
'filename' => 'test.pdf',
'path' => '/uploads/test.pdf',
'mimeType' => 'application/pdf',
'size' => 1024,
],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'renamed-doc']);
}
public function testDelete(): void
{
$doc = $this->createDocumentInDb();
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('documents', $doc->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/documents');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotDelete(): void
{
$doc = $this->createDocumentInDb();
$client = $this->createViewerClient();
$client->request('DELETE', self::iri('documents', $doc->getId()));
$this->assertResponseStatusCodeSame(403);
}
public function testDocumentLinkedToMachine(): void
{
$machine = $this->createMachine('Machine A');
$doc = $this->createDocumentInDb($machine->getId());
$client = $this->createViewerClient();
$client->request('GET', '/api/documents?machine[exists]=true');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testDocumentLinkedToSite(): void
{
$site = $this->createSite('Site A');
$doc = $this->createDocumentInDb(siteId: $site->getId());
$client = $this->createViewerClient();
$client->request('GET', '/api/documents?site[exists]=true');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['totalItems' => 1]);
}
private function createDocumentInDb(?string $machineId = null, ?string $siteId = null): Document
{
$doc = new Document();
$doc->setName('test-doc');
$doc->setFilename('test.pdf');
$doc->setPath('/uploads/test.pdf');
$doc->setMimeType('application/pdf');
$doc->setSize(1024);
if ($machineId) {
$machine = $this->getEntityManager()->getRepository(Machine::class)->find($machineId);
if ($machine) {
$doc->setMachine($machine);
}
}
if ($siteId) {
$site = $this->getEntityManager()->getRepository(Site::class)->find($siteId);
if ($site) {
$doc->setSite($site);
}
}
$em = $this->getEntityManager();
$em->persist($doc);
$em->flush();
return $doc;
}
}

View File

@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class MachineComponentLinkTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$this->createMachineComponentLink($machine, $composant);
$client = $this->createViewerClient();
$client->request('GET', '/api/machine_component_links');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testGetItem(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$link = $this->createMachineComponentLink($machine, $composant);
$client = $this->createViewerClient();
$client->request('GET', self::iri('machine_component_links', $link->getId()));
$this->assertResponseIsSuccessful();
}
public function testPost(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_component_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'composant' => self::iri('composants', $composant->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPostWithOverrides(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_component_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'composant' => self::iri('composants', $composant->getId()),
'nameOverride' => 'Pompe spéciale',
'referenceOverride' => 'REF-OVERRIDE',
'prixOverride' => '500.00',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains([
'nameOverride' => 'Pompe spéciale',
'referenceOverride' => 'REF-OVERRIDE',
'prixOverride' => '500.00',
]);
}
public function testPatch(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$link = $this->createMachineComponentLink($machine, $composant);
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('machine_component_links', $link->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['nameOverride' => 'Overridden'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['nameOverride' => 'Overridden']);
}
public function testDelete(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$link = $this->createMachineComponentLink($machine, $composant);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('machine_component_links', $link->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/machine_component_links');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$client = $this->createViewerClient();
$client->request('POST', '/api/machine_component_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'composant' => self::iri('composants', $composant->getId()),
],
]);
$this->assertResponseStatusCodeSame(403);
}
}

View File

@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class MachinePieceLinkTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint A');
$this->createMachinePieceLink($machine, $piece);
$client = $this->createViewerClient();
$client->request('GET', '/api/machine_piece_links');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testPost(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_piece_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'piece' => self::iri('pieces', $piece->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPostWithParentComponentLink(): void
{
$machine = $this->createMachine('Machine A');
$composant = $this->createComposant('Pompe A');
$piece = $this->createPiece('Joint A');
$compLink = $this->createMachineComponentLink($machine, $composant);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_piece_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'piece' => self::iri('pieces', $piece->getId()),
'parentLink' => self::iri('machine_component_links', $compLink->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPostWithOverrides(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint A');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machine_piece_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'piece' => self::iri('pieces', $piece->getId()),
'nameOverride' => 'Joint spécial',
'prixOverride' => '25.00',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains([
'nameOverride' => 'Joint spécial',
'prixOverride' => '25.00',
]);
}
public function testDelete(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint A');
$link = $this->createMachinePieceLink($machine, $piece);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('machine_piece_links', $link->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/machine_piece_links');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$machine = $this->createMachine('Machine A');
$piece = $this->createPiece('Joint A');
$client = $this->createViewerClient();
$client->request('POST', '/api/machine_piece_links', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'machine' => self::iri('machines', $machine->getId()),
'piece' => self::iri('pieces', $piece->getId()),
],
]);
$this->assertResponseStatusCodeSame(403);
}
}

View File

@@ -0,0 +1,121 @@
<?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);
}
}

View File

@@ -0,0 +1,135 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class MachineTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$site = $this->createSite();
$this->createMachine('Machine A', $site);
$this->createMachine('Machine B', $site);
$client = $this->createViewerClient();
$client->request('GET', '/api/machines');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$m = $this->createMachine('Machine A', reference: 'REF-001');
$client = $this->createViewerClient();
$client->request('GET', self::iri('machines', $m->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Machine A',
'reference' => 'REF-001',
]);
}
public function testPost(): void
{
$site = $this->createSite('Usine');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machines', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Nouvelle Machine',
'reference' => 'REF-NEW',
'site' => self::iri('sites', $site->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Nouvelle Machine']);
}
public function testPut(): void
{
$m = $this->createMachine('Machine A');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('machines', $m->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Machine A Renommée',
'site' => self::iri('sites', $m->getSite()->getId()),
],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Machine A Renommée']);
}
public function testPatch(): void
{
$m = $this->createMachine('Machine A');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('machines', $m->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['prix' => '1500.00'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['prix' => '1500.00']);
}
public function testDelete(): void
{
$m = $this->createMachine('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('machines', $m->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/machines');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$site = $this->createSite();
$client = $this->createViewerClient();
$client->request('POST', '/api/machines', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Blocked',
'site' => self::iri('sites', $site->getId()),
],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testPostWithoutSiteFails(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/machines', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'No site'],
]);
$this->assertResponseStatusCodeSame(422);
}
}

View File

@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ModelTypeTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$this->createModelType('Joint', 'JOINT-001', ModelCategory::PIECE);
$client = $this->createViewerClient();
$client->request('GET', '/api/model_types');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$mt = $this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$client = $this->createViewerClient();
$client->request('GET', self::iri('model_types', $mt->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Pompe',
'code' => 'POMPE-001',
'category' => 'COMPONENT',
]);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/model_types', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Roulement',
'code' => 'ROUL-001',
'category' => 'PIECE',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains([
'name' => 'Roulement',
'category' => 'PIECE',
]);
}
public function testPut(): void
{
$mt = $this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('model_types', $mt->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Pompe hydraulique',
'code' => 'POMPE-001',
'category' => 'COMPONENT',
],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Pompe hydraulique']);
}
public function testPatch(): void
{
$mt = $this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('model_types', $mt->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['notes' => 'Updated notes'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['notes' => 'Updated notes']);
}
public function testDelete(): void
{
$mt = $this->createModelType('ToDelete', 'DEL-001', ModelCategory::PRODUCT);
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('model_types', $mt->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/model_types');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/model_types', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Blocked',
'code' => 'BLK-001',
'category' => 'COMPONENT',
],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testFilterByCategory(): void
{
$this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$this->createModelType('Joint', 'JOINT-001', ModelCategory::PIECE);
$client = $this->createViewerClient();
$client->request('GET', '/api/model_types?category=COMPONENT');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['totalItems' => 1]);
}
public function testUniqueCodeConstraint(): void
{
$this->createModelType('Pompe', 'POMPE-001', ModelCategory::COMPONENT);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/model_types', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Autre',
'code' => 'POMPE-001',
'category' => 'PIECE',
],
]);
$this->assertResponseStatusCodeSame(409);
}
}

View File

@@ -0,0 +1,145 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class PieceTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createPiece('Joint A');
$this->createPiece('Joint B');
$client = $this->createViewerClient();
$client->request('GET', '/api/pieces');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$p = $this->createPiece('Joint A', 'REF-J001');
$client = $this->createViewerClient();
$client->request('GET', self::iri('pieces', $p->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Joint A',
'reference' => 'REF-J001',
]);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/pieces', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Nouvelle pièce',
'reference' => 'REF-NEW',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Nouvelle pièce']);
}
public function testPostWithType(): void
{
$mt = $this->createModelType('Joint', 'JOINT-001', ModelCategory::PIECE);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/pieces', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Pièce typée',
'typePiece' => self::iri('model_types', $mt->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPut(): void
{
$p = $this->createPiece('Joint A');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('pieces', $p->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Joint A Renommé'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Joint A Renommé']);
}
public function testPatch(): void
{
$p = $this->createPiece('Joint A');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('pieces', $p->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['prix' => '15.50'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['prix' => '15.50']);
}
public function testDelete(): void
{
$p = $this->createPiece('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('pieces', $p->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/pieces');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/pieces', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Blocked'],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testUniqueReferenceConstraint(): void
{
$this->createPiece('Joint A', 'REF-UNIQUE');
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/pieces', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Joint B',
'reference' => 'REF-UNIQUE',
],
]);
$this->assertResponseStatusCodeSame(422);
}
}

View File

@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Enum\ModelCategory;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ProductTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createProduct('Produit A');
$this->createProduct('Produit B');
$client = $this->createViewerClient();
$client->request('GET', '/api/products');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$p = $this->createProduct('Produit A', 'REF-P001');
$client = $this->createViewerClient();
$client->request('GET', self::iri('products', $p->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Produit A',
'reference' => 'REF-P001',
]);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/products', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Nouveau produit',
'reference' => 'REF-NEW',
'supplierPrice' => '99.99',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Nouveau produit']);
}
public function testPostWithType(): void
{
$mt = $this->createModelType('Huile', 'HUILE-001', ModelCategory::PRODUCT);
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/products', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Produit typé',
'typeProduct' => self::iri('model_types', $mt->getId()),
],
]);
$this->assertResponseStatusCodeSame(201);
}
public function testPut(): void
{
$p = $this->createProduct('Produit A');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('products', $p->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Produit A Renommé'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Produit A Renommé']);
}
public function testPatch(): void
{
$p = $this->createProduct('Produit A');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('products', $p->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['supplierPrice' => '150.00'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['supplierPrice' => '150.00']);
}
public function testDelete(): void
{
$p = $this->createProduct('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('products', $p->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/products');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/products', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Blocked'],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testSearchFilter(): void
{
$this->createProduct('Huile moteur');
$this->createProduct('Filtre à air');
$client = $this->createViewerClient();
$client->request('GET', '/api/products?name=huile');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['totalItems' => 1]);
}
}

View File

@@ -0,0 +1,125 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class ProfileTest extends AbstractApiTestCase
{
public function testGetCollectionAsAdmin(): void
{
$this->createProfile(firstName: 'Alice', lastName: 'Dupont');
$client = $this->createAdminClient();
$client->request('GET', '/api/profiles');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
}
public function testGetCollectionForbiddenForViewer(): void
{
$client = $this->createViewerClient();
$client->request('GET', '/api/profiles');
$this->assertResponseStatusCodeSame(403);
}
public function testGetItemForbiddenForViewer(): void
{
$profile = $this->createProfile(firstName: 'Alice', lastName: 'Dupont');
$client = $this->createViewerClient();
$client->request('GET', self::iri('profiles', $profile->getId()));
$this->assertResponseStatusCodeSame(403);
}
public function testGetItemAsAdmin(): void
{
$profile = $this->createProfile(firstName: 'Alice', lastName: 'Dupont');
$client = $this->createAdminClient();
$client->request('GET', self::iri('profiles', $profile->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'firstName' => 'Alice',
'lastName' => 'Dupont',
]);
}
public function testPostAsAdmin(): void
{
$client = $this->createAdminClient();
$client->request('POST', '/api/profiles', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'firstName' => 'Nouveau',
'lastName' => 'Profil',
'email' => 'new@test.local',
'plainPassword' => 'secret123',
'roles' => ['ROLE_VIEWER'],
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains([
'firstName' => 'Nouveau',
'lastName' => 'Profil',
]);
}
public function testPostForbiddenForGestionnaire(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/profiles', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'firstName' => 'Blocked',
'lastName' => 'User',
],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testPatchAsAdmin(): void
{
$profile = $this->createProfile(firstName: 'Alice', lastName: 'Dupont');
$client = $this->createAdminClient();
$client->request('PATCH', self::iri('profiles', $profile->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['firstName' => 'Alice modifiée'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['firstName' => 'Alice modifiée']);
}
public function testDeleteAsAdmin(): void
{
$profile = $this->createProfile(firstName: 'ToDelete', lastName: 'User');
$client = $this->createAdminClient();
$client->request('DELETE', self::iri('profiles', $profile->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$profile = $this->createProfile();
$client = $this->createUnauthenticatedClient();
$client->request('GET', self::iri('profiles', $profile->getId()));
$this->assertResponseStatusCodeSame(401);
}
}

View File

@@ -0,0 +1,124 @@
<?php
declare(strict_types=1);
namespace App\Tests\Api\Entity;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class SiteTest extends AbstractApiTestCase
{
public function testGetCollection(): void
{
$this->createSite('Usine Nord');
$this->createSite('Usine Sud');
$client = $this->createViewerClient();
$client->request('GET', '/api/sites');
$this->assertResponseIsSuccessful();
$this->assertJsonContainsHydraCollection();
$this->assertJsonContains(['totalItems' => 2]);
}
public function testGetItem(): void
{
$site = $this->createSite('Usine Nord', ['contactCity' => 'Lyon']);
$client = $this->createViewerClient();
$client->request('GET', self::iri('sites', $site->getId()));
$this->assertResponseIsSuccessful();
$this->assertJsonContains([
'name' => 'Usine Nord',
'contactCity' => 'Lyon',
]);
}
public function testPost(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/sites', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => [
'name' => 'Usine Est',
'contactName' => 'Jean Dupont',
'contactCity' => 'Strasbourg',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains(['name' => 'Usine Est']);
}
public function testPut(): void
{
$site = $this->createSite('Usine Nord');
$client = $this->createGestionnaireClient();
$client->request('PUT', self::iri('sites', $site->getId()), [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Usine Nord Renommée'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['name' => 'Usine Nord Renommée']);
}
public function testPatch(): void
{
$site = $this->createSite('Usine Nord');
$client = $this->createGestionnaireClient();
$client->request('PATCH', self::iri('sites', $site->getId()), [
'headers' => ['Content-Type' => 'application/merge-patch+json'],
'json' => ['contactPhone' => '0612345678'],
]);
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['contactPhone' => '0612345678']);
}
public function testDelete(): void
{
$site = $this->createSite('ToDelete');
$client = $this->createGestionnaireClient();
$client->request('DELETE', self::iri('sites', $site->getId()));
$this->assertResponseStatusCodeSame(204);
}
public function testUnauthenticatedAccess(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('GET', '/api/sites');
$this->assertResponseStatusCodeSame(401);
}
public function testViewerCannotWrite(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/sites', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['name' => 'Blocked'],
]);
$this->assertResponseStatusCodeSame(403);
}
public function testValidationNameRequired(): void
{
$client = $this->createGestionnaireClient();
$client->request('POST', '/api/sites', [
'headers' => ['Content-Type' => 'application/ld+json'],
'json' => ['contactCity' => 'Paris'],
]);
$this->assertResponseStatusCodeSame(422);
}
}