2018-08-19 11:08:16 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
namespace Tests\Unit\Services\ApiClients;
|
2018-08-19 11:08:16 +00:00
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
2018-08-31 13:47:15 +00:00
|
|
|
use Mockery;
|
2022-07-29 06:47:10 +00:00
|
|
|
use Mockery\LegacyMockInterface;
|
|
|
|
use Mockery\MockInterface;
|
2018-08-19 11:08:16 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
use Tests\Unit\Stubs\ConcreteApiClient;
|
|
|
|
|
|
|
|
class ApiClientTest extends TestCase
|
|
|
|
{
|
|
|
|
use WithoutMiddleware;
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
private Client|LegacyMockInterface|MockInterface $wrapped;
|
2018-08-31 13:47:15 +00:00
|
|
|
|
2018-09-03 12:41:49 +00:00
|
|
|
public function setUp(): void
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
2018-08-31 13:47:15 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$this->wrapped = Mockery::mock(Client::class);
|
2018-08-31 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildUri(): void
|
|
|
|
{
|
2022-08-08 16:00:59 +00:00
|
|
|
$api = new ConcreteApiClient($this->wrapped);
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame('https://foo.com/get/param?key=bar', $api->buildUrl('get/param'));
|
|
|
|
self::assertSame('https://foo.com/get/param?baz=moo&key=bar', $api->buildUrl('/get/param?baz=moo'));
|
|
|
|
self::assertSame('https://baz.com/?key=bar', $api->buildUrl('https://baz.com/'));
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return array<mixed> */
|
2018-09-03 12:41:49 +00:00
|
|
|
public function provideRequestData(): array
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['get', '{"foo":"bar"}'],
|
|
|
|
['post', '{"foo":"bar"}'],
|
|
|
|
['put', '{"foo":"bar"}'],
|
|
|
|
['delete', '{"foo":"bar"}'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @dataProvider provideRequestData */
|
2018-09-03 12:41:49 +00:00
|
|
|
public function testRequest(string $method, string $responseBody): void
|
2018-08-19 11:08:16 +00:00
|
|
|
{
|
|
|
|
/** @var Client $client */
|
2018-08-31 13:47:15 +00:00
|
|
|
$client = Mockery::mock(Client::class, [
|
2018-08-19 11:08:16 +00:00
|
|
|
$method => new Response(200, [], $responseBody),
|
|
|
|
]);
|
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$api = new ConcreteApiClient($client);
|
2018-08-19 11:08:16 +00:00
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
self::assertSame((array) json_decode($responseBody), (array) $api->$method('/'));
|
2018-08-19 11:08:16 +00:00
|
|
|
}
|
|
|
|
}
|