Skip to content

Commit a4c3d16

Browse files
authored
Merge pull request #482 from jimmy4o4/users-projects-request-parameters
Added users projects request parameters normalization.
2 parents 14e2901 + 1754075 commit a4c3d16

File tree

2 files changed

+164
-19
lines changed

2 files changed

+164
-19
lines changed

lib/Gitlab/Api/Users.php

+69-2
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,78 @@ public function show($id)
6666

6767
/**
6868
* @param int $id
69+
* @param array $parameters {
70+
*
71+
* @var bool $archived Limit by archived status.
72+
* @var string $visibility Limit by visibility public, internal, or private.
73+
* @var string $order_by Return projects ordered by id, name, path, created_at, updated_at,
74+
* or last_activity_at fields. Default is created_at.
75+
* @var string $sort Return projects sorted in asc or desc order. Default is desc.
76+
* @var string $search Return list of projects matching the search criteria.
77+
* @var bool $simple Return only the ID, URL, name, and path of each project.
78+
* @var bool $owned Limit by projects owned by the current user.
79+
* @var bool $membership Limit by projects that the current user is a member of.
80+
* @var bool $starred Limit by projects starred by the current user.
81+
* @var bool $statistics Include project statistics.
82+
* @var bool $with_issues_enabled Limit by enabled issues feature.
83+
* @var bool $with_merge_requests_enabled Limit by enabled merge requests feature.
84+
* @var int $min_access_level Limit by current user minimal access level
85+
* }
6986
* @return mixed
7087
*/
71-
public function usersProjects($id, array $params = array())
88+
public function usersProjects($id, array $parameters = [])
7289
{
73-
return $this->get('users/'.$this->encodePath($id).'/projects', $params);
90+
$resolver = $this->createOptionsResolver();
91+
$booleanNormalizer = function (Options $resolver, $value) {
92+
return $value ? 'true' : 'false';
93+
};
94+
$resolver->setDefined('archived')
95+
->setAllowedTypes('archived', 'bool')
96+
->setNormalizer('archived', $booleanNormalizer)
97+
;
98+
$resolver->setDefined('visibility')
99+
->setAllowedValues('visibility', ['public', 'internal', 'private'])
100+
;
101+
$resolver->setDefined('order_by')
102+
->setAllowedValues('order_by', ['id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at'])
103+
;
104+
$resolver->setDefined('sort')
105+
->setAllowedValues('sort', ['asc', 'desc'])
106+
;
107+
$resolver->setDefined('search');
108+
$resolver->setDefined('simple')
109+
->setAllowedTypes('simple', 'bool')
110+
->setNormalizer('simple', $booleanNormalizer)
111+
;
112+
$resolver->setDefined('owned')
113+
->setAllowedTypes('owned', 'bool')
114+
->setNormalizer('owned', $booleanNormalizer)
115+
;
116+
$resolver->setDefined('membership')
117+
->setAllowedTypes('membership', 'bool')
118+
->setNormalizer('membership', $booleanNormalizer)
119+
;
120+
$resolver->setDefined('starred')
121+
->setAllowedTypes('starred', 'bool')
122+
->setNormalizer('starred', $booleanNormalizer)
123+
;
124+
$resolver->setDefined('statistics')
125+
->setAllowedTypes('statistics', 'bool')
126+
->setNormalizer('statistics', $booleanNormalizer)
127+
;
128+
$resolver->setDefined('with_issues_enabled')
129+
->setAllowedTypes('with_issues_enabled', 'bool')
130+
->setNormalizer('with_issues_enabled', $booleanNormalizer)
131+
;
132+
$resolver->setDefined('with_merge_requests_enabled')
133+
->setAllowedTypes('with_merge_requests_enabled', 'bool')
134+
->setNormalizer('with_merge_requests_enabled', $booleanNormalizer)
135+
;
136+
$resolver->setDefined('min_access_level')
137+
->setAllowedValues('min_access_level', [null, 10, 20, 30, 40, 50])
138+
;
139+
140+
return $this->get('users/'.$this->encodePath($id).'/projects', $resolver->resolve($parameters));
74141
}
75142

