duration->toMilliseconds())->toBe(150.0); }); it('can be created from seconds', function () { $responseTime = ResponseTime::fromSeconds(0.5); expect($responseTime->duration->toSeconds())->toBe(0.5); expect($responseTime->duration->toMilliseconds())->toBe(500.0); }); it('can be created from milliseconds', function () { $responseTime = ResponseTime::fromMilliseconds(250); expect($responseTime->duration->toMilliseconds())->toBe(250.0); }); it('can measure execution time', function () { $responseTime = ResponseTime::measure(function () { usleep(10000); // 10ms }); expect($responseTime->duration->toMilliseconds())->toBeGreaterThan(5); expect($responseTime->duration->toMilliseconds())->toBeLessThan(50); // Allow some variance }); it('can be created from start time', function () { $startTime = microtime(true); usleep(5000); // 5ms $responseTime = ResponseTime::fromStartTime($startTime); expect($responseTime->duration->toMilliseconds())->toBeGreaterThan(3); expect($responseTime->duration->toMilliseconds())->toBeLessThan(20); }); it('creates zero response time', function () { $responseTime = ResponseTime::zero(); expect($responseTime->duration->isZero())->toBeTrue(); expect($responseTime->duration->toMilliseconds())->toBe(0.0); }); it('classifies performance levels correctly', function () { $fast = ResponseTime::fromMilliseconds(50); $acceptable = ResponseTime::fromMilliseconds(300); $slow = ResponseTime::fromMilliseconds(1000); $verySlow = ResponseTime::fromMilliseconds(3000); expect($fast->isFast())->toBeTrue(); expect($fast->getPerformanceLevel())->toBe(ResponseTimeLevel::FAST); expect($acceptable->isAcceptable())->toBeTrue(); expect($acceptable->getPerformanceLevel())->toBe(ResponseTimeLevel::ACCEPTABLE); expect($slow->isSlow())->toBeTrue(); expect($slow->getPerformanceLevel())->toBe(ResponseTimeLevel::SLOW); expect($verySlow->isVerySlow())->toBeTrue(); expect($verySlow->getPerformanceLevel())->toBe(ResponseTimeLevel::VERY_SLOW); }); it('checks threshold compliance', function () { $responseTime = ResponseTime::fromMilliseconds(300); $threshold = Duration::fromMilliseconds(500); $strictThreshold = Duration::fromMilliseconds(200); expect($responseTime->isWithinThreshold($threshold))->toBeTrue(); expect($responseTime->exceedsThreshold($threshold))->toBeFalse(); expect($responseTime->isWithinThreshold($strictThreshold))->toBeFalse(); expect($responseTime->exceedsThreshold($strictThreshold))->toBeTrue(); }); it('compares response times correctly', function () { $fast = ResponseTime::fromMilliseconds(100); $slow = ResponseTime::fromMilliseconds(500); $same = ResponseTime::fromMilliseconds(100); expect($slow->isSlowerThan($fast))->toBeTrue(); expect($fast->isFasterThan($slow))->toBeTrue(); expect($fast->equals($same))->toBeTrue(); expect($fast->equals($slow))->toBeFalse(); }); it('formats to string correctly', function () { $fast = ResponseTime::fromMilliseconds(50); $slow = ResponseTime::fromSeconds(2.5); expect($fast->toString())->toBe('50 ms'); expect($slow->toString())->toBe('2.5 s'); expect((string) $fast)->toBe('50 ms'); }); it('converts to array correctly', function () { $responseTime = ResponseTime::fromMilliseconds(750); $array = $responseTime->toArray(); expect($array)->toHaveKey('milliseconds'); expect($array)->toHaveKey('seconds'); expect($array)->toHaveKey('formatted'); expect($array)->toHaveKey('level'); expect($array)->toHaveKey('is_fast'); expect($array)->toHaveKey('is_acceptable'); expect($array)->toHaveKey('is_slow'); expect($array)->toHaveKey('is_very_slow'); expect($array['milliseconds'])->toBe(750.0); expect($array['seconds'])->toBe(0.75); expect($array['level'])->toBe('slow'); expect($array['is_slow'])->toBeTrue(); expect($array['is_fast'])->toBeFalse(); }); it('throws exception for excessive response time', function () { $excessiveDuration = Duration::fromSeconds(400); // > 5 minutes expect(fn () => ResponseTime::fromDuration($excessiveDuration)) ->toThrow(InvalidArgumentException::class); }); it('allows response times up to 5 minutes', function () { $maxDuration = Duration::fromSeconds(300); // exactly 5 minutes $responseTime = ResponseTime::fromDuration($maxDuration); expect($responseTime->duration->toSeconds())->toBe(300.0); expect($responseTime->duration->toMilliseconds())->toBe(300000.0); }); });