From 406a760ca4cee5c00e9106bef0622bf5bc0c9b0e Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Sat, 25 Nov 2023 13:21:25 -0500 Subject: [PATCH 1/8] accept the parameters updated_before and updated_after (Introduced in GitLab 15.10) --- src/Api/GroupsMilestones.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Api/GroupsMilestones.php b/src/Api/GroupsMilestones.php index f870f78c..e4a827e9 100644 --- a/src/Api/GroupsMilestones.php +++ b/src/Api/GroupsMilestones.php @@ -33,6 +33,10 @@ class GroupsMilestones extends AbstractApi * @var int[] $iids return only the milestones having the given iids * @var string $state return only active or closed milestones * @var string $search Return only milestones with a title or description matching the provided string. + * @var string $updated_before Return only milestones updated before the given datetime. + * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 + * @var string $updated_after Return only milestones updated after the given datetime. + * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 * } * * @return mixed @@ -50,6 +54,8 @@ public function all($group_id, array $parameters = []) ->setAllowedValues('state', [self::STATE_ACTIVE, self::STATE_CLOSED]) ; $resolver->setDefined('search'); + $resolver->setDefined('updated_before'); + $resolver->setDefined('updated_after'); return $this->get('groups/'.self::encodePath($group_id).'/milestones', $resolver->resolve($parameters)); } From d9dc2359668fa995dada5ce3702dd788b469444d Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Sat, 25 Nov 2023 13:22:15 -0500 Subject: [PATCH 2/8] =?UTF-8?q?add=20missing=20phpunit=20tests=20Groups=20?= =?UTF-8?q?Milestones=20(Gitlab\Tests\Api\GroupsMilestones)=20=20=E2=9C=94?= =?UTF-8?q?=20Should=20get=20all=20milestones=20with=20parameter=20one=20i?= =?UTF-8?q?ids=20value=20=20=E2=9C=94=20Should=20get=20all=20milestones=20?= =?UTF-8?q?with=20parameter=20two=20iids=20values=20=20=E2=9C=94=20Should?= =?UTF-8?q?=20get=20all=20milestones=20with=20parameter=20state=20with=20a?= =?UTF-8?q?ctive=20=20=E2=9C=94=20Should=20get=20all=20milestones=20with?= =?UTF-8?q?=20parameter=20state=20with=20closed=20=20=E2=9C=94=20Should=20?= =?UTF-8?q?get=20all=20milestones=20with=20parameter=20search=20=20?= =?UTF-8?q?=E2=9C=94=20Should=20get=20all=20milestones=20with=20parameter?= =?UTF-8?q?=20updated=20before=20=20=E2=9C=94=20Should=20get=20all=20miles?= =?UTF-8?q?tones=20with=20parameter=20updated=20after?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Api/GroupsMilestonesTest.php | 101 +++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index 3dc5d7a7..7d313857 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -38,6 +38,107 @@ public function shouldGetAllMilestones(): void $this->assertEquals($expectedArray, $api->all(1)); } + /** + * @test + */ + public function shouldGetAllMilestonesWithParameterOneIidsValue(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['iids' => [456]]) + ; + + $api->all(1, ['iids' => [456]]); + } + + /** + * @test + */ + public function shouldGetAllMilestonesWithParameterTwoIidsValues(): void + { + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['iids' => [456, 789]]) + ; + + $api->all(1, ['iids' => [456, 789]]); + } + + public function getAllMilestonesWithParameterStateDataProvider() + { + return [ + GroupsMilestones::STATE_ACTIVE => [GroupsMilestones::STATE_ACTIVE], + GroupsMilestones::STATE_CLOSED => [GroupsMilestones::STATE_CLOSED], + ]; + } + + /** + * @test + * @dataProvider getAllMilestonesWithParameterStateDataProvider + */ + public function shouldGetAllMilestonesWithParameterState(string $state): void + { + $updatedBefore = '2023-11-25T08:00:00Z'; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['state' => $state]) + ; + + $api->all(1, ['state' => $state]); + } + + /** + * @test + */ + public function shouldGetAllMilestonesWithParameterSearch(): void + { + $searchValue = 'abc'; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['search' => $searchValue]) + ; + + $api->all(1, ['search' => $searchValue]); + } + + /** + * @test + */ + public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void + { + $updatedBefore = '2023-11-25T08:00:00Z'; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['updated_before' => $updatedBefore]) + ; + + $api->all(1, ['updated_before' => $updatedBefore]); + } + + /** + * @test + */ + public function shouldGetAllMilestonesWithParameterUpdatedAfter(): void + { + $updatedBefore = '2023-11-25T08:00:00Z'; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('groups/1/milestones', ['updated_after' => $updatedBefore]) + ; + + $api->all(1, ['updated_after' => $updatedBefore]); + } + /** * @test */ From 944c16538ce839cd68792be90a1b0b20b3181fc5 Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Sat, 25 Nov 2023 13:29:34 -0500 Subject: [PATCH 3/8] fix code style --- src/Api/GroupsMilestones.php | 6 +++--- tests/Api/GroupsMilestonesTest.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Api/GroupsMilestones.php b/src/Api/GroupsMilestones.php index e4a827e9..3adf7d21 100644 --- a/src/Api/GroupsMilestones.php +++ b/src/Api/GroupsMilestones.php @@ -32,10 +32,10 @@ class GroupsMilestones extends AbstractApi * * @var int[] $iids return only the milestones having the given iids * @var string $state return only active or closed milestones - * @var string $search Return only milestones with a title or description matching the provided string. - * @var string $updated_before Return only milestones updated before the given datetime. + * @var string $search Return only milestones with a title or description matching the provided string + * @var string $updated_before Return only milestones updated before the given datetime * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 - * @var string $updated_after Return only milestones updated after the given datetime. + * @var string $updated_after Return only milestones updated after the given datetime * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 * } * diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index 7d313857..2f070e8c 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -76,6 +76,7 @@ public function getAllMilestonesWithParameterStateDataProvider() /** * @test + * * @dataProvider getAllMilestonesWithParameterStateDataProvider */ public function shouldGetAllMilestonesWithParameterState(string $state): void From 5d5acce82977e850c717ca7637eef10af34c5598 Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Sat, 25 Nov 2023 13:45:51 -0500 Subject: [PATCH 4/8] remove unused variable and rename meaningful variable --- tests/Api/GroupsMilestonesTest.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index 2f070e8c..bbbf84da 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -81,8 +81,6 @@ public function getAllMilestonesWithParameterStateDataProvider() */ public function shouldGetAllMilestonesWithParameterState(string $state): void { - $updatedBefore = '2023-11-25T08:00:00Z'; - $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') @@ -129,15 +127,15 @@ public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void */ public function shouldGetAllMilestonesWithParameterUpdatedAfter(): void { - $updatedBefore = '2023-11-25T08:00:00Z'; + $updatedAfter = '2023-11-25T08:00:00Z'; $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/milestones', ['updated_after' => $updatedBefore]) + ->with('groups/1/milestones', ['updated_after' => $updatedAfter]) ; - $api->all(1, ['updated_after' => $updatedBefore]); + $api->all(1, ['updated_after' => $updatedAfter]); } /** From 20599dbacb9f0715a23c743f4def246ae5faa943 Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Sat, 25 Nov 2023 13:58:10 -0500 Subject: [PATCH 5/8] Change the dataProvider function to static (deprecated in PHPUnit 10) --- tests/Api/GroupsMilestonesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index bbbf84da..7e40c545 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -66,7 +66,7 @@ public function shouldGetAllMilestonesWithParameterTwoIidsValues(): void $api->all(1, ['iids' => [456, 789]]); } - public function getAllMilestonesWithParameterStateDataProvider() + public static function getAllMilestonesWithParameterStateDataProvider() { return [ GroupsMilestones::STATE_ACTIVE => [GroupsMilestones::STATE_ACTIVE], From f35c0a180d7f545a55fded709a2d5e696ddf13a2 Mon Sep 17 00:00:00 2001 From: Wenchao Xue Date: Wed, 6 Dec 2023 08:46:11 -0500 Subject: [PATCH 6/8] use DateTimeInterface instead of string for $updated_after and $updated_before update unit tests --- src/Api/GroupsMilestones.php | 20 ++++++++++++++------ tests/Api/GroupsMilestonesTest.php | 8 ++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Api/GroupsMilestones.php b/src/Api/GroupsMilestones.php index 3adf7d21..970e8da1 100644 --- a/src/Api/GroupsMilestones.php +++ b/src/Api/GroupsMilestones.php @@ -14,6 +14,8 @@ namespace Gitlab\Api; +use Symfony\Component\OptionsResolver\Options; + class GroupsMilestones extends AbstractApi { /** @@ -33,10 +35,8 @@ class GroupsMilestones extends AbstractApi * @var int[] $iids return only the milestones having the given iids * @var string $state return only active or closed milestones * @var string $search Return only milestones with a title or description matching the provided string - * @var string $updated_before Return only milestones updated before the given datetime - * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 - * @var string $updated_after Return only milestones updated after the given datetime - * Expected in ISO 8601 format (2019-03-15T08:00:00Z). Introduced in GitLab 15.10 + * @var \DateTimeInterface $updated_after Return only milestones updated on or after the given datetime. Expected in ISO 8601 format (2019-03-15T08:00:00Z) + * @var \DateTimeInterface $updated_before Return only milestones updated on or before the given datetime. Expected in ISO 8601 format (2019-03-15T08:00:00Z) * } * * @return mixed @@ -44,6 +44,9 @@ class GroupsMilestones extends AbstractApi public function all($group_id, array $parameters = []) { $resolver = $this->createOptionsResolver(); + $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string { + return $value->format('c'); + }; $resolver->setDefined('iids') ->setAllowedTypes('iids', 'array') ->setAllowedValues('iids', function (array $value) { @@ -54,8 +57,13 @@ public function all($group_id, array $parameters = []) ->setAllowedValues('state', [self::STATE_ACTIVE, self::STATE_CLOSED]) ; $resolver->setDefined('search'); - $resolver->setDefined('updated_before'); - $resolver->setDefined('updated_after'); + + $resolver->setDefined('updated_after') + ->setAllowedTypes('updated_after', \DateTimeInterface::class) + ->setNormalizer('updated_after', $datetimeNormalizer); + $resolver->setDefined('updated_before') + ->setAllowedTypes('updated_before', \DateTimeInterface::class) + ->setNormalizer('updated_before', $datetimeNormalizer); return $this->get('groups/'.self::encodePath($group_id).'/milestones', $resolver->resolve($parameters)); } diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index 7e40c545..2e934a81 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -111,12 +111,12 @@ public function shouldGetAllMilestonesWithParameterSearch(): void */ public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void { - $updatedBefore = '2023-11-25T08:00:00Z'; + $updatedBefore = new \DateTimeImmutable('2023-11-25T08:00:00Z'); $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/milestones', ['updated_before' => $updatedBefore]) + ->with('groups/1/milestones', ['updated_before' => $updatedBefore->format('c')]) ; $api->all(1, ['updated_before' => $updatedBefore]); @@ -127,12 +127,12 @@ public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void */ public function shouldGetAllMilestonesWithParameterUpdatedAfter(): void { - $updatedAfter = '2023-11-25T08:00:00Z'; + $updatedAfter = new \DateTimeImmutable('2023-11-25T08:00:00Z'); $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/milestones', ['updated_after' => $updatedAfter]) + ->with('groups/1/milestones', ['updated_after' => $updatedAfter->format('c')]) ; $api->all(1, ['updated_after' => $updatedAfter]); From db9547f9b03e0a5fa0d3ffdb40da011368f2f0d3 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Mar 2024 21:21:14 +0000 Subject: [PATCH 7/8] Update GroupsMilestones.php --- src/Api/GroupsMilestones.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Api/GroupsMilestones.php b/src/Api/GroupsMilestones.php index 970e8da1..fad1c5d8 100644 --- a/src/Api/GroupsMilestones.php +++ b/src/Api/GroupsMilestones.php @@ -45,7 +45,9 @@ public function all($group_id, array $parameters = []) { $resolver = $this->createOptionsResolver(); $datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string { - return $value->format('c'); + $utc = (new \DateTimeImmutable($value->format(\DateTimeImmutable::RFC3339_EXTENDED)))->setTimezone(new \DateTimeZone('UTC')); + + return $utc->format('Y-m-d\TH:i:s.v\Z'); }; $resolver->setDefined('iids') ->setAllowedTypes('iids', 'array') From 7a13bcf8b1a09f80298712656dd963cb33acb363 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 17 Mar 2024 21:21:53 +0000 Subject: [PATCH 8/8] Update GroupsMilestonesTest.php --- tests/Api/GroupsMilestonesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Api/GroupsMilestonesTest.php b/tests/Api/GroupsMilestonesTest.php index 2e934a81..9fcd80cf 100644 --- a/tests/Api/GroupsMilestonesTest.php +++ b/tests/Api/GroupsMilestonesTest.php @@ -116,7 +116,7 @@ public function shouldGetAllMilestonesWithParameterUpdatedBefore(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/milestones', ['updated_before' => $updatedBefore->format('c')]) + ->with('groups/1/milestones', ['updated_before' => '2023-11-25T08:00:00.000Z']) ; $api->all(1, ['updated_before' => $updatedBefore]); @@ -132,7 +132,7 @@ public function shouldGetAllMilestonesWithParameterUpdatedAfter(): void $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('groups/1/milestones', ['updated_after' => $updatedAfter->format('c')]) + ->with('groups/1/milestones', ['updated_after' => '2023-11-25T08:00:00.000Z']) ; $api->all(1, ['updated_after' => $updatedAfter]);