2017-04-14 11:31:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2017-11-22 11:37:04 +03:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2017-04-14 11:31:13 +03:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mailgun\Tests\Api;
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use Mailgun\Api\Message;
|
2018-08-05 12:33:47 +03:00
|
|
|
use Mailgun\Model\Message\SendResponse;
|
|
|
|
use Mailgun\Model\Message\ShowResponse;
|
2017-04-14 11:31:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
|
|
*/
|
|
|
|
class MessageTest extends TestCase
|
|
|
|
{
|
2018-08-05 12:33:47 +03:00
|
|
|
public function testSend()
|
|
|
|
{
|
|
|
|
$this->setRequestMethod('POST');
|
|
|
|
$this->setRequestUri('/v3/example.com/messages');
|
|
|
|
$this->setHydrateClass(SendResponse::class);
|
|
|
|
$this->setRequestBody([
|
2018-08-05 13:00:05 +03:00
|
|
|
'from' => 'bob@example.com',
|
|
|
|
'to' => 'sally@example.com',
|
2018-08-05 12:33:47 +03:00
|
|
|
'subject' => 'Test file path attachments',
|
2018-08-05 13:00:05 +03:00
|
|
|
'text' => 'Test',
|
2018-08-05 12:33:47 +03:00
|
|
|
'attachment' => 'resource',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$api = $this->getApiInstance();
|
|
|
|
$api->send('example.com', [
|
2018-08-05 13:00:05 +03:00
|
|
|
'from' => 'bob@example.com',
|
|
|
|
'to' => 'sally@example.com',
|
2018-08-05 12:33:47 +03:00
|
|
|
'subject' => 'Test file path attachments',
|
2018-08-05 13:00:05 +03:00
|
|
|
'text' => 'Test',
|
2018-08-05 12:33:47 +03:00
|
|
|
'attachment' => [
|
2018-08-05 13:00:05 +03:00
|
|
|
['filePath' => __DIR__.'/../TestAssets/mailgun_icon1.png', 'filename' => 'mailgun_icon1.png'],
|
|
|
|
],
|
2018-08-05 12:33:47 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:31:13 +03:00
|
|
|
public function testSendMime()
|
|
|
|
{
|
|
|
|
$api = $this->getApiMock();
|
|
|
|
$api->expects($this->once())
|
|
|
|
->method('httpPostRaw')
|
|
|
|
->with('/v3/foo/messages.mime',
|
|
|
|
$this->callback(function ($multipartBody) {
|
|
|
|
$parameters = ['o:Foo' => 'bar', 'to' => 'mailbox@myapp.com'];
|
|
|
|
|
|
|
|
// Verify all parameters
|
|
|
|
foreach ($parameters as $name => $content) {
|
|
|
|
$found = false;
|
|
|
|
foreach ($multipartBody as $body) {
|
|
|
|
if ($body['name'] === $name && $body['content'] === $content) {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($multipartBody as $body) {
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('message' === $body['name']) {
|
2017-04-14 11:31:13 +03:00
|
|
|
// Make sure message exists.
|
|
|
|
$found = true;
|
|
|
|
// Make sure content is what we expect
|
2019-01-05 10:23:41 +03:00
|
|
|
if (!isset($body['content'])) {
|
2017-04-14 11:31:13 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}))
|
|
|
|
->willReturn(new Response());
|
|
|
|
|
|
|
|
$api->sendMime('foo', ['mailbox@myapp.com'], 'mime message', ['o:Foo' => 'bar']);
|
|
|
|
}
|
|
|
|
|
2018-08-05 12:33:47 +03:00
|
|
|
public function testShow()
|
|
|
|
{
|
|
|
|
$this->setRequestMethod('GET');
|
|
|
|
$this->setRequestUri('url');
|
|
|
|
$this->setHydrateClass(ShowResponse::class);
|
|
|
|
|
|
|
|
$api = $this->getApiInstance();
|
|
|
|
$api->show('url');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowRaw()
|
|
|
|
{
|
|
|
|
$this->setRequestMethod('GET');
|
|
|
|
$this->setRequestUri('url');
|
|
|
|
$this->setRequestHeaders([
|
2018-08-05 13:00:05 +03:00
|
|
|
'Accept' => 'message/rfc2822',
|
2018-08-05 12:33:47 +03:00
|
|
|
]);
|
|
|
|
$this->setHydrateClass(ShowResponse::class);
|
|
|
|
|
|
|
|
$api = $this->getApiInstance();
|
|
|
|
$api->show('url', true);
|
|
|
|
}
|
|
|
|
|
2017-04-14 11:31:13 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function getApiClass()
|
|
|
|
{
|
|
|
|
return Message::class;
|
|
|
|
}
|
|
|
|
}
|