Permalink
Browse files

Refactor RegisterTest using helper method

  • Loading branch information...
nafiesl committed Dec 12, 2018
1 parent fd58467 commit e37a615a8662585d5f03dbc7a64e8e9860efa527
Showing with 30 additions and 59 deletions.
  1. +30 −59 tests/Feature/Auth/RegisterTest.php
@@ -11,19 +11,24 @@ class RegisterTest extends TestCase
// Trait refresh database agar migration dijalankan
use RefreshDatabase;
private function getRegisterFields($overrides = [])
{
return array_merge([
'name' => 'John Thor',
'email' => 'username@example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
], $overrides);
}
/** @test */
public function user_can_register()
{
// Kunjungi halaman '/register'
$this->visit('/register');
// Submit form register dengan nama, email dan password 2 kali
$this->submitForm('Register', [
'name' => 'John Thor',
'email' => 'username@example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->submitForm('Register', $this->getRegisterFields());
// Lihat halaman ter-redirect ke url '/home' (register sukses).
$this->seePageIs('/home');
@@ -41,16 +46,11 @@ public function user_can_register()
$this->assertTrue(app('hash')->check('secret', User::first()->password));
}
/** @test */
/** @test */
public function user_name_is_required()
{
// Submit form untuk register dengan field 'name' kosong.
$this->post('/register', [
'name' => '',
'email' => 'username@example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields(['name' => '']));
// Cek pada session apakah ada error untuk field 'name'.
$this->assertSessionHasErrors(['name']);
@@ -60,12 +60,9 @@ public function user_name_is_required()
public function user_name_maximum_is_255_characters()
{
// Submit form untuk register dengan field 'name' 260 karakter.
$this->post('/register', [
'name' => str_repeat('John Thor ', 26),
'email' => 'username@example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields([
'name' => str_repeat('John Thor ', 26),
]));
// Cek pada session apakah ada error untuk field 'name'.
$this->assertSessionHasErrors(['name']);
@@ -75,12 +72,7 @@ public function user_name_maximum_is_255_characters()
public function user_email_is_required()
{
// Submit form untuk register dengan field 'email' kosong.
$this->post('/register', [
'name' => 'John Thor',
'email' => '',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields(['email' => '']));
// Cek pada session apakah ada error untuk field 'email'.
$this->assertSessionHasErrors(['email']);
@@ -90,12 +82,9 @@ public function user_email_is_required()
public function user_email_must_be_a_valid_email()
{
// Submit form untuk register dengan field 'email' tidak valid.
$this->post('/register', [
'name' => 'John Thor',
'email' => 'username.example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields([
'email' => 'username.example.net',
]));
// Cek pada session apakah ada error untuk field 'email'.
$this->assertSessionHasErrors(['email']);
@@ -105,12 +94,9 @@ public function user_email_must_be_a_valid_email()
public function user_email_maximum_is_255_characters()
{
// Submit form untuk register dengan field 'email' 260 karakter.
$this->post('/register', [
'name' => 'John Thor',
'email' => str_repeat('username@example.net', 13),
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields([
'email' => str_repeat('username@example.net', 13),
]));
// Cek pada session apakah ada error untuk field 'email'.
$this->assertSessionHasErrors(['email']);
@@ -124,12 +110,9 @@ public function user_email_must_be_unique_on_users_table()
// Submit form untuk register dengan field
// 'email' yang sudah ada di tabel users.
$this->post('/register', [
'name' => 'John Thor',
'email' => 'emailsama@example.net',
'password' => 'secret',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields([
'email' => 'emailsama@example.net',
]));
// Cek pada session apakah ada error untuk field 'email'.
$this->assertSessionHasErrors(['email']);
@@ -139,12 +122,7 @@ public function user_email_must_be_unique_on_users_table()
public function user_password_is_required()
{
// Submit form untuk register dengan field 'password' kosong.
$this->post('/register', [
'name' => 'John Thor',
'email' => 'username@example.net',
'password' => '',
'password_confirmation' => 'secret',
]);
$this->post('/register', $this->getRegisterFields(['password' => '']));
// Cek pada session apakah ada error untuk field 'password'.
$this->assertSessionHasErrors(['password']);
@@ -154,12 +132,7 @@ public function user_password_is_required()
public function user_password_minimum_is_6_characters()
{
// Submit form untuk register dengan field 'password' 5 karakter.
$this->post('/register', [
'name' => 'John Thor',
'email' => 'username@example.net',
'password' => 'ecret',
'password_confirmation' => 'ecret',
]);
$this->post('/register', $this->getRegisterFields(['password' => 'ecret']));
// Cek pada session apakah ada error untuk field 'password'.
$this->assertSessionHasErrors(['password']);
@@ -170,12 +143,10 @@ public function user_password_must_be_same_with_password_confirmation_field()
{
// Submit form untuk register dengan field 'password'
// beda dengan 'password_confirmation'.
$this->post('/register', [
'name' => 'John Thor',
'email' => 'username@example.net',
$this->post('/register', $this->getRegisterFields([
'password' => 'secret',
'password_confirmation' => 'escret',
]);
]));
// Cek pada session apakah ada error untuk field 'password'.
$this->assertSessionHasErrors(['password']);

0 comments on commit e37a615

Please sign in to comment.