php测试工具_php单元测试

php测试工具_php单元测试guzzle.png本文将介绍Guzzle,Guzzle在单元测试中的使用。来自Guzzle中文文档的解释:Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTPcookies、上传JSON数据等等。发送同步或异步的请求均使用相同的接口。使用PSR-7接口来请求、响应、分流,允许你使用其…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

3de203392ec4

guzzle.png

本文将介绍Guzzle,Guzzle在单元测试中的使用。

来自Guzzle中文文档的解释:

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。

接口简单:构建查询语句、POST请求、分流上传下载大文件、使用HTTP cookies、上传JSON数据等等。

发送同步或异步的请求均使用相同的接口。

使用PSR-7接口来请求、响应、分流,允许你使用其他兼容的PSR-7类库与Guzzle共同开发。

抽象了底层的HTTP传输,允许你改变环境以及其他的代码,如:对cURL与PHP的流或socket并非重度依赖,非阻塞事件循环。

中间件系统允许你创建构成客户端行为。

$client = new GuzzleHttp\Client();

$res = $client->request(‘GET’, ‘https://api.github.com/user’, [

‘auth’ => [‘user’, ‘pass’]

]);

echo $res->getStatusCode();

// “200”

echo $res->getHeader(‘content-type’);

// ‘application/json; charset=utf8’

echo $res->getBody();

// {“type”:”User”…’

// 发送一个异步请求

$request = new \GuzzleHttp\Psr7\Request(‘GET’, ‘http://httpbin.org’);

$promise = $client->sendAsync($request)->then(function ($response) {

echo ‘I completed! ‘ . $response->getBody();

});

$promise->wait();

安装Guzzle

使用composer安装

php composer.phar require guzzlehttp/guzzle:~6.0

或者编辑项目的composer.json文件,添加Guzzle作为依赖

{

“require”: {

“guzzlehttp/guzzle”: “~6.0”

}

}

执行 composer update

Guzzle基本使用

发送请求

use GuzzleHttp\Client;

$client = new Client([

// Base URI is used with relative requests

‘base_uri’ => ‘http://httpbin.org’,

// You can set any number of default request options.

‘timeout’ => 2.0,

]);

$response = $client->get(‘http://httpbin.org/get’);

$response = $client->delete(‘http://httpbin.org/delete’);

$response = $client->head(‘http://httpbin.org/get’);

$response = $client->options(‘http://httpbin.org/get’);

$response = $client->patch(‘http://httpbin.org/patch’);

$response = $client->post(‘http://httpbin.org/post’);

$response = $client->put(‘http://httpbin.org/put’);

设置查询字符串

$response = $client->request(‘GET’, ‘http://httpbin.org?foo=bar’);

或使用 query 请求参数来声明查询字符串参数:

$client->request(‘GET’, ‘http://httpbin.org’, [

‘query’ => [‘foo’ => ‘bar’]

]);

设置POST表单

传入 form_params 数组参数

$response = $client->request(‘POST’, ‘http://httpbin.org/post’, [

‘form_params’ => [

‘field_name’ => ‘abc’,

‘other_field’ => ‘123’,

‘nested_field’ => [

‘nested’ => ‘hello’

]

]

]);

使用响应

# 状态码

$code = $response->getStatusCode(); // 200

$reason = $response->getReasonPhrase(); // OK

# header

// Check if a header exists.

if ($response->hasHeader(‘Content-Length’)) {

echo “It exists”;

}

// Get a header from the response.

echo $response->getHeader(‘Content-Length’);

// Get all of the response headers.

foreach ($response->getHeaders() as $name => $values) {

echo $name . ‘: ‘ . implode(‘, ‘, $values) . “\r\n”;

}

# 响应体

$body = $response->getBody();

// Implicitly cast the body to a string and echo it

echo $body;

// Explicitly cast the body to a string

$stringBody = (string) $body;

// Read 10 bytes from the body

$tenBytes = $body->read(10);

// Read the remaining contents of the body as a string

$remainingBytes = $body->getContents();

安装PHPUnit

同Guzzle的安装, 也适用Composer工具。

composer global require “phpunit/phpunit=5.5.*”

或者在composer.json文件中声明对phpunit/phpunit的依赖

{

“require-dev”: {

“phpunit/phpunit”: “5.5.*”

}

}

执行安装

API 单元测试

我们在tests\unit\MyApiTest.php中定义了两个测试用例

class MyApiTest extends \PHPUnit_Framework_TestCase

{

protected $client;

public function setUp()

{

$this->client = new \GuzzleHttp\Client( [

‘base_uri’ => ‘http://myhost.com’,

‘http_errors’ => false, #设置成 false 来禁用HTTP协议抛出的异常(如 4xx 和 5xx 响应),默认情况下HTPP协议出错时会抛出异常。

]);

}

public function testAction1()

{

$response = $this->client->get(‘/api/v1/action1’);

$body = $response->getBody();

//添加测试

$this->assertEquals(200, $response->getStatusCode());

$data = json_decode($body, true);

$this->assertArrayHasKey(‘errorno’, $data);

$this->assertArrayHasKey(‘errormsg’, $data);

$this->assertArrayHasKey(‘data’, $data);

$this->assertEquals(0, $data[‘errorno’]);

$this->assertInternalType(‘array’, $data[‘data’]);

}

public function testAction2()

{

$response = $this->client->post(‘/api/v1/action2’, [

‘form_params’ => [

‘name’ => ‘myname’,

‘age’ => 20,

],

]);

$body = $response->getBody();

//添加测试

$this->assertEquals(200, $response->getStatusCode());

$data = json_decode($body, true);

$this->assertArrayHasKey(‘errorno’, $data);

$this->assertArrayHasKey(‘errormsg’, $data);

$this->assertArrayHasKey(‘data’, $data);

$this->assertEquals(0, $data[‘errorno’]);

$this->assertInternalType(‘array’, $data[‘data’]);

}

}

运行测试

在项目根目录执行命令

php vendor/bin/phpunit tests/unit/MyApiTest.php

总结

通过Guzzle强大的功能,可以方便进行API单元测试。大家可以查看Guzzle文档,详细了解Guzzle的使用。

参考文档

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234333.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js ajax 跨域问题 解决方案[通俗易懂]

    js ajax 跨域问题 解决方案[通俗易懂]什么是跨域问题?跨域问题来源于JavaScript的”同源策略”,即只有协议+主机名+端口号(如存在)相同,则允许相互访问。也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源。跨域问题是针对JS和ajax的,html本身没有跨域问题。查看浏览器开发者工具Console报错:Failedtoloadhttp://a.a.com:8080/A/FromServlet?userName=123:No’Access-Control-Allow-Origi.

    2022年8月24日
    5
  • Vue 关闭eslint 严格模式「建议收藏」

    Vue 关闭eslint 严格模式「建议收藏」找到.eslintrc.js文件

    2022年10月8日
    0
  • 数据库服务器系统,数据库服务器的操作系统

    数据库服务器系统,数据库服务器的操作系统

    2021年11月28日
    41
  • cnpm安装步骤[通俗易懂]

    cnpm安装步骤[通俗易懂]安装nodeJS官网下载:https://nodejs.org/zh-cn/download/releases/选版本点击下载然后下载后缀名为msi,因为安装简单二、创建文件夹安装完成后我们打开它的目录创建两个文件夹(后面配置环境变量需要)node_cachenode_global三、配置npm的全局模块的存放路径、cache的路径win+r输入cmd打开命令提示符窗口,输入:npmconfigsetprefix”选择刚刚创建node_global文件路径”np

    2022年10月16日
    0
  • Java实现 蓝桥杯 算法提高 三进制数位和

    Java实现 蓝桥杯 算法提高 三进制数位和算法提高三进制数位和时间限制:1.0s内存限制:256.0MB提交此题问题描述  给定L和R,你需要对于每一个6位三进制数(允许前导零),计算其每一个数位上的数字和,设其在十进制下为S。一个三进制数被判断为合法,当且仅当S为质数,或者S属于区间[L,R]。你的任务是给出合法三进制数的个数。输入格式  一行两个非负整数L,R。输出格式  一行一个非负整数表示答案。样例输…

    2022年6月16日
    26
  • js鼠标事件 clientX、clientY、offsetX、offsetY、layerX、layerY、pageX、 pageY、screenX、screenY「建议收藏」

    js鼠标事件 clientX、clientY、offsetX、offsetY、layerX、layerY、pageX、 pageY、screenX、screenY「建议收藏」MouseEvent的类别有以下:mousedown鼠标按下mouseup鼠标释放click左键单击dblclick左键双击mousemove鼠标移动mouseover鼠标经过mouseout鼠标滑出mouseenter鼠标进入mouseleave鼠标离开contextmenu右键菜单注意:执行顺序:mousedown—&g…

    2025年7月27日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号