atmd atmd - 6 months ago 37
PHP Question

Laravel unit testing of controllers

I am trying to start a new Laravel app following TDD

My first step is to check that the /login controller is called on the home url.

Despite following several tutorials I can't get the test to work and I can't see what I'm doing wrong at all.

My set up is:
composer to install laravel
composer to install phpunit

here is my route:

<?php
Route::get('/login', 'AuthenticationController@login');


my controller:

<?php

class AuthenticationController extends BaseController {

public function login () {
return View::make('authentication.login');
}

}


And my test:

<?php

class AuthenticationTest extends TestCase {

public function testSomeTest () {

$response = $this->action('GET', 'AuthenticationController@login');

$view = $response->original;

$this->assertEquals('authentication.login', $view['name']);
}
}


The error I get is

ErrorException: Undefined index: name


The code as a copy (pretty much exactly) from the Laravel site, yet it doesn't run.

Can anyone see what I'm doing wrong?

It claims $view has no index name, but that can't be right as its the example on the laravel website, plus the view is being rendered using its name (it is showing correctly on the front end too)

EDIT::

So it seems from a comment that the laravel unit testing section isn't clear and that the $view['name'] is checking for a variable called $name. If that is the case, how do you test the controller/route used, IE. what controller name/action name has been used for route('X')

Answer

Ok, as already explained a little in the comments, let's first take a step back and think about the scenario.

"My first step is to check that the /login controller is called on the home url."

So that means: When the user hits the home route, you want to check if the user is logged in. If he's not, you want to redirect them to the login, maybe with some flash message. After they have logged in, you want to redirect them back to the home page. If the login fails, you want to redirect them back to the login form, maybe also with a flash message.

So there are several things to test now: The home controller and the login controller. So following the TDD spirit, let's create the tests first.

Note: I'll follow some naming convention that is used by phpspec, but don't let that bother you.

class HomeControllerTest extends TestCase
{
    /**
     * @test
     */
    public function it_redirects_to_login_if_user_is_not_authenticated()
    {
        Auth::shouldReceive('check')->once()->andReturn(false);

        $response = $this->call('GET', 'home');

        // Now we have several ways to go about this, choose the
        // one you're most comfortable with.

        // Check that you're redirecting to a specific controller action 
        // with a flash message
        $this->assertRedirectedToAction(
             'AuthenticationController@login', 
             null, 
             ['flash_message']
        );

        // Only check that you're redirecting to a specific URI
        $this->assertRedirectedTo('login');

        // Just check that you don't get a 200 OK response.
        $this->assertFalse($response->isOk());

        // Make sure you've been redirected.
        $this->assertTrue($response->isRedirection());
    }

    /**
     * @test
     */
    public function it_returns_home_page_if_user_is_authenticated()
    {
        Auth::shouldReceive('check')->once()->andReturn(true);

        $this->call('GET', 'home');

        $this->assertResponseOk();
    }
}

And that's it for the Home controller. In most cases you actually don't care where you are redirected to, because that may change over time and you would have to change the tests. So the least you should do is to check whether you are being redirected or not and only check for more details if you really think that it matters for your test.

Let's have a look at the Authentication controller:

class AuthenticationControllerTest extends TestCase
{
    /**
     * @test
     */
    public function it_shows_the_login_form()
    {
        $response = $this->call('GET', 'login');

        $this->assertTrue($response->isOk());

        // Even though the two lines above may be enough,
        // you could also check for something like this:

        View::shouldReceive('make')->with('login');
    }

    /**
     * @test
     */
    public function it_redirects_back_to_form_if_login_fails()
    {
        $credentials = [
            'email' => 'test@test.com',
            'password' => 'secret',
        ];

        Auth::shouldReceive('attempt')
             ->once()
             ->with($credentials)
             ->andReturn(false);

        $this->call('POST', 'login', $credentials);

        $this->assertRedirectedToAction(
            'AuthenticationController@login', 
            null, 
            ['flash_message']
        );
    }

    /**
     * @test
     */
    public function it_redirects_to_home_page_after_user_logs_in()
    {
        $credentials = [
            'email' => 'test@test.com',
            'password' => 'secret',
        ];

        Auth::shouldReceive('attempt')
             ->once()
             ->with($credentials)
             ->andReturn(true);

        $this->call('POST', 'login', $credentials);

        $this->assertRedirectedTo('home');
    }
}

Again, always think about what you really want to test. Do you really need to know which controller action is triggered on which route? Or what the name of the view is that is returned? Actually, you just need to make sure that the controller actually attempts to do it. You pass it some data and then test if it behaves as expected.

And always make sure you're not trying to test any framework functionality, for instance if a specific route triggers a specific action or if a View is loaded correctly. This has already been tested, so you don't need to worry about that. Focus on the functionality of your application and not on the underlying framework.