diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-02-07 12:32:47 -0500 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-02-07 12:32:47 -0500 |
commit | b34e151d6a9f2d18d8f3baea3bcbae97abee4cc8 (patch) | |
tree | 8e779c1a4e79d539223b609d0c92a772a891d185 | |
parent | 7b9e8897ebbebe4dfca596aa9bdb7ae81ab3f147 (diff) |
Use mock object instead of FakeHttpClient class
-rw-r--r-- | tests/units/Base.php | 51 | ||||
-rw-r--r-- | tests/units/Core/Http/OAuth2Test.php | 28 | ||||
-rw-r--r-- | tests/units/ExternalLink/WebLinkTest.php | 12 | ||||
-rw-r--r-- | tests/units/Notification/WebhookTest.php | 88 |
4 files changed, 30 insertions, 149 deletions
diff --git a/tests/units/Base.php b/tests/units/Base.php index bfcef418..22c6304f 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -12,49 +12,6 @@ use Kanboard\Core\Session\FlashMessage; use Kanboard\Core\Session\SessionStorage; use Kanboard\ServiceProvider\ActionProvider; -class FakeHttpClient -{ - private $url = ''; - private $data = array(); - private $headers = array(); - - public function getUrl() - { - return $this->url; - } - - public function getData() - { - return $this->data; - } - - public function getHeaders() - { - return $this->headers; - } - - public function toPrettyJson() - { - return json_encode($this->data, JSON_PRETTY_PRINT); - } - - public function postJson($url, array $data, array $headers = array()) - { - $this->url = $url; - $this->data = $data; - $this->headers = $headers; - return true; - } - - public function postForm($url, array $data, array $headers = array()) - { - $this->url = $url; - $this->data = $data; - $this->headers = $headers; - return true; - } -} - abstract class Base extends PHPUnit_Framework_TestCase { protected $container; @@ -91,7 +48,13 @@ abstract class Base extends PHPUnit_Framework_TestCase $this->container['logger'] = new Logger; $this->container['logger']->setLogger(new File($this->isWindows() ? 'NUL' : '/dev/null')); - $this->container['httpClient'] = new FakeHttpClient; + + $this->container['httpClient'] = $this + ->getMockBuilder('\Kanboard\Core\Http\Client') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('get', 'getJson', 'postJson', 'postForm')) + ->getMock(); + $this->container['emailClient'] = $this ->getMockBuilder('\Kanboard\Core\Mail\Client') ->setConstructorArgs(array($this->container)) diff --git a/tests/units/Core/Http/OAuth2Test.php b/tests/units/Core/Http/OAuth2Test.php index d703dd7a..c68ae116 100644 --- a/tests/units/Core/Http/OAuth2Test.php +++ b/tests/units/Core/Http/OAuth2Test.php @@ -27,17 +27,27 @@ class OAuth2Test extends Base public function testAccessToken() { + $params = array( + 'code' => 'something', + 'client_id' => 'A', + 'client_secret' => 'B', + 'redirect_uri' => 'C', + 'grant_type' => 'authorization_code', + ); + + $response = json_encode(array( + 'token_type' => 'bearer', + 'access_token' => 'plop', + )); + + $this->container['httpClient'] + ->expects($this->once()) + ->method('postForm') + ->with('E', $params, array('Accept: application/json')) + ->will($this->returnValue($response)); + $oauth = new OAuth2($this->container); $oauth->createService('A', 'B', 'C', 'D', 'E', array('f', 'g')); $oauth->getAccessToken('something'); - - $data = $this->container['httpClient']->getData(); - $this->assertEquals('something', $data['code']); - $this->assertEquals('A', $data['client_id']); - $this->assertEquals('B', $data['client_secret']); - $this->assertEquals('C', $data['redirect_uri']); - $this->assertEquals('authorization_code', $data['grant_type']); - - $this->assertEquals('E', $this->container['httpClient']->getUrl()); } } diff --git a/tests/units/ExternalLink/WebLinkTest.php b/tests/units/ExternalLink/WebLinkTest.php index 0644620f..85487e15 100644 --- a/tests/units/ExternalLink/WebLinkTest.php +++ b/tests/units/ExternalLink/WebLinkTest.php @@ -12,12 +12,6 @@ class WebLinkTest extends Base $title = 'My title'; $html = '<!DOCTYPE html><html><head><title> '.$title.' </title></head><body>Test</body></html>'; - $this->container['httpClient'] = $this - ->getMockBuilder('\Kanboard\Core\Http\Client') - ->setConstructorArgs(array($this->container)) - ->setMethods(array('get')) - ->getMock(); - $webLink = new WebLink($this->container); $webLink->setUrl($url); $this->assertEquals($url, $webLink->getUrl()); @@ -36,12 +30,6 @@ class WebLinkTest extends Base $url = 'http://kanboard.net/something'; $html = '<!DOCTYPE html><html><head></head><body>Test</body></html>'; - $this->container['httpClient'] = $this - ->getMockBuilder('\Kanboard\Core\Http\Client') - ->setConstructorArgs(array($this->container)) - ->setMethods(array('get')) - ->getMock(); - $webLink = new WebLink($this->container); $webLink->setUrl($url); $this->assertEquals($url, $webLink->getUrl()); diff --git a/tests/units/Notification/WebhookTest.php b/tests/units/Notification/WebhookTest.php index b55cd6e2..7215baeb 100644 --- a/tests/units/Notification/WebhookTest.php +++ b/tests/units/Notification/WebhookTest.php @@ -21,92 +21,12 @@ class WebhookTest extends Base $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']['task']); - $this->assertArrayHasKey('id', $event['event_data']['task']); - $this->assertArrayHasKey('title', $event['event_data']['task']); - $this->assertArrayHasKey('column_id', $event['event_data']['task']); - $this->assertArrayHasKey('color_id', $event['event_data']['task']); - $this->assertArrayHasKey('swimlane_id', $event['event_data']['task']); - $this->assertArrayHasKey('date_creation', $event['event_data']['task']); - $this->assertArrayHasKey('date_modification', $event['event_data']['task']); - $this->assertArrayHasKey('date_moved', $event['event_data']['task']); - $this->assertArrayHasKey('position', $event['event_data']['task']); - } - - 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 NotificationSubscriber($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']['task']); - $this->assertArrayHasKey('id', $event['event_data']['task']); - $this->assertArrayHasKey('title', $event['event_data']['task']); - $this->assertArrayHasKey('column_id', $event['event_data']['task']); - $this->assertArrayHasKey('color_id', $event['event_data']['task']); - $this->assertArrayHasKey('swimlane_id', $event['event_data']['task']); - $this->assertArrayHasKey('date_creation', $event['event_data']['task']); - $this->assertArrayHasKey('date_modification', $event['event_data']['task']); - $this->assertArrayHasKey('date_moved', $event['event_data']['task']); - $this->assertArrayHasKey('position', $event['event_data']['task']); - } - - 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 NotificationSubscriber($this->container)); - - $c->save(array('webhook_url' => 'http://localhost/comment')); + $this->container['httpClient'] + ->expects($this->once()) + ->method('postJson') + ->with($this->stringContains('http://localhost/?task-creation&token='), $this->anything()); $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']['comment']); - $this->assertArrayHasKey('user_id', $event['event_data']['comment']); - $this->assertArrayHasKey('comment', $event['event_data']['comment']); - $this->assertArrayHasKey('id', $event['event_data']['comment']); - $this->assertEquals('test comment', $event['event_data']['comment']['comment']); } } |