Skip to content

Commit c37f3fb

Browse files
committed
test: add build url tests
1 parent 9062751 commit c37f3fb

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class YourApi extends Api
6969
- [HTTP client (PSR-18) and HTTP factories (PSR-17)](#http-client-psr-18-and-http-factories-psr-17)
7070
- [Cache (PSR-6)](#cache-psr-6)
7171
- [Logger (PSR-3)](#logger-psr-3)
72-
- [Configure options](#configure-options)
7372

7473
### Base URL
7574

tests/Integration/ApiTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Http\Message\Authentication;
66
use Http\Mock\Client;
77
use Nyholm\Psr7\Response;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use ProgrammatorDev\Api\Api;
910
use ProgrammatorDev\Api\Builder\CacheBuilder;
1011
use ProgrammatorDev\Api\Builder\ClientBuilder;
1112
use ProgrammatorDev\Api\Builder\LoggerBuilder;
13+
use ProgrammatorDev\Api\Event\PreRequestEvent;
1214
use ProgrammatorDev\Api\Event\ResponseContentsEvent;
1315
use ProgrammatorDev\Api\Test\AbstractTestCase;
1416
use ProgrammatorDev\Api\Test\MockResponse;
@@ -192,6 +194,30 @@ public function testResponseContentsListener()
192194
$this->assertIsArray($response);
193195
}
194196

197+
#[DataProvider('provideBuildUrlData')]
198+
public function testBuildUrl(?string $baseUrl, string $path, array $query, string $expectedUrl)
199+
{
200+
$this->api->addPreRequestListener(function(PreRequestEvent $event) use ($expectedUrl) {
201+
$url = (string) $event->getRequest()->getUri();
202+
203+
$this->assertSame($expectedUrl, $url);
204+
});
205+
206+
$this->api->setBaseUrl($baseUrl);
207+
$this->api->request(method: 'GET', path: $path, query: $query);
208+
}
209+
210+
public static function provideBuildUrlData(): \Generator
211+
{
212+
yield 'no base url' => [null, '/path', [], '/path'];
213+
yield 'base url' => [self::BASE_URL, '/path', [], 'https://base.com/url/path'];
214+
yield 'path full url' => [self::BASE_URL, 'https://fullurl.com/path', [], 'https://fullurl.com/path'];
215+
yield 'duplicated slashes' => [self::BASE_URL, '////path', [], 'https://base.com/url/path'];
216+
yield 'query' => [self::BASE_URL, '/path', ['foo' => 'bar'], 'https://base.com/url/path?foo=bar'];
217+
yield 'path query' => [self::BASE_URL, '/path?test=true', ['foo' => 'bar'], 'https://base.com/url/path?test=true&foo=bar'];
218+
yield 'query replace' => [self::BASE_URL, '/path?test=true', ['test' => 'false'], 'https://base.com/url/path?test=false'];
219+
}
220+
195221
public function testBuildPath()
196222
{
197223
$path = $this->api->buildPath('/path/{parameter1}/multiple/{parameter2}', [

0 commit comments

Comments
 (0)