Skip to content

Commit 3787a1b

Browse files
committed
Merge pull request #75 from AD7six/feature/create-labels
Implement listing all labels, and creating labels
2 parents d68723a + 19d794c commit 3787a1b

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/Github/Api/Issue/Labels.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,27 @@
1111
*/
1212
class Labels extends AbstractApi
1313
{
14-
public function all($username, $repository, $issue)
14+
public function all($username, $repository, $issue = null)
1515
{
16+
if ($issue === null) {
17+
return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/labels');
18+
}
19+
1620
return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels');
1721
}
1822

23+
public function create($username, $repository, array $params)
24+
{
25+
if (!isset($params['name'])) {
26+
throw new MissingArgumentException('name');
27+
}
28+
if (!isset($params['color'])) {
29+
$params['color'] = 'FFFFFF';
30+
}
31+
32+
return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/labels', $params);
33+
}
34+
1935
public function add($username, $repository, $issue, $labels)
2036
{
2137
if (is_string($labels)) {

test/Github/Tests/Api/Issue/LabelsTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
class LabelsTest extends TestCase
88
{
9+
10+
/**
11+
* @test
12+
*/
13+
public function shouldGetProjectLabels()
14+
{
15+
$expectedValue = array(
16+
array('name' => 'l3l0repo'),
17+
array('name' => 'other'),
18+
);
19+
20+
$api = $this->getApiMock();
21+
$api->expects($this->once())
22+
->method('get')
23+
->with('repos/KnpLabs/php-github-api/labels', array())
24+
->will($this->returnValue($expectedValue));
25+
26+
$this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api'));
27+
}
28+
929
/**
1030
* @test
1131
*/
@@ -22,6 +42,40 @@ public function shouldGetAllIssueLabels()
2242
$this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api', '123'));
2343
}
2444

45+
/**
46+
* @test
47+
*/
48+
public function shouldCreateLabel()
49+
{
50+
$expectedValue = array(array('name' => 'label', 'color' => 'FFFFFF'));
51+
$data = array('name' => 'label');
52+
53+
$api = $this->getApiMock();
54+
$api->expects($this->once())
55+
->method('post')
56+
->with('repos/KnpLabs/php-github-api/labels', $data + array('color' => 'FFFFFF'))
57+
->will($this->returnValue($expectedValue));
58+
59+
$this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data));
60+
}
61+
62+
/**
63+
* @test
64+
*/
65+
public function shouldCreateLabelWithColor()
66+
{
67+
$expectedValue = array(array('name' => 'label', 'color' => '111111'));
68+
$data = array('name' => 'label', 'color' => '111111');
69+
70+
$api = $this->getApiMock();
71+
$api->expects($this->once())
72+
->method('post')
73+
->with('repos/KnpLabs/php-github-api/labels', $data)
74+
->will($this->returnValue($expectedValue));
75+
76+
$this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data));
77+
}
78+
2579
/**
2680
* @test
2781
*/

0 commit comments

Comments
 (0)