Skip to content

Commit 26a4f3a

Browse files
[11.14] Group milestones API: able to call GitLab API with addtional parameters updated_before and updated_after (#786)
* accept the parameters updated_before and updated_after (Introduced in GitLab 15.10) * add missing phpunit tests Groups Milestones (Gitlab\Tests\Api\GroupsMilestones) ✔ Should get all milestones with parameter one iids value ✔ Should get all milestones with parameter two iids values ✔ Should get all milestones with parameter state with active ✔ Should get all milestones with parameter state with closed ✔ Should get all milestones with parameter search ✔ Should get all milestones with parameter updated before ✔ Should get all milestones with parameter updated after * fix code style * remove unused variable and rename meaningful variable * Change the dataProvider function to static (deprecated in PHPUnit 10) * use DateTimeInterface instead of string for $updated_after and $updated_before update unit tests * Update GroupsMilestones.php * Update GroupsMilestonesTest.php --------- Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
1 parent f6fcad6 commit 26a4f3a

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/Api/GroupsMilestones.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace Gitlab\Api;
1616

17+
use Symfony\Component\OptionsResolver\Options;
18+
1719
class GroupsMilestones extends AbstractApi
1820
{
1921
/**
@@ -32,14 +34,21 @@ class GroupsMilestones extends AbstractApi
3234
*
3335
* @var int[] $iids return only the milestones having the given iids
3436
* @var string $state return only active or closed milestones
35-
* @var string $search Return only milestones with a title or description matching the provided string.
37+
* @var string $search Return only milestones with a title or description matching the provided string
38+
* @var \DateTimeInterface $updated_after Return only milestones updated on or after the given datetime. Expected in ISO 8601 format (2019-03-15T08:00:00Z)
39+
* @var \DateTimeInterface $updated_before Return only milestones updated on or before the given datetime. Expected in ISO 8601 format (2019-03-15T08:00:00Z)
3640
* }
3741
*
3842
* @return mixed
3943
*/
4044
public function all($group_id, array $parameters = [])
4145
{
4246
$resolver = $this->createOptionsResolver();
47+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
48+
$utc = (new \DateTimeImmutable($value->format(\DateTimeImmutable::RFC3339_EXTENDED)))->setTimezone(new \DateTimeZone('UTC'));
49+
50+
return $utc->format('Y-m-d\TH:i:s.v\Z');
51+
};
4352
$resolver->setDefined('iids')
4453
->setAllowedTypes('iids', 'array')
4554
->setAllowedValues('iids', function (array $value) {
@@ -51,6 +60,13 @@ public function all($group_id, array $parameters = [])
5160
;
5261
$resolver->setDefined('search');
5362

63+
$resolver->setDefined('updated_after')
64+
->setAllowedTypes('updated_after', \DateTimeInterface::class)
65+
->setNormalizer('updated_after', $datetimeNormalizer);
66+
$resolver->setDefined('updated_before')
67+
->setAllowedTypes('updated_before', \DateTimeInterface::class)
68+
->setNormalizer('updated_before', $datetimeNormalizer);
69+
5470
return $this->get('groups/'.self::encodePath($group_id).'/milestones', $resolver->resolve($parameters));
5571
}
5672

tests/Api/GroupsMilestonesTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,106 @@ public function shouldGetAllMilestones(): void
3838
$this->assertEquals($expectedArray, $api->all(1));
3939
}
4040

41+
/**
42+
* @test
43+
*/
44+
public function shouldGetAllMilestonesWithParameterOneIidsValue(): void
45+
{
46+
$api = $this->getApiMock();
47+
$api->expects($this->once())
48+
->method('get')
49+
->with('groups/1/milestones', ['iids' => [456]])
50+
;
51+
52+
$api->all(1, ['iids' => [456]]);
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function shouldGetAllMilestonesWithParameterTwoIidsValues(): void
59+
{
60+
$api = $this->getApiMock();
61+
$api->expects($this->once())
62+
->method('get')
63+
->with('groups/1/milestones', ['iids' => [456, 789]])
64+
;
65+
66+
$api->all(1, ['iids' => [456, 789]]);
67+
}
68+
69+
public static function getAllMilestonesWithParameterStateDataProvider()
70+
{
71+
return [
72+
GroupsMilestones::STATE_ACTIVE => [GroupsMilestones::STATE_ACTIVE],
73+
GroupsMilestones::STATE_CLOSED => [GroupsMilestones::STATE_CLOSED],
74+
];
75+
}
76+
77+
/**
78+
* @test
79+
*
80+
* @dataProvider getAllMilestonesWithParameterStateDataProvider
81+
*/
82+
public function shouldGetAllMilestonesWithParameterState(string $state): void
83+
{
84+
$api = $this->getApiMock();
85+
$api->expects($this->once())
86+
->method('get')
87+
->with('groups/1/milestones', ['state' => $state])
88+
;
89+
90+
$api->all(1, ['state' => $state]);
91+
}
92+
93+
/**
94+
* @test
95+
*/
96+
public function shouldGetAllMilestonesWithParameterSearch(): void
97+
{
98+
$searchValue = 'abc';
99+
100+
$api = $this->getApiMock();
101+
$api->expects($this->once())
102+
->method('get')
103+
->with('groups/1/milestones', ['search' => $searchValue])
104+
;
105+
106+
$api->all(1, ['search' => $searchValue]);
107+
}
108+
109+
/**
110+
* @test
111+
*/
112+
public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void
113+
{
114+
$updatedBefore = new \DateTimeImmutable('2023-11-25T08:00:00Z');
115+
116+
$api = $this->getApiMock();
117+
$api->expects($this->once())
118+
->method('get')
119+
->with('groups/1/milestones', ['updated_before' => '2023-11-25T08:00:00.000Z'])
120+
;
121+
122+
$api->all(1, ['updated_before' => $updatedBefore]);
123+
}
124+
125+
/**
126+
* @test
127+
*/
128+
public function shouldGetAllMilestonesWithParameterUpdatedAfter(): void
129+
{
130+
$updatedAfter = new \DateTimeImmutable('2023-11-25T08:00:00Z');
131+
132+
$api = $this->getApiMock();
133+
$api->expects($this->once())
134+
->method('get')
135+
->with('groups/1/milestones', ['updated_after' => '2023-11-25T08:00:00.000Z'])
136+
;
137+
138+
$api->all(1, ['updated_after' => $updatedAfter]);
139+
}
140+
41141
/**
42142
* @test
43143
*/

0 commit comments

Comments
 (0)