Skip to content

Instantly share code, notes, and snippets.

@appkr
Created August 17, 2016 12:35
Show Gist options
  • Select an option

  • Save appkr/c79cb230e5ac25564a878f99a1202e96 to your computer and use it in GitHub Desktop.

Select an option

Save appkr/c79cb230e5ac25564a878f99a1202e96 to your computer and use it in GitHub Desktop.
<?php
namespace Test\Http\Controllers;
trait AuthHelper
{
/**
* @var array
*/
protected $userPayload = [
'name' => 'foo',
'email' => '[email protected]',
'password' => 'password'
];
/**
* Visit login page and attempt login.
*
* @param array $overrides
* @return mixed
*/
public function login($overrides = [])
{
return $this->visit(route('login'))
->submitForm(
'Login',
array_merge([
'email' => $this->user->email,
'password' => 'password',
], $overrides)
);
}
/**
* Visit login route.
*
* @return mixed
*/
public function logout()
{
return $this->visit('logout');
}
/**
* Visit signup page and attempt user registration.
*
* @param array $overrides
* @return $this
*/
public function register($overrides = [])
{
return $this->visit('register')
->submitForm(
'Register',
array_merge(
$this->userPayload,
['password_confirmation' => $this->userPayload['password']],
$overrides
)
);
}
/**
* Visit password remind page and attempt the password remind.
*
* @param array $overrides
* @return $this
*/
public function remind($overrides = [])
{
return $this->visit("password/email")
->submitForm(
'Send Password Reset Link',
[
'email' => array_key_exists('email', $overrides)
? $overrides['email']
: $this->user->email
]
);
}
/**
* Visit password reset page and attempt reset.
*
* @param array $overrides
* @return $this
*/
public function reset($overrides = [])
{
$email = $this->user->email;
$token = str_random(64);
\DB::table('password_resets')->insert([
'email' => $email,
'token' => $token,
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
]);
// Override token
$token = array_key_exists('token', $overrides)
? $overrides['token'] : $token;
return $this->visit("password/reset/{$token}")
->submitForm('Reset Password', array_merge([
'email' => array_key_exists('email', $overrides)
? $overrides['email'] : $email,
'password' => 'password',
'password_confirmation' => 'password',
'token' => $token
], $overrides)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment