|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Gitlab API library. |
| 7 | + * |
| 8 | + * (c) Matt Humphrey <matth@windsor-telecom.co.uk> |
| 9 | + * (c) Graham Campbell <hello@gjcampbell.co.uk> |
| 10 | + * |
| 11 | + * For the full copyright and license information, please view the LICENSE |
| 12 | + * file that was distributed with this source code. |
| 13 | + */ |
| 14 | + |
| 15 | +namespace Gitlab\Tests; |
| 16 | + |
| 17 | +use Generator; |
| 18 | +use Gitlab\Client; |
| 19 | +use Gitlab\ResultPager; |
| 20 | +use GuzzleHttp\Psr7\Response; |
| 21 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Psr\Http\Client\ClientInterface; |
| 24 | +use Psr\Http\Message\RequestInterface; |
| 25 | + |
| 26 | +class ResultPagerTest extends TestCase |
| 27 | +{ |
| 28 | + #[DataProvider('linkProvider')] |
| 29 | + public function testFetchNextRequestsNextPage(string $link, string $expected): void |
| 30 | + { |
| 31 | + $requests = []; |
| 32 | + $responses = [ |
| 33 | + new Response(200, ['Content-Type' => 'application/json', 'Link' => '<'.$link.'>; rel="next"'], '[{"id":1}]'), |
| 34 | + new Response(200, ['Content-Type' => 'application/json'], '[{"id":2}]'), |
| 35 | + ]; |
| 36 | + |
| 37 | + $httpClient = $this->createMock(ClientInterface::class); |
| 38 | + $httpClient |
| 39 | + ->expects($this->exactly(2)) |
| 40 | + ->method('sendRequest') |
| 41 | + ->willReturnCallback(static function (RequestInterface $request) use (&$requests, &$responses): Response { |
| 42 | + $requests[] = $request; |
| 43 | + |
| 44 | + return \array_shift($responses); |
| 45 | + }); |
| 46 | + |
| 47 | + $client = Client::createWithHttpClient($httpClient); |
| 48 | + $client->setUrl('http://gitlab.internal:8080'); |
| 49 | + $pager = new ResultPager($client, 1); |
| 50 | + |
| 51 | + $pager->fetch($client->projects(), 'all', [['search' => 'my project']]); |
| 52 | + |
| 53 | + $this->assertSame([['id' => 2]], $pager->fetchNext()); |
| 54 | + $this->assertSame($expected, (string) $requests[1]->getUri()); |
| 55 | + } |
| 56 | + |
| 57 | + public static function linkProvider(): Generator |
| 58 | + { |
| 59 | + yield 'other origin' => [ |
| 60 | + 'https://gitlab.example.com/api/v4/projects?page=2&per_page=1&search=my%20project', |
| 61 | + 'http://gitlab.internal:8080/api/v4/projects?page=2&per_page=1&search=my%20project', |
| 62 | + ]; |
| 63 | + |
| 64 | + yield 'path with a colon followed by digits' => [ |
| 65 | + 'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project', |
| 66 | + 'https://gitlab.example.com/api/v4/projects:80/?page=2&per_page=1&search=my%20project', |
| 67 | + ]; |
| 68 | + |
| 69 | + yield 'path beginning with two slashes' => [ |
| 70 | + 'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project', |
| 71 | + 'https://gitlab.example.com//api/v4/projects?page=2&per_page=1&search=my%20project', |
| 72 | + ]; |
| 73 | + } |
| 74 | +} |
0 commit comments