76143
/**

test/Gitlab/Tests/Api/UsersTest.php

+95-17
Original file line numberDiff line numberDiff line change
@@ -92,43 +92,121 @@ public function shouldShowUser()
9292
$this->assertEquals($expectedArray, $api->show(1));
9393
}
9494

95-
/**
96-
* @test
97-
*/
98-
public function shouldShowUsersProjects()
95+
protected function getUsersProjectsData()
9996
{
100-
$expectedArray = array(
97+
return array(
10198
array('id' => 1, 'name' => 'matt-project-1'),
10299
array('id' => 2, 'name' => 'matt-project-2')
103100
);
101+
}
104102

103+
protected function getUsersProjectsRequestMock($path, $expectedArray = array(), $expectedParameters = array())
104+
{
105105
$api = $this->getApiMock();
106106
$api->expects($this->once())
107107
->method('get')
108-
->with('users/1/projects')
108+
->with($path, $expectedParameters)
109109
->will($this->returnValue($expectedArray))
110110
;
111111

112-
$this->assertEquals($expectedArray, $api->usersProjects(1));
112+
return $api;
113113
}
114114

115+
/**
116+
* @test
117+
*/
118+
public function shouldShowUsersProjects()
119+
{
120+
$expectedArray = $this->getUsersProjectsData();
121+
122+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray);
123+
124+
$this->assertEquals($expectedArray, $api->usersProjects(1));
125+
}
126+
115127
/**
116128
* @test
117129
*/
118130
public function shouldShowUsersProjectsWithLimit()
119131
{
120-
$expectedArray = array(
121-
array('id' => 1, 'name' => 'matt-project-1')
122-
);
132+
$expectedArray = [$this->getUsersProjectsData()[0]];
123133

124-
$api = $this->getApiMock();
125-
$api->expects($this->once())
126-
->method('get')
127-
->with('users/1/projects')
128-
->will($this->returnValue($expectedArray))
129-
;
134+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['per_page' => 1]);
135+
136+
$this->assertEquals($expectedArray, $api->usersProjects(1, ['per_page' => 1]));
137+
}
138+
139+
/**
140+
* @test
141+
*/
142+
public function shouldGetAllUsersProjectsSortedByName()
143+
{
144+
$expectedArray = $this->getUsersProjectsData();
145+
146+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray,
147+
['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']);
148+
149+
$this->assertEquals($expectedArray,
150+
$api->usersProjects(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'name', 'sort' => 'asc']));
151+
}
152+
153+
/**
154+
* @test
155+
*/
156+
public function shouldGetNotArchivedUsersProjects()
157+
{
158+
$expectedArray = $this->getUsersProjectsData();
159+
160+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['archived' => 'false']);
161+
162+
$this->assertEquals($expectedArray, $api->usersProjects(1, ['archived' => false]));
163+
}
164+
165+
/**
166+
* @test
167+
*/
168+
public function shouldGetOwnedUsersProjects()
169+
{
170+
$expectedArray = $this->getUsersProjectsData();
171+
172+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['owned' => 'true']);
173+
174+
$this->assertEquals($expectedArray, $api->usersProjects(1, ['owned' => true]));
175+
}
176+
177+
public function possibleAccessLevels()
178+
{
179+
return [
180+
[10],
181+
[20],
182+
[30],
183+
[40],
184+
[50],
185+
];
186+
}
187+
188+
/**
189+
* @test
190+
* @dataProvider possibleAccessLevels
191+
*/
192+
public function shouldGetProjectsWithMinimumAccessLevel($level)
193+
{
194+
$expectedArray = $this->getUsersProjectsData();
195+
196+
$api = $this->getUsersProjectsRequestMock('users/1/projects', $expectedArray, ['min_access_level' => $level]);
197+
198+
$this->assertEquals($expectedArray, $api->usersProjects(1, ['min_access_level' => $level]));
199+
}
200+
201+
/**
202+
* @test
203+
*/
204+
public function shouldSearchUsersProjects()
205+
{
206+
$expectedArray = $this->getUsersProjectsData();
130207

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

134212
/**

0 commit comments

Comments
 (0)