diff --git a/src/Api/AbstractApi.php b/src/Api/AbstractApi.php index 5c81d6fd..6cf92c25 100644 --- a/src/Api/AbstractApi.php +++ b/src/Api/AbstractApi.php @@ -191,7 +191,18 @@ protected static function encodePath($uri): string */ protected function getProjectPath($id, string $uri): string { - return 'projects/'.self::encodePath($id).'/'.$uri; + return $this->attachUri('projects/'.self::encodePath($id), $uri); + } + + /** + * @param string $path + * @param string $uri + * + * @return string + */ + private function attachUri(string $path, string $uri) + { + return '' != $uri ? "{$path}/{$uri}" : $path; } /** diff --git a/tests/Api/AbstractApiTest.php b/tests/Api/AbstractApiTest.php new file mode 100644 index 00000000..d92a50b7 --- /dev/null +++ b/tests/Api/AbstractApiTest.php @@ -0,0 +1,73 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Tests\Api; + +use Gitlab\Api\AbstractApi; +use Gitlab\Client; +use Psr\Http\Client\ClientInterface; + +class AbstractApiTest extends TestCase +{ + public function setUp(): void + { + parent::setUp(); + + $this->api = $this->getTestApi(); + } + + /** + * @test + */ + public function shouldNotHaveTrailingSlashForEmptyUri(): void + { + $expectedString = 'projects/1'; + + $this->assertEquals($expectedString, + $this->api->getProjectPath($id = 1, $uri = '') + ); + } + + /** + * @test + */ + public function shouldHaveTrailingSlashIfProvidedInUri(): void + { + $expectedString = 'projects/1/commits/'; + + $this->assertEquals($expectedString, + $this->api->getProjectPath($id = 1, $uri = 'commits/') + ); + } + + protected function getTestApi(): TestApi + { + $httpClient = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = Client::createWithHttpClient($httpClient); + + return new TestApi($client); + } + + protected function getApiClass(): void + { + } +} + +class TestApi extends AbstractApi +{ + public function getProjectPath($id, string $uri): string + { + return parent::getProjectPath($id, $uri); + } +} diff --git a/tests/Api/ProjectsTest.php b/tests/Api/ProjectsTest.php index bc7e64e0..6def3b0d 100644 --- a/tests/Api/ProjectsTest.php +++ b/tests/Api/ProjectsTest.php @@ -2780,7 +2780,7 @@ public function shouldUploadAvatar(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('put') - ->with('projects/1/', [], [], ['avatar' => $fileName]) + ->with('projects/1', [], [], ['avatar' => $fileName]) ->will($this->returnValue($expectedArray)); $this->assertEquals($expectedArray, $api->uploadAvatar(1, $fileName)); \unlink($fileName);