2013-08-23 2 views
0

응용 프로그램/컨트롤러/SecurityController.php단위 테스트 laravel 4 컨트롤러 통과 JSON 페이로드

class SecurityController extends Controller { 

    public function login() 
    {  
     $payload = file_get_contents("php://input"); 
     $payload = json_decode($payload); 

     $input = array('mail' => $payload->mail, 
         'password' => $payload->password, 
       ); 


     if (Auth::attempt($input)) 
     { 
     } 
    } 
} 

응용 프로그램/테스트/SecurityTest.php

class SecurityTest extends TestCase { 
    public function testLogin() 
    { 
     $data = array(
      'mail' => '[email protected]', 
      'password' => 'mypasswprd', 
     ); 

     $crawler = $this->client->request('POST', '/v2/login', $data); 
    } 

내가이 오류를 받고 있어요 phpunit을 실행하면 : { "error": { "type": "ErrorException", "message": "객체가 아닌 속성을 얻으려고 시도 중", "file": app/controllers/SecurityController.php ","line ": 20} }

+0

가 지금까지를 디버깅하는 노력을 기울 적이 있습니까? 너 무슨 짓을 한거야? –

+0

컨트롤러가 빈 페이로드를 수신하고 있습니다. – mookofe

답변

1

왜 사용하고 있습니까 file_get_contents("php://input")? Laravel을 사용하면 양식 또는 json에서 입력 데이터를 쉽게 검색 할 수있는 Input:get() 메서드를 사용할 수 있습니다. 나는 그것이 더 시험 할 것이 틀림 없을 것이다.

컨트롤러는 것과 같아야합니다 :

class SecurityController extends Controller { 

    public function login() 
    {  
     $input = array(
      'mail'  => Input::get('mail'), 
      'password' => Input::get('password'), 
     ); 

     if (Auth::attempt($input)) 
     { 
     } 
    } 
}