hyperf 十二、自动化测试

文档教程:Hyperf

co-phpunit提供测试,在composer中测试。

 "scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}

测试中使用Hyperf\Testing\Client模拟请求,该类调用Hyperf\HttpServer\Server中的request()方法获取请求数据。

#HyperfTest\HttpTestCase
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;abstract class HttpTestCase extends TestCase
{/*** @var Client*/protected $client;public function __construct($name = null, array $data = [], $dataName = ''){parent::__construct($name, $data, $dataName);$this->client = make(Client::class);}
public function __call($name, $arguments){return $this->client->{$name}(...$arguments);}
}#HyperfTest\Cases\ExampleTest
class ExampleTest extends HttpTestCase
{
}#Hyperf\Testing\Client
use Hyperf\HttpServer\Server;
class Client extends Server
{protected $baseUri = 'http://127.0.0.1/';public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http'){parent::__construct($container, $container->get(HttpDispatcher::class), $container->get(ExceptionHandlerDispatcher::class), $container->get(ResponseEmitter::class));$this->packer = $packer ?? new JsonPacker();$this->initCoreMiddleware($server);$this->initBaseUri($server);}public function get($uri, $data = [], $headers = []){$response = $this->request('GET', $uri, ['headers' => $headers,'query' => $data,]);return $this->packer->unpack((string) $response->getBody());}
}

相关方法:

Hyperf\Testing\Client::get($uri, $data = [], $headers = [])

Hyperf\Testing\Client::post($uri, $data = [], $headers = [])

Hyperf\Testing\Client::put($uri, $data = [], $headers = [])

Hyperf\Testing\Client::delete($uri, $data = [], $headers = [])

Hyperf\Testing\Client::json($uri, $data = [], $headers = [])

Hyperf\Testing\Client::file($uri, $data = [], $headers = [])

Hyperf\Testing\Client::request(string $method, string $path, array $options = [])

Hyperf\Testing\Client::initRequest(string $method, string $path, array $options = [])

Hyperf\Testing\Client::sendRequest(ServerRequestInterface $psr7Request)

具体断言函数参照phpunit。

一、编写测试

namespace HyperfTest\Cases;use HyperfTest\HttpTestCase;
/*** @internal* @coversNothing*/
class ExampleTest extends HttpTestCase
{public function testExample(){$this->assertTrue(true);//var_dump($this->get('/'));$this->assertTrue(is_array($this->get('/')));}
}
composer test -- --filter=test

因为co-phpunit脱胎于phpunit,所以filter其实也是phpunit选项。

二、测试替身

测试替身文章原文:PHPUnit 手册 – 第 9 章 测试替身

测试替身用mockery实现。

mockery:

composer地址:mockery/mockery - Packagist

文档地址:Mockery — Mockery Docs 1.0-alpha documentation

github地址:https://github.com/mockery/mockery

