Is it possible in saloon-laravel override bootConnector
method so its resolve dependency from laravel service container?
Problem is when writing tests cannot manage to mock it because of this line: https://github.com/Sammyjo20/Saloon/blob/v1/src/Http/SaloonRequest.php#L110
$this->setConnector(new $this->connector);
should be easy fix (not tested)
$this->setConnector(resolve($this->connector::class));
I did a hacky workaround by overriding getConnector()
so I can dependency injection my FetchifyConnector to FetchifyUKPostcodeLookupRequest https://github.com/Sammyjo20/Saloon/blob/v1/src/Http/SaloonRequest.php#L120
<?php
declare(strict_types=1);
namespace App\Http\Saloon\Requests\Fetchify;
use App\Http\Saloon\Connectors\Fetchify\FetchifyConnector;
use App\Http\Saloon\Responses\Fetchify\FetchifyUKPostcodeLookupResponse;
use App\Libraries\Helpers\LaravelGlobals;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonConnector;
class FetchifyUKPostcodeLookupRequest extends SaloonRequest
{
/**
* Define the method that the request will use.
*
* @var string|null
*/
protected ?string $method = Saloon::GET;
/**
* Define a custom response that the request will return.
*
* @var string|null
*/
protected ?string $response = FetchifyUKPostcodeLookupResponse::class;
private string $postcode;
public function __construct(
private readonly FetchifyConnector $fetchifyConnector,
private readonly LaravelGlobals $laravelGlobals,
) {
}
/**
* Need to set like this to get it working with dependency injection
*
* @return SaloonConnector
*/
public function getConnector(): SaloonConnector
{
return $this->fetchifyConnector;
}
/**
* @link https://fetchify.com/docs/json-api/uk-postcode-lookup.html#full-address-rapidaddress
* @return array
*/
public function defaultQuery(): array
{
return [
'key' => $this->laravelGlobals->config('services.fetchify.key'),
'include_geocode' => false,
];
}
}
<?php
declare(strict_types=1);
namespace App\Http\Saloon\Connectors\Fetchify;
use App\Libraries\Helpers\LaravelGlobals;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
class FetchifyConnector extends SaloonConnector
{
use AcceptsJson;
public function __construct(private LaravelGlobals $laravelGlobals)
{
}
/**
* Define the base url of the api.
* @link https://fetchify.com/docs/json-api/uk-postcode-lookup.html#full-address-rapidaddress
* @return string
*/
public function defineBaseUrl(): string
{
return $this->laravelGlobals->config('services.fetchify.endpoint');
}
}
Hope you got the point, cheers.