From 1af5b1d7edd8cfdacd74b42b0aca35b468252eca Mon Sep 17 00:00:00 2001 From: ncoronel Date: Mon, 6 Jul 2026 08:50:52 -0300 Subject: [PATCH] feat: add client method to BaseIntegrationService for pre-configured HTTP client and implement test for client functionality --- .../Services/BaseIntegrationService.php | 14 +++++ .../Integration/IntegrationServiceTest.php | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/app/Domains/Integration/Services/BaseIntegrationService.php b/app/Domains/Integration/Services/BaseIntegrationService.php index 7175602..d7b8583 100644 --- a/app/Domains/Integration/Services/BaseIntegrationService.php +++ b/app/Domains/Integration/Services/BaseIntegrationService.php @@ -4,6 +4,8 @@ namespace App\Domains\Integration\Services; use App\Domains\Integration\Models\Integration; use App\Domains\Integration\Models\TenantIntegration; +use Illuminate\Support\Facades\Http; +use Illuminate\Http\Client\PendingRequest; use Exception; abstract class BaseIntegrationService @@ -132,6 +134,18 @@ abstract class BaseIntegrationService return $this->tenantIntegration->integration_data[$key] ?? $default; } + /** + * Get a pre-configured HTTP client builder. + * + * @return PendingRequest + * @throws Exception + */ + public function client(): PendingRequest + { + return Http::baseUrl($this->getUrl()) + ->withHeaders($this->getHeaders()); + } + /** * Get the headers for the integration. * diff --git a/tests/Feature/Integration/IntegrationServiceTest.php b/tests/Feature/Integration/IntegrationServiceTest.php index 4e2b9ea..534e3d8 100644 --- a/tests/Feature/Integration/IntegrationServiceTest.php +++ b/tests/Feature/Integration/IntegrationServiceTest.php @@ -247,4 +247,56 @@ class IntegrationServiceTest extends TestCase $service->getToken(); } + + public function test_it_returns_preconfigured_client(): void + { + Integration::create([ + 'integration_code' => 'telepagos_homo', + 'name' => 'Telepagos', + 'url' => 'https://api.telepagos.com.ar', + 'integration_data_schema' => [ + 'username' => 'required|string', + 'password' => 'required|string', + ] + ]); + + TenantIntegration::create([ + 'tenant_code' => $this->tenant->codigo, + 'integration_code' => 'telepagos_homo', + 'integration_data' => [ + 'username' => 'tele_user', + 'password' => 'tele_pass', + ] + ]); + + Http::fake([ + 'https://api.telepagos.com.ar/v2/auth/token' => Http::response([ + 'status' => 'ok', + 'token' => 'mock-jwt-token-123', + 'expires_at' => now()->addHour()->toDateTimeString() + ], 200), + 'https://api.telepagos.com.ar/v1/payments' => Http::response([ + 'status' => 'success', + 'payment_id' => 999 + ], 200) + ]); + + $service = new TelepagosIntegrationService('telepagos'); + $service->forTenant($this->tenant->codigo); + + // Get configured client and perform GET request + $client = $service->client(); + $this->assertInstanceOf(\Illuminate\Http\Client\PendingRequest::class, $client); + + $response = $client->get('/v1/payments'); + + $this->assertTrue($response->successful()); + $this->assertEquals(999, $response->json('payment_id')); + + // Assert the authorization header was correctly set during request + Http::assertSent(function ($request) { + return $request->hasHeader('Authorization', 'Bearer mock-jwt-token-123') + && $request->url() === 'https://api.telepagos.com.ar/v1/payments'; + }); + } }