mirror of
https://github.com/tiennm99/coolify.git
synced 2026-04-17 23:20:43 +00:00
Refactor: Centralize service application prerequisites
Refactors the Appwrite and Beszel service-specific application settings to use a centralized constant-based approach, following the same pattern as NEEDS_TO_CONNECT_TO_PREDEFINED_NETWORK. Changes: - Added NEEDS_TO_DISABLE_GZIP constant for services requiring gzip disabled - Added NEEDS_TO_DISABLE_STRIPPREFIX constant for services requiring stripprefix disabled - Created applyServiceApplicationPrerequisites() helper function in bootstrap/helpers/services.php - Updated all service creation flows to use the centralized helper: * app/Livewire/Project/Resource/Create.php (web handler) * app/Http/Controllers/Api/ServicesController.php (API handler - BUG FIX) * app/Livewire/Project/New/DockerCompose.php (custom compose handler) * app/Http/Controllers/Api/ApplicationsController.php (API custom compose handler) - Added comprehensive unit tests for the new helper function Benefits: - Single source of truth for service prerequisites - DRY - eliminates code duplication between web and API handlers - Fixes bug where API-created services didn't get prerequisites applied - Easy to extend for future services (just edit the constant) - More maintainable and testable Related commits:3a94f1ea1(Beszel),02b18c86e(Appwrite) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
103
tests/Unit/ServiceApplicationPrerequisitesTest.php
Normal file
103
tests/Unit/ServiceApplicationPrerequisitesTest.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Service;
|
||||
use App\Models\ServiceApplication;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
it('applies beszel gzip prerequisite correctly', function () {
|
||||
$application = Mockery::mock(ServiceApplication::class);
|
||||
$application->shouldReceive('save')->once();
|
||||
$application->is_gzip_enabled = true; // Start as enabled
|
||||
|
||||
$query = Mockery::mock();
|
||||
$query->shouldReceive('whereName')
|
||||
->with('beszel')
|
||||
->once()
|
||||
->andReturnSelf();
|
||||
$query->shouldReceive('first')
|
||||
->once()
|
||||
->andReturn($application);
|
||||
|
||||
$service = Mockery::mock(Service::class);
|
||||
$service->name = 'beszel-test-uuid';
|
||||
$service->id = 1;
|
||||
$service->shouldReceive('applications')
|
||||
->once()
|
||||
->andReturn($query);
|
||||
|
||||
applyServiceApplicationPrerequisites($service);
|
||||
|
||||
expect($application->is_gzip_enabled)->toBeFalse();
|
||||
});
|
||||
|
||||
it('applies appwrite stripprefix prerequisite correctly', function () {
|
||||
$applications = [];
|
||||
|
||||
foreach (['appwrite', 'appwrite-console', 'appwrite-realtime'] as $name) {
|
||||
$app = Mockery::mock(ServiceApplication::class);
|
||||
$app->is_stripprefix_enabled = true; // Start as enabled
|
||||
$app->shouldReceive('save')->once();
|
||||
$applications[$name] = $app;
|
||||
}
|
||||
|
||||
$service = Mockery::mock(Service::class);
|
||||
$service->name = 'appwrite-test-uuid';
|
||||
$service->id = 1;
|
||||
|
||||
$service->shouldReceive('applications')->times(3)->andReturnUsing(function () use (&$applications) {
|
||||
static $callCount = 0;
|
||||
$names = ['appwrite', 'appwrite-console', 'appwrite-realtime'];
|
||||
$currentName = $names[$callCount++];
|
||||
|
||||
$query = Mockery::mock();
|
||||
$query->shouldReceive('whereName')
|
||||
->with($currentName)
|
||||
->once()
|
||||
->andReturnSelf();
|
||||
$query->shouldReceive('first')
|
||||
->once()
|
||||
->andReturn($applications[$currentName]);
|
||||
|
||||
return $query;
|
||||
});
|
||||
|
||||
applyServiceApplicationPrerequisites($service);
|
||||
|
||||
foreach ($applications as $app) {
|
||||
expect($app->is_stripprefix_enabled)->toBeFalse();
|
||||
}
|
||||
});
|
||||
|
||||
it('handles missing applications gracefully', function () {
|
||||
$query = Mockery::mock();
|
||||
$query->shouldReceive('whereName')
|
||||
->with('beszel')
|
||||
->once()
|
||||
->andReturnSelf();
|
||||
$query->shouldReceive('first')
|
||||
->once()
|
||||
->andReturn(null);
|
||||
|
||||
$service = Mockery::mock(Service::class);
|
||||
$service->name = 'beszel-test-uuid';
|
||||
$service->id = 1;
|
||||
$service->shouldReceive('applications')
|
||||
->once()
|
||||
->andReturn($query);
|
||||
|
||||
// Should not throw exception
|
||||
applyServiceApplicationPrerequisites($service);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
it('skips services without prerequisites', function () {
|
||||
$service = Mockery::mock(Service::class);
|
||||
$service->name = 'unknown-service-uuid';
|
||||
$service->id = 1;
|
||||
$service->shouldNotReceive('applications');
|
||||
|
||||
applyServiceApplicationPrerequisites($service);
|
||||
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user