[PHP] hyperf单元测试模拟http请求

hyperf框架自带单元测试工具

安装完框架后

composer create-project hyperf/hyperf-skeleton

直接在test/Cases下编写单元测试代码

比如我的两个接口一个是 /   , 一个是 /hello , 返回的必须都是json信息才可以,直接返回字符串,测试框架get方法得到的是null

在不启动服务的情况下就可以进行测试,也可以看到打印信息

测试/hello 接口

composer test --  --filter=testHello

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace HyperfTestCases;

use HyperfTestHttpTestCase;

/**
 * @internal
 * @coversNothing
 */
class ExampleTest extends HttpTestCase
{
    public function testExample()
    {
        $this->assertTrue(true);
        $res=$this->get('/');
        var_dump($res);
        $this->assertTrue(is_array($res));
    }
    public function testHello()
    {
        $res=$this->get("/hello");
        var_dump($res);
        $this->assertSame(["hello","tsh"],$res);
    }
}

开源作品

GO-FLY,一套可私有化部署的免费开源客服系统,安装过程不超过五分钟(超过你打我 !),基于Golang开发,二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的网页在线客服系统,致力于帮助广大开发者/中小站长快速整合私有客服功能
github地址:go-fly
官网地址:https://gofly.sopans.com

赞赏作者

微信交流

原文地址:https://www.cnblogs.com/taoshihan/p/14930109.html