教你进行phpstorm hyperf单元测试配置

藏色散人
发布: 2020-07-23 13:26:30
转载
5419 人浏览过

下面由phpstorm教程栏目给大家介绍phpstorm hyperf单元测试配置,希望对需要的朋友有所帮助!

教你进行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 = &#39;test_admin_token&#39;;

    // token
    protected $header = [];


    public function __construct($name = null, array $data = [], $dataName = &#39;&#39;)
    {
        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[&#39;token&#39;] = $token;
        if (!$token) {
            $userId = 1;
            $user = SysUser::query()->where([&#39;user_id&#39; => $userId])->first();
            $token = JwtInstance::instance()->encode($user);
            $this->header[&#39;token&#39;] = $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(&#39;/&#39;);

        // $this->assertSame(0, $res[&#39;code&#39;]);

        $this->pretty($res);
    }


    /**
     * 后台用户列表
     * 执行命令:composer test -- --filter testGetSysUserList --group adminUser
     *
     * @group adminUser
     */
    public function testGetSysUserList()
    {
        $params = [
            &#39;username&#39; => &#39;&#39;,
            &#39;page&#39; => 1,
            &#39;limit&#39; => 20
        ];
        $result = $this->get(&#39;/admin/sys/user/list&#39;, $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单元测试配置的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:learnku.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!