Skip to content

Added users projects request parameters normalization. #482

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

Merged
Merged
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
71 changes: 69 additions & 2 deletions lib/Gitlab/Api/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,78 @@ public function show($id)

/**
* @param int $id
* @param array $parameters {
*
* @var bool $archived Limit by archived status.
* @var string $visibility Limit by visibility public, internal, or private.
* @var string $order_by Return projects ordered by id, name, path, created_at, updated_at,
* or last_activity_at fields. Default is created_at.
* @var string $sort Return projects sorted in asc or desc order. Default is desc.
* @var string $search Return list of projects matching the search criteria.
* @var bool $simple Return only the ID, URL, name, and path of each project.
* @var bool $owned Limit by projects owned by the current user.
* @var bool $membership Limit by projects that the current user is a member of.
* @var bool $starred Limit by projects starred by the current user.
* @var bool $statistics Include project statistics.
* @var bool $with_issues_enabled Limit by enabled issues feature.
* @var bool $with_merge_requests_enabled Limit by enabled merge requests feature.
* @var int $min_access_level Limit by current user minimal access level
* }
* @return mixed
*/
public function usersProjects($id, array $params = array())
public function usersProjects($id, array $parameters = [])
{
return $this->get('users/'.$this->encodePath($id).'/projects', $params);
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function (Options $resolver, $value) {
return $value ? 'true' : 'false';
};
$resolver->setDefined('archived')
->setAllowedTypes('archived', 'bool')
->setNormalizer('archived', $booleanNormalizer)
;
$resolver->setDefined('visibility')
->setAllowedValues('visibility', ['public', 'internal', 'private'])
;
$resolver->setDefined('order_by')
->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at'])
;
$resolver->setDefined('sort')
->setAllowedValues('sort', ['asc', 'desc'])
;
$resolver->setDefined('search');
$resolver->setDefined('simple')
->setAllowedTypes('simple', 'bool')
->setNormalizer('simple', $booleanNormalizer)
;
$resolver->setDefined('owned')
->setAllowedTypes('owned', 'bool')
->setNormalizer('owned', $booleanNormalizer)
;
$resolver->setDefined('membership')
->setAllowedTypes('membership', 'bool')
->setNormalizer('membership', $booleanNormalizer)
;
$resolver->setDefined('starred')
->setAllowedTypes('starred', 'bool')
->setNormalizer('starred', $booleanNormalizer)
;
$resolver->setDefined('statistics')
->setAllowedTypes('statistics', 'bool')
->setNormalizer('statistics', $booleanNormalizer)
;
$resolver->setDefined('with_issues_enabled')
->setAllowedTypes('with_issues_enabled', 'bool')
->setNormalizer('with_issues_enabled', $booleanNormalizer)
;
$resolver->setDefined('with_merge_requests_enabled')
->setAllowedTypes('with_merge_requests_enabled', 'bool')
->setNormalizer('with_merge_requests_enabled', $booleanNormalizer)
;
$resolver->setDefined('min_access_level')
->setAllowedValues('min_access_level', [null, 10, 20, 30, 40, 50])
;

return $this->get('users/'.$this->encodePath($id).'/projects', $resolver->resolve($parameters));
}

/**
Expand Down
112 changes: 95 additions & 17 deletions test/Gitlab/Tests/Api/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,43 +92,121 @@ public function shouldShowUser()
$this->assertEquals($expectedArray, $api->show(1));
}

/**
* @test
*/
public function shouldShowUsersProjects()
protected function getUsersProjectsData()
{
$expectedArray = array(
return array(
array('id' => 1, 'name' => 'matt-project-1'),
array('id' => 2, 'name' => 'matt-project-2')
);
}

protected function getUsersProjectsRequestMock($path, $expectedArray = array(), $expectedParameters = array())
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('users/1/projects')
->with($path, $expectedParameters)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->usersProjects(1));
return $api;
}

/**
* @test
*/
public function shouldShowUsersProjects()
{
$expectedArray = $this->getUsersProjectsData();

$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray);

$this->assertEquals($expectedArray, $api->usersProjects(1));
}

/**
* @test
*/
public function shouldShowUsersProjectsWithLimit()
{
$expectedArray = array(
array('id' => 1, 'name' => 'matt-project-1')
);
$expectedArray = [$this->getUsersProjectsData()[0]];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('users/1/projects')
->will($this->returnValue($expectedArray))
;
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['per_page' => 1]);

$this->assertEquals($expectedArray, $api->usersProjects(1, ['per_page' => 1]));
}

/**
* @test
*/
public function shouldGetAllUsersProjectsSortedByName()
{
$expectedArray = $this->getUsersProjectsData();

$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray,
['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']);

$this->assertEquals($expectedArray,
$api->usersProjects(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']));
}

/**
* @test
*/
public function shouldGetNotArchivedUsersProjects()
{
$expectedArray = $this->getUsersProjectsData();

$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['archived' => 'false']);

$this->assertEquals($expectedArray, $api->usersProjects(1, ['archived' => false]));
}

/**
* @test
*/
public function shouldGetOwnedUsersProjects()
{
$expectedArray = $this->getUsersProjectsData();

$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['owned' => 'true']);

$this->assertEquals($expectedArray, $api->usersProjects(1, ['owned' => true]));
}

public function possibleAccessLevels()
{
return [
[10],
[20],
[30],
[40],
[50],
];
}

/**
* @test
* @dataProvider possibleAccessLevels
*/
public function shouldGetProjectsWithMinimumAccessLevel($level)
{
$expectedArray = $this->getUsersProjectsData();

$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['min_access_level' => $level]);

$this->assertEquals($expectedArray, $api->usersProjects(1, ['min_access_level' => $level]));
}

/**
* @test
*/
public function shouldSearchUsersProjects()
{
$expectedArray = $this->getUsersProjectsData();

$this->assertEquals($expectedArray, $api->usersProjects(1, ['per_page'=>1]));
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['search' => 'a project']);
$this->assertEquals($expectedArray, $api->usersProjects(1, ['search' => 'a project']));
}

/**
Expand Down