Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/tests/EventBundle/Controller/
Upload File :
Current File : /var/www/v3.cesa.co.za/tests/EventBundle/Controller/SchoolEventControllerTest.php

<?php

namespace App\Tests\EventBundle\Controller;

use App\EventBundle\Controller\SchoolEventController;
use App\EventBundle\Entity\SchoolEvent;
use App\EventBundle\Entity\SchoolAttendee;
use App\UserBundle\Entity\UserList;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class SchoolEventControllerTest extends TestCase
{
    private SchoolEventController $controller;
    private EntityManagerInterface&MockObject $entityManager;
    private TokenStorageInterface&MockObject $tokenStorage;
    private ContainerInterface&MockObject $container;

    protected function setUp(): void
    {
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
        $this->container = $this->createMock(ContainerInterface::class);
        $this->controller = new SchoolEventController();
        $this->controller->setContainer($this->container);
    }

    public function testGetSchoolEventsWithoutAuthentication()
    {
        $request = new Request();

        // Mock token storage to return null (no authentication)
        $this->tokenStorage->method('getToken')->willReturn(null);

        // Since authentication is commented out, we need to mock the repository
        $this->entityManager->method('getRepository')
            ->with(SchoolEvent::class)
            ->willReturn($this->createMockRepository());

        // The controller should now work without authentication
        $response = $this->controller->getSchoolEvents($request, $this->tokenStorage, $this->entityManager);

        $this->assertInstanceOf(Response::class, $response);
        $this->assertEquals(200, $response->getStatusCode());
    }

    public function testGetSchoolEventsWithInvalidUser()
    {
        $request = new Request();

        // Mock token with invalid user type (return a mock object that's not UserList)
        $invalidUser = $this->createMock(\Symfony\Component\Security\Core\User\UserInterface::class);
        $token = $this->createMock(TokenInterface::class);
        $token->method('getUser')->willReturn($invalidUser);

        $this->tokenStorage->method('getToken')->willReturn($token);

        // Since authentication is commented out, we need to mock the repository
        $this->entityManager->method('getRepository')
            ->with(SchoolEvent::class)
            ->willReturn($this->createMockRepository());

        // The controller should now work without authentication
        $response = $this->controller->getSchoolEvents($request, $this->tokenStorage, $this->entityManager);

        $this->assertInstanceOf(Response::class, $response);
        $this->assertEquals(200, $response->getStatusCode());
    }

    public function testGetSchoolEventsWithValidUser()
    {
        $request = new Request();

        // Mock token with valid user
        $user = $this->createMock(UserList::class);
        $token = $this->createMock(TokenInterface::class);
        $token->method('getUser')->willReturn($user);

        $this->tokenStorage->method('getToken')->willReturn($token);

        // Mock the entity manager to return some test data
        $this->entityManager->method('getRepository')
            ->with(SchoolEvent::class)
            ->willReturn($this->createMockRepository());

        $response = $this->controller->getSchoolEvents($request, $this->tokenStorage, $this->entityManager);

        $this->assertInstanceOf(Response::class, $response);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
    }

    public function testGetSchoolEventsReturnsCorrectJsonFormat()
    {
        $request = new Request();

        // Mock token with valid user
        $user = $this->createMock(UserList::class);
        $token = $this->createMock(TokenInterface::class);
        $token->method('getUser')->willReturn($user);

        $this->tokenStorage->method('getToken')->willReturn($token);

        // Mock repository with test data
        $this->entityManager->method('getRepository')
            ->with(SchoolEvent::class)
            ->willReturn($this->createMockRepositoryWithData());

        $response = $this->controller->getSchoolEvents($request, $this->tokenStorage, $this->entityManager);
        $content = json_decode($response->getContent(), true);

        // Check JSON structure
        $this->assertArrayHasKey('headers', $content);
        $this->assertArrayHasKey('data', $content);
        $this->assertIsArray($content['headers']);
        $this->assertIsArray($content['data']);

        // Check that headers contain expected fields
        $expectedHeaders = ['id', 'event_name', 'level', 'city', 'venue'];
        foreach ($expectedHeaders as $header) {
            $this->assertContains($header, $content['headers']);
        }
    }

    public function testGetSchoolAttendeesWithoutEventId()
    {
        $request = new Request();

        $response = $this->controller->getSchoolAttendees($request, $this->tokenStorage, $this->entityManager);
        $content = json_decode($response->getContent(), true);

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertArrayHasKey('error', $content);
        $this->assertEquals('eventId parameter is required', $content['error']);
    }

    public function testGetSchoolAttendeesWithInvalidEventId()
    {
        $request = new Request(['eventId' => 'invalid']);

        $response = $this->controller->getSchoolAttendees($request, $this->tokenStorage, $this->entityManager);
        $content = json_decode($response->getContent(), true);

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertArrayHasKey('error', $content);
        $this->assertEquals('eventId must be a valid number', $content['error']);
    }

    public function testGetSchoolAttendeesWithInvalidLastAttendeeId()
    {
        $request = new Request(['eventId' => '1', 'lastAttendeeId' => 'invalid']);

        $response = $this->controller->getSchoolAttendees($request, $this->tokenStorage, $this->entityManager);
        $content = json_decode($response->getContent(), true);

        $this->assertEquals(400, $response->getStatusCode());
        $this->assertArrayHasKey('error', $content);
        $this->assertEquals('lastAttendeeId must be a valid number', $content['error']);
    }

    public function testGetSchoolAttendeesWithValidParameters()
    {
        $request = new Request(['eventId' => '1', 'lastAttendeeId' => '0']);

        // Mock repository with test data
        $this->entityManager->method('getRepository')
            ->with(SchoolAttendee::class)
            ->willReturn($this->createMockSchoolAttendeeRepository());

        $response = $this->controller->getSchoolAttendees($request, $this->tokenStorage, $this->entityManager);

        $this->assertInstanceOf(Response::class, $response);
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertEquals('application/json', $response->headers->get('Content-Type'));
    }

    public function testGetSchoolAttendeesReturnsCorrectJsonFormat()
    {
        $request = new Request(['eventId' => '1', 'lastAttendeeId' => '0']);

        // Mock repository with test data
        $this->entityManager->method('getRepository')
            ->with(SchoolAttendee::class)
            ->willReturn($this->createMockSchoolAttendeeRepositoryWithData());

        $response = $this->controller->getSchoolAttendees($request, $this->tokenStorage, $this->entityManager);
        $content = json_decode($response->getContent(), true);

        // Check JSON structure
        $this->assertArrayHasKey('headers', $content);
        $this->assertArrayHasKey('data', $content);
        $this->assertIsArray($content['headers']);
        $this->assertIsArray($content['data']);

        // Check that headers contain expected fields
        $expectedHeaders = ['id', 'contact_title', 'contact_first_name', 'contact_last_name', 'email_address'];
        foreach ($expectedHeaders as $header) {
            $this->assertContains($header, $content['headers']);
        }
    }

    private function createMockRepository()
    {
        $repository = $this->createMock(\Doctrine\ORM\EntityRepository::class);
        $repository->method('findAll')->willReturn([]);
        return $repository;
    }

    private function createMockRepositoryWithData()
    {
        $repository = $this->createMock(\Doctrine\ORM\EntityRepository::class);

        $event = $this->createMock(SchoolEvent::class);
        $event->method('getId')->willReturn(1);
        $event->method('getEventName')->willReturn('Test Event');
        $event->method('getLevel')->willReturn('Beginner');
        $event->method('getCity')->willReturn('Test City');
        $event->method('getVenue')->willReturn('Test Venue');
        $event->method('getStartDate')->willReturn(new \DateTime('2024-01-01'));
        $event->method('getEndDate')->willReturn(new \DateTime('2024-01-02'));
        $event->method('getStartTime')->willReturn(new \DateTime('09:00:00'));
        $event->method('getEndTime')->willReturn(new \DateTime('17:00:00'));
        $event->method('getAvailableSpaces')->willReturn(50);
        $event->method('getCostPerPerson')->willReturn('1000.00');
        $event->method('getEventDescription')->willReturn('Test Description');
        $event->method('getHours')->willReturn(8);
        $event->method('getCpd')->willReturn('8 CPD Points');
        $event->method('getCode')->willReturn('TEST001');
        $event->method('getAccredNum')->willReturn('ACC001');
        $event->method('getProvince')->willReturn('Gauteng');
        $event->method('getRegDeadline')->willReturn(new \DateTime('2024-01-01'));
        $event->method('getMemberDiscount')->willReturn('100.00');
        $event->method('getEarlyBirdDiscount')->willReturn('200.00');
        $event->method('getEarlyBirdDate')->willReturn(new \DateTime('2024-01-01'));
        $event->method('isCancelled')->willReturn(false);
        $event->method('getBankAccName')->willReturn('Test Bank');
        $event->method('getExternalLink')->willReturn('https://example.com');
        $event->method('getFileAttachment')->willReturn('test.pdf');
        $event->method('getCandidateAcademy')->willReturn('Y');
        $event->method('getBookingConfirmed')->willReturn('Y');
        $event->method('getAttendanceRegister')->willReturn('register.pdf');
        $event->method('getSacpcmpNum')->willReturn('SACPCMP001');

        $repository->method('findAll')->willReturn([$event]);
        return $repository;
    }

    private function createMockSchoolAttendeeRepository()
    {
        $repository = $this->createMock(\App\EventBundle\Repository\SchoolAttendeeRepository::class);
        $repository->method('findByEventIdAndAttendeeIdGreaterThan')->willReturn([]);
        return $repository;
    }

    private function createMockSchoolAttendeeRepositoryWithData()
    {
        $repository = $this->createMock(\App\EventBundle\Repository\SchoolAttendeeRepository::class);

        $attendee = $this->createMock(SchoolAttendee::class);
        $schoolEvent = $this->createMock(SchoolEvent::class);
        $schoolEvent->method('getId')->willReturn(1);

        $attendee->method('getId')->willReturn(1);
        $attendee->method('getContactTitle')->willReturn('Mr');
        $attendee->method('getContactFirstName')->willReturn('John');
        $attendee->method('getContactLastName')->willReturn('Doe');
        $attendee->method('getCompanyName')->willReturn('Test Company');
        $attendee->method('getAddressLine1')->willReturn('123 Test Street');
        $attendee->method('getAddressLine2')->willReturn('');
        $attendee->method('getAddressLine3')->willReturn('');
        $attendee->method('getCity')->willReturn('Test City');
        $attendee->method('getPostalCode')->willReturn('1234');
        $attendee->method('getPhoneNumber')->willReturn('0123456789');
        $attendee->method('getMobileNumber')->willReturn('0821234567');
        $attendee->method('getFaxNumber')->willReturn('');
        $attendee->method('getEmailAddress')->willReturn('john.doe@example.com');
        $attendee->method('getDietaryReq')->willReturn('');
        $attendee->method('getAttended')->willReturn('y');
        $attendee->method('getOrderNum')->willReturn('ORD001');
        $attendee->method('getOrgVatNum')->willReturn('VAT123');
        $attendee->method('getIdNumber')->willReturn('8001015009087');
        $attendee->method('getSaaceMemNumber')->willReturn('');
        $attendee->method('getEcsaNumber')->willReturn('');
        $attendee->method('getDesignation')->willReturn('Engineer');
        $attendee->method('isCancelled')->willReturn(false);
        $attendee->method('getDateCancelled')->willReturn(null);
        $attendee->method('getBookingName')->willReturn('John Doe');
        $attendee->method('getBookingTel')->willReturn('0123456789');
        $attendee->method('getBookingEmail')->willReturn('john.doe@example.com');
        $attendee->method('getBookingFax')->willReturn('');
        $attendee->method('getDateBooked')->willReturn(new \DateTime('2024-01-01'));
        $attendee->method('getKnownAs')->willReturn('John');
        $attendee->method('getComments')->willReturn('');
        $attendee->method('getCertificateSent')->willReturn(null);
        $attendee->method('getCompanyType')->willReturn('Consulting');
        $attendee->method('isSaiceMember')->willReturn(false);
        $attendee->method('getSaiceMemNumber')->willReturn('');
        $attendee->method('isCesaStaff')->willReturn(false);
        $attendee->method('getBookingForm')->willReturn('');
        $attendee->method('getResponsiblePayment')->willReturn('John Doe');
        $attendee->method('getMarketingSource')->willReturn('Website');
        $attendee->method('getPackageChosen')->willReturn('Standard');
        $attendee->method('getSchoolEvent')->willReturn($schoolEvent);

        $repository->method('findByEventIdAndAttendeeIdGreaterThan')->willReturn([$attendee]);
        return $repository;
    }
}