phpstorm hyperf單元測試配置

chenrenhui發表於2020-07-21

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 單元除錯啦

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章