2
0

03_reactions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. use \WikipaliApi\Api\ReactionsApi;
  3. use \WikipaliApi\Model\PostApiV3MeReactionsRequest;
  4. /* ---- 公共 reactions / tally(只读,无需 token)---- */
  5. test('GET /v3/reactions?target_id=… → 200', function () {
  6. $res = request(fn () => api(ReactionsApi::class)->getApiV3ReactionsWithHttpInfo(fixtures()['target_id']));
  7. assert_status(200, $res, 'reactions');
  8. assert_has_key('data', $res['body'], 'reactions');
  9. assert_has_key('meta', $res['body'], 'reactions');
  10. });
  11. test('GET /v3/reactions(缺 target_id)→ 422', function () {
  12. $res = raw('GET', '/v3/reactions');
  13. assert_status(422, $res, 'reactions 缺 target_id');
  14. assert_problem($res, 'reactions 缺 target_id');
  15. });
  16. test('GET /v3/reactions/tally?target_id=… → 200', function () {
  17. $res = request(fn () => api(ReactionsApi::class)->getApiV3ReactionsTallyWithHttpInfo(fixtures()['target_id']));
  18. assert_status(200, $res, 'tally');
  19. assert_has_key('data', $res['body'], 'tally');
  20. $first = $res['body']['data'][0] ?? null;
  21. assert_has_key('type', $first, 'tally.data[0]');
  22. assert_has_key('count', $first, 'tally.data[0]');
  23. });
  24. test('GET /v3/reactions/tally(缺 target_id)→ 422', function () {
  25. $res = raw('GET', '/v3/reactions/tally');
  26. assert_status(422, $res, 'tally 缺 target_id');
  27. assert_problem($res, 'tally 缺 target_id');
  28. });
  29. /* ---- /v3/me/reactions:无 token → 401(每个动作各一条)---- */
  30. test('GET /v3/me/reactions(无 token)→ 401', function () {
  31. $res = request(fn () => api(ReactionsApi::class)->getApiV3MeReactionsWithHttpInfo());
  32. assert_status(401, $res, 'me/reactions 无 token');
  33. assert_problem($res, 'me/reactions 无 token');
  34. });
  35. test('POST /v3/me/reactions(无 token)→ 401', function () {
  36. $body = new PostApiV3MeReactionsRequest();
  37. $body->setType('watch');
  38. $body->setTargetId('11111111-1111-4111-8111-111111111111');
  39. $body->setTargetType('collection');
  40. $res = request(fn () => api(ReactionsApi::class)->postApiV3MeReactionsWithHttpInfo($body));
  41. assert_status(401, $res, 'me/reactions POST 无 token');
  42. assert_problem($res, 'me/reactions POST 无 token');
  43. });
  44. test('GET /v3/me/reactions(非法 token)→ 401', function () {
  45. $res = request(fn () => api(ReactionsApi::class, 'garbage-token')->getApiV3MeReactionsWithHttpInfo());
  46. assert_status(401, $res, 'me/reactions 非法 token');
  47. assert_problem($res, 'me/reactions 非法 token');
  48. });
  49. /* ---- /v3/me/reactions:带 token → 实际增删查(幂等 + 无 token 删 401 + 删后 404)---- */
  50. test('me/reactions 增删查:POST 建 → GET 查到 → 再 POST 幂等 → 无 token DELETE 401 → DELETE 204 → 再 DELETE 404', function () {
  51. $token = required_token();
  52. // 垃圾 target(uuid 格式即可,不要求真实存在),测完删掉
  53. $targetId = 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee';
  54. $body = new PostApiV3MeReactionsRequest();
  55. $body->setType('watch');
  56. $body->setTargetId($targetId);
  57. $body->setTargetType('collection');
  58. // 增:POST
  59. $create = request(fn () => api(ReactionsApi::class, $token)->postApiV3MeReactionsWithHttpInfo($body));
  60. if (! in_array($create['status'], [200, 201], true)) {
  61. fail('POST 创建:期望 201(新建)或 200(已存在),实际 ' . $create['status']);
  62. }
  63. $id = $create['body']['data']['id'] ?? null;
  64. if (! $id) {
  65. fail('POST 创建:应返回 reaction id');
  66. }
  67. // 查:GET 按 type 过滤,能查到刚建的(用 target_id 定位,因为列表没有 target_id 过滤参数)
  68. $list = request(fn () => api(ReactionsApi::class, $token)->getApiV3MeReactionsWithHttpInfo(200, 'watch'));
  69. assert_status(200, $list, 'GET 列表');
  70. $found = false;
  71. foreach (($list['body']['data'] ?? []) as $row) {
  72. if (($row['target_id'] ?? null) === $targetId) {
  73. $found = true;
  74. break;
  75. }
  76. }
  77. if (! $found) {
  78. fail('GET 列表:查不到刚建的 reaction(target_id=' . $targetId . ')');
  79. }
  80. // 幂等:再 POST 同一条 → 200,id 不变
  81. $again = request(fn () => api(ReactionsApi::class, $token)->postApiV3MeReactionsWithHttpInfo($body));
  82. assert_status(200, $again, '再 POST 幂等');
  83. assert_eq($id, $again['body']['data']['id'] ?? null, '幂等:两次 id 一致');
  84. // 无 token DELETE → 401(此时该 reaction 存在,路由绑定成功 → 鉴权 401)
  85. $noTokenDel = raw('DELETE', '/v3/me/reactions/' . $id);
  86. assert_status(401, $noTokenDel, '无 token DELETE');
  87. assert_problem($noTokenDel, '无 token DELETE');
  88. // 删:DELETE → 204
  89. $del = request(fn () => api(ReactionsApi::class, $token)->deleteApiV3MeReactionsReactionWithHttpInfo($id));
  90. assert_status(204, $del, 'DELETE');
  91. // 再删:已删除 → 404
  92. $delAgain = request(fn () => api(ReactionsApi::class, $token)->deleteApiV3MeReactionsReactionWithHttpInfo($id));
  93. assert_status(404, $delAgain, '再 DELETE');
  94. assert_problem($delAgain, '再 DELETE');
  95. });