join('urs_promotion_user_mappings', 'kku_urs_promotion_user_relation_cache.urs_user_id', '=', 'urs_promotion_user_mappings.urs_user_id') ->where('urs_promotion_user_mappings.status', UrsUserMapping::STATUS_VALID) ->whereDate('urs_promotion_user_mappings.mapping_time', today()) ->selectRaw(' COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth = 1 THEN 1 END) as direct_new_count, COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth <= 3 THEN 1 END) as team_new_count '); $actualSql = $query->toSql(); // 清理SQL中的多余空格和换行 $cleanExpectedSql = preg_replace('/\s+/', ' ', trim($expectedSql)); $cleanActualSql = preg_replace('/\s+/', ' ', trim($actualSql)); $this->assertEquals($cleanExpectedSql, $cleanActualSql); } /** * 测试查询参数绑定(基于mapping_time) */ public function test_query_bindings_with_mapping_time() { $farmUserId = 20001; $query = UrsUserRelationCache::where('related_user_id', $farmUserId) ->join('urs_promotion_user_mappings', 'kku_urs_promotion_user_relation_cache.urs_user_id', '=', 'urs_promotion_user_mappings.urs_user_id') ->where('urs_promotion_user_mappings.status', UrsUserMapping::STATUS_VALID) ->whereDate('urs_promotion_user_mappings.mapping_time', today()) ->selectRaw(' COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth = 1 THEN 1 END) as direct_new_count, COUNT(CASE WHEN kku_urs_promotion_user_relation_cache.depth <= 3 THEN 1 END) as team_new_count '); $bindings = $query->getBindings(); // 验证绑定参数:related_user_id, status, mapping_time $this->assertEquals($farmUserId, $bindings[0]); $this->assertEquals(UrsUserMapping::STATUS_VALID, $bindings[1]); $this->assertEquals(today()->format('Y-m-d'), $bindings[2]); } /** * 测试优化前后的性能对比逻辑 */ public function test_performance_comparison_logic() { // 优化前:需要多次查询 // 1. 调用 UrsReferralService::getTeamMembers() - 1次查询 // 2. 对每个团队成员调用 UrsUserMappingService::getFarmUserId() - N次查询 // 3. 对每个团队成员调用 UrsUserMappingService::getMappingDetail() - N次查询 // 总计:1 + 2N 次查询 // 优化后:只需要1次查询 // 1. 直接查询 UrsUserRelationCache 表 - 1次查询 // 总计:1次查询 $this->assertTrue(true, '优化后查询次数从 1+2N 减少到 1'); } /** * 测试查询结果处理逻辑 */ public function test_result_processing_logic() { // 模拟查询结果 $mockResult = (object)[ 'direct_new_count' => '2', // 数据库返回字符串 'team_new_count' => '5' // 数据库返回字符串 ]; // 模拟处理逻辑 $directNewCount = $mockResult->direct_new_count ?? 0; $teamNewCount = $mockResult->team_new_count ?? 0; $result = [ 'direct_new_count' => (int)$directNewCount, 'team_new_count' => (int)$teamNewCount ]; // 验证结果格式 $this->assertEquals([ 'direct_new_count' => 2, 'team_new_count' => 5 ], $result); // 验证数据类型 $this->assertIsInt($result['direct_new_count']); $this->assertIsInt($result['team_new_count']); } /** * 测试空结果处理逻辑 */ public function test_empty_result_processing_logic() { // 模拟空查询结果 $mockResult = null; // 模拟处理逻辑 $directNewCount = $mockResult->direct_new_count ?? 0; $teamNewCount = $mockResult->team_new_count ?? 0; $result = [ 'direct_new_count' => (int)$directNewCount, 'team_new_count' => (int)$teamNewCount ]; // 验证空结果处理 $this->assertEquals([ 'direct_new_count' => 0, 'team_new_count' => 0 ], $result); } /** * 测试层级统计逻辑 */ public function test_depth_counting_logic() { // 验证层级统计逻辑 // depth = 1: 直推关系 // depth <= 3: 团队关系(包含直推、间推、三推) $this->assertTrue(1 <= 3, 'depth=1 应该被包含在团队统计中'); $this->assertTrue(2 <= 3, 'depth=2 应该被包含在团队统计中'); $this->assertTrue(3 <= 3, 'depth=3 应该被包含在团队统计中'); $this->assertFalse(4 <= 3, 'depth=4 不应该被包含在团队统计中'); } /** * 测试日期过滤逻辑 */ public function test_date_filtering_logic() { $today = Carbon::today(); $yesterday = Carbon::yesterday(); $tomorrow = Carbon::tomorrow(); // 验证日期过滤逻辑 $this->assertTrue($today->isToday(), '今天的记录应该被包含'); $this->assertFalse($yesterday->isToday(), '昨天的记录不应该被包含'); $this->assertFalse($tomorrow->isToday(), '明天的记录不应该被包含'); } }