4

I'm a bit confused about the unit testing in Laravel 5.1.

I want to test the updating of settings from a user account. I'm using phpunit for testing.

This is my test case.

/** @test */
    public function it_updates_personal_details(){

        $this->withoutMiddleware();
        $this->visit('/account/settings')
            ->post('/account/settings', ["firstname"=>"RingoUpdated", 'lastname'=>"RingyUpdated", "username"=>"ringo", "bio"=>"My Personal Information", "facebook"=>"myFbUsername", "twitter"=>"myTwUsername", ])
            ->seeJson([
                "signup"=>true
            ]);
    }

But in my controller, I'm using Auth::id() to get the current logged in user's id. How can I mock this in Laravel 5.1 ?

3 Answers 3

7

The simplest way is to make use of Mockery in the Facade:

public function it_updates_personal_details(){
    Auth::shouldReceive('id')->andReturn(1);

    $this->withoutMiddleware();
    $this->visit('/account/settings')
        ->post('/account/settings', ["firstname"=>"RingoUpdated", 'lastname'=>"RingyUpdated", "username"=>"ringo", "bio"=>"My Personal Information", "facebook"=>"myFbUsername", "twitter"=>"myTwUsername", ])
        ->seeJson([
            "signup"=>true
        ]);
}
Sign up to request clarification or add additional context in comments.

6 Comments

When running the test, I'm creating a test user. Is there any way to get the id of that user?
You could do something like this (it's ugly, but possible): Auth::shouldReceive('id')->andReturn(DB::table('users')->orderBy('id', 'desc')->first(['id'])->id); - keep in mind that I haven't tested this, but that should get the ID of the last created user.
Actually... that may not work. It would get the last user before the new one you added.
Would there be any other way for doing this?
Hard to say without knowing your architecture or the function that you're calling. You could use that method, but add 1 to the result, and pretend that that's correct (even less ideal). It really depends on how you're actually creating the user, etc.
|
3

That’s awesome, we can move to class TestCase for all test case and we can give a name, like this:

public function loginWithFakeUser()
{
      $user = new User([
        'id' => 1,
        'name' => 'yish'
      ]);

      $this->be($user);
}

When we need to authenticated user we can call loginWithFakeUser to pretend user.

1 Comment

Using this, I get a Call to a member function guard() on null at this line: $this->app['auth']->guard($driver)->setUser($user);. This is in Laravel 5.8
0

The actingAs helper method. Laravel 5.3

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\User; // user model

class ExampleTest extends TestCase
{
    public function testApplication()
    {
        $user = User::find(1); // find specific user

        $this->actingAs($user)
             ->visit('/')
             ->see('Hello, '.$user->name);
    }
}

The actingAs helper method. provides a simple way to authenticate a given user as the current user.

You may also use a model factory to generate and authenticate a user:

        $user = factory(App\User::class)->create();
        $this->actingAs($user)
         ->visit('/')
         ->see('Hello, '.$user->name);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.