diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-05-18 21:04:06 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-05-18 21:04:06 -0400 |
commit | 6d5ffaa848dcbc09538f79cb0cdea08f5306f4a4 (patch) | |
tree | 2b782b7bc4b7f25cfa9021f7250450c7534f86cb /tests | |
parent | db95e96f92a15660dc043eb033a0f7737b59a030 (diff) |
Send all Kanboard events to the webhook (breaking change)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/units/Base.php | 30 | ||||
-rw-r--r-- | tests/units/TimetableTest.php | 39 | ||||
-rw-r--r-- | tests/units/WebhookTest.php | 112 |
3 files changed, 163 insertions, 18 deletions
diff --git a/tests/units/Base.php b/tests/units/Base.php index b6302942..20f4a8cc 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -11,6 +11,35 @@ use SimpleLogger\File; date_default_timezone_set('UTC'); +class FakeHttpClient +{ + private $url = ''; + private $data = array(); + + public function getUrl() + { + return $this->url; + } + + public function getData() + { + return $this->data; + } + + public function toPrettyJson() + { + return json_encode($this->data, JSON_PRETTY_PRINT); + } + + public function post($url, array $data) + { + $this->url = $url; + $this->data = $data; + //echo $this->toPrettyJson(); + return true; + } +} + abstract class Base extends PHPUnit_Framework_TestCase { protected $container; @@ -43,6 +72,7 @@ abstract class Base extends PHPUnit_Framework_TestCase $this->container['logger'] = new Logger; $this->container['logger']->setLogger(new File('/dev/null')); + $this->container['httpClient'] = new FakeHttpClient; } public function tearDown() diff --git a/tests/units/TimetableTest.php b/tests/units/TimetableTest.php index 2f38b534..5f698941 100644 --- a/tests/units/TimetableTest.php +++ b/tests/units/TimetableTest.php @@ -138,15 +138,15 @@ class TimetableTest extends Base $this->assertNotFalse($w->create(1, 2, '09:30', '12:00')); $this->assertNotFalse($w->create(1, 2, '13:00', '17:00')); - $monday = new DateTime('Monday'); - $tuesday = new DateTime('Tuesday'); + $monday = new DateTime('next Monday'); + $tuesday = new DateTime('next Monday + 1 day'); $timetable = $t->calculate(1, new DateTime('Monday'), new DateTime('Monday + 6 days')); $this->assertNotEmpty($timetable); $this->assertCount(4, $timetable); // Start to work before timetable - $date = new DateTime('Monday'); + $date = clone($monday); $date->setTime(5, 02); $slot = $t->findClosestTimeSlot($date, $timetable); @@ -155,7 +155,7 @@ class TimetableTest extends Base $this->assertEquals($monday->format('Y-m-d').' 12:00', $slot[1]->format('Y-m-d H:i')); // Start to work at the end of the timeslot - $date = new DateTime('Monday'); + $date = clone($monday); $date->setTime(12, 02); $slot = $t->findClosestTimeSlot($date, $timetable); @@ -164,7 +164,7 @@ class TimetableTest extends Base $this->assertEquals($monday->format('Y-m-d').' 12:00', $slot[1]->format('Y-m-d H:i')); // Start to work at lunch time - $date = new DateTime('Monday'); + $date = clone($monday); $date->setTime(12, 32); $slot = $t->findClosestTimeSlot($date, $timetable); @@ -173,7 +173,7 @@ class TimetableTest extends Base $this->assertEquals($monday->format('Y-m-d').' 17:00', $slot[1]->format('Y-m-d H:i')); // Start to work early in the morning - $date = new DateTime('Tuesday'); + $date = clone($tuesday); $date->setTime(8, 02); $slot = $t->findClosestTimeSlot($date, $timetable); @@ -192,47 +192,50 @@ class TimetableTest extends Base $this->assertNotFalse($w->create(1, 2, '09:30', '12:00')); $this->assertNotFalse($w->create(1, 2, '13:00', '17:00')); + $monday = new DateTime('next Monday'); + $tuesday = new DateTime('next Monday + 1 day'); + // Different day - $start = new DateTime('Monday'); + $start = clone($monday); $start->setTime(16, 02); - $end = new DateTime('Tuesday'); + $end = clone($tuesday); $end->setTime(10, 03); $this->assertEquals(1.5, $t->calculateEffectiveDuration(1, $start, $end)); // Same time slot - $start = new DateTime('Monday'); + $start = clone($monday); $start->setTime(16, 02); - $end = new DateTime('Monday'); + $end = clone($monday); $end->setTime(17, 03); $this->assertEquals(1, $t->calculateEffectiveDuration(1, $start, $end)); // Intermediate time slot - $start = new DateTime('Monday'); + $start = clone($monday); $start->setTime(10, 02); - $end = new DateTime('Tuesday'); + $end = clone($tuesday); $end->setTime(16, 03); $this->assertEquals(11.5, $t->calculateEffectiveDuration(1, $start, $end)); // Different day - $start = new DateTime('Monday'); + $start = clone($monday); $start->setTime(9, 02); - $end = new DateTime('Tuesday'); + $end = clone($tuesday); $end->setTime(10, 03); $this->assertEquals(7, $t->calculateEffectiveDuration(1, $start, $end)); // Start before first time slot - $start = new DateTime('Monday'); + $start = clone($monday); $start->setTime(5, 32); - $end = new DateTime('Tuesday'); + $end = clone($tuesday); $end->setTime(11, 17); $this->assertEquals(8.25, $t->calculateEffectiveDuration(1, $start, $end)); @@ -242,10 +245,10 @@ class TimetableTest extends Base { $t = new Timetable($this->container); - $start = new DateTime('Monday'); + $start = new DateTime('next Monday'); $start->setTime(16, 02); - $end = new DateTime('Monday'); + $end = new DateTime('next Monday'); $end->setTime(17, 03); $this->assertEquals(1, $t->calculateEffectiveDuration(1, $start, $end)); diff --git a/tests/units/WebhookTest.php b/tests/units/WebhookTest.php new file mode 100644 index 00000000..946d744c --- /dev/null +++ b/tests/units/WebhookTest.php @@ -0,0 +1,112 @@ +<?php + +require_once __DIR__.'/Base.php'; + +use Model\Config; +use Model\Task; +use Model\TaskCreation; +use Model\TaskModification; +use Model\Project; +use Model\Comment; +use Subscriber\WebhookSubscriber; + +class WebhookTest extends Base +{ + public function testTaskCreation() + { + $c = new Config($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $this->container['dispatcher']->addSubscriber(new WebhookSubscriber($this->container)); + + $c->save(array('webhook_url' => 'http://localhost/?task-creation')); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); + + $this->assertStringStartsWith('http://localhost/?task-creation&token=', $this->container['httpClient']->getUrl()); + + $event = $this->container['httpClient']->getData(); + $this->assertNotEmpty($event); + $this->assertArrayHasKey('event_name', $event); + $this->assertArrayHasKey('event_data', $event); + $this->assertEquals('task.create', $event['event_name']); + $this->assertNotEmpty($event['event_data']); + + $this->assertArrayHasKey('project_id', $event['event_data']); + $this->assertArrayHasKey('task_id', $event['event_data']); + $this->assertArrayHasKey('title', $event['event_data']); + $this->assertArrayHasKey('column_id', $event['event_data']); + $this->assertArrayHasKey('color_id', $event['event_data']); + $this->assertArrayHasKey('swimlane_id', $event['event_data']); + $this->assertArrayHasKey('date_creation', $event['event_data']); + $this->assertArrayHasKey('date_modification', $event['event_data']); + $this->assertArrayHasKey('date_moved', $event['event_data']); + $this->assertArrayHasKey('position', $event['event_data']); + } + + public function testTaskModification() + { + $c = new Config($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $tm = new TaskModification($this->container); + $this->container['dispatcher']->addSubscriber(new WebhookSubscriber($this->container)); + + $c->save(array('webhook_url' => 'http://localhost/modif/')); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertTrue($tm->update(array('id' => 1, 'title' => 'test update'))); + + $this->assertStringStartsWith('http://localhost/modif/?token=', $this->container['httpClient']->getUrl()); + + $event = $this->container['httpClient']->getData(); + $this->assertNotEmpty($event); + $this->assertArrayHasKey('event_name', $event); + $this->assertArrayHasKey('event_data', $event); + $this->assertEquals('task.update', $event['event_name']); + $this->assertNotEmpty($event['event_data']); + + $this->assertArrayHasKey('project_id', $event['event_data']); + $this->assertArrayHasKey('task_id', $event['event_data']); + $this->assertArrayHasKey('title', $event['event_data']); + $this->assertArrayHasKey('column_id', $event['event_data']); + $this->assertArrayHasKey('color_id', $event['event_data']); + $this->assertArrayHasKey('swimlane_id', $event['event_data']); + $this->assertArrayHasKey('date_creation', $event['event_data']); + $this->assertArrayHasKey('date_modification', $event['event_data']); + $this->assertArrayHasKey('date_moved', $event['event_data']); + $this->assertArrayHasKey('position', $event['event_data']); + } + + public function testCommentCreation() + { + $c = new Config($this->container); + $p = new Project($this->container); + $tc = new TaskCreation($this->container); + $cm = new Comment($this->container); + $this->container['dispatcher']->addSubscriber(new WebhookSubscriber($this->container)); + + $c->save(array('webhook_url' => 'http://localhost/comment')); + + $this->assertEquals(1, $p->create(array('name' => 'test'))); + $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test'))); + $this->assertEquals(1, $cm->create(array('task_id' => 1, 'comment' => 'test comment', 'user_id' => 1))); + + $this->assertStringStartsWith('http://localhost/comment?token=', $this->container['httpClient']->getUrl()); + + $event = $this->container['httpClient']->getData(); + $this->assertNotEmpty($event); + $this->assertArrayHasKey('event_name', $event); + $this->assertArrayHasKey('event_data', $event); + $this->assertEquals('comment.create', $event['event_name']); + $this->assertNotEmpty($event['event_data']); + + $this->assertArrayHasKey('task_id', $event['event_data']); + $this->assertArrayHasKey('user_id', $event['event_data']); + $this->assertArrayHasKey('comment', $event['event_data']); + $this->assertArrayHasKey('id', $event['event_data']); + $this->assertEquals('test comment', $event['event_data']['comment']); + } +} |