2017-05-16 16:20:33 +03:00
|
|
|
<?php
|
2016-08-05 02:50:13 +03:00
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
/*
|
2017-11-22 11:37:04 +03:00
|
|
|
* Copyright (C) 2013 Mailgun
|
2016-09-18 10:56:14 +03:00
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
2016-12-06 21:12:52 +03:00
|
|
|
* of the MIT license. See the LICENSE file for details.
|
2016-09-18 10:56:14 +03:00
|
|
|
*/
|
|
|
|
|
2016-08-05 02:50:13 +03:00
|
|
|
namespace Mailgun\Tests\Messages;
|
|
|
|
|
|
|
|
use Mailgun\Connection\RestClient;
|
2016-09-18 10:56:14 +03:00
|
|
|
use Mailgun\Tests\Mock\Mailgun;
|
2016-08-05 02:50:13 +03:00
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
class mockRestClient extends RestClient
|
|
|
|
{
|
|
|
|
public function send($method, $uri, $body = null, $files = [], array $headers = [])
|
|
|
|
{
|
|
|
|
$result = new \stdClass();
|
2016-08-05 02:50:13 +03:00
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
$result->method = $method;
|
|
|
|
$result->uri = $uri;
|
|
|
|
$result->body = $body;
|
|
|
|
$result->files = $files;
|
|
|
|
$result->headers = $headers;
|
2016-08-05 02:50:13 +03:00
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
return $result;
|
|
|
|
}
|
2016-08-05 02:50:13 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 10:56:14 +03:00
|
|
|
class mockMailgun extends Mailgun
|
|
|
|
{
|
|
|
|
public function __construct(
|
2016-08-05 02:50:13 +03:00
|
|
|
$apiKey = null,
|
|
|
|
HttpClient $httpClient = null,
|
|
|
|
$apiEndpoint = 'api.mailgun.net'
|
|
|
|
) {
|
2016-09-18 10:56:14 +03:00
|
|
|
$this->apiKey = $apiKey;
|
|
|
|
$this->restClient = new mockRestClient($apiKey, $apiEndpoint, $httpClient);
|
|
|
|
}
|
2016-08-05 02:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class ComplexMessageTest extends \Mailgun\Tests\MailgunTestCase
|
|
|
|
{
|
|
|
|
private $client;
|
|
|
|
private $sampleDomain = 'samples.mailgun.org';
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->client = new mockMailgun('My-Super-Awesome-API-Key');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSendComplexMessage()
|
|
|
|
{
|
|
|
|
$message = [
|
2016-10-15 03:20:46 +03:00
|
|
|
'to' => 'test@test.mailgun.org',
|
|
|
|
'from' => 'sender@test.mailgun.org',
|
2016-08-05 02:50:13 +03:00
|
|
|
'subject' => 'This is my test subject',
|
2016-10-15 03:20:46 +03:00
|
|
|
'text' => 'Testing!',
|
2016-08-05 02:50:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$files = [
|
|
|
|
'inline' => [
|
|
|
|
[
|
2016-09-18 10:56:14 +03:00
|
|
|
'remoteName' => 'mailgun_icon1.png',
|
2016-10-15 03:20:46 +03:00
|
|
|
'filePath' => 'tests/TestAssets/mailgun_icon1.png',
|
2016-08-05 02:50:13 +03:00
|
|
|
],
|
|
|
|
[
|
2016-09-18 10:56:14 +03:00
|
|
|
'remoteName' => 'mailgun_icon2.png',
|
2016-10-15 03:20:46 +03:00
|
|
|
'filePath' => 'tests/TestAssets/mailgun_icon2.png',
|
2016-09-18 10:56:14 +03:00
|
|
|
],
|
|
|
|
],
|
2016-08-05 02:50:13 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$result = $this->client->sendMessage('test.mailgun.org', $message, $files);
|
|
|
|
|
|
|
|
$this->assertEquals('POST', $result->method);
|
|
|
|
$this->assertEquals('test.mailgun.org/messages', $result->uri);
|
|
|
|
$this->assertEquals([], $result->body);
|
|
|
|
|
|
|
|
// Start a counter, make sure all files are asserted
|
|
|
|
$testCount = 0;
|
|
|
|
|
2017-04-08 02:24:37 +03:00
|
|
|
$expectedFilenames = ['mailgun_icon1.png', 'mailgun_icon2.png'];
|
2016-09-18 10:56:14 +03:00
|
|
|
foreach ($result->files as $file) {
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('to' == $file['name']) {
|
2016-09-18 10:56:14 +03:00
|
|
|
$this->assertEquals($file['contents'], 'test@test.mailgun.org');
|
2016-10-15 03:20:46 +03:00
|
|
|
++$testCount;
|
2016-09-18 10:56:14 +03:00
|
|
|
}
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('from' == $file['name']) {
|
2016-09-18 10:56:14 +03:00
|
|
|
$this->assertEquals($file['contents'], 'sender@test.mailgun.org');
|
2016-10-15 03:20:46 +03:00
|
|
|
++$testCount;
|
2016-09-18 10:56:14 +03:00
|
|
|
}
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('subject' == $file['name']) {
|
2016-09-18 10:56:14 +03:00
|
|
|
$this->assertEquals($file['contents'], 'This is my test subject');
|
2016-10-15 03:20:46 +03:00
|
|
|
++$testCount;
|
2016-09-18 10:56:14 +03:00
|
|
|
}
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('text' == $file['name']) {
|
2016-09-18 10:56:14 +03:00
|
|
|
$this->assertEquals($file['contents'], 'Testing!');
|
2016-10-15 03:20:46 +03:00
|
|
|
++$testCount;
|
2016-09-18 10:56:14 +03:00
|
|
|
}
|
2017-11-22 11:37:04 +03:00
|
|
|
if ('inline' == $file['name']) {
|
2017-04-08 02:24:37 +03:00
|
|
|
$expectedFilename = array_shift($expectedFilenames);
|
|
|
|
$this->assertNotNull($expectedFilename);
|
|
|
|
$this->assertSame($expectedFilename, $file['filename']);
|
2016-10-15 03:20:46 +03:00
|
|
|
++$testCount;
|
2016-09-18 10:56:14 +03:00
|
|
|
}
|
2016-08-05 02:50:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure all "files" are asserted
|
|
|
|
$this->assertEquals(count($result->files), $testCount);
|
|
|
|
|
|
|
|
$this->assertEquals([], $result->body);
|
|
|
|
$this->assertEquals([], $result->headers);
|
|
|
|
}
|
|
|
|
}
|