Easy_solution/tests/Controller/NotificationController.php

110 lines
4.2 KiB
PHP

<?php
namespace App\Tests\Controller;
use App\Tests\Functional\AbstractFunctional;
use PHPUnit\Framework\Attributes\Test;
class NotificationController extends AbstractFunctional{
//region index tests
#[Test]
public function test_index_super_admin_success(): void
{
$admin = $this->createUser('admin@test.com', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($admin);
$this->createNotification($admin, 'Test Notification 1');
$this->createNotification($admin, 'Test Notification 2', true);
$this->client->request('GET', '/notifications/');
self::assertResponseIsSuccessful();
$responseData = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('notifications', $responseData);
$this->assertArrayHasKey('unreadCount', $responseData);
$this->assertCount(2, $responseData['notifications']);
$this->assertEquals(1, $responseData['unreadCount']);
}
#[Test]
public function test_index_non_super_admin_forbidden(): void
{
$admin = $this->createUser('admin@test.com', ['ROLE_ADMIN']);
$user = $this->createUser('user@test.com', ['ROLE_USER']);
$this->client->loginUser($admin);
$this->client->request('GET', '/notifications/');
self::assertResponseStatusCodeSame(403);
$this->client->loginUser($user);
$this->client->request('GET', '/notifications/');
self::assertResponseStatusCodeSame(403);
}
//endregion
//region unread tests
#[Test]
public function test_unread_authenticated_user_success(): void
{
$user = $this->createUser('s', ['ROLE_SUPER_ADMIN']);
$this->client->loginUser($user);
$this->createNotification($user, 'Unread Notification 1');
$this->createNotification($user, 'Read Notification 1', true);
$this->client->request('GET', '/notifications/unread');
self::assertResponseIsSuccessful();
$responseData = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('notifications', $responseData);
$this->assertArrayHasKey('unreadCount', $responseData);
$this->assertCount(1, $responseData['notifications']);
$this->assertEquals(1, $responseData['unreadCount']);
}
#[Test]
public function test_unread_unauthenticated_user_forbidden(): void
{
$this->client->request('GET', '/notifications/unread');
self::assertResponseRedirects('/login');
$this->client->followRedirect();
self::assertResponseStatusCodeSame(200); // Login page
}
//endregion
//region markAsRead tests
#[Test]
public function test_markAsRead_authenticated_user_success(): void
{
$user = $this->createUser('user');
$this->client->loginUser($user);
$notification = $this->createNotification($user, 'Notification to Mark Read');
$this->client->request('POST', '/notifications/' . $notification->getId() . '/read');
self::assertResponseIsSuccessful();
$responseData = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('success', $responseData);
$this->assertTrue($responseData['success']);
}
#[Test]
public function test_markAsRead_notification_not_found(): void
{
$user = $this->createUser('user');
$this->client->loginUser($user);
$notification = $this->createNotification($user, 'Notification to Mark Read');
$this->client->request('POST', '/notifications/9999/read'); // Non-existent ID
self::assertResponseStatusCodeSame(404);
$responseData = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $responseData);
$this->assertEquals('Notification not found', $responseData['error']);
}
#[Test]
public function test_markAsRead_unauthenticated_user_forbidden(): void
{
$this->client->request('POST', '/notifications/1/read');
self::assertResponseRedirects('/login');
$this->client->followRedirect();
self::assertResponseStatusCodeSame(200); // Login page
}
//endregion
}