Skip to content

Commit f338fc8

Browse files
committed
Use OptionsResolver in Groups::all
1 parent 2de90ec commit f338fc8

File tree

3 files changed

+46
-62
lines changed

3 files changed

+46
-62
lines changed

UPGRADE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ See [documentation](doc/customize.md) to know how to customize the client timeou
2121
## `Gitlab\Api\Groups` changes
2222

2323
* The `visibility_level` parameter have been removed from `create` method. Use `visibility` instead.
24+
* The `all` method now take a single argument which is an associative array of query string parameters.
25+
* The `search` method have been removed. Use `all` method instead.
2426

2527
## `Gitlab\Api\Issues` changes
2628

lib/Gitlab/Api/Groups.php

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,52 @@
33
class Groups extends AbstractApi
44
{
55
/**
6-
* @param int $page
7-
* @param int $per_page
6+
* @param array $parameters (
7+
*
8+
* @var int[] $skip_groups Skip the group IDs passes.
9+
* @var bool $all_available Show all the groups you have access to.
10+
* @var string $search Return list of authorized groups matching the search criteria.
11+
* @var string $order_by Order groups by name or path. Default is name.
12+
* @var string $sort Order groups in asc or desc order. Default is asc.
13+
* @var bool $statistics Include group statistics (admins only).
14+
* @var bool $owned Limit by groups owned by the current user.
15+
* )
816
* @return mixed
917
*/
10-
public function all($page = 1, $per_page = self::PER_PAGE)
18+
public function all(array $parameters = [])
1119
{
12-
return $this->get('groups', array(
13-
'page' => $page,
14-
'per_page' => $per_page
15-
));
16-
}
20+
$resolver = $this->createOptionsResolver();
21+
$booleanNormalizer = function ($value) {
22+
return $value ? 'true' : 'false';
23+
};
1724

18-
/**
19-
* @param string $query
20-
* @param int $page
21-
* @param int $per_page
22-
* @return mixed
23-
*/
24-
public function search($query, $page = 1, $per_page = self::PER_PAGE)
25-
{
26-
return $this->get('groups?search='.$this->encodePath($query), array(
27-
'page' => $page,
28-
'per_page' => $per_page
29-
));
25+
$resolver->setDefined('skip_groups')
26+
->setAllowedTypes('skip_groups', 'array')
27+
->setAllowedValues('skip_groups', function (array $value) {
28+
return count($value) == count(array_filter($value, 'is_int'));
29+
})
30+
;
31+
$resolver->setDefined('all_available')
32+
->setAllowedTypes('all_available', 'bool')
33+
->setNormalizer('all_available', $booleanNormalizer)
34+
;
35+
$resolver->setDefined('search');
36+
$resolver->setDefined('order_by')
37+
->setAllowedValues('order_by', ['name', 'path'])
38+
;
39+
$resolver->setDefined('sort')
40+
->setAllowedValues('sort', ['asc', 'desc'])
41+
;
42+
$resolver->setDefined('statistics')
43+
->setAllowedTypes('statistics', 'bool')
44+
->setNormalizer('statistics', $booleanNormalizer)
45+
;
46+
$resolver->setDefined('owned')
47+
->setAllowedTypes('owned', 'bool')
48+
->setNormalizer('owned', $booleanNormalizer)
49+
;
50+
51+
return $this->get('groups', $resolver->resolve($parameters));
3052
}
3153

3254
/**

test/Gitlab/Tests/Api/GroupsTest.php

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function shouldGetAllGroups()
2121
->will($this->returnValue($expectedArray))
2222
;
2323

24-
$this->assertEquals($expectedArray, $api->all(1, 10));
24+
$this->assertEquals($expectedArray, $api->all(['page' => 1, 'per_page' => 10]));
2525
}
2626

2727
/**
@@ -37,53 +37,13 @@ public function shouldNotNeedPaginationWhenGettingGroups()
3737
$api = $this->getApiMock();
3838
$api->expects($this->once())
3939
->method('get')
40-
->with('groups', array('page' => 1, 'per_page' => AbstractApi::PER_PAGE))
40+
->with('groups', array())
4141
->will($this->returnValue($expectedArray))
4242
;
4343

4444
$this->assertEquals($expectedArray, $api->all());
4545
}
4646

47-
/**
48-
* @test
49-
*/
50-
public function shouldSearchGroups()
51-
{
52-
$expectedArray = array(
53-
array('id' => 1, 'name' => 'A group'),
54-
array('id' => 2, 'name' => 'Another group'),
55-
);
56-
57-
$api = $this->getApiMock();
58-
$api->expects($this->once())
59-
->method('get')
60-
->with('groups?search=some%20group', array('page' => 1, 'per_page' => AbstractApi::PER_PAGE))
61-
->will($this->returnValue($expectedArray))
62-
;
63-
64-
$this->assertEquals($expectedArray, $api->search('some group'));
65-
}
66-
67-
/**
68-
* @test
69-
*/
70-
public function shouldSearchGroupsWithPagination()
71-
{
72-
$expectedArray = array(
73-
array('id' => 1, 'name' => 'A group'),
74-
array('id' => 2, 'name' => 'Another group'),
75-
);
76-
77-
$api = $this->getApiMock();
78-
$api->expects($this->once())
79-
->method('get')
80-
->with('groups?search=group', array('page' => 2, 'per_page' => 5))
81-
->will($this->returnValue($expectedArray))
82-
;
83-
84-
$this->assertEquals($expectedArray, $api->search('group', 2, 5));
85-
}
86-
8747
/**
8848
* @test
8949
*/

0 commit comments

Comments
 (0)