feat: add client method to BaseIntegrationService for pre-configured HTTP client and implement test for client functionality
This commit is contained in:
parent
387e136707
commit
1af5b1d7ed
|
|
@ -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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue