Skip to content

Commit 4aba39d

Browse files
authored
Merge pull request #297 from m-bymike/master
fix issues with symfony options resolver
2 parents 461776a + bf708e5 commit 4aba39d

10 files changed

+204
-9
lines changed

lib/Gitlab/Api/Groups.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\Options;
4+
35
class Groups extends AbstractApi
46
{
57
/**
@@ -18,7 +20,7 @@ class Groups extends AbstractApi
1820
public function all(array $parameters = [])
1921
{
2022
$resolver = $this->createOptionsResolver();
21-
$booleanNormalizer = function ($value) {
23+
$booleanNormalizer = function (Options $resolver, $value) {
2224
return $value ? 'true' : 'false';
2325
};
2426

@@ -180,7 +182,7 @@ public function removeMember($group_id, $user_id)
180182
public function projects($id, array $parameters = [])
181183
{
182184
$resolver = $this->createOptionsResolver();
183-
$booleanNormalizer = function ($value) {
185+
$booleanNormalizer = function (Options $resolver, $value) {
184186
return $value ? 'true' : 'false';
185187
};
186188

lib/Gitlab/Api/MergeRequests.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\Options;
34
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
45
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
56

@@ -36,7 +37,7 @@ class MergeRequests extends AbstractApi
3637
public function all($project_id, array $parameters = [])
3738
{
3839
$resolver = $this->createOptionsResolver();
39-
$datetimeNormalizer = function (\DateTimeInterface $value) {
40+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
4041
return $value->format('c');
4142
};
4243
$resolver->setDefined('iids')

lib/Gitlab/Api/Projects.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\Options;
34
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
45
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
56
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -33,7 +34,7 @@ class Projects extends AbstractApi
3334
public function all(array $parameters = [])
3435
{
3536
$resolver = $this->createOptionsResolver();
36-
$booleanNormalizer = function ($value) {
37+
$booleanNormalizer = function (Options $resolver, $value) {
3738
return $value ? 'true' : 'false';
3839
};
3940
$resolver->setDefined('archived')
@@ -171,7 +172,7 @@ public function unarchive($project_id)
171172
public function pipelines($project_id, array $parameters = [])
172173
{
173174
$resolver = $this->createOptionsResolver();
174-
$booleanNormalizer = function ($value) {
175+
$booleanNormalizer = function (Options $resolver, $value) {
175176
return $value ? 'true' : 'false';
176177
};
177178

@@ -430,7 +431,7 @@ public function enableDeployKey($project_id, $key_id)
430431
public function events($project_id, array $parameters = [])
431432
{
432433
$resolver = $this->createOptionsResolver();
433-
$datetimeNormalizer = function (\DateTimeInterface $value) {
434+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
434435
return $value->format('Y-m-d');
435436
};
436437

lib/Gitlab/Api/Repositories.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\Options;
34
use Symfony\Component\OptionsResolver\OptionsResolver;
45

56
class Repositories extends AbstractApi
@@ -148,7 +149,7 @@ public function updateRelease($project_id, $tag_name, $description)
148149
public function commits($project_id, array $parameters = [])
149150
{
150151
$resolver = $this->createOptionsResolver();
151-
$datetimeNormalizer = function (\DateTimeInterface $value) {
152+
$datetimeNormalizer = function (Options $options, \DateTimeInterface $value) {
152153
return $value->format('c');
153154
};
154155

@@ -212,7 +213,7 @@ public function createCommit($project_id, array $parameters = [])
212213
->setAllowedValues('actions', function (array $actions) {
213214
return !empty($actions);
214215
})
215-
->setNormalizer('actions', function (OptionsResolver $resolver, array $actions) {
216+
->setNormalizer('actions', function (Options $resolver, array $actions) {
216217
$actionsOptionsResolver = new OptionsResolver();
217218
$actionsOptionsResolver->setDefined('action')
218219
->setRequired('action')

lib/Gitlab/Api/Users.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\Options;
4+
35
class Users extends AbstractApi
46
{
57
/**
@@ -21,7 +23,7 @@ class Users extends AbstractApi
2123
public function all(array $parameters = [])
2224
{
2325
$resolver = $this->createOptionsResolver();
24-
$datetimeNormalizer = function (\DateTimeInterface $value) {
26+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value) {
2527
return $value->format('c');
2628
};
2729

test/Gitlab/Tests/Api/GroupsTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@ public function shouldGetAllGroups()
2424
$this->assertEquals($expectedArray, $api->all(['page' => 1, 'per_page' => 10]));
2525
}
2626

27+
/**
28+
* @test
29+
*/
30+
public function shouldGetAllGroupsWithBooleanParam()
31+
{
32+
$expectedArray = array(
33+
array('id' => 1, 'name' => 'A group'),
34+
array('id' => 2, 'name' => 'Another group'),
35+
);
36+
37+
$api = $this->getApiMock();
38+
$api->expects($this->once())
39+
->method('get')
40+
->with('groups', ['all_available' => 'false'])
41+
->will($this->returnValue($expectedArray))
42+
;
43+
44+
$this->assertEquals($expectedArray, $api->all(['all_available' => false]));
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function shouldGetAllGroupProjectsWithBooleanParam()
51+
{
52+
$expectedArray = array(
53+
array('id' => 1, 'name' => 'A group'),
54+
array('id' => 2, 'name' => 'Another group'),
55+
);
56+
57+
$api = $this->getApiMock();
58+
$api->expects($this->once())
59+
->method('get')
60+
->with('groups/1/projects', ['archived' => 'false'])
61+
->will($this->returnValue($expectedArray))
62+
;
63+
64+
$this->assertEquals($expectedArray, $api->projects(1, ['archived' => false]));
65+
}
66+
2767
/**
2868
* @test
2969
*/

test/Gitlab/Tests/Api/MergeRequestsTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ public function shouldGetAllWithParams()
3939
$this->assertEquals($expectedArray, $api->all(1, ['page' => 2, 'per_page' => 5, 'order_by' => 'updated_at', 'sort' => 'desc']));
4040
}
4141

42+
/**
43+
* @test
44+
*/
45+
public function shouldGetAllWithDateTimeParams()
46+
{
47+
$expectedArray = $this->getMultipleMergeRequestsData();
48+
49+
$createdAfter = new \DateTime('2018-01-01 00:00:00');
50+
$createdBefore = new \DateTime('2018-01-31 00:00:00');
51+
52+
$expectedWithArray = [
53+
'created_after' => $createdAfter->format(DATE_ATOM),
54+
'created_before' => $createdBefore->format(DATE_ATOM),
55+
];
56+
57+
$api = $this->getApiMock();
58+
$api->expects($this->once())
59+
->method('get')
60+
->with('projects/1/merge_requests', $expectedWithArray)
61+
->will($this->returnValue($expectedArray))
62+
;
63+
64+
$this->assertEquals(
65+
$expectedArray,
66+
$api->all(1, ['created_after' => $createdAfter, 'created_before' => $createdBefore])
67+
);
68+
}
69+
4270
/**
4371
* @test
4472
*/

test/Gitlab/Tests/Api/ProjectsTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ public function shouldGetOwnedProjects()
7070
$this->assertEquals($expectedArray, $api->all(['owned' => true]));
7171
}
7272

73+
/**
74+
* @test
75+
*/
76+
public function shouldGetNotArchivedProjects()
77+
{
78+
$expectedArray = $this->getMultipleProjectsData();
79+
80+
$api = $this->getMultipleProjectsRequestMock('projects', $expectedArray, ['archived' => 'false']);
81+
82+
$this->assertEquals($expectedArray, $api->all(['archived' => false]));
83+
}
84+
7385
/**
7486
* @test
7587
*/
@@ -228,6 +240,27 @@ public function shouldGetPipelines()
228240
$this->assertEquals($expectedArray, $api->pipelines(1));
229241
}
230242

243+
/**
244+
* @test
245+
*/
246+
public function shouldGetPipelinesWithBooleanParam()
247+
{
248+
$expectedArray = array(
249+
array('id' => 1, 'status' => 'success','ref' => 'new-pipeline'),
250+
array('id' => 2, 'status' => 'failed', 'ref' => 'new-pipeline'),
251+
array('id' => 3, 'status' => 'pending', 'ref'=> 'test-pipeline')
252+
);
253+
254+
$api = $this->getApiMock();
255+
$api->expects($this->once())
256+
->method('get')
257+
->with('projects/1/pipelines', ['yaml_errors' => 'false'])
258+
->will($this->returnValue($expectedArray))
259+
;
260+
261+
$this->assertEquals($expectedArray, $api->pipelines(1, ['yaml_errors' => false]));
262+
}
263+
231264
/**
232265
* @test
233266
*/
@@ -660,6 +693,34 @@ public function shouldGetEvents()
660693
$this->assertEquals($expectedArray, $api->events(1));
661694
}
662695

