From 19d794c9d1ff793cc694a7057d4447f35cb6da61 Mon Sep 17 00:00:00 2001 From: AD7six Date: Tue, 24 Sep 2013 09:16:36 +0000 Subject: [PATCH] make it possible to list all labels and create them Allow the following syntax to work, from doc/issue/labels.md ```php $labels = $client->api('issue')->labels()->all('KnpLabs', 'php-github-api'); ``` Also add an interface for creating labels. --- lib/Github/Api/Issue/Labels.php | 18 +++++++- test/Github/Tests/Api/Issue/LabelsTest.php | 54 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/Github/Api/Issue/Labels.php b/lib/Github/Api/Issue/Labels.php index 55e68273d3a..ccb81b768cb 100644 --- a/lib/Github/Api/Issue/Labels.php +++ b/lib/Github/Api/Issue/Labels.php @@ -11,11 +11,27 @@ */ class Labels extends AbstractApi { - public function all($username, $repository, $issue) + public function all($username, $repository, $issue = null) { + if ($issue === null) { + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/labels'); + } + return $this->get('repos/'.urlencode($username).'/'.urlencode($repository).'/issues/'.urlencode($issue).'/labels'); } + public function create($username, $repository, array $params) + { + if (!isset($params['name'])) { + throw new MissingArgumentException('name'); + } + if (!isset($params['color'])) { + $params['color'] = 'FFFFFF'; + } + + return $this->post('repos/'.urlencode($username).'/'.urlencode($repository).'/labels', $params); + } + public function add($username, $repository, $issue, $labels) { if (is_string($labels)) { diff --git a/test/Github/Tests/Api/Issue/LabelsTest.php b/test/Github/Tests/Api/Issue/LabelsTest.php index 484f9be2a80..1bfa73c69ca 100644 --- a/test/Github/Tests/Api/Issue/LabelsTest.php +++ b/test/Github/Tests/Api/Issue/LabelsTest.php @@ -6,6 +6,26 @@ class LabelsTest extends TestCase { + + /** + * @test + */ + public function shouldGetProjectLabels() + { + $expectedValue = array( + array('name' => 'l3l0repo'), + array('name' => 'other'), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('repos/KnpLabs/php-github-api/labels', array()) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api')); + } + /** * @test */ @@ -22,6 +42,40 @@ public function shouldGetAllIssueLabels() $this->assertEquals($expectedValue, $api->all('KnpLabs', 'php-github-api', '123')); } + /** + * @test + */ + public function shouldCreateLabel() + { + $expectedValue = array(array('name' => 'label', 'color' => 'FFFFFF')); + $data = array('name' => 'label'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('repos/KnpLabs/php-github-api/labels', $data + array('color' => 'FFFFFF')) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data)); + } + + /** + * @test + */ + public function shouldCreateLabelWithColor() + { + $expectedValue = array(array('name' => 'label', 'color' => '111111')); + $data = array('name' => 'label', 'color' => '111111'); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('repos/KnpLabs/php-github-api/labels', $data) + ->will($this->returnValue($expectedValue)); + + $this->assertEquals($expectedValue, $api->create('KnpLabs', 'php-github-api', $data)); + } + /** * @test */