如何通过PhpUnit Testing在POST方法中传递JSON? [英] How to pass JSON in POST method with PhpUnit Testing?

查看:223
本文介绍了如何通过PhpUnit Testing在POST方法中传递JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将symfony 3.0与phpUnit框架3.7.18一起使用

I am using symfony 3.0 with phpUnit framework 3.7.18

单元测试文件. abcControllerTest.php

Unit Test file. abcControllerTest.php

namespace AbcBundle\Tests\Controller;


use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;

class AbcControllerTest extends WebTestCase {


    public function testWithParams() {
        $Params = array("params" => array("page_no" => 5));
        $expectedData = $this->listData($Params);

        print_r($expectedData);
    }

    private function listData($Params) {
        $client = static::createClient();        
        $server = array('HTTP_CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json');
        $crawler = $client->request('POST', $GLOBALS['host'] . '/abc/list', $Params,array(), $server);
        $response = $client->getResponse();
        $this->assertSame('text/html; charset=UTF-8', $response->headers->get('Content-Type'));
        $expectedData = json_decode($response->getContent());
        return $expectedData;
    }

}

操作:abc/列表

abcController.php

public function listAction(Request $request) {      
        $Params = json_decode($request->getContent(), true);
}

代码工作正常,但未获得预期结果.由于我试图将json参数从我的phpunit文件 abcControllerTest.php 传递给控制器​​ abcController.php 文件. 任何人都可以建议我如何实现相同的目标.

Code is working fine but not given expected result.Since i am trying to pass json parameter from my phpunit file abcControllerTest.php to controller abcController.php file. Anyone can suggest me how can i achieve the same things.

推荐答案

我更喜欢将GuzzleHttp用于外部请求:

I prefer using GuzzleHttp for external requests :

use GuzzleHttp\Client;

$client = new Client();

$response = $client->post($url, [
    GuzzleHttp\RequestOptions::JSON => ['title' => 'title', 'body' => 'body']
]);

注意:GuzzleHttp应该与例如使用作曲家.

Note: GuzzleHttp should be installed with e.g. using composer.

但是您始终可以使用与Symfony捆绑在一起的客户端:

But you can always use client bundled with Symfony:

public function testJsonPostPageAction()
{
    $this->client = static::createClient();
    $this->client->request(
        'POST', 
        '/api/v1/pages.json',  
        array(),
        array(),
        array('CONTENT_TYPE' => 'application/json'),
        '[{"title":"title1","body":"body1"},{"title":"title2","body":"body2"}]'
    );
    $this->assertJsonResponse($this->client->getResponse(), 201, false);
}

protected function assertJsonResponse($response, $statusCode = 200)
{
    $this->assertEquals(
        $statusCode, $response->getStatusCode(),
        $response->getContent()
    );
    $this->assertTrue(
        $response->headers->contains('Content-Type', 'application/json'),
        $response->headers
    );
}

这篇关于如何通过PhpUnit Testing在POST方法中传递JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