Pest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. use App\Models\Channel;
  3. use App\Models\GroupInfo;
  4. use App\Models\GroupMember;
  5. use App\Models\Sentence;
  6. use App\Models\UserInfo;
  7. use App\Services\AuthService;
  8. use Firebase\JWT\JWT;
  9. use Firebase\JWT\Key;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Str;
  13. use Tests\TestCase;
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Test Case
  17. |--------------------------------------------------------------------------
  18. |
  19. | The closure you provide to your test functions is always bound to a specific PHPUnit test
  20. | case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
  21. | need to change it using the "pest()" function to bind different classes or traits.
  22. |
  23. */
  24. pest()->extend(TestCase::class)
  25. // ->use(RefreshDatabase::class)
  26. ->in('Feature');
  27. /*
  28. |--------------------------------------------------------------------------
  29. | Expectations
  30. |--------------------------------------------------------------------------
  31. |
  32. | When you're writing tests, you often need to check that values meet certain conditions. The
  33. | "expect()" function gives you access to a set of "expectations" methods that you can use
  34. | to assert different things. Of course, you may extend the Expectation API at any time.
  35. |
  36. */
  37. expect()->extend('toBeOne', function () {
  38. return $this->toBe(1);
  39. });
  40. /*
  41. |--------------------------------------------------------------------------
  42. | Functions
  43. |--------------------------------------------------------------------------
  44. |
  45. | While Pest is very powerful out-of-the-box, you may have some testing code specific to your
  46. | project that you don't want to repeat in every file. Here you can also expose helpers as
  47. | global functions to help you to reduce the number of lines of code in your test files.
  48. |
  49. */
  50. function something()
  51. {
  52. // ..
  53. }
  54. /**
  55. * 造一个用户 token。
  56. *
  57. * AuthService::current() 只解 JWT、不查库,所以测试里不必真的建用户;
  58. * payload 结构必须与 AuthService::getUserToken() 保持一致。
  59. */
  60. function userToken(string $userUid, int $userId = 1): string
  61. {
  62. return JWT::encode([
  63. 'nbf' => time(),
  64. 'exp' => time() + 3600,
  65. 'uid' => $userUid,
  66. 'id' => $userId,
  67. ], config('mint.app.jwt_secrets_key'), 'HS512');
  68. }
  69. /**
  70. * 解开一个 token 的 payload。
  71. */
  72. function decodeToken(string $token): object
  73. {
  74. return JWT::decode($token, new Key(config('mint.app.jwt_secrets_key'), 'HS512'));
  75. }
  76. /**
  77. * 把 token 交给 AuthService::current() 判定,返回 user_uid,无效则返回 false。
  78. *
  79. * 所有端点的鉴权都走这里,故用它来断言「token 是否还有效」。
  80. *
  81. * @return string|false
  82. */
  83. function currentUid(string $token)
  84. {
  85. $request = Request::create('/', 'GET');
  86. $request->headers->set('Authorization', 'Bearer '.$token);
  87. $user = AuthService::current($request);
  88. return $user ? $user['user_uid'] : false;
  89. }
  90. /**
  91. * 带用户 token 的请求头。
  92. */
  93. function authHeader(string $userUid): array
  94. {
  95. return ['Authorization' => 'Bearer '.userToken($userUid)];
  96. }
  97. /**
  98. * 建一个用户及其个人 studio,返回 user uid。
  99. *
  100. * StudioApi::getIdByName() 查的是 user_infos.username,所以 studio 名即用户名。
  101. */
  102. function makeStudio(string $username): string
  103. {
  104. $userId = (string) Str::uuid();
  105. (new UserInfo)->forceFill([
  106. 'userid' => $userId,
  107. 'username' => $username,
  108. 'nickname' => $username,
  109. 'password' => 'x',
  110. 'email' => $username.'@example.test',
  111. ])->save();
  112. return $userId;
  113. }
  114. /**
  115. * 建一个属于指定用户的 channel,返回 channel uid。
  116. *
  117. * channels.id 不是自增列,必须显式给值。
  118. */
  119. function makeChannel(string $ownerUid, string $name = 'test channel'): string
  120. {
  121. $uid = (string) Str::uuid();
  122. (new Channel)->forceFill([
  123. 'id' => random_int(1, PHP_INT_MAX),
  124. 'uid' => $uid,
  125. 'type' => 'translation',
  126. 'owner_uid' => $ownerUid,
  127. 'editor_id' => 0,
  128. 'name' => $name,
  129. 'lang' => 'zh-Hans',
  130. 'status' => 30,
  131. 'create_time' => time() * 1000,
  132. 'modify_time' => time() * 1000,
  133. ])->save();
  134. return $uid;
  135. }
  136. /**
  137. * 建一个句子,返回模型
  138. */
  139. function makeSentence(string $channelUid, int $book, int $para, int $wordStart, string $content): Sentence
  140. {
  141. $sentence = new Sentence;
  142. $sentence->forceFill([
  143. // sentences.id 不是自增列,必须显式给值
  144. 'id' => random_int(1, PHP_INT_MAX),
  145. 'uid' => (string) Str::uuid(),
  146. 'book_id' => $book,
  147. 'paragraph' => $para,
  148. 'word_start' => $wordStart,
  149. 'word_end' => $wordStart,
  150. 'channel_uid' => $channelUid,
  151. 'editor_uid' => (string) Str::uuid(),
  152. 'content' => $content,
  153. 'content_type' => 'markdown',
  154. 'strlen' => mb_strlen($content),
  155. 'status' => 30,
  156. 'create_time' => time() * 1000,
  157. 'modify_time' => time() * 1000,
  158. 'language' => 'zh-Hans',
  159. ])->save();
  160. return $sentence;
  161. }
  162. /**
  163. * 建一个团队(group),创建者以 owner(power 0)入团,返回 group uid。
  164. *
  165. * 与 v2 `GroupController@store` 写入的两行一致。
  166. */
  167. function makeGroup(string $ownerUid, string $name = 'test team'): string
  168. {
  169. $uid = (string) Str::uuid();
  170. (new GroupInfo)->forceFill([
  171. 'id' => random_int(1, PHP_INT_MAX),
  172. 'uid' => $uid,
  173. 'name' => $name,
  174. 'owner' => $ownerUid,
  175. 'create_time' => time() * 1000,
  176. 'modify_time' => time() * 1000,
  177. ])->save();
  178. addGroupMember($uid, $ownerUid, 0);
  179. return $uid;
  180. }
  181. /**
  182. * 把用户加进团队。power:0 owner、1 manager、2 普通成员(v2 口径)。
  183. */
  184. function addGroupMember(string $groupUid, string $userUid, int $power = 2, ?string $role = null): GroupMember
  185. {
  186. $member = new GroupMember;
  187. $member->forceFill(array_filter([
  188. 'id' => random_int(1, PHP_INT_MAX),
  189. 'user_id' => $userUid,
  190. 'group_id' => $groupUid,
  191. 'group_name' => GroupInfo::where('uid', $groupUid)->value('name'),
  192. 'power' => $power,
  193. 'role' => $role,
  194. ], fn ($value) => $value !== null))->save();
  195. return $member;
  196. }