Skip to content

[11.9] Remove Trailing slash on project path #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
73 changes: 73 additions & 0 deletions tests/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <[email protected]>
* (c) Graham Campbell <[email protected]>
*
* 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);
}
}
2 changes: 1 addition & 1 deletion tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down