koel/tests/Unit/Services/ApiClients/ApiClientTest.php

60 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2022-08-08 16:00:59 +00:00
namespace Tests\Unit\Services\ApiClients;
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;
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
public function setUp(): void
{
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);
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/'));
}
2020-12-22 20:11:22 +00:00
/** @return array<mixed> */
public function provideRequestData(): array
{
return [
['get', '{"foo":"bar"}'],
['post', '{"foo":"bar"}'],
['put', '{"foo":"bar"}'],
['delete', '{"foo":"bar"}'],
];
}
2020-12-22 20:11:22 +00:00
/** @dataProvider provideRequestData */
public function testRequest(string $method, string $responseBody): void
{
/** @var Client $client */
2018-08-31 13:47:15 +00:00
$client = Mockery::mock(Client::class, [
$method => new Response(200, [], $responseBody),
]);
2022-08-08 16:00:59 +00:00
$api = new ConcreteApiClient($client);
2018-08-31 13:47:15 +00:00
self::assertSame((array) json_decode($responseBody), (array) $api->$method('/'));
}
}