vendredi 1 novembre 2019

How to handle controller dependecies in PHPUnit?

I am trying to write the test for my index controller, but I see that I need to pass the dependencies to it to be able to test its behavior, i applied the SOLID principles so I always instance the controllers using a dependency injection container, so I can do the same to instance the controller in the test file.

The question that is burning my head is whether or not that solution is correct because I understand that the point of the Unit Testing is to test the classes independently, and if I use the dependency container I'm are already putting another entity in game .. I mean , if the dependency container fails, then all my controller tests will fail too, without meaning that the controllers are doing their job wrong, then what would be the right solution?

<?php namespace App\Tests\Controller;

use \PHPUnit\Framework\TestCase;

use App\Controller\indexController;
use App\Controller\DependencyInjection;

class indexControllerTest
{
    public function testIndexControllerCheckIfReturnedCorrectHttpResponseCode ()
    {
        $controller = DependencyInjection::getInstance(indexController::class );

        $httpResponse = indexController()->index();

        return assertEqual( $httpResponse->getStatusCode(), 200 );
    }
}

Aucun commentaire:

Enregistrer un commentaire