lundi 1 octobre 2018

Laravel TDD Browser Kit Testing trying to get property of non-object

I have test to check whether my VerificationController working. If I test it manually, it works, but it doesn't work whenever I try to run it through phpunit.

This is my VerificationController looks like:

class VerificationController extends Controller
{
    public function verify(Request $request)
    {
        $user = User::where('email', $request->email)->where('verification_token', $request->token)->firstOrFail();

        $user->update([
            'verified' => 1,
            'verification_token' => null
        ]);
        $user->student()->update([
            'status' => 1
        ]);

        auth()->loginUsingId($user->id);
        return redirect()->route('student.home')->with('flash_msg', 'Alamat E-Mail Berhasil Diverifikasi');
    }
}

And this is my unit testing class:

class RegisterStudentEmailTest extends TestCase
{
use RefreshDatabase;
/**
 * A basic test example.
 *
 * @return void
 */
public function test_inactive_student_register()
{
    Mail::fake();
    Notification::fake();

    $admins = factory(User::class, 3)->states('a')->create();

    $this->visitRoute('register')
        ->type('Mary', 'firstname')
        ->type('Jane', 'lastname')
        ->type('mary@mail.org', 'email')
        ->type('081330956056', 'phone')
        ->type('Surabaya', 'address')
        ->select('f', 'gender')
        ->select('Manual', 'car_type')
        ->type('123456', 'password')
        ->type('123456', 'password_confirmation')
        ->press('Daftar');

    $student = User::where('role', 'Student')->first();

    Mail::assertSent(
        VerificationMail::class,
        function ($mail) use ($student) {
            return $mail->student->id === $student->id;
        }
    );

    Notification::assertSentTo(
        [$admins],
        InactiveStudent::class,
        function ($notification, $channels) use ($student) {
            return $notification->inactive_student->id === $student->id;
        }
    );

    $this->visit('verify?token=' . $student->verification_token . '&email=mary%40mail.org')
         ->seeRouteIs('student.home');
    }
}

The error I get is trying to get 'name' property of non-object which located in sidebar.blade.php file. This is the code:

<li class="user-header">
    <p>
        
        <br>
    </p>
</li>

This URL (verify?token=' . $student->verification_token . '&email=mary%40mail.org') allows user to autologin so it will automatically show user's name in sidebar (I test that URL manually and there's no problem)

Aucun commentaire:

Enregistrer un commentaire