Home >Development Tools >phpstorm >Teach you how to configure phpstorm hyperf unit test

Teach you how to configure phpstorm hyperf unit test

藏色散人
藏色散人forward
2020-07-23 13:26:165561browse

The following tutorial column will introduce you to the phpstorm hyperf unit test configuration, I hope it will be helpful to friends in need!

Teach you how to configure phpstorm hyperf unit test

1. Create a testCase base class inherited from PHPUnit\Framework\TestCasetips: Put the token after successful login to the cache, and the next interface request can be fetched directly from the cache.

<?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. Write a test controller that inherits AdminTestCase, and then writes a test case

<?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);
    }
}

    Click the green triangle to the left of the testGetSysUserList method:
  • phpstorm hyperf单元测试配置
  • Or you can use the command directly in the project directory:
    composer test -- --filter testGetSysUserList --group adminUser

  • Execution result:

    phpstorm hyperf单元测试配置

#3. If hyperf turns on the coroutine, phpunit cannot be used. You need to use the co-phpunit that comes with the hyperf framework, so you need to modify the phpstorm configuration

Step one: Open phpstorm->settings->languages ​​& Frameworks->PHP->CLI Interpreter

phpstorm hyperf单元测试配置

phpstorm hyperf单元测试配置

phpstorm hyperf单元测试配置

##After configuration, click [OK] or [Apply]

phpstorm hyperf单元测试配置

Step 2: Mapping the project directory

Click [OK]

phpstorm hyperf单元测试配置

Step 3: Configure the co-phpunit command

Open phpstorm-> ;settings->languages ​​& Frameworks->PHP->Test Frameworks

phpstorm hyperf单元测试配置

phpstorm hyperf单元测试配置##As shown in the picture Display the configuration, click [OK] or [Apply] to save

, and then you can happily debug the hyperf unit. phpstorm hyperf单元测试配置

The above is the detailed content of Teach you how to configure phpstorm hyperf unit test. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete
Previous article:PhpStorm sets php versionNext article:PhpStorm sets php version