Files
Inventory/tests/Api/Controller/CommentControllerTest.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

151 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Api\Controller;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class CommentControllerTest extends AbstractApiTestCase
{
public function testCreateComment(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createViewerClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'Test comment',
'entityType' => 'machine',
'entityId' => $machine->getId(),
'entityName' => 'Machine A',
],
]);
$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains([
'content' => 'Test comment',
'entityType' => 'machine',
'status' => 'open',
]);
}
public function testCreateCommentUnauthenticated(): void
{
$client = $this->createUnauthenticatedClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'No auth',
'entityType' => 'machine',
'entityId' => 'fake-id',
],
]);
$this->assertResponseStatusCodeSame(401);
}
public function testCreateCommentEmptyContent(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => '',
'entityType' => 'machine',
'entityId' => 'some-id',
],
]);
$this->assertResponseStatusCodeSame(400);
$this->assertJsonContains(['message' => 'Le contenu est requis.']);
}
public function testCreateCommentInvalidEntityType(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'Invalid type',
'entityType' => 'invalid',
'entityId' => 'some-id',
],
]);
$this->assertResponseStatusCodeSame(400);
$this->assertJsonContains(['message' => "Type d'entité invalide."]);
}
public function testCreateCommentMissingEntityId(): void
{
$client = $this->createViewerClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'Missing ID',
'entityType' => 'machine',
'entityId' => '',
],
]);
$this->assertResponseStatusCodeSame(400);
}
public function testResolveComment(): void
{
$machine = $this->createMachine('Machine A');
$viewerClient = $this->createViewerClient();
$response = $viewerClient->request('POST', '/api/comments', [
'json' => [
'content' => 'To resolve',
'entityType' => 'machine',
'entityId' => $machine->getId(),
],
]);
$data = $response->toArray();
$commentId = $data['id'];
$gestionnaireClient = $this->createGestionnaireClient();
$gestionnaireClient->request('PATCH', sprintf('/api/comments/%s/resolve', $commentId));
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['status' => 'resolved']);
}
public function testResolveCommentNotFound(): void
{
$client = $this->createGestionnaireClient();
$client->request('PATCH', '/api/comments/nonexistent-id/resolve');
$this->assertResponseStatusCodeSame(404);
}
public function testUnresolvedCount(): void
{
$machine = $this->createMachine('Machine A');
$client = $this->createViewerClient();
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'Open 1',
'entityType' => 'machine',
'entityId' => $machine->getId(),
],
]);
$client->request('POST', '/api/comments', [
'json' => [
'content' => 'Open 2',
'entityType' => 'machine',
'entityId' => $machine->getId(),
],
]);
$client->request('GET', '/api/comments/stats/unresolved-count');
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['count' => 2]);
}
}