Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.1.0 #4

Merged
merged 9 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"guzzlehttp/guzzle": "^7.8",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.16",
"orchestra/testbench": "^8.23",
"orchestra/testbench": "^8.24",
"phpunit/phpunit": "^9.6"
},
"minimum-stability": "dev",
Expand Down
63 changes: 55 additions & 8 deletions src/Concerns/SendsRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace RepCard\Concerns;

use GuzzleHttp\Psr7\Uri;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use RepCard\RepCardService;

/**
Expand All @@ -15,32 +18,76 @@
*/
trait SendsRequests
{
public function __construct(
protected PendingRequest $client,
protected int $companyId,
) {}
protected int $companyId;

public function __construct()
{
$this->companyId = config('repcard.company_id');
}

public function buildUrl(string $path, ?array $query = null): string
{
$url = $this->baseUri();

$url = $url->withPath(
path: $url->getPath().Str::start($path, '/')
);

if ($query !== null) {
$url = $url->withQuery(
query: http_build_query($query)
);
}

return (string) $url;
}

public function baseUri(): Uri
{
return (new Uri())->withScheme(
scheme: 'https'
)->withHost(
host: config('repcard.fqdn')
)->withPath(
path: config('repcard.endpoint')
);
}

public function client(): PendingRequest
{
return Http::acceptJson()->baseUrl(
url: (string) $this->baseUri()
)->withHeader(
name: 'x-api-key',
value: config('repcard.key')
)->timeout(
seconds: config('repcard.timeout')
)->connectTimeout(
seconds: config('repcard.connect_timeout')
);
}

/**
* Issue a `GET` request to the given path.
*/
public function get(string $path, ?array $query = null): Response
public function get(string $path, array|null|string $query = null): Response
{
return $this->client->get($path, $query ?? []);
return $this->client()->get($path, $query);
}

/**
* Issue a `POST` request to the given path.
*/
public function post(string $path, array $data = []): Response
{
return $this->client->post($path, $data);
return $this->client()->post($path, $data);
}

/**
* Issue a `PUT` request to the given path.
*/
public function put(string $path, array $data = []): Response
{
return $this->client->put($path, $data);
return $this->client()->put($path, $data);
}
}
45 changes: 18 additions & 27 deletions src/Facades/RepCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

namespace RepCard\Facades;

use GuzzleHttp\Psr7\Uri;
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use RepCard\RepCardService;

/**
Expand All @@ -27,38 +26,30 @@
* @method static \Illuminate\Http\Client\Response createUser(\RepCard\Http\Requests\UserRequest $request)
* @method static \Illuminate\Http\Client\Response updateUser(int $userId, \RepCard\Http\Requests\UserRequest $request)
* @method static \Illuminate\Http\Client\Response unlinkUser(int $userId)
* @method static \Illuminate\Http\Client\Response get(string $path, array|null $query = null)
* @method static string buildUrl(string $path, array|null $query = null)
* @method static \GuzzleHttp\Psr7\Uri baseUri()
* @method static \Illuminate\Http\Client\PendingRequest client()
* @method static \Illuminate\Http\Client\Response get(string $path, array|string|null $query = null)
* @method static \Illuminate\Http\Client\Response post(string $path, array $data = [])
* @method static \Illuminate\Http\Client\Response put(string $path, array $data = [])
*
* @see \RepCard\RepCardService
*/
class RepCard extends Facade
{
public static function fake(string $path, ?array $query = null): Factory
{
$instance = new Factory();

Http::swap($instance);

$url = (new Uri())->withScheme(
scheme: 'https'
)->withHost(
host: config('repcard.fqdn')
)->withPath(
path: config('repcard.endpoint').Str::start($path, '/')
);

if ($query !== null) {
$url = $url->withQuery(
query: http_build_query($query)
);
}

$url = (string) $url;

return $instance->fake([
$url => Http::response(),
public static function fake(
string $path = '*',
?array $query = null,
array|null|string $body = null,
int $status = Response::HTTP_OK
): Factory {
/** @var RepCardService $instance */
$instance = static::getFacadeRoot();

$url = $instance->buildUrl($path, $query);

return Http::fake([
$url => Http::response($body, $status),
]);
}

Expand Down
25 changes: 4 additions & 21 deletions src/RepCardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace RepCard;

use GuzzleHttp\Psr7\Uri;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;

class RepCardServiceProvider extends ServiceProvider implements DeferrableProvider
Expand All @@ -24,25 +22,10 @@ public function register(): void
$this->mergeConfigFrom($configPath, 'repcard');
}

$this->app->singleton(RepCardService::class, static fn (): RepCardService => new RepCardService(
client: Http::acceptJson()->baseUrl(
url: (string) (new Uri())->withScheme(
scheme: 'https'
)->withHost(
host: config('repcard.fqdn')
)->withPath(
path: config('repcard.endpoint')
)
)->withHeader(
name: 'x-api-key',
value: config('repcard.key')
)->timeout(
seconds: config('repcard.timeout')
)->connectTimeout(
seconds: config('repcard.connect_timeout')
),
companyId: config('repcard.company_id')
));
$this->app->singleton(
abstract: RepCardService::class,
concrete: static fn (): RepCardService => new RepCardService()
);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Facades/RepCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,55 @@ public static function statusTypeProvider(): array
];
}

public function test_it_can_fake_a_response(): void
{
RepCard::fake();

$this->assertOk(RepCard::get('/'));
}

public function test_it_can_build_a_url(): void
{
$url = RepCard::buildUrl('/test');

$this->assertSame('https://app.repcard.com/api/test', $url);
}

public function test_it_can_build_a_url_with_query(): void
{
$url = RepCard::buildUrl('/test', [
'foo' => 'bar',
]);

$this->assertSame('https://app.repcard.com/api/test?foo=bar', $url);
}

public function test_it_can_get_the_base_uri(): void
{
$uri = RepCard::baseUri();

$this->assertSame('https', $uri->getScheme());
$this->assertSame('app.repcard.com', $uri->getHost());
$this->assertSame('/api', $uri->getPath());
}

public function test_it_can_get_the_client(): void
{
$expected = [
'connect_timeout' => config('repcard.connect_timeout'),
'http_errors' => false,
'timeout' => config('repcard.timeout'),
'headers' => [
'Accept' => 'application/json',
'x-api-key' => config('repcard.key'),
],
];

$options = RepCard::client()->getOptions();

$this->assertSame($expected, $options);
}

public function test_get_companies(): void
{
RepCard::fake('/companies');
Expand Down
Loading