Easy_solution/tests/Controller/ActionController.php

63 lines
2.1 KiB
PHP

<?php
namespace App\Tests\Controller;
use App\Entity\Actions;
use App\Tests\Functional\AbstractFunctional;
use PHPUnit\Framework\Attributes\Test;
class ActionController extends AbstractFunctional
{
#[Test]
public function fetch_activities_ajax_returns_json_response(): void
{
// 1. Arrange: Authenticate
$user = $this->createUser('user@user.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($user);
$organization = $this->createOrganization('org');
// 3. Arrange: Create an Action linked to the Organization
$action = new Actions();
$action->setOrganization($organization); // Link to the org
$action->setUsers($user); // Link to the user
$action->setActionType('UPDATE'); // Required string
$action->setDescription('Updated profile details');
// Date is set automatically in __construct
$this->entityManager->persist($action);
$this->entityManager->flush();
// 4. Act: Request the URL using the Organization ID
$url = sprintf('/actions/organization/%d/activities-ajax', $organization->getId());
$this->client->request('GET', $url);
// 5. Assert: Verify Success
$this->assertResponseIsSuccessful(); // Status 200
$this->assertResponseHeaderSame('content-type', 'application/json');
// 6. Assert: Verify JSON Content
$responseContent = $this->client->getResponse()->getContent();
$this->assertJson($responseContent);
$data = json_decode($responseContent, true);
// Since we created 1 action, we expect the array to be non-empty
$this->assertIsArray($data);
$this->assertNotEmpty($data);
}
#[Test]
public function fetch_activities_returns_404_for_invalid_organization(): void
{
$user = $this->createUser('user@user.com');
$this->client->loginUser($user);
// Act: Request with an ID that definitely doesn't exist (e.g., extremely high int)
$this->client->request('GET', '/actions/organization/99999999/activities-ajax');
$this->assertResponseStatusCodeSame(404);
}
}