InfoHandlerTodayStatsTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Tests\Feature\AppGame\Handler\Promotion;
  3. use Tests\TestCase;
  4. use App\Module\AppGame\Handler\Promotion\InfoHandler;
  5. use App\Module\UrsPromotion\Models\UrsUserMapping;
  6. use App\Module\UrsPromotion\Models\UrsUserRelationCache;
  7. use App\Module\UrsPromotion\Services\UrsUserMappingService;
  8. use App\Module\User\Services\UserService;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Facades\DB;
  11. use Carbon\Carbon;
  12. /**
  13. * InfoHandler getTodayStats 方法优化测试
  14. *
  15. * 测试使用 UrsUserRelationCache 优化后的 getTodayStats 方法
  16. */
  17. class InfoHandlerTodayStatsTest extends TestCase
  18. {
  19. use RefreshDatabase;
  20. private InfoHandler $handler;
  21. private int $testUrsUserId;
  22. private int $testFarmUserId;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. $this->handler = new InfoHandler();
  27. $this->testUrsUserId = 10001;
  28. $this->testFarmUserId = 20001;
  29. }
  30. /**
  31. * 测试优化后的 getTodayStats 方法 - 无关系缓存数据
  32. */
  33. public function test_getTodayStats_no_relation_cache()
  34. {
  35. // 创建用户映射但不创建关系缓存
  36. UrsUserMapping::create([
  37. 'urs_user_id' => $this->testUrsUserId,
  38. 'user_id' => $this->testFarmUserId,
  39. 'mapping_time' => now(),
  40. 'status' => UrsUserMapping::STATUS_VALID,
  41. ]);
  42. $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
  43. $this->assertEquals([
  44. 'direct_new_count' => 0,
  45. 'team_new_count' => 0
  46. ], $result);
  47. }
  48. /**
  49. * 测试优化后的 getTodayStats 方法 - 有今日新增关系
  50. */
  51. public function test_getTodayStats_with_today_relations()
  52. {
  53. // 创建用户映射
  54. UrsUserMapping::create([
  55. 'urs_user_id' => $this->testUrsUserId,
  56. 'user_id' => $this->testFarmUserId,
  57. 'mapping_time' => now(),
  58. 'status' => UrsUserMapping::STATUS_VALID,
  59. ]);
  60. // 创建今日新增的关系缓存记录
  61. // 1个直推(depth=1)
  62. UrsUserRelationCache::create([
  63. 'user_id' => 20002,
  64. 'related_user_id' => $this->testFarmUserId,
  65. 'urs_user_id' => 10002,
  66. 'urs_related_user_id' => $this->testUrsUserId,
  67. 'level' => 1,
  68. 'depth' => 1,
  69. 'path' => (string)$this->testFarmUserId,
  70. 'urs_path' => (string)$this->testUrsUserId,
  71. 'created_at' => today(),
  72. ]);
  73. // 1个间推(depth=2)
  74. UrsUserRelationCache::create([
  75. 'user_id' => 20003,
  76. 'related_user_id' => $this->testFarmUserId,
  77. 'urs_user_id' => 10003,
  78. 'urs_related_user_id' => $this->testUrsUserId,
  79. 'level' => 2,
  80. 'depth' => 2,
  81. 'path' => $this->testFarmUserId . ',20002',
  82. 'urs_path' => $this->testUrsUserId . ',10002',
  83. 'created_at' => today(),
  84. ]);
  85. // 1个三推(depth=3)
  86. UrsUserRelationCache::create([
  87. 'user_id' => 20004,
  88. 'related_user_id' => $this->testFarmUserId,
  89. 'urs_user_id' => 10004,
  90. 'urs_related_user_id' => $this->testUrsUserId,
  91. 'level' => 2,
  92. 'depth' => 3,
  93. 'path' => $this->testFarmUserId . ',20002,20003',
  94. 'urs_path' => $this->testUrsUserId . ',10002,10003',
  95. 'created_at' => today(),
  96. ]);
  97. $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
  98. $this->assertEquals([
  99. 'direct_new_count' => 1, // 1个直推
  100. 'team_new_count' => 3 // 3个团队成员(1直推+1间推+1三推)
  101. ], $result);
  102. }
  103. /**
  104. * 测试优化后的 getTodayStats 方法 - 有昨日关系但无今日关系
  105. */
  106. public function test_getTodayStats_with_yesterday_relations_only()
  107. {
  108. // 创建用户映射
  109. UrsUserMapping::create([
  110. 'urs_user_id' => $this->testUrsUserId,
  111. 'user_id' => $this->testFarmUserId,
  112. 'mapping_time' => now(),
  113. 'status' => UrsUserMapping::STATUS_VALID,
  114. ]);
  115. // 创建昨日的关系缓存记录
  116. UrsUserRelationCache::create([
  117. 'user_id' => 20002,
  118. 'related_user_id' => $this->testFarmUserId,
  119. 'urs_user_id' => 10002,
  120. 'urs_related_user_id' => $this->testUrsUserId,
  121. 'level' => 1,
  122. 'depth' => 1,
  123. 'path' => (string)$this->testFarmUserId,
  124. 'urs_path' => (string)$this->testUrsUserId,
  125. 'created_at' => yesterday(),
  126. ]);
  127. $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
  128. $this->assertEquals([
  129. 'direct_new_count' => 0,
  130. 'team_new_count' => 0
  131. ], $result);
  132. }
  133. /**
  134. * 测试优化后的 getTodayStats 方法 - 用户不存在映射关系
  135. */
  136. public function test_getTodayStats_user_not_mapped()
  137. {
  138. $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [99999]);
  139. $this->assertEquals([
  140. 'direct_new_count' => 0,
  141. 'team_new_count' => 0
  142. ], $result);
  143. }
  144. /**
  145. * 调用私有方法的辅助方法
  146. */
  147. private function invokePrivateMethod($object, $methodName, array $parameters = [])
  148. {
  149. $reflection = new \ReflectionClass(get_class($object));
  150. $method = $reflection->getMethod($methodName);
  151. $method->setAccessible(true);
  152. return $method->invokeArgs($object, $parameters);
  153. }
  154. }