Laravel Testing login Credential using phpunit + multiprocess

Viewed 6240

I'm pretty new on unit testing and I want to try to test my login page my Goal for this unit are : -> if it match in database -> redirect to route '/' -> if not -> redirect to route '/login'

<?php

namespace Tests\Feature;

use App\Domain\Core\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class userTest extends TestCase
{
    use DatabaseMigrations;
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testLoginTrue()
    {
        $credential = [
            'email' => 'user@ad.com',
            'password' => 'user'
        ];
         $this->post('login',$credential)->assertRedirect('/');
    }

    public function testLoginFalse()
    {
        $credential = [
            'email' => 'user@ad.com',
            'password' => 'usera'
        ];
        $this->post('login',$credential)->assertRedirect('/login');
    }
}

when I test on TestLoginTrue , its successfully return to '/' But when i try the TestLoginFalse ... it return same like TestLoginTrue, it should be stayed on '/login' route, Any Idea?

Plus I want to try to check if when I already login I couldn't access the login page so my initial idea is : public function testLoginTrue()

{
    $credential = [
        'email' => 'user@ad.com',
        'password' => 'user'
    ];
     $this->post('login',$credential)
         ->assertRedirect('/')
         ->get('/login')
         ->assertRedirect('/');
}

but... it returns

1) Tests\Feature\userTest::testLoginTrue BadMethodCallException: Method [get] does not exist on Redirect.

So how to do it correctly?

Thanks in advance

1 Answers
Related