Skip to content

Commit bf3056c

Browse files
[11.1] Add order_by and sort params support to Deployments::all() (#616)
Co-authored-by: Graham Campbell <[email protected]>
1 parent 992dc61 commit bf3056c

File tree

3 files changed

+91
-47
lines changed

3 files changed

+91
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Added CI schedule variables endpoints
1111
* Added support for triggering a pipeline
1212
* Added support for the search_namespaces projects parameter
13+
* Added support for order_by and sort deployments parameters
1314

1415
[11.1.0]: https://github.com/GitLabPHP/Client/compare/11.0.0...11.1.0
1516

src/Api/Deployments.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ class Deployments extends AbstractApi
1818
{
1919
/**
2020
* @param int|string $project_id
21-
* @param array $parameters
21+
* @param array $parameters {
22+
*
23+
* @var string $order_by Return deployments ordered by id, iid, created_at, updated_at,
24+
* or ref fields (default is id)
25+
* @var string $sort Return deployments sorted in asc or desc order (default is desc)
26+
* }
2227
*
2328
* @return mixed
2429
*/
2530
public function all($project_id, array $parameters = [])
2631
{
2732
$resolver = $this->createOptionsResolver();
33+
$resolver->setDefined('order_by')
34+
->setAllowedTypes('order_by', 'string')
35+
->setAllowedValues('order_by', ['id', 'iid', 'created_at', 'updated_at', 'ref']);
36+
$resolver->setDefined('sort')
37+
->setAllowedTypes('sort', 'string')
38+
->setAllowedValues('sort', ['desc', 'asc']);
2839

2940
return $this->get($this->getProjectPath($project_id, 'deployments'), $resolver->resolve($parameters));
3041
}

tests/Api/DeploymentsTest.php

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,43 @@ class DeploymentsTest extends TestCase
2222
* @test
2323
*/
2424
public function shouldGetAllDeployments(): void
25+
{
26+
$expectedArray = $this->getMultipleDeploymentsData();
27+
28+
$api = $this->getMultipleDeploymentsRequestMock('projects/1/deployments', $expectedArray, []);
29+
30+
$this->assertEquals($expectedArray, $api->all(1));
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function shouldShowDeployment(): void
2537
{
2638
$expectedArray = [
2739
[
28-
'created_at' => '2016-08-11T07:36:40.222Z',
40+
'created_at' => '2016-08-11T11:32:35.444Z',
2941
'deployable' => [
3042
'commit' => [
3143
'author_email' => '[email protected]',
3244
'author_name' => 'Administrator',
33-
'created_at' => '2016-08-11T09:36:01.000+02:00',
34-
'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8',
35-
'message' => 'Merge branch \'new-title\' into \'master\'
45+
'created_at' => '2016-08-11T13:28:26.000+02:00',
46+
'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a',
47+
'message' => 'Merge branch \'rename-readme\' into \'master\'
3648
37-
Update README
49+
Rename README
3850
3951
4052
41-
See merge request !1',
42-
'short_id' => '99d03678',
43-
'title' => 'Merge branch \'new-title\' into \'master\'
53+
See merge request !2',
54+
'short_id' => 'a91957a8',
55+
'title' => 'Merge branch \'rename-readme\' into \'master\'
4456
',
4557
],
4658
'coverage' => null,
47-
'created_at' => '2016-08-11T07:36:27.357Z',
48-
'finished_at' => '2016-08-11T07:36:39.851Z',
49-
'id' => 657,
59+
'created_at' => '2016-08-11T11:32:24.456Z',
60+
'finished_at' => '2016-08-11T11:32:35.145Z',
61+
'id' => 664,
5062
'name' => 'deploy',
5163
'ref' => 'master',
5264
'runner' => null,
@@ -75,10 +87,10 @@ public function shouldGetAllDeployments(): void
7587
'id' => 9,
7688
'name' => 'production',
7789
],
78-
'id' => 41,
79-
'iid' => 1,
90+
'id' => 42,
91+
'iid' => 2,
8092
'ref' => 'master',
81-
'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8',
93+
'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a',
8294
'user' => [
8395
'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
8496
'id' => 1,
@@ -88,29 +100,42 @@ public function shouldGetAllDeployments(): void
88100
'web_url' => 'http://localhost:3000/root',
89101
],
90102
],
103+
];
104+
105+
$api = $this->getApiMock();
106+
$api->expects($this->once())
107+
->method('get')
108+
->with('projects/1/deployments/42')
109+
->will($this->returnValue($expectedArray));
110+
$this->assertEquals($expectedArray, $api->show(1, 42));
111+
}
112+
113+
private function getMultipleDeploymentsData()
114+
{
115+
return [
91116
[
92-
'created_at' => '2016-08-11T11:32:35.444Z',
117+
'created_at' => '2016-08-11T07:36:40.222Z',
93118
'deployable' => [
94119
'commit' => [
95120
'author_email' => '[email protected]',
96121
'author_name' => 'Administrator',
97-
'created_at' => '2016-08-11T13:28:26.000+02:00',
98-
'id' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a',
99-
'message' => 'Merge branch \'rename-readme\' into \'master\'
122+
'created_at' => '2016-08-11T09:36:01.000+02:00',
123+
'id' => '99d03678b90d914dbb1b109132516d71a4a03ea8',
124+
'message' => 'Merge branch \'new-title\' into \'master\'
100125
101-
Rename README
126+
Update README
102127
103128
104129
105-
See merge request !2',
106-
'short_id' => 'a91957a8',
107-
'title' => 'Merge branch \'rename-readme\' into \'master\'
130+
See merge request !1',
131+
'short_id' => '99d03678',
132+
'title' => 'Merge branch \'new-title\' into \'master\'
108133
',
109134
],
110135
'coverage' => null,
111-
'created_at' => '2016-08-11T11:32:24.456Z',
112-
'finished_at' => '2016-08-11T11:32:35.145Z',
113-
'id' => 664,
136+
'created_at' => '2016-08-11T07:36:27.357Z',
137+
'finished_at' => '2016-08-11T07:36:39.851Z',
138+
'id' => 657,
114139
'name' => 'deploy',
115140
'ref' => 'master',
116141
'runner' => null,
@@ -139,10 +164,10 @@ public function shouldGetAllDeployments(): void
139164
'id' => 9,
140165
'name' => 'production',
141166
],
142-
'id' => 42,
143-
'iid' => 2,
167+
'id' => 41,
168+
'iid' => 1,
144169
'ref' => 'master',
145-
'sha' => 'a91957a858320c0e17f3a0eca7cfacbff50ea29a',
170+
'sha' => '99d03678b90d914dbb1b109132516d71a4a03ea8',
146171
'user' => [
147172
'avatar_url' => 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
148173
'id' => 1,
@@ -152,22 +177,6 @@ public function shouldGetAllDeployments(): void
152177
'web_url' => 'http://localhost:3000/root',
153178
],
154179
],
155-
];
156-
157-
$api = $this->getApiMock();
158-
$api->expects($this->once())
159-
->method('get')
160-
->with('projects/1/deployments')
161-
->will($this->returnValue($expectedArray));
162-
$this->assertEquals($expectedArray, $api->all(1));
163-
}
164-
165-
/**
166-
* @test
167-
*/
168-
public function shouldShowDeployment(): void
169-
{
170-
$expectedArray = [
171180
[
172181
'created_at' => '2016-08-11T11:32:35.444Z',
173182
'deployable' => [
@@ -233,13 +242,36 @@ public function shouldShowDeployment(): void
233242
],
234243
],
235244
];
245+
}
236246

247+
protected function getMultipleDeploymentsRequestMock(string $path, array $expectedArray, array $expectedParameters)
248+
{
237249
$api = $this->getApiMock();
238250
$api->expects($this->once())
239251
->method('get')
240-
->with('projects/1/deployments/42')
252+
->with($path, $expectedParameters)
241253
->will($this->returnValue($expectedArray));
242-
$this->assertEquals($expectedArray, $api->show(1, 42));
254+
255+
return $api;
256+
}
257+
258+
/**
259+
* @test
260+
*/
261+
public function shouldGetAllDeploymentsSortedByCreatedAt(): void
262+
{
263+
$expectedArray = $this->getMultipleDeploymentsData();
264+
265+
$api = $this->getMultipleDeploymentsRequestMock(
266+
'projects/1/deployments',
267+
$expectedArray,
268+
['page' => 1, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'asc']
269+
);
270+
271+
$this->assertEquals(
272+
$expectedArray,
273+
$api->all(1, ['page' => 1, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'asc'])
274+
);
243275
}
244276

245277
protected function getApiClass()

0 commit comments

Comments
 (0)