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

136 lines
3.9 KiB
PHP

<?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);
}
}