From 0a7c96c34ab8bcdf00fdf13e62c796b71229800b Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Thu, 4 Aug 2016 18:50:13 -0500 Subject: [PATCH] Fixed inline image issue. Added new tests. --- .gitignore | 1 + src/Mailgun/Connection/RestClient.php | 10 +- .../Tests/Messages/ComplexMessageTest.php | 111 ++++++++++++++++++ 3 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 tests/Mailgun/Tests/Messages/ComplexMessageTest.php diff --git a/.gitignore b/.gitignore index dc48fa8..dddbbe4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ build composer.lock nbproject/* .idea +phpunit.phar diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index c93d663..af1dfe3 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -119,8 +119,10 @@ class RestClient foreach ($fields as $fieldName) { if (isset($files[$fieldName])) { if (is_array($files[$fieldName])) { + $fileIndex = 0; foreach ($files[$fieldName] as $file) { - $postFiles[] = $this->prepareFile($fieldName, $file); + $postFiles[] = $this->prepareFile($fieldName, $file, $fileIndex); + $fileIndex++; } } else { $postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]); @@ -251,10 +253,11 @@ class RestClient * * @param string $fieldName * @param string|array $filePath + * @param integer $fileIndex * * @return array */ - protected function prepareFile($fieldName, $filePath) + protected function prepareFile($fieldName, $filePath, $fileIndex=0) { $filename = null; // Backward compatibility code @@ -268,6 +271,9 @@ class RestClient $filePath = substr($filePath, 1); } + // Add index for multiple file support + $fieldName .= '[' . $fileIndex . ']'; + return [ 'name' => $fieldName, 'contents' => fopen($filePath, 'r'), diff --git a/tests/Mailgun/Tests/Messages/ComplexMessageTest.php b/tests/Mailgun/Tests/Messages/ComplexMessageTest.php new file mode 100644 index 0000000..c1dc063 --- /dev/null +++ b/tests/Mailgun/Tests/Messages/ComplexMessageTest.php @@ -0,0 +1,111 @@ +method = $method; + $result->uri = $uri; + $result->body = $body; + $result->files = $files; + $result->headers = $headers; + + return $result; + } +} + +class mockMailgun extends Mailgun{ + public function __construct( + $apiKey = null, + HttpClient $httpClient = null, + $apiEndpoint = 'api.mailgun.net' + ) { + $this->apiKey = $apiKey; + $this->restClient = new mockRestClient($apiKey, $apiEndpoint, $httpClient); + } +} + +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 = [ + 'to' => 'test@test.mailgun.org', + 'from' => 'sender@test.mailgun.org', + 'subject' => 'This is my test subject', + 'text' => 'Testing!' + ]; + + $files = [ + 'inline' => [ + [ + 'remoteName'=> 'mailgun_icon1.png', + 'filePath' => 'tests/Mailgun/Tests/TestAssets/mailgun_icon1.png' + ], + [ + 'remoteName'=> 'mailgun_icon2.png', + 'filePath' => 'tests/Mailgun/Tests/TestAssets/mailgun_icon2.png' + ] + ] + ]; + + $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; + + foreach($result->files as $file){ + if ($file['name'] == 'to'){ + $this->assertEquals($file['contents'], 'test@test.mailgun.org'); + $testCount++; + } + if ($file['name'] == 'from'){ + $this->assertEquals($file['contents'], 'sender@test.mailgun.org'); + $testCount++; + } + if ($file['name'] == 'subject'){ + $this->assertEquals($file['contents'], 'This is my test subject'); + $testCount++; + } + if ($file['name'] == 'text'){ + $this->assertEquals($file['contents'], 'Testing!'); + $testCount++; + } + if ($file['name'] == 'inline[0]'){ + $this->assertEquals($file['filename'], 'mailgun_icon1.png'); + $testCount++; + } + if ($file['name'] == 'inline[1]'){ + $this->assertEquals($file['filename'], 'mailgun_icon2.png'); + $testCount++; + } + } + + // Make sure all "files" are asserted + $this->assertEquals(count($result->files), $testCount); + + $this->assertEquals([], $result->body); + $this->assertEquals([], $result->headers); + + } +}