温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何进行phpstorm hyperf单元测试配置

发布时间:2021-03-10 09:38:18 来源:亿速云 阅读:307 作者:小新 栏目:软件技术

这篇文章给大家分享的是有关如何进行phpstorm hyperf单元测试配置的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1、创建一个testCase基类继承于PHPUnit\Framework\TestCase

tips:把登录成功后的token放到缓存, 下次接口请求可以直接从缓存取。

<?php

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

namespace HyperfTest;

use App\Model\SysUser;
use App\Service\Instance\JwtInstance;
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;

/**
 * Class HttpTestCase.
 * @method get($uri, $data = [], $headers = [])
 * @method post($uri, $data = [], $headers = [])
 * @method json($uri, $data = [], $headers = [])
 * @method file($uri, $data = [], $headers = [])
 */
abstract class AdminTestCase extends TestCase
{
    /**
     * @var Client
     */
    protected $client;

    // token缓存key
    protected $cacheKey = 'test_admin_token';

    // token
    protected $header = [];


    public function __construct($name = null, array $data = [], $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
        $this->client = di(Client::class);
        $this->login();
    }

    public function __call($name, $arguments)
    {
        return $this->client->{$name}(...$arguments);
    }

    /**
     * @return mixed|string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function login()
    {
        $token = cache()->get($this->cacheKey);
        $this->header['token'] = $token;
        if (!$token) {
            $userId = 1;
            $user = SysUser::query()->where(['user_id' => $userId])->first();
            $token = JwtInstance::instance()->encode($user);
            $this->header['token'] = $token;
            // 设置到缓存
             cache()->set($this->cacheKey,  $token, 43200);
        }
        return $token;
    }

    /**
     * @param array $result
     * @return false|string
     */
    public function pretty(array $result)
    {
        // 表示成功
        $this->assertSame(0, 0);
        echo  json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
    }
}

2、写一个test控制器继承AdminTestCase, 然后写测试用例

<?php
/**
 * Created by PhpStorm.
 * User: phpstorm
 * Date: 2020/6/9 14:36
 * Description:
 */


namespace HyperfTest\Cases\Admin;


use App\Service\SysUserService;
use HyperfTest\AdminTestCase;
use Swoole\Coroutine\Channel;
use Hyperf\Utils\Context;

class SysUserControllerTest extends AdminTestCase
{
    // 测试
    public function testGet()
    {
        // $this->assertTrue(true);

        $res = $this->client->get('/');

        // $this->assertSame(0, $res['code']);

        $this->pretty($res);
    }


    /**
     * 后台用户列表
     * 执行命令:composer test -- --filter testGetSysUserList --group adminUser
     *
     * @group adminUser
     */
    public function testGetSysUserList()
    {
        $params = [
            'username' => '',
            'page' => 1,
            'limit' => 20
        ];
        $result = $this->get('/admin/sys/user/list', $params, $this->header);

        $this->pretty($result);
    }
}
  • 点击testGetSysUserList方法左边的绿色三角号:

    如何进行phpstorm hyperf单元测试配置

  • 或者可以在项目的跟目录下直接使用命令:
    composer test -- --filter testGetSysUserList --group adminUser
  • 执行结果:

    如何进行phpstorm hyperf单元测试配置

3、如果hyperf开启协程、phpunit就无法使用,需要使用hyperf框架自带的co-phpunit,所以需要修改phpstorm配置

第一步:打开phpstorm->settings->languages & Frameworks->PHP->CLI Interpreter

如何进行phpstorm hyperf单元测试配置

如何进行phpstorm hyperf单元测试配置

如何进行phpstorm hyperf单元测试配置

如何进行phpstorm hyperf单元测试配置
配置完点击【OK】或者【Apply】

第二步:映射项目目录

如何进行phpstorm hyperf单元测试配置
点击【OK】

第三步:配置 co-phpunit命令

打开phpstorm->settings->languages & Frameworks->PHP->Test Frameworks

如何进行phpstorm hyperf单元测试配置

如何进行phpstorm hyperf单元测试配置

如何进行phpstorm hyperf单元测试配置
如图所示配置,点击【OK】或者 【Apply】保存

然后就可以愉快的hyperf  单元调试啦。

感谢各位的阅读!关于“如何进行phpstorm hyperf单元测试配置”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI