Pratical step-by-step how to build auth tests in a RESTful API made in Laravel 5.5
- Apache
- PHP
- Composer
- Laravel new app created
- Laravel api auth with passport done
The project in this repo contains all the steps finalized
In terminal run
php artisan make:seeder UsersTableSeeder
Edit 'database/seeds/UsersTableSeeder.php' file
<?php
use Illuminate\Database\Seeder;
use App\User;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//clear table
User::truncate();
User::create([
'name' => 'username',
'email' => 'user@email.com',
'password' => bcrypt('userpass'),
]);
}
}
Adjust seeds/DatabaseSeeder.php to call users seed
public function run()
{
$this->call(UsersTableSeeder::class);
}
In the config/database.php configure sqlite to work in memory
[...]
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
...
]
[...]
In the root dir of the project adjust phpunit.xml adding DB_CONNECTION
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
</php>
In the tests/TestCase.php add commands necessary to prepare database for tests
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\Artisan;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations;
public function setUp()
{
parent::setUp();
Artisan::call('db:seed');
Artisan::call('passport:install');
}
}
In the composer.json, add script to run tests
"scripts": {
"test" : [
"vendor/bin/phpunit"
]
...
},
In the terminal run
php artisan make:test Auth/LoginControllerTest
In the tests/Feature/Auth/LoginControllerTest.php add tests for validations, success login and logout
<?php
namespace Tests\Feature\Auth;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
class LoginControllerTest extends TestCase
{
public function testRequireEmailAndLogin()
{
$this->json('POST', 'api/login')
->assertStatus(422)
->assertJson([
'message' => 'The given data was invalid.',
'errors' => [
'email' => ['The email field is required.'],
'password' => ['The password field is required.']
]
]);
}
public function testUserLoginSuccessfully()
{
$user = ['email' => 'user@email.com', 'password' => 'userpass'];
$this->json('POST', 'api/login', $user)
->assertStatus(200)
->assertJsonStructure([
'token',
'user' => [
'id',
'name',
'email',
'created_at',
'updated_at'
]
]);
}
public function testLogoutSuccessfully()
{
$user = ['email' => 'user@email.com',
'password' => 'userpass'
];
Auth::attempt($user);
$token = Auth::user()->createToken('nfce_client')->accessToken;
$headers = ['Authorization' => "Bearer $token"];
$this->json('GET', 'api/logout', [], $headers)
->assertStatus(204);
}
}
In the terminal run
php artisan make:test Auth/RegisterControllerTest
In the tests/Feature/Auth/RegisterControllerTest.php add tests for validations and success register
class RegisterControllerTest extends TestCase
{
public function testRegisterSuccessfully()
{
$register = [
'name' => 'UserTest',
'email' => 'user@test.com',
'password' => 'testpass',
'password_confirmation' => 'testpass'
];
$this->json('POST', 'api/register', $register)
->assertStatus(201)
->assertJsonStructure([
'token',
'user' => [
'id',
'name',
'email',
'created_at',
'updated_at'
]
]);
}
public function testRequireNameEmailAndPassword()
{
$this->json('POST', 'api/register')
->assertStatus(422)
->assertJson([
'message' => 'The given data was invalid.',
'errors' => [
'name' => ['The name field is required.'],
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
]
]);
}
public function testRequirePasswordConfirmation()
{
$register = [
'name' => 'User',
'email' => 'user@test.com',
'password' => 'userpass'
];
$this->json('POST', 'api/register', $register)
->assertStatus(422)
->assertJson([
'message' => 'The given data was invalid.',
'errors' => [
'password' => ['The password confirmation does not match.']
]
]);
}
}
In the terminal run
composer test
- Laravel docs - Laravel Documentation
- Laravel Passport Post - Create REST API with authentication
- Laravel API Tutorial - How to Build and Test a RESTful API