From 9d9e3afba2054bfa23ba6f019b7c8885c2d8415e Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 6 Jun 2015 14:10:31 -0400 Subject: Improve email sending system and add Postmark as mail transport --- tests/units/Base.php | 10 +++- tests/units/PostmarkTest.php | 106 ++++++++++++++++++++++++++++++++++++ tests/units/PostmarkWebhookTest.php | 83 ---------------------------- 3 files changed, 114 insertions(+), 85 deletions(-) create mode 100644 tests/units/PostmarkTest.php delete mode 100644 tests/units/PostmarkWebhookTest.php (limited to 'tests/units') diff --git a/tests/units/Base.php b/tests/units/Base.php index 20f4a8cc..d5fea977 100644 --- a/tests/units/Base.php +++ b/tests/units/Base.php @@ -15,6 +15,7 @@ class FakeHttpClient { private $url = ''; private $data = array(); + private $headers = array(); public function getUrl() { @@ -26,16 +27,21 @@ class FakeHttpClient return $this->data; } + public function getHeaders() + { + return $this->headers; + } + public function toPrettyJson() { return json_encode($this->data, JSON_PRETTY_PRINT); } - public function post($url, array $data) + public function post($url, array $data, array $headers = array()) { $this->url = $url; $this->data = $data; - //echo $this->toPrettyJson(); + $this->headers = $headers; return true; } } diff --git a/tests/units/PostmarkTest.php b/tests/units/PostmarkTest.php new file mode 100644 index 00000000..b708217d --- /dev/null +++ b/tests/units/PostmarkTest.php @@ -0,0 +1,106 @@ +container); + $pm->sendEmail('test@localhost', 'Me', 'Test', 'Content', 'Bob'); + + $this->assertEquals('https://api.postmarkapp.com/email', $this->container['httpClient']->getUrl()); + + $data = $this->container['httpClient']->getData(); + + $this->assertArrayHasKey('From', $data); + $this->assertArrayHasKey('To', $data); + $this->assertArrayHasKey('Subject', $data); + $this->assertArrayHasKey('HtmlBody', $data); + + $this->assertEquals('Me ', $data['To']); + $this->assertEquals('Bob ', $data['From']); + $this->assertEquals('Test', $data['Subject']); + $this->assertEquals('Content', $data['HtmlBody']); + + $this->assertContains('Accept: application/json', $this->container['httpClient']->getHeaders()); + $this->assertContains('X-Postmark-Server-Token: ', $this->container['httpClient']->getHeaders()); + } + + public function testHandlePayload() + { + $w = new Postmark($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); + + $this->assertEquals(1, $p->create(array('name' => 'test1'))); + $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + + // Empty payload + $this->assertFalse($w->receiveEmail(array())); + + // Unknown user + $this->assertFalse($w->receiveEmail(array('From' => 'a@b.c', 'Subject' => 'Email task', 'MailboxHash' => 'foobar', 'TextBody' => 'boo'))); + + // Project not found + $this->assertFalse($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test', 'TextBody' => 'boo'))); + + // User is not member + $this->assertFalse($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + $this->assertTrue($pp->addMember(2, 2)); + + // The task must be created + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(2, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('boo', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } + + public function testHtml2Markdown() + { + $w = new Postmark($this->container); + $p = new Project($this->container); + $pp = new ProjectPermission($this->container); + $u = new User($this->container); + $tc = new TaskCreation($this->container); + $tf = new TaskFinder($this->container); + + $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); + $this->assertEquals(1, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); + $this->assertTrue($pp->addMember(1, 2)); + + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo', 'HtmlBody' => '

boo

'))); + + $task = $tf->getById(1); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + + $this->assertTrue($w->receiveEmail(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => '**boo**', 'HtmlBody' => ''))); + + $task = $tf->getById(2); + $this->assertNotEmpty($task); + $this->assertEquals(1, $task['project_id']); + $this->assertEquals('Email task', $task['title']); + $this->assertEquals('**boo**', $task['description']); + $this->assertEquals(2, $task['creator_id']); + } +} diff --git a/tests/units/PostmarkWebhookTest.php b/tests/units/PostmarkWebhookTest.php deleted file mode 100644 index 34be8515..00000000 --- a/tests/units/PostmarkWebhookTest.php +++ /dev/null @@ -1,83 +0,0 @@ -container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - $tc = new TaskCreation($this->container); - $tf = new TaskFinder($this->container); - - $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); - - $this->assertEquals(1, $p->create(array('name' => 'test1'))); - $this->assertEquals(2, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); - - // Empty payload - $this->assertFalse($w->parsePayload(array())); - - // Unknown user - $this->assertFalse($w->parsePayload(array('From' => 'a@b.c', 'Subject' => 'Email task', 'MailboxHash' => 'foobar', 'TextBody' => 'boo'))); - - // Project not found - $this->assertFalse($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test', 'TextBody' => 'boo'))); - - // User is not member - $this->assertFalse($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); - $this->assertTrue($pp->addMember(2, 2)); - - // The task must be created - $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(2, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('boo', $task['description']); - $this->assertEquals(2, $task['creator_id']); - } - - public function testHtml2Markdown() - { - $w = new PostmarkWebhook($this->container); - $p = new Project($this->container); - $pp = new ProjectPermission($this->container); - $u = new User($this->container); - $tc = new TaskCreation($this->container); - $tf = new TaskFinder($this->container); - - $this->assertEquals(2, $u->create(array('name' => 'me', 'email' => 'me@localhost'))); - $this->assertEquals(1, $p->create(array('name' => 'test2', 'identifier' => 'TEST1'))); - $this->assertTrue($pp->addMember(1, 2)); - - $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => 'boo', 'HtmlBody' => '

boo

'))); - - $task = $tf->getById(1); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('**boo**', $task['description']); - $this->assertEquals(2, $task['creator_id']); - - $this->assertTrue($w->parsePayload(array('From' => 'me@localhost', 'Subject' => 'Email task', 'MailboxHash' => 'test1', 'TextBody' => '**boo**', 'HtmlBody' => ''))); - - $task = $tf->getById(2); - $this->assertNotEmpty($task); - $this->assertEquals(1, $task['project_id']); - $this->assertEquals('Email task', $task['title']); - $this->assertEquals('**boo**', $task['description']); - $this->assertEquals(2, $task['creator_id']); - } -} -- cgit v1.2.3