Skip to content

Instantly share code, notes, and snippets.

@j4k
Created July 13, 2014 17:42
Show Gist options
  • Select an option

  • Save j4k/cb4b6bd86a8fa4bda4b6 to your computer and use it in GitHub Desktop.

Select an option

Save j4k/cb4b6bd86a8fa4bda4b6 to your computer and use it in GitHub Desktop.
Laravel UserController Test
<?php
use \Illuminate\Session\TokenMismatchException;
class UserControllerTest extends BaseControllerTestCase {
public function testShouldLogin()
{
$this->requestAction('GET', 'UserController@getLogin');
$this->assertRequestOk();
}
public function testShouldDoLogin()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => Session::getToken()
);
$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');
$this->assertRedirection( URL::action('BlogController@getIndex') );
}
public function testShouldNotDoLoginWhenWrong()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'wrong',
'csrf_token' => Session::getToken());
$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');
$this->assertRedirection( URL::action('UserController@getLogin') );
}
public function testShouldNotDoLoginWhenTokenWrong()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => ''
);
try {
$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');
} catch (TokenMismatchException $e) {
// threw an exception when token doesn't match.
return true;
}
return false;
}
/**
* Testing redirect with logged in user.
*/
public function testLoginShouldRedirectUser()
{
$credentials = array(
'email'=>'[email protected]',
'password'=>'admin',
'csrf_token' => Session::getToken()
);
$this->withInput( $credentials )
->requestAction('POST', 'UserController@postLogin');
$this->requestAction('GET', 'UserController@getLogin');
$this->assertRedirection( URL::to('/') );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment