Skip to content

Commit 8a534d0

Browse files
authored
Added user events (#557)
1 parent f8bd91b commit 8a534d0

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

lib/Gitlab/Api/Users.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,44 @@ public function removeImpersonationToken($user_id, $impersonation_token_id)
448448
{
449449
return $this->delete('users/'.$this->encodePath($user_id).'/impersonation_tokens/'.$this->encodePath($impersonation_token_id));
450450
}
451+
452+
/**
453+
* @param int $user_id
454+
* @param array $parameters {
455+
*
456+
* @var string $action include only events of a particular action type
457+
* @var string $target_type include only events of a particular target type
458+
* @var \DateTimeInterface $before include only events created before a particular date
459+
* @var \DateTimeInterface $after include only events created after a particular date
460+
* @var string $sort Sort events in asc or desc order by created_at (default is desc)
461+
* }
462+
*
463+
* @return mixed
464+
*/
465+
public function events($user_id, array $parameters = [])
466+
{
467+
$resolver = $this->createOptionsResolver();
468+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
469+
return $value->format('Y-m-d');
470+
};
471+
472+
$resolver->setDefined('action')
473+
->setAllowedValues('action', ['created', 'updated', 'closed', 'reopened', 'pushed', 'commented', 'merged', 'joined', 'left', 'destroyed', 'expired'])
474+
;
475+
$resolver->setDefined('target_type')
476+
->setAllowedValues('target_type', ['issue', 'milestone', 'merge_request', 'note', 'project', 'snippet', 'user'])
477+
;
478+
$resolver->setDefined('before')
479+
->setAllowedTypes('before', \DateTimeInterface::class)
480+
->setNormalizer('before', $datetimeNormalizer);
481+
$resolver->setDefined('after')
482+
->setAllowedTypes('after', \DateTimeInterface::class)
483+
->setNormalizer('after', $datetimeNormalizer)
484+
;
485+
$resolver->setDefined('sort')
486+
->setAllowedValues('sort', ['asc', 'desc'])
487+
;
488+
489+
return $this->get('users/'.$this->encodePath($user_id).'/events', $resolver->resolve($parameters));
490+
}
451491
}

test/Gitlab/Tests/Api/UsersTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,72 @@ protected function getApiClass()
723723
{
724724
return 'Gitlab\Api\Users';
725725
}
726+
727+
/**
728+
* @test
729+
*/
730+
public function shouldGetEvents()
731+
{
732+
$expectedArray = [
733+
['id' => 1, 'title' => 'An event'],
734+
['id' => 2, 'title' => 'Another event'],
735+
];
736+
737+
$api = $this->getApiMock();
738+
$api->expects($this->once())
739+
->method('get')
740+
->with('users/1/events', [])
741+
->will($this->returnValue($expectedArray));
742+
743+
$this->assertEquals($expectedArray, $api->events(1));
744+
}
745+
746+
/**
747+
* @test
748+
*/
749+
public function shouldGetEventsWithDateTimeParams()
750+
{
751+
$expectedArray = [
752+
['id' => 1, 'title' => 'An event'],
753+
['id' => 2, 'title' => 'Another event'],
754+
];
755+
756+
$after = new \DateTime('2018-01-01 00:00:00');
757+
$before = new \DateTime('2018-01-31 00:00:00');
758+
759+
$expectedWithArray = [
760+
'after' => $after->format('Y-m-d'),
761+
'before' => $before->format('Y-m-d'),
762+
];
763+
764+
$api = $this->getApiMock();
765+
$api->expects($this->once())
766+
->method('get')
767+
->with('users/1/events', $expectedWithArray)
768+
->will($this->returnValue($expectedArray));
769+
770+
$this->assertEquals($expectedArray, $api->events(1, ['after' => $after, 'before' => $before]));
771+
}
772+
773+
/**
774+
* @test
775+
*/
776+
public function shouldGetEventsWithPagination()
777+
{
778+
$expectedArray = [
779+
['id' => 1, 'title' => 'An event'],
780+
['id' => 2, 'title' => 'Another event'],
781+
];
782+
783+
$api = $this->getApiMock();
784+
$api->expects($this->once())
785+
->method('get')
786+
->with('users/1/events', [
787+
'page' => 2,
788+
'per_page' => 15,
789+
])
790+
->will($this->returnValue($expectedArray));
791+
792+
$this->assertEquals($expectedArray, $api->events(1, ['page' => 2, 'per_page' => 15]));
793+
}
726794
}

0 commit comments

Comments
 (0)