| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace Tests\Unit\AppGame\Handler\Promotion;
- use Tests\TestCase;
- use App\Module\AppGame\Handler\Promotion\InfoHandler;
- use App\Module\UrsPromotion\Models\UrsUserRelationCache;
- use App\Module\UrsPromotion\Services\UrsUserMappingService;
- use Mockery;
- use Illuminate\Support\Facades\Log;
- /**
- * InfoHandler getTodayStats 方法优化单元测试
- *
- * 测试使用 UrsUserRelationCache 优化后的 getTodayStats 方法逻辑
- */
- class InfoHandlerTodayStatsUnitTest extends TestCase
- {
- private InfoHandler $handler;
- protected function setUp(): void
- {
- parent::setUp();
- $this->handler = new InfoHandler();
- }
- protected function tearDown(): void
- {
- Mockery::close();
- parent::tearDown();
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 用户不存在映射关系
- */
- public function test_getTodayStats_user_not_mapped()
- {
- // Mock UrsUserMappingService::getFarmUserId 返回 null
- $mockService = Mockery::mock('alias:' . UrsUserMappingService::class);
- $mockService->shouldReceive('getFarmUserId')
- ->with(99999)
- ->once()
- ->andReturn(null);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [99999]);
- $this->assertEquals([
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 有今日新增关系
- */
- public function test_getTodayStats_with_today_relations()
- {
- $ursUserId = 10001;
- $farmUserId = 20001;
- // Mock UrsUserMappingService::getFarmUserId
- $mockService = Mockery::mock('alias:' . UrsUserMappingService::class);
- $mockService->shouldReceive('getFarmUserId')
- ->with($ursUserId)
- ->once()
- ->andReturn($farmUserId);
- // Mock UrsUserRelationCache 查询结果
- $mockResult = (object)[
- 'direct_new_count' => 2,
- 'team_new_count' => 5
- ];
- $mockQuery = Mockery::mock();
- $mockQuery->shouldReceive('whereDate')
- ->with('created_at', today())
- ->once()
- ->andReturnSelf();
- $mockQuery->shouldReceive('selectRaw')
- ->once()
- ->andReturnSelf();
- $mockQuery->shouldReceive('first')
- ->once()
- ->andReturn($mockResult);
- $mockModel = Mockery::mock('alias:' . UrsUserRelationCache::class);
- $mockModel->shouldReceive('where')
- ->with('related_user_id', $farmUserId)
- ->once()
- ->andReturn($mockQuery);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$ursUserId]);
- $this->assertEquals([
- 'direct_new_count' => 2,
- 'team_new_count' => 5
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 无今日新增关系
- */
- public function test_getTodayStats_no_today_relations()
- {
- $ursUserId = 10001;
- $farmUserId = 20001;
- // Mock UrsUserMappingService::getFarmUserId
- $mockService = Mockery::mock('alias:' . UrsUserMappingService::class);
- $mockService->shouldReceive('getFarmUserId')
- ->with($ursUserId)
- ->once()
- ->andReturn($farmUserId);
- // Mock UrsUserRelationCache 查询结果 - 无数据
- $mockResult = (object)[
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ];
- $mockQuery = Mockery::mock();
- $mockQuery->shouldReceive('whereDate')
- ->with('created_at', today())
- ->once()
- ->andReturnSelf();
- $mockQuery->shouldReceive('selectRaw')
- ->once()
- ->andReturnSelf();
- $mockQuery->shouldReceive('first')
- ->once()
- ->andReturn($mockResult);
- $mockModel = Mockery::mock('alias:' . UrsUserRelationCache::class);
- $mockModel->shouldReceive('where')
- ->with('related_user_id', $farmUserId)
- ->once()
- ->andReturn($mockQuery);
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$ursUserId]);
- $this->assertEquals([
- 'direct_new_count' => 0,
- 'team_new_count' => 0
- ], $result);
- }
- /**
- * 测试优化后的 getTodayStats 方法 - 异常处理
- */
- public function test_getTodayStats_exception_handling()
- {
- $ursUserId = 10001;
- // Mock UrsUserMappingService::getFarmUserId 抛出异常
- $mockService = Mockery::mock('alias:' . UrsUserMappingService::class);
- $mockService->shouldReceive('getFarmUserId')
- ->with($ursUserId)
- ->once()
- ->andThrow(new \Exception('Database error'));
- // Mock Log::error
- Log::shouldReceive('error')
- ->with('获取今日统计数据失败', [
- 'urs_user_id' => $ursUserId,
- 'error' => 'Database error'
- ])
- ->once();
- $result = $this->invokePrivateMethod($this->handler, 'getTodayStats', [$ursUserId]);
- $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);
- }
- }
|