Created
July 13, 2014 17:42
-
-
Save j4k/cb4b6bd86a8fa4bda4b6 to your computer and use it in GitHub Desktop.
Laravel UserController Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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