Skip to content

Commit 5db3f1b

Browse files
author
Matt Humphrey
committed
Issues tests
1 parent 4350eba commit 5db3f1b

File tree

2 files changed

+229
-3
lines changed

2 files changed

+229
-3
lines changed

lib/Gitlab/Api/Issues.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function all($project_id = null, $page = 1, $per_page = self::PER_PAGE, a
1313
{
1414
$path = $project_id === null ? 'issues' : $this->getProjectPath($project_id, 'issues');
1515

16-
$params = array_intersect_key($params, array('labels' => '', 'state' => ''));
16+
$params = array_intersect_key($params, array('labels' => '', 'state' => '', 'sort' => '', 'order_by' => ''));
1717
$params = array_merge(array(
1818
'page' => $page,
1919
'per_page' => $per_page
@@ -66,11 +66,43 @@ public function showComments($project_id, $issue_id)
6666
/**
6767
* @param int $project_id
6868
* @param int $issue_id
69-
* @param array $params
69+
* @param int $note_id
70+
* @return mixed
71+
*/
72+
public function showComment($project_id, $issue_id, $note_id)
73+
{
74+
return $this->get($this->getProjectPath($project_id, 'issues/'.urlencode($issue_id)).'/notes/'.urlencode($note_id));
75+
}
76+
77+
/**
78+
* @param int $project_id
79+
* @param int $issue_id
80+
* @param string|array $body
7081
* @return mixed
7182
*/
72-
public function addComment($project_id, $issue_id, array $params)
83+
public function addComment($project_id, $issue_id, $body)
7384
{
85+
// backwards compatibility
86+
if (is_array($body)) {
87+
$params = $body;
88+
} else {
89+
$params = array('body' => $body);
90+
}
91+
7492
return $this->post($this->getProjectPath($project_id, 'issues/'.urlencode($issue_id).'/notes'), $params);
7593
}
94+
95+
/**
96+
* @param int $project_id
97+
* @param int $issue_id
98+
* @param int $note_id
99+
* @param string $body
100+
* @return mixed
101+
*/
102+
public function updateComment($project_id, $issue_id, $note_id, $body)
103+
{
104+
return $this->put($this->getProjectPath($project_id, 'issues/'.urlencode($issue_id).'/notes/'.urlencode($note_id)), array(
105+
'body' => $body
106+
));
107+
}
76108
}

test/Gitlab/Tests/Api/IssuesTest.php

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
use Gitlab\Api\AbstractApi;
4+
5+
class IssuesTest extends TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
public function shouldGetAllIssues()
11+
{
12+
$expectedArray = array(
13+
array('id' => 1, 'title' => 'An issue'),
14+
array('id' => 2, 'title' => 'Another issue'),
15+
);
16+
17+
$api = $this->getApiMock();
18+
$api->expects($this->once())
19+
->method('get')
20+
->with('issues', array('page' => 1, 'per_page' => AbstractApi::PER_PAGE))
21+
->will($this->returnValue($expectedArray))
22+
;
23+
24+
$this->assertEquals($expectedArray, $api->all());
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function shouldGetProjectIssuesWithPagination()
31+
{
32+
$expectedArray = array(
33+
array('id' => 1, 'title' => 'An issue'),
34+
array('id' => 2, 'title' => 'Another issue'),
35+
);
36+
37+
$api = $this->getApiMock();
38+
$api->expects($this->once())
39+
->method('get')
40+
->with('projects/1/issues', array('page' => 2, 'per_page' => 5))
41+
->will($this->returnValue($expectedArray))
42+
;
43+
44+
$this->assertEquals($expectedArray, $api->all(1, 2, 5));
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function shouldGetProjectIssuesWithParams()
51+
{
52+
$expectedArray = array(
53+
array('id' => 1, 'title' => 'An issue'),
54+
array('id' => 2, 'title' => 'Another issue'),
55+
);
56+
57+
$api = $this->getApiMock();
58+
$api->expects($this->once())
59+
->method('get')
60+
->with('projects/1/issues', array('page' => 2, 'per_page' => 5, 'order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'open'))
61+
->will($this->returnValue($expectedArray))
62+
;
63+
64+
$this->assertEquals($expectedArray, $api->all(1, 2, 5, array('order_by' => 'created_at', 'sort' => 'desc', 'labels' => 'foo,bar', 'state' => 'open')));
65+
}
66+
67+
/**
68+
* @test
69+
*/
70+
public function shouldShowIssue()
71+
{
72+
$expectedArray = array('id' => 2, 'title' => 'Another issue');
73+
74+
$api = $this->getApiMock();
75+
$api->expects($this->once())
76+
->method('get')
77+
->with('projects/1/issues/2')
78+
->will($this->returnValue($expectedArray))
79+
;
80+
81+
$this->assertEquals($expectedArray, $api->show(1, 2));
82+
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function shouldCreateIssue()
88+
{
89+
$expectedArray = array('id' => 3, 'title' => 'A new issue');
90+
91+
$api = $this->getApiMock();
92+
$api->expects($this->once())
93+
->method('post')
94+
->with('projects/1/issues', array('title' => 'A new issue', 'labels' => 'foo,bar'))
95+
->will($this->returnValue($expectedArray))
96+
;
97+
98+
$this->assertEquals($expectedArray, $api->create(1, array('title' => 'A new issue', 'labels' => 'foo,bar')));
99+
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function shouldUpdateIssue()
105+
{
106+
$expectedArray = array('id' => 2, 'title' => 'A renamed issue');
107+
108+
$api = $this->getApiMock();
109+
$api->expects($this->once())
110+
->method('put')
111+
->with('projects/1/issues/2', array('title' => 'A renamed issue', 'labels' => 'foo'))
112+
->will($this->returnValue($expectedArray))
113+
;
114+
115+
$this->assertEquals($expectedArray, $api->update(1, 2, array('title' => 'A renamed issue', 'labels' => 'foo')));
116+
}
117+
118+
/**
119+
* @test
120+
*/
121+
public function shouldGetIssueComments()
122+
{
123+
$expectedArray = array(
124+
array('id' => 1, 'body' => 'A comment'),
125+
array('id' => 2, 'body' => 'Another comment')
126+
);
127+
128+
$api = $this->getApiMock();
129+
$api->expects($this->once())
130+
->method('get')
131+
->with('projects/1/issues/2/notes')
132+
->will($this->returnValue($expectedArray))
133+
;
134+
135+
$this->assertEquals($expectedArray, $api->showComments(1, 2));
136+
}
137+
138+
/**
139+
* @test
140+
*/
141+
public function shouldGetIssueComment()
142+
{
143+
$expectedArray = array('id' => 3, 'body' => 'A new comment');
144+
145+
$api = $this->getApiMock();
146+
$api->expects($this->once())
147+
->method('get')
148+
->with('projects/1/issues/2/notes/3')
149+
->will($this->returnValue($expectedArray))
150+
;
151+
152+
$this->assertEquals($expectedArray, $api->showComment(1, 2, 3));
153+
}
154+
155+
/**
156+
* @test
157+
*/
158+
public function shouldCreateComment()
159+
{
160+
$expectedArray = array('id' => 3, 'body' => 'A new comment');
161+
162+
$api = $this->getApiMock();
163+
$api->expects($this->exactly(2))
164+
->method('post')
165+
->with('projects/1/issues/2/notes', array('body' => 'A new comment'))
166+
->will($this->returnValue($expectedArray))
167+
;
168+
169+
$this->assertEquals($expectedArray, $api->addComment(1, 2, array('body' => 'A new comment')));
170+
$this->assertEquals($expectedArray, $api->addComment(1, 2, 'A new comment'));
171+
}
172+
173+
/**
174+
* @test
175+
*/
176+
public function shouldUpdateComment()
177+
{
178+
$expectedArray = array('id' => 3, 'body' => 'An edited comment');
179+
180+
$api = $this->getApiMock();
181+
$api->expects($this->once())
182+
->method('put')
183+
->with('projects/1/issues/2/notes/3', array('body' => 'An edited comment'))
184+
->will($this->returnValue($expectedArray))
185+
;
186+
187+
$this->assertEquals($expectedArray, $api->updateComment(1, 2, 3, 'An edited comment'));
188+
}
189+
190+
protected function getApiClass()
191+
{
192+
return 'Gitlab\Api\Issues';
193+
}
194+
}

0 commit comments

Comments
 (0)