Skip to content

Commit

Permalink
Merge pull request #297 from m-bymike/master
Browse files Browse the repository at this point in the history
fix issues with symfony options resolver
  • Loading branch information
fbourigault authored Feb 25, 2018
2 parents 461776a + bf708e5 commit 4aba39d
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/Gitlab/Api/Groups.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;

class Groups extends AbstractApi
{
/**
Expand All @@ -18,7 +20,7 @@ class Groups extends AbstractApi
public function all(array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function ($value) {
$booleanNormalizer = function (Options $resolver, $value) {
return $value ? 'true' : 'false';
};

Expand Down Expand Up @@ -180,7 +182,7 @@ public function removeMember($group_id, $user_id)
public function projects($id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function ($value) {
$booleanNormalizer = function (Options $resolver, $value) {
return $value ? 'true' : 'false';
};

Expand Down
3 changes: 2 additions & 1 deletion lib/Gitlab/Api/MergeRequests.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;

Expand Down Expand Up @@ -36,7 +37,7 @@ class MergeRequests extends AbstractApi
public function all($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$datetimeNormalizer = function (\DateTimeInterface $value) {
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
return $value->format('c');
};
$resolver->setDefined('iids')
Expand Down
7 changes: 4 additions & 3 deletions lib/Gitlab/Api/Projects.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -33,7 +34,7 @@ class Projects extends AbstractApi
public function all(array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function ($value) {
$booleanNormalizer = function (Options $resolver, $value) {
return $value ? 'true' : 'false';
};
$resolver->setDefined('archived')
Expand Down Expand Up @@ -171,7 +172,7 @@ public function unarchive($project_id)
public function pipelines($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$booleanNormalizer = function ($value) {
$booleanNormalizer = function (Options $resolver, $value) {
return $value ? 'true' : 'false';
};

Expand Down Expand Up @@ -430,7 +431,7 @@ public function enableDeployKey($project_id, $key_id)
public function events($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$datetimeNormalizer = function (\DateTimeInterface $value) {
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
return $value->format('Y-m-d');
};

Expand Down
5 changes: 3 additions & 2 deletions lib/Gitlab/Api/Repositories.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Repositories extends AbstractApi
Expand Down Expand Up @@ -148,7 +149,7 @@ public function updateRelease($project_id, $tag_name, $description)
public function commits($project_id, array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$datetimeNormalizer = function (\DateTimeInterface $value) {
$datetimeNormalizer = function (Options $options, \DateTimeInterface $value) {
return $value->format('c');
};

Expand Down Expand Up @@ -212,7 +213,7 @@ public function createCommit($project_id, array $parameters = [])
->setAllowedValues('actions', function (array $actions) {
return !empty($actions);
})
->setNormalizer('actions', function (OptionsResolver $resolver, array $actions) {
->setNormalizer('actions', function (Options $resolver, array $actions) {
$actionsOptionsResolver = new OptionsResolver();
$actionsOptionsResolver->setDefined('action')
->setRequired('action')
Expand Down
4 changes: 3 additions & 1 deletion lib/Gitlab/Api/Users.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\Options;

class Users extends AbstractApi
{
/**
Expand All @@ -21,7 +23,7 @@ class Users extends AbstractApi
public function all(array $parameters = [])
{
$resolver = $this->createOptionsResolver();
$datetimeNormalizer = function (\DateTimeInterface $value) {
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
return $value->format('c');
};

Expand Down
40 changes: 40 additions & 0 deletions test/Gitlab/Tests/Api/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,46 @@ public function shouldGetAllGroups()
$this->assertEquals($expectedArray, $api->all(['page' => 1, 'per_page' => 10]));
}

/**
* @test
*/
public function shouldGetAllGroupsWithBooleanParam()
{
$expectedArray = array(
array('id' => 1, 'name' => 'A group'),
array('id' => 2, 'name' => 'Another group'),
);

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

$this->assertEquals($expectedArray, $api->all(['all_available' => false]));
}

/**
* @test
*/
public function shouldGetAllGroupProjectsWithBooleanParam()
{
$expectedArray = array(
array('id' => 1, 'name' => 'A group'),
array('id' => 2, 'name' => 'Another group'),
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('groups/1/projects', ['archived' => 'false'])
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->projects(1, ['archived' => false]));
}

/**
* @test
*/
Expand Down
28 changes: 28 additions & 0 deletions test/Gitlab/Tests/Api/MergeRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ public function shouldGetAllWithParams()
$this->assertEquals($expectedArray, $api->all(1, ['page' => 2, 'per_page' => 5, 'order_by' => 'updated_at', 'sort' => 'desc']));
}

/**
* @test
*/
public function shouldGetAllWithDateTimeParams()
{
$expectedArray = $this->getMultipleMergeRequestsData();

$createdAfter = new \DateTime('2018-01-01 00:00:00');
$createdBefore = new \DateTime('2018-01-31 00:00:00');

$expectedWithArray = [
'created_after' => $createdAfter->format(DATE_ATOM),
'created_before' => $createdBefore->format(DATE_ATOM),
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/merge_requests', $expectedWithArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals(
$expectedArray,
$api->all(1, ['created_after' => $createdAfter, 'created_before' => $createdBefore])
);
}

/**
* @test
*/
Expand Down
61 changes: 61 additions & 0 deletions test/Gitlab/Tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public function shouldGetOwnedProjects()
$this->assertEquals($expectedArray, $api->all(['owned' => true]));
}

/**
* @test
*/
public function shouldGetNotArchivedProjects()
{
$expectedArray = $this->getMultipleProjectsData();

$api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['archived' => 'false']);

$this->assertEquals($expectedArray, $api->all(['archived' => false]));
}

/**
* @test
*/
Expand Down Expand Up @@ -228,6 +240,27 @@ public function shouldGetPipelines()
$this->assertEquals($expectedArray, $api->pipelines(1));
}

/**
* @test
*/
public function shouldGetPipelinesWithBooleanParam()
{
$expectedArray = array(
array('id' => 1, 'status' => 'success','ref' => 'new-pipeline'),
array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'),
array('id' => 3, 'status' => 'pending', 'ref'=> 'test-pipeline')
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/pipelines', ['yaml_errors' => 'false'])
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->pipelines(1, ['yaml_errors' => false]));
}

/**
* @test
*/
Expand Down Expand Up @@ -660,6 +693,34 @@ public function shouldGetEvents()
$this->assertEquals($expectedArray, $api->events(1));
}

/**
* @test
*/
public function shouldGetEventsWithDateTimeParams()
{
$expectedArray = [
['id' => 1, 'title' => 'An event'],
['id' => 2, 'title' => 'Another event']
];

$after = new \DateTime('2018-01-01 00:00:00');
$before = new \DateTime('2018-01-31 00:00:00');

$expectedWithArray = [
'after' => $after->format('Y-m-d'),
'before' => $before->format('Y-m-d'),
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/events', $expectedWithArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->events(1, ['after' => $after, 'before' => $before]));
}

/**
* @test
*/
Expand Down
28 changes: 28 additions & 0 deletions test/Gitlab/Tests/Api/RepositoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,34 @@ public function shouldGetCommitsWithParams()
$this->assertEquals($expectedArray, $api->commits(1, ['page' => 2, 'per_page' => 25, 'ref_name' => 'master']));
}

/**
* @test
*/
public function shouldGetCommitsWithTimeParams()
{
$expectedArray = [
['id' => 'abcd1234', 'title' => 'A commit'],
['id' => 'efgh5678', 'title' => 'Another commit']
];

$since = new \DateTime('2018-01-01 00:00:00');
$until = new \DateTime('2018-01-31 00:00:00');

$expectedWithArray = [
'since' => $since->format(DATE_ATOM),
'until' => $until->format(DATE_ATOM),
];

$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with('projects/1/repository/commits', $expectedWithArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->commits(1, ['since' => $since, 'until' => $until]));
}

/**
* @test
*/
Expand Down
31 changes: 31 additions & 0 deletions test/Gitlab/Tests/Api/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ public function shouldGetActiveUsers()
$this->assertEquals($expectedArray, $api->all(['active' => true]));
}

/**
* @test
*/
public function shouldGetUsersWithDateTimeParams()
{
$expectedArray = [
['id' => 1, 'name' => 'Matt'],
['id' => 2, 'name' => 'John'],
];

$createdAfter = new \DateTime('2018-01-01 00:00:00');
$createdBefore = new \DateTime('2018-01-31 00:00:00');

$expectedWithArray = [
'created_after' => $createdAfter->format(DATE_ATOM),
'created_before' => $createdBefore->format(DATE_ATOM),
];

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

$this->assertEquals(
$expectedArray,
$api->all(['created_after' => $createdAfter, 'created_before' => $createdBefore])
);
}

/**
* @test
*/
Expand Down

0 comments on commit 4aba39d

Please sign in to comment.