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


本文摘自PHP中文网,作者藏色散人,侵删。

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

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

<?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, 然后写测试用例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

<?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单元测试配置
  • 或者可以在项目的跟目录下直接使用命令:

    1

    composer test -- --filter testGetSysUserList --group adminUser

  • 执行结果:

    phpstorm hyperf单元测试配置

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

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

phpstorm hyperf单元测试配置

阅读剩余部分

相关阅读 >>

phpstorm2019出现错误网关怎么办

phpstorm设置行号及文件编码格式

phpstorm中怎么同时修改很多一样的部分

phpstorm如何操作数据库建立外键

解决phpstorm中terminal中编写命令报错php不是内部或外部命令,也不是可运行的程序或批处理文件

图文详解phpstorm连接ftp同步上传

phpstorm怎么设置中文

phpstorm显示502怎么办

webstorm和phpstorm的区别

解决phpstrom中文光标跟随问题

更多相关阅读请进入《phpstorm》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...