0

应用程序/控制器/SecurityController.php

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' => 'test@test.com',
            'password' => 'mypasswprd',
        );

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

当我运行 phpunit 时出现此错误: .{"error":{"type":"ErrorException","message":"Trying to get property of non-object","file":app/controllers/SecurityController .php","line":20}}

4

1 回答 1

1

你为什么用file_get_contents("php://input")?Laravel 允许您使用该Input:get()方法,这是一种从表单或 json 检索输入数据的简单方法。我敢打赌它会更容易测试。

你的控制器应该是这样的:

class SecurityController extends Controller { 

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

        if (Auth::attempt($input))
        {
        }
    }
}
于 2013-08-25T16:10:38.120 回答