Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/Api/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Niclas Hoyer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;

class Events extends AbstractApi
{
/**
* @param array $parameters {
*
* @var string $action include only events of a particular action type
* @var string $target_type include only events of a particular target type
* @var \DateTimeInterface $before include only events created before a particular date
* @var \DateTimeInterface $after include only events created after a particular date
* @var string $scope include all events across a user’s projects
* @var string $sort sort events in asc or desc order by created_at
*
* }
*
* @return mixed
*/
public function all(array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
return $value->format('Y-m-d');
};

$resolver->setDefined('action');
$resolver->setDefined('target_type');
$resolver->setDefined('before')
->setAllowedTypes('before', \DateTimeInterface::class)
->setNormalizer('before', $datetimeNormalizer)
;
$resolver->setDefined('after')
->setAllowedTypes('after', \DateTimeInterface::class)
->setNormalizer('after', $datetimeNormalizer)
;
$resolver->setDefined('scope');
$resolver->setDefined('sort');

return $this->get('events', $resolver->resolve($parameters));
}
}
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Gitlab\Api\DeployKeys;
use Gitlab\Api\Deployments;
use Gitlab\Api\Environments;
use Gitlab\Api\Events;
use Gitlab\Api\Groups;
use Gitlab\Api\GroupsBoards;
use Gitlab\Api\GroupsEpics;
Expand Down Expand Up @@ -174,6 +175,14 @@ public function environments(): Environments
return new Environments($this);
}

/**
* @return Events
*/
public function events(): Events
{
return new Events($this);
}

/**
* @return Groups
*/
Expand Down
64 changes: 64 additions & 0 deletions tests/Api/EventsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Niclas Hoyer <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Gitlab\Tests\Api;

use Gitlab\Api\Events;

class EventsTest extends TestCase
{
protected function getApiClass()
{
return Events::class;
}

/**
* @test
*/
public function shouldGetAllEvents(): void
{
$expectedArray = [
['id' => 1, 'target_type' => 'Issue'],
['id' => 2, 'target_type' => null],
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('events', [])
->will($this->returnValue($expectedArray))
;

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

/**
* @test
*/
public function shouldGetEventsAfter(): void
{
$expectedArray = [
['id' => 1, 'target_type' => 'Issue'],
['id' => 2, 'target_type' => null],
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('events', ['after' => '1970-01-01'])
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->all(['after' => new \DateTime('1970-01-01')]));
}
}