set up controller test for action

This commit is contained in:
Charles 2025-12-15 15:48:27 +01:00
parent 923d36ba4e
commit cb8eabef4d
1 changed files with 106 additions and 0 deletions

View File

@ -0,0 +1,106 @@
<?php
namespace App\Tests\Controller;
use App\Entity\Actions;
use App\Entity\Organizations;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use PHPUnit\Framework\Attributes\Test;
class ActionControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
$this->client = static::createClient();
// Retrieve the EntityManager from the test container
$this->entityManager = static::getContainer()->get('doctrine')->getManager();
}
/**
* Helper to create a valid User entity with all required fields and log them in.
*/
private function authenticateUser(): User
{
$user = new User();
$user->setEmail('test_' . uniqid() . '@example.com'); // Ensure uniqueness
$user->setPassword('secure_password');
$user->setName('Test');
$user->setSurname('User');
$user->setRoles(['ROLE_USER']);
// Defaults (isActive, isDeleted, dates) are handled by the User constructor
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->client->loginUser($user);
return $user;
}
#[Test]
public function fetch_activities_ajax_returns_json_response(): void
{
// 1. Arrange: Authenticate
$user = $this->authenticateUser();
// 2. Arrange: Create a valid Organization
$organization = new Organizations();
$organization->setName('Test Corp');
$organization->setEmail('contact@testcorp.com');
$organization->setNumber(101); // Required int
$organization->setAddress('123 Main St'); // Required string
$organization->setLogoUrl('logo.png'); // Required string
// Defaults (isActive, isDeleted, collections) handled by Constructor
$this->entityManager->persist($organization);
// 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
{
$this->authenticateUser();
// Act: Request with an ID that definitely doesn't exist (e.g., extremely high int)
$this->client->request('GET', '/actions/organization/99999999/activities-ajax');
// Assert: 404 Not Found (Standard Symfony ParamConverter behavior)
$this->assertResponseStatusCodeSame(404);
}
}