From 5266b821446b14b79a3c5a5c77c57791b985f0a9 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 28 Dec 2014 22:22:15 -0500 Subject: Add Gitlab webhook --- tests/units/ActionTaskCloseTest.php | 2 +- tests/units/ActionTest.php | 6 +- tests/units/GitlabWebhookTest.php | 116 ++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 tests/units/GitlabWebhookTest.php (limited to 'tests') diff --git a/tests/units/ActionTaskCloseTest.php b/tests/units/ActionTaskCloseTest.php index a5087af0..7f2c42de 100644 --- a/tests/units/ActionTaskCloseTest.php +++ b/tests/units/ActionTaskCloseTest.php @@ -7,7 +7,7 @@ use Model\Task; use Model\TaskCreation; use Model\TaskFinder; use Model\Project; -use Model\GithubWebhook; +use Integration\GithubWebhook; class ActionTaskCloseTest extends Base { diff --git a/tests/units/ActionTest.php b/tests/units/ActionTest.php index 77a939e0..429a181a 100644 --- a/tests/units/ActionTest.php +++ b/tests/units/ActionTest.php @@ -10,10 +10,10 @@ use Model\TaskPosition; use Model\TaskCreation; use Model\TaskFinder; use Model\Category; -use Model\GithubWebhook; +use Integration\GithubWebhook; class ActionTest extends Base -{/* +{ public function testSingleAction() { $tp = new TaskPosition($this->container); @@ -61,7 +61,7 @@ class ActionTest extends Base $this->assertEquals(4, $t1['column_id']); $this->assertEquals(0, $t1['is_active']); } -*/ + public function testMultipleActions() { $tp = new TaskPosition($this->container); diff --git a/tests/units/GitlabWebhookTest.php b/tests/units/GitlabWebhookTest.php new file mode 100644 index 00000000..0f2a5c12 --- /dev/null +++ b/tests/units/GitlabWebhookTest.php @@ -0,0 +1,116 @@ +container); + + $this->assertEquals(GitlabWebhook::TYPE_PUSH, $g->getType(json_decode($this->push_payload, true))); + $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode($this->issue_open_payload, true))); + $this->assertEquals(GitlabWebhook::TYPE_ISSUE, $g->getType(json_decode($this->issue_closed_payload, true))); + $this->assertEquals('', $g->getType(array())); + } + + public function testHandleCommit() + { + $g = new GitlabWebhook($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_COMMIT, function() {}); + + $event = json_decode($this->push_payload, true); + + // No task + $this->assertFalse($g->handleCommit($event['commits'][0])); + + // Create task with the wrong id + $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1))); + $this->assertFalse($g->handleCommit($event['commits'][0])); + + // Create task with the right id + $this->assertEquals(2, $tc->create(array('title' => 'test', 'project_id' => 1))); + $this->assertTrue($g->handleCommit($event['commits'][0])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_COMMIT.'.closure', $called); + } + + public function testHandleIssueOpened() + { + $g = new GitlabWebhook($this->container); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_OPENED, array($this, 'onOpen')); + + $event = json_decode($this->issue_open_payload, true); + $this->assertTrue($g->handleIssueOpened($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_OPENED.'.GitlabWebhookTest::onOpen', $called); + } + + public function testHandleIssueClosed() + { + $g = new GitlabWebhook($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $g->setProjectId(1); + + $this->container['dispatcher']->addListener(GitlabWebhook::EVENT_ISSUE_CLOSED, array($this, 'onClose')); + + $event = json_decode($this->issue_closed_payload, true); + + // Issue not there + $this->assertFalse($g->handleIssueClosed($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertEmpty($called); + + // Create a task with the issue reference + $this->assertEquals(1, $tc->create(array('title' => 'A', 'project_id' => 1, 'reference' => 103361))); + $task = $tf->getByReference(103361); + $this->assertNotEmpty($task); + + $this->assertTrue($g->handleIssueClosed($event['object_attributes'])); + + $called = $this->container['dispatcher']->getCalledListeners(); + $this->assertArrayHasKey(GitlabWebhook::EVENT_ISSUE_CLOSED.'.GitlabWebhookTest::onClose', $called); + } + + public function onOpen($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(103356, $data['reference']); + $this->assertEquals('Test Webhook', $data['title']); + $this->assertEquals("- test1\r\n- test2\n\n[Gitlab Issue](https://gitlab.com/minicoders/kanboard/issues/1)", $data['description']); + } + + public function onClose($event) + { + $data = $event->getAll(); + $this->assertEquals(1, $data['project_id']); + $this->assertEquals(1, $data['task_id']); + $this->assertEquals(103361, $data['reference']); + } +} -- cgit v1.2.3