|
5 | 5 | use Http\Message\Authentication; |
6 | 6 | use Http\Mock\Client; |
7 | 7 | use Nyholm\Psr7\Response; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
8 | 9 | use ProgrammatorDev\Api\Api; |
9 | 10 | use ProgrammatorDev\Api\Builder\CacheBuilder; |
10 | 11 | use ProgrammatorDev\Api\Builder\ClientBuilder; |
11 | 12 | use ProgrammatorDev\Api\Builder\LoggerBuilder; |
| 13 | +use ProgrammatorDev\Api\Event\PreRequestEvent; |
12 | 14 | use ProgrammatorDev\Api\Event\ResponseContentsEvent; |
13 | 15 | use ProgrammatorDev\Api\Test\AbstractTestCase; |
14 | 16 | use ProgrammatorDev\Api\Test\MockResponse; |
@@ -192,6 +194,30 @@ public function testResponseContentsListener() |
192 | 194 | $this->assertIsArray($response); |
193 | 195 | } |
194 | 196 |
|
| 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 | + |
195 | 221 | public function testBuildPath() |
196 | 222 | { |
197 | 223 | $path = $this->api->buildPath('/path/{parameter1}/multiple/{parameter2}', [ |
|
0 commit comments