| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace Tests\Feature\AppGame\Handler\Promotion;
- use Tests\TestCase;
- use App\Module\AppGame\Handler\Promotion\InfoHandler;
- use App\Module\UrsPromotion\Models\UrsUserMapping;
- use App\Module\UrsPromotion\Models\UrsUserRelationCache;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use App\Module\User\Services\UserService;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\DB;
- use Carbon\Carbon;
- /**
- * InfoHandler getTodayStats 方法优化测试
- *
- * 测试使用 UrsUserRelationCache 优化后的 getTodayStats 方法
- */
- class InfoHandlerTodayStatsTest extends TestCase
- {
- use RefreshDatabase;
- private InfoHandler $handler;
- private int $testUrsUserId;
- private int $testFarmUserId;
- protected function setUp(): void
- {
- parent::setUp();
-
- $this->handler = new InfoHandler();
- $this->testUrsUserId = 10001;
- $this->testFarmUserId = 20001;
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 无关系缓存数据
- */
- public function test_getTodayStats_no_relation_cache()
- {
- // 创建用户映射但不创建关系缓存
- UrsUserMapping::create([
- 'urs_user_id' => $this->testUrsUserId,
- 'user_id' => $this->testFarmUserId,
- 'mapping_time' => now(),
- 'status' => UrsUserMapping::STATUS_VALID,
- ]);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
- $this->assertEquals([
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 有今日新增关系
- */
- public function test_getTodayStats_with_today_relations()
- {
- // 创建用户映射
- UrsUserMapping::create([
- 'urs_user_id' => $this->testUrsUserId,
- 'user_id' => $this->testFarmUserId,
- 'mapping_time' => now(),
- 'status' => UrsUserMapping::STATUS_VALID,
- ]);
- // 创建今日新增的关系缓存记录
- // 1个直推(depth=1)
- UrsUserRelationCache::create([
- 'user_id' => 20002,
- 'related_user_id' => $this->testFarmUserId,
- 'urs_user_id' => 10002,
- 'urs_related_user_id' => $this->testUrsUserId,
- 'level' => 1,
- 'depth' => 1,
- 'path' => (string)$this->testFarmUserId,
- 'urs_path' => (string)$this->testUrsUserId,
- 'created_at' => today(),
- ]);
- // 1个间推(depth=2)
- UrsUserRelationCache::create([
- 'user_id' => 20003,
- 'related_user_id' => $this->testFarmUserId,
- 'urs_user_id' => 10003,
- 'urs_related_user_id' => $this->testUrsUserId,
- 'level' => 2,
- 'depth' => 2,
- 'path' => $this->testFarmUserId . ',20002',
- 'urs_path' => $this->testUrsUserId . ',10002',
- 'created_at' => today(),
- ]);
- // 1个三推(depth=3)
- UrsUserRelationCache::create([
- 'user_id' => 20004,
- 'related_user_id' => $this->testFarmUserId,
- 'urs_user_id' => 10004,
- 'urs_related_user_id' => $this->testUrsUserId,
- 'level' => 2,
- 'depth' => 3,
- 'path' => $this->testFarmUserId . ',20002,20003',
- 'urs_path' => $this->testUrsUserId . ',10002,10003',
- 'created_at' => today(),
- ]);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
- $this->assertEquals([
- 'direct_new_count' => 1, // 1个直推
- 'team_new_count' => 3 // 3个团队成员(1直推+1间推+1三推)
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 有昨日关系但无今日关系
- */
- public function test_getTodayStats_with_yesterday_relations_only()
- {
- // 创建用户映射
- UrsUserMapping::create([
- 'urs_user_id' => $this->testUrsUserId,
- 'user_id' => $this->testFarmUserId,
- 'mapping_time' => now(),
- 'status' => UrsUserMapping::STATUS_VALID,
- ]);
- // 创建昨日的关系缓存记录
- UrsUserRelationCache::create([
- 'user_id' => 20002,
- 'related_user_id' => $this->testFarmUserId,
- 'urs_user_id' => 10002,
- 'urs_related_user_id' => $this->testUrsUserId,
- 'level' => 1,
- 'depth' => 1,
- 'path' => (string)$this->testFarmUserId,
- 'urs_path' => (string)$this->testUrsUserId,
- 'created_at' => yesterday(),
- ]);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$this->testUrsUserId]);
- $this->assertEquals([
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 用户不存在映射关系
- */
- public function test_getTodayStats_user_not_mapped()
- {
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [99999]);
- $this->assertEquals([
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ], $result);
- }
- /**
- * 调用私有方法的辅助方法
- */
- private function invokePrivateMethod($object, $methodName, array $parameters = [])
- {
- $reflection = new \ReflectionClass(get_class($object));
- $method = $reflection->getMethod($methodName);
- $method->setAccessible(true);
- return $method->invokeArgs($object, $parameters);
- }
- }
|