教你進行phpstorm hyperf單元測試配置

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由phpstorm教程欄目給大家介紹phpstorm hyperf單元測試配置,希望對需要的朋友有所幫助!

教你進行phpstorm hyperf單元測試配置

1、創建一個testCase基類繼承于PHPUnitFrameworkTestCase

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 AppModelSysUser; use AppServiceInstanceJwtInstance; use HyperfTestingClient; use PHPUnitFrameworkTestCase;  /**  * 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-&gt;login(); ????}  ????public?function?__call($name,?$arguments) ????{ ????????return?$this-&gt;client-&gt;{$name}(...$arguments); ????}  ????/** ?????*?@return?mixed|string ?????*?@throws?PsrSimpleCacheInvalidArgumentException ?????*/ ????public?function?login() ????{ ????????$token?=?cache()-&gt;get($this-&gt;cacheKey); ????????$this-&gt;header['token']?=?$token; ????????if?(!$token)?{ ????????????$userId?=?1; ????????????$user?=?SysUser::query()-&gt;where(['user_id'?=&gt;?$userId])-&gt;first(); ????????????$token?=?JwtInstance::instance()-&gt;encode($user); ????????????$this-&gt;header['token']?=?$token; ????????????//?設置到緩存 ?????????????cache()-&gt;set($this-&gt;cacheKey,??$token,?43200); ????????} ????????return?$token; ????}  ????/** ?????*?@param?array?$result ?????*?@return?false|string ?????*/ ????public?function?pretty(array?$result) ????{ ????????//?表示成功 ????????$this-&gt;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 HyperfTestCasesAdmin;   use AppServiceSysUserService; use HyperfTestAdminTestCase; use SwooleCoroutineChannel; use HyperfUtilsContext;  class SysUserControllerTest extends AdminTestCase {     // 測試     public function testGet()     {         // $this->assertTrue(true);  ????????$res?=?$this-&gt;client-&gt;get('/');  ????????//?$this-&gt;assertSame(0,?$res['code']);  ????????$this-&gt;pretty($res); ????}   ????/** ?????*?后臺用戶列表 ?????*?執行命令:composer?test?--?--filter?testGetSysUserList?--group?adminUser ?????* ?????*?@group?adminUser ?????*/ ????public?function?testGetSysUserList() ????{ ????????$params?=?[ ????????????'username'?=&gt;?'', ????????????'page'?=&gt;?1, ????????????'limit'?=&gt;?20 ????????]; ????????$result?=?$this-&gt;get('/admin/sys/user/list',?$params,?$this-&gt;header);  ????????$this-&gt;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】

立即學習PHP免費學習筆記(深入)”;

第二步:映射項目目錄

教你進行phpstorm hyperf單元測試配置
點擊【OK】

第三步:配置 co-phpunit命令

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

教你進行phpstorm hyperf單元測試配置

教你進行phpstorm hyperf單元測試配置

教你進行phpstorm hyperf單元測試配置
如圖所示配置,點擊【OK】或者 【Apply】保存

然后就可以愉快的hyperf ?單元調試啦。

以上就是教你進行

? 版權聲明
THE END
喜歡就支持一下吧
點贊9 分享