 创建的类名必须设置名称以Test结尾,否则不会调用。

#./phpunit.xml 限制后缀内容
<testsuites><testsuite name="Tests"><directory suffix="Test.php">./test</directory></testsuite>
</testsuites># composer 命令配置
"scripts": {"test": "co-phpunit --prepend test/bootstrap.php -c phpunit.xml --colors=always",
}
declare (strict_types = 1);
namespace HyperfTest\Cases;use App\Api\TestApi;
use App\Service\TestingService;
use HyperfTest\HttpTestCase;
use Hyperf\Di\Container;
use Mockery;/*** @internal* @coversNothing*/
class Example2Test extends HttpTestCase
{protected function tearDown(): void{Mockery::close();}public function test2(){$container = $this->getContainer();$res = $container->get(TestingService::class)->test();$this->assertEquals(1, $res['status']);}/*** @return Container*/protected function getContainer(){$container = Mockery::mock(Container::class);$apiStub = $this->createMock(TestApi::class);$apiStub->method('test')->willReturn(['status' => 1,]);//旧版//$container->shouldReceive('get')->with(TestingService::class)->andReturn(new TestingService($apiStub));//新版$container->allows()->get(TestingService::class)->andReturn(new TestingService($apiStub));return $container;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/24226.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vscode中无法使用git解决方案

1 首先查看git安装目录 where git 2 找到bash.exe 的路径 比如&#xff1a;C:/Users/Wangzd/AppData/Local/Programs/Git/bin/bash 3 找到vscode的配置项setting.json 4 添加 "terminal.integrated.shell.windowns": "C:/Users/Wangzd/AppData/Local/Pr…

vue2-vue中mixin到底是什么?

1、mixin是什么&#xff1f; Mixin是面向对象程序设计语言中的类&#xff0c;提供了方法的实现。其他类可以访问mixin类的方法而不必成为其子类。 Mixin类通常作为功能模块使用&#xff0c;在需要该功能时“混入”&#xff0c;有利于代码的复用又避免了多继承的复杂。 1.1 vue中…

stl_list类(使用+实现)(C++)

list 一、list-简单介绍二、list的常用接口1.常见构造2.iterator的使用3.Capacity和Element access4.Modifiers5.list的迭代器失效 三、list实现四、vector 和 list 对比五、迭代器1.迭代器的实现2.迭代器的分类&#xff08;按照功能分类&#xff09;3.反向迭代器(1)、包装逻辑…

wpf画刷学习1

在这2篇博文有提到wpf画刷&#xff0c; https://blog.csdn.net/bcbobo21cn/article/details/109699703 https://blog.csdn.net/bcbobo21cn/article/details/107133703 下面单独学习一下画刷&#xff1b; wpf有五种画刷&#xff0c;也可以自定义画刷&#xff0c;画刷的基类都…

Maven分模块-继承-聚合-私服的高级用法

Maven分模块-继承-聚合-私服的高级用法 JavaWeb知识&#xff0c;介绍Maven的高级用法&#xff01;&#xff01;&#xff01; 文章目录 Maven分模块-继承-聚合-私服的高级用法1. 分模块设计与开发1.1 介绍1.2 实践1.2.1 分析1.2.2 实现 1.3 总结 2. 继承与聚合2.1 继承2.1.1 继承…

无人机巢的作用及应用领域解析

无人机巢作为无人机领域的创新设备&#xff0c;不仅可以实现无人机的自主充电和电池交换&#xff0c;还为无人机提供安全便捷的存放空间。为了帮助大家更好地了解无人机巢&#xff0c;本文将着重解析无人机巢的作用和应用领域。 一、无人机巢的作用 无人机巢作为无人机技术的重…

【chrome扩展开发】vue-i18n使用问题及解决方案

记录chrome扩展开发时调用vue-i18n的一些问题和解决方法 环境 vue: ^3.3.4vue-i18n: ^9.2.2vite: ^4.4.8 错误1 Uncaught (in promise) EvalError: Refused to evaluate a string as JavaScript because unsafe-eval is not an allowed source of script in the following Con…

Spring Bean的生命周期

文章目录 Spring Bean的生命周期加载Bean对象创建Bean对象构造对象填充属性初始化实例注册销毁 销毁 Spring Bean的生命周期 Spring Bean的生命周期就是指Bean对象从创建到销毁的过程&#xff0c;大体可以分为&#xff1a;实例化、属性赋值、初始化、使用、销毁。 加载Bean对象…

Modelsim打开后报unable to checkout a viewer license

找到Modelsim安装包中的MentorKG.exe文件和patch64_dll.bat文件&#xff0c;将这两个文件拷贝到Modelsim安装目录中的win64文件夹&#xff1a; 在win64文件夹中找到mgls64.dll&#xff0c;将它拷贝粘贴一份后修改名字为mgls.dll&#xff1a; 双击win64文件夹中的patch64_dll.ba…

【C++】数据结构与算法:常用排序算法

&#x1f60f;★,:.☆(&#xffe3;▽&#xffe3;)/$:.★ &#x1f60f; 这篇文章主要介绍常用排序算法。 学其所用&#xff0c;用其所学。——梁启超 欢迎来到我的博客&#xff0c;一起学习&#xff0c;共同进步。 喜欢的朋友可以关注一下&#xff0c;下次更新不迷路&#x1…

【前端版】分布式医疗云平台【Open-his 环境搭建、前台 vue-element-ui 搭建】(十六)

目录 1、Open-his 环境搭建 1.1.【前端】vue-element-ui-admin 1.2.【前端】安装 node 服务器 1.3.【前端】安装 VSCode

python字符串常用操作

目录 1. find() 字符串查找2. 字符串匹配 6 种方法 1. find() 字符串查找 python字符串find的应用 查找到字符串的位置&#xff0c;给出所在字符串的下标位置。如下给出的只是下标第6个&#xff1a; a "伤感上单乱杀" # 01 23 4 567 print(a.find("乱杀&…

什么是SYN攻击

SYN攻击属于DOS攻击的一种&#xff0c;它利用TCP协议缺陷&#xff0c;通过发送大量的半连接请求&#xff0c;耗费CPU和内存资源。TCP协议建立连接的时候需要双方相互确认信息&#xff0c;来防止连接被伪造和精确控制整个数据传输过程数据完整有效。所以TCP协议采用三次握手建立…

Red Hat 安装MySQL 8.0与 Navicat

目录 Red Hat 安装 MySQL 8.0 1、更新软件包列表 2、安装MySQL服务器和客户端 3、启动MySQL服务 4、确保MySQL服务器正在运行 5、root 用户的密码 6、登录MySQL&#xff0c;输入mysql密码 7、MySQL默认位置 Red Hat 安装 Navicat 1、下载 Navicat 2、执行命令 Red H…

【单片机】晨启科技,酷黑版,密码锁

密码锁 任务要求&#xff1a; 当输入密码&#xff08;至少6位密码&#xff09;时&#xff0c;OLED显示屏显示输入的数字&#xff08;或者字符&#xff09;&#xff0c;当密码位数输入完毕按下确认键时&#xff0c;对输入的密码与设定的密码进行比较&#xff08;可使用外设键盘&…

数字化转型的本质、路径、阶段和挑战一篇讲明白

01企业数字化转型的本质 数字化可以将人类所处的真实世界和虚拟数字连接起来&#xff0c;从中寻求全新的商业模式。数字化转型基于数字化新技术出现和发展&#xff0c;能够帮助企业将原有传统业务与数字化技术进行结合&#xff0c;以解决企业发展过程中的实际问题&#xff0c;同…

Spark知识点总结

1. Spark支持哪几种运行模式&#xff1f; 本地模式&#xff08;Local Mode&#xff09;&#xff1a;在这种模式下&#xff0c;Spark在单个机器上运行。所有的Spark操作都在一个单独的JVM进程中进行。这种模式适合开发和测试&#xff0c;但不适合处理大规模的数据。 集群模式&a…

C语言--strcat(拼接)

字符串拼接strcat使用及实现 拼接—strcat 原型&#xff1a;charstrcat(chardest,const charsrc) 把src所指向的字符串&#xff08;包括’\0’&#xff09;复制到dest所指向的字符串后面&#xff08;删除dest原来末尾的’\0’&#xff09;&#xff0c;要保证dest足够长&#xf…

STM32的电动自行车信息采集上报系统(学习)

摘要 针对电动自行车实时监管不便的问题&#xff0c;设计了一种基于STM32的电动自行车信息采集系统&#xff0c;通过获取电池、位置和行驶状态信息并上报到服务器中&#xff0c;实现实时监管。 通过多路串口请求电池、行驶状态和位置信息&#xff0c;以并发方式进行数据接收、…

Podman Desktop安装与使用-Windows10

下载 containers/podman 地址 Podman Desktop Downloads 地址 我这里演示的是podman-v4.4.4.msi和podman-desktop-0.13.0-setup.exe 安装 先决条件&#xff1a;由于 Podman 使用 WSL&#xff0c;因此您需要最新版本的 Windows 10 或 Windows 11。在 x64 上&#xff0c;WSL…