|
|
@@ -0,0 +1,194 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Tests\Unit;
|
|
|
+
|
|
|
+use App\Module\Game\AdminControllers\FarmUserSummaryController;
|
|
|
+use Carbon\Carbon;
|
|
|
+use PHPUnit\Framework\TestCase;
|
|
|
+use ReflectionClass;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 农场用户信息汇总相对时间测试
|
|
|
+ */
|
|
|
+class FarmUserSummaryRelativeTimeTest extends TestCase
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取控制器实例并访问受保护的方法
|
|
|
+ */
|
|
|
+ private function getController()
|
|
|
+ {
|
|
|
+ $controller = new FarmUserSummaryController();
|
|
|
+ $reflection = new ReflectionClass($controller);
|
|
|
+ $method = $reflection->getMethod('formatRelativeTime');
|
|
|
+ $method->setAccessible(true);
|
|
|
+
|
|
|
+ return [$controller, $method];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试空值处理
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithNull()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $result = $method->invoke($controller, null);
|
|
|
+ $this->assertEquals('无', $result);
|
|
|
+
|
|
|
+ $result = $method->invoke($controller, '');
|
|
|
+ $this->assertEquals('无', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试已过期时间
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithPastTime()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $pastTime = Carbon::now()->subHours(1);
|
|
|
+ $result = $method->invoke($controller, $pastTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('已过期', $result);
|
|
|
+ $this->assertStringContains('text-danger', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试秒级时间差
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithSeconds()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addSeconds(30);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('秒后', $result);
|
|
|
+ $this->assertStringContains('text-warning', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试分钟级时间差
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithMinutes()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addMinutes(30);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('分钟后', $result);
|
|
|
+ $this->assertStringContains('text-info', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试小时级时间差
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithHours()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addHours(5);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('小时后', $result);
|
|
|
+ $this->assertStringContains('text-primary', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试天级时间差
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithDays()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addDays(3);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('天后', $result);
|
|
|
+ $this->assertStringContains('text-secondary', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试超过7天的时间差
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithWeeks()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addDays(10);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ // 超过7天应该显示具体日期
|
|
|
+ $this->assertStringContains($futureTime->format('Y-m-d H:i:s'), $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试字符串时间输入
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithStringInput()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTimeString = Carbon::now()->addHours(2)->toDateTimeString();
|
|
|
+ $result = $method->invoke($controller, $futureTimeString);
|
|
|
+
|
|
|
+ $this->assertStringContains('小时后', $result);
|
|
|
+ $this->assertStringContains('text-primary', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试无效时间格式
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeWithInvalidFormat()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $result = $method->invoke($controller, 'invalid-date-format');
|
|
|
+
|
|
|
+ $this->assertStringContains('时间格式错误', $result);
|
|
|
+ $this->assertStringContains('text-muted', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试边界情况 - 正好1分钟
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeExactlyOneMinute()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addMinutes(1);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('分钟后', $result);
|
|
|
+ $this->assertStringContains('text-info', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试边界情况 - 正好1小时
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeExactlyOneHour()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addHours(1);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('小时后', $result);
|
|
|
+ $this->assertStringContains('text-primary', $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 测试边界情况 - 正好1天
|
|
|
+ */
|
|
|
+ public function testFormatRelativeTimeExactlyOneDay()
|
|
|
+ {
|
|
|
+ [$controller, $method] = $this->getController();
|
|
|
+
|
|
|
+ $futureTime = Carbon::now()->addDays(1);
|
|
|
+ $result = $method->invoke($controller, $futureTime);
|
|
|
+
|
|
|
+ $this->assertStringContains('天后', $result);
|
|
|
+ $this->assertStringContains('text-secondary', $result);
|
|
|
+ }
|
|
|
+}
|