696+
/**
697+
* @test
698+
*/
699+
public function shouldGetEventsWithDateTimeParams()
700+
{
701+
$expectedArray = [
702+
['id' => 1, 'title' => 'An event'],
703+
['id' => 2, 'title' => 'Another event']
704+
];
705+
706+
$after = new \DateTime('2018-01-01 00:00:00');
707+
$before = new \DateTime('2018-01-31 00:00:00');
708+
709+
$expectedWithArray = [
710+
'after' => $after->format('Y-m-d'),
711+
'before' => $before->format('Y-m-d'),
712+
];
713+
714+
$api = $this->getApiMock();
715+
$api->expects($this->once())
716+
->method('get')
717+
->with('projects/1/events', $expectedWithArray)
718+
->will($this->returnValue($expectedArray))
719+
;
720+
721+
$this->assertEquals($expectedArray, $api->events(1, ['after' => $after, 'before' => $before]));
722+
}
723+
663724
/**
664725
* @test
665726
*/

test/Gitlab/Tests/Api/RepositoriesTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,34 @@ public function shouldGetCommitsWithParams()
257257
$this->assertEquals($expectedArray, $api->commits(1, ['page' => 2, 'per_page' => 25, 'ref_name' => 'master']));
258258
}
259259

260+
/**
261+
* @test
262+
*/
263+
public function shouldGetCommitsWithTimeParams()
264+
{
265+
$expectedArray = [
266+
['id' => 'abcd1234', 'title' => 'A commit'],
267+
['id' => 'efgh5678', 'title' => 'Another commit']
268+
];
269+
270+
$since = new \DateTime('2018-01-01 00:00:00');
271+
$until = new \DateTime('2018-01-31 00:00:00');
272+
273+
$expectedWithArray = [
274+
'since' => $since->format(DATE_ATOM),
275+
'until' => $until->format(DATE_ATOM),
276+
];
277+
278+
$api = $this->getApiMock();
279+
$api->expects($this->once())
280+
->method('get')
281+
->with('projects/1/repository/commits', $expectedWithArray)
282+
->will($this->returnValue($expectedArray))
283+
;
284+
285+
$this->assertEquals($expectedArray, $api->commits(1, ['since' => $since, 'until' => $until]));
286+
}
287+
260288
/**
261289
* @test
262290
*/

test/Gitlab/Tests/Api/UsersTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ public function shouldGetActiveUsers()
4444
$this->assertEquals($expectedArray, $api->all(['active' => true]));
4545
}
4646

47+
/**
48+
* @test
49+
*/
50+
public function shouldGetUsersWithDateTimeParams()
51+
{
52+
$expectedArray = [
53+
['id' => 1, 'name' => 'Matt'],
54+
['id' => 2, 'name' => 'John'],
55+
];
56+
57+
$createdAfter = new \DateTime('2018-01-01 00:00:00');
58+
$createdBefore = new \DateTime('2018-01-31 00:00:00');
59+
60+
$expectedWithArray = [
61+
'created_after' => $createdAfter->format(DATE_ATOM),
62+
'created_before' => $createdBefore->format(DATE_ATOM),
63+
];
64+
65+
$api = $this->getApiMock();
66+
$api->expects($this->once())
67+
->method('get')
68+
->with('users', $expectedWithArray)
69+
->will($this->returnValue($expectedArray))
70+
;
71+
72+
$this->assertEquals(
73+
$expectedArray,
74+
$api->all(['created_after' => $createdAfter, 'created_before' => $createdBefore])
75+
);
76+
}
77+
4778
/**
4879
* @test
4980
*/

0 commit comments

Comments
 (0)