- Move 12 markdown files from root to docs/ subdirectories - Organize documentation by category: • docs/troubleshooting/ (1 file) - Technical troubleshooting guides • docs/deployment/ (4 files) - Deployment and security documentation • docs/guides/ (3 files) - Feature-specific guides • docs/planning/ (4 files) - Planning and improvement proposals Root directory cleanup: - Reduced from 16 to 4 markdown files in root - Only essential project files remain: • CLAUDE.md (AI instructions) • README.md (Main project readme) • CLEANUP_PLAN.md (Current cleanup plan) • SRC_STRUCTURE_IMPROVEMENTS.md (Structure improvements) This improves: ✅ Documentation discoverability ✅ Logical organization by purpose ✅ Clean root directory ✅ Better maintainability
155 lines
5.8 KiB
PHP
155 lines
5.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Framework\Health;
|
|
|
|
use App\Framework\Health\HealthCheckResult;
|
|
use App\Framework\Health\HealthStatus;
|
|
use App\Framework\Health\ValueObjects\HealthDetails;
|
|
use App\Framework\Health\ValueObjects\HealthMessage;
|
|
use App\Framework\Health\ValueObjects\ResponseTime;
|
|
|
|
describe('HealthCheckResult with Value Objects', function () {
|
|
it('can be created with Value Objects', function () {
|
|
$result = HealthCheckResult::create(
|
|
HealthStatus::HEALTHY,
|
|
HealthMessage::success('Test Service'),
|
|
HealthDetails::fromArray(['version' => '1.0']),
|
|
ResponseTime::fromMilliseconds(50)
|
|
);
|
|
|
|
expect($result->status)->toBe(HealthStatus::HEALTHY);
|
|
expect($result->message->toString())->toBe('Test Service is working correctly');
|
|
expect($result->details->get('version'))->toBe('1.0');
|
|
expect($result->responseTime->duration->toMilliseconds())->toBe(50.0);
|
|
});
|
|
|
|
it('creates healthy result correctly', function () {
|
|
$result = HealthCheckResult::healthy(
|
|
'Database',
|
|
['host' => 'localhost', 'port' => 3306],
|
|
150.5
|
|
);
|
|
|
|
expect($result->isHealthy())->toBeTrue();
|
|
expect($result->status)->toBe(HealthStatus::HEALTHY);
|
|
expect($result->message->isSuccess())->toBeTrue();
|
|
expect($result->details->get('host'))->toBe('localhost');
|
|
expect($result->responseTime->duration->toMilliseconds())->toBeGreaterThanOrEqual(150.0);
|
|
expect($result->responseTime->duration->toMilliseconds())->toBeLessThanOrEqual(151.0);
|
|
});
|
|
|
|
it('creates warning result correctly', function () {
|
|
$result = HealthCheckResult::warning(
|
|
'Cache',
|
|
'High memory usage',
|
|
['usage' => '85%'],
|
|
300.0
|
|
);
|
|
|
|
expect($result->isWarning())->toBeTrue();
|
|
expect($result->status)->toBe(HealthStatus::WARNING);
|
|
expect($result->message->isWarning())->toBeTrue();
|
|
expect($result->message->toString())->toContain('High memory usage');
|
|
expect($result->details->get('usage'))->toBe('85%');
|
|
});
|
|
|
|
it('creates unhealthy result correctly', function () {
|
|
$exception = new \RuntimeException('Connection refused');
|
|
$result = HealthCheckResult::unhealthy(
|
|
'API',
|
|
'Connection failed',
|
|
['endpoint' => 'https://api.example.com'],
|
|
500.0,
|
|
$exception
|
|
);
|
|
|
|
expect($result->isUnhealthy())->toBeTrue();
|
|
expect($result->status)->toBe(HealthStatus::UNHEALTHY);
|
|
expect($result->message->isError())->toBeTrue();
|
|
expect($result->exception)->toBe($exception);
|
|
expect($result->details->get('endpoint'))->toBe('https://api.example.com');
|
|
expect($result->details->get('error_message'))->toBe('Connection refused');
|
|
});
|
|
|
|
it('creates timeout result correctly', function () {
|
|
$result = HealthCheckResult::timeout(
|
|
'External API',
|
|
5000.0,
|
|
['url' => 'https://slow.api.com']
|
|
);
|
|
|
|
expect($result->isUnhealthy())->toBeTrue();
|
|
expect($result->message->toString())->toContain('timed out after 5000ms');
|
|
expect($result->responseTime->duration->toMilliseconds())->toBe(5000.0);
|
|
expect($result->responseTime->isVerySlow())->toBeTrue();
|
|
});
|
|
|
|
it('creates degraded result correctly', function () {
|
|
$result = HealthCheckResult::degraded(
|
|
'Queue',
|
|
'Processing slowly',
|
|
['pending_jobs' => 1000],
|
|
1500.0
|
|
);
|
|
|
|
expect($result->isWarning())->toBeTrue();
|
|
expect($result->message->toString())->toContain('degraded');
|
|
expect($result->details->get('pending_jobs'))->toBe(1000);
|
|
});
|
|
|
|
it('converts to array with all information', function () {
|
|
$result = HealthCheckResult::healthy(
|
|
'Test Service',
|
|
['version' => '2.0', 'environment' => 'production'],
|
|
75.5
|
|
);
|
|
|
|
$array = $result->toArray();
|
|
|
|
expect($array)->toHaveKey('status');
|
|
expect($array)->toHaveKey('message');
|
|
expect($array)->toHaveKey('message_formatted');
|
|
expect($array)->toHaveKey('message_type');
|
|
expect($array)->toHaveKey('details');
|
|
expect($array)->toHaveKey('response_time_ms');
|
|
expect($array)->toHaveKey('response_time_formatted');
|
|
expect($array)->toHaveKey('response_time_level');
|
|
|
|
expect($array['status'])->toBe('healthy');
|
|
expect($array['message_type'])->toBe('success');
|
|
expect($array['response_time_ms'])->toBeGreaterThanOrEqual(75.0);
|
|
expect($array['response_time_ms'])->toBeLessThanOrEqual(76.0);
|
|
expect($array['response_time_level'])->toBe('fast');
|
|
expect($array['details']['version'])->toBe('2.0');
|
|
});
|
|
|
|
it('handles null response time correctly', function () {
|
|
$result = HealthCheckResult::healthy('Service', []);
|
|
|
|
expect($result->responseTime)->toBeNull();
|
|
|
|
$array = $result->toArray();
|
|
expect($array['response_time_ms'])->toBeNull();
|
|
expect($array['response_time_formatted'])->toBeNull();
|
|
expect($array['response_time_level'])->toBeNull();
|
|
});
|
|
|
|
it('merges error details with provided details', function () {
|
|
$exception = new \Exception('Database error', 500);
|
|
$result = HealthCheckResult::unhealthy(
|
|
'Database',
|
|
'Query failed',
|
|
['query' => 'SELECT * FROM users'],
|
|
100.0,
|
|
$exception
|
|
);
|
|
|
|
expect($result->details->get('query'))->toBe('SELECT * FROM users');
|
|
expect($result->details->get('error_type'))->toBe('Exception');
|
|
expect($result->details->get('error_message'))->toBe('Database error');
|
|
expect($result->details->get('error_code'))->toBe(500);
|
|
});
|
|
});
|