使用JSON请求体测试laravel控制器

问题描述 投票:11回答:6

我正在尝试为Laravel控制器编写一个phpunit测试,该控制器期望使用JSON格式的主体发布请求。

控制器的简化版本:

class Account_Controller extends Base_Controller
{
    public $restful = true;

    public function post_login()
    {
        $credentials = Input::json();
        return json_encode(array(
            'email' => $credentials->email,
            'session' => 'random_session_key'
        ));
    }
}

目前我有一个测试方法正确地将数据作为urlencoded表单数据发送,但我无法弄清楚如何将数据作为JSON发送。

我的测试方法(我在编写测试时使用了github gist here

class AccountControllerTest extends PHPUnit_Framework_TestCase {
    public function testLogin()
    {
        $post_data = array(
            'email' => '[email protected]',
            'password' => 'example_password'
        );
        Request::foundation()->server->set('REQUEST_METHOD', 'POST');
        Request::foundation()->request->add($post_data);
        $response = Controller::call('account@login', $post_data);
        //check the $response
    }
}

我在前端使用angularjs,默认情况下,发送到服务器的请求是JSON格式。我宁愿不改变它发送urlencoded形式。

有谁知道如何编写一个为控制器提供JSON编码体的测试方法?

angularjs laravel phpunit laravel-3
6个回答
2
投票

这样做有很多简单的方法。您可以简单地将Input :: $ json属性设置为要作为post参数发送的对象。请参阅下面的示例代码

 $data = array(
        'name' => 'sample name',
        'email' => '[email protected]',
 );

 Input::$json = (object)$data;

 Request::setMethod('POST');
 $response = Controller::call('client@create');
 $this->assertNotNull($response);
 $this->assertEquals(200, $response->status());

我希望这可以帮助您完成测试用例

更新:原始文章可在此处获取http://forums.laravel.io/viewtopic.php?id=2521


7
投票

这就是我在Laravel 4中做到这一点的方法

// Now Up-vote something with id 53
$this->client->request('POST', '/api/1.0/something/53/rating', array('rating' => 1) );

// I hope we always get a 200 OK
$this->assertTrue($this->client->getResponse()->isOk());

// Get the response and decode it
$jsonResponse = $this->client->getResponse()->getContent();
$responseData = json_decode($jsonResponse);

$responseData将是一个等于json响应的PHP对象,然后允许你测试响应:)


6
投票

在Laravel 5中,call()方法已经改变:

$this->call(
    'PUT', 
    $url, 
    [], 
    [], 
    [], 
    ['CONTENT_TYPE' => 'application/json'],
    json_encode($data_array)
);

我认为Symphony的request()方法被称为:http://symfony.com/doc/current/book/testing.html


5
投票

这对我有用。

$postData = array('foo' => 'bar');
$postRequest = $this->action('POST', 'MyController@myaction', array(), array(), array(), array(), json_encode($postData));
$this->assertTrue($this->client->getResponse()->isOk());

$this->action的第七个论点是content。请参阅http://laravel.com/api/source-class-Illuminate.Foundation.Testing.TestCase.html#_action上的文档


1
投票

一个简单的解决方案是使用CURL - 这将允许您从服务器捕获'响应'。

class AccountControllerTest extends PHPUnit_Framework_TestCase
{

 public function testLogin()
 {
    $url = "account/login";

    $post_data = array(
        'email' => '[email protected]',
        'password' => 'example_password'
    );
    $content = json_encode($post_data);

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

    $json_response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    $response = json_decode($json_response, true);

    // Do some $this->Assert() stuff here on the $status
  }
}

CURL实际上将使用JSON模拟原始HTTP帖子 - 因此您知道您正在测试您的功能;


0
投票

从Laravel 5.1开始,有一种更简单的方法可以通过PHPunit测试JSON控制器。只需传递一个包含数据的数组,它就会自动编码。

public function testBasicExample()
{
    $this->post('/user', ['name' => 'Sally'])
         ->seeJson([
            'created' => true,
         ]);
}

来自文档:http://laravel.com/docs/5.1/testing#testing-json-apis

© www.soinside.com 2019 - 2024. All rights reserved.