您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何進行phpstorm hyperf單元測試配置的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
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; } }
<?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); } }
composer test -- --filter testGetSysUserList --group adminUser
配置完點擊【OK】或者【Apply】
點擊【OK】
打開phpstorm->settings->languages & Frameworks->PHP->Test Frameworks
如圖所示配置,點擊【OK】或者 【Apply】保存
然后就可以愉快的hyperf 單元調試啦。
感謝各位的閱讀!關于“如何進行phpstorm hyperf單元測試配置”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。