mailgun-php/tests/Functional/FileFromMemoryTest.php

58 lines
2.1 KiB
PHP
Raw Normal View History

2016-09-06 14:05:50 +03:00
<?php
2016-11-24 01:02:12 +03:00
/*
* Copyright (C) 2013 Mailgun
2016-11-24 01:02:12 +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-11-24 01:02:12 +03:00
*/
2016-09-06 14:05:50 +03:00
namespace Mailgun\Tests\Functional;
/**
2016-09-27 16:23:08 +03:00
* Add attachment with file from memory.
2016-09-06 14:05:50 +03:00
*
* @author Wim Verstuyf <wim.verstuyf@codelicious.be>
*/
class FileFromMemoryTest extends \PHPUnit_Framework_TestCase
{
public function testAddFileFromMemory()
{
2016-09-27 16:23:08 +03:00
$fileValidator = function ($files) {
2016-09-06 14:05:50 +03:00
// Get only the attachments so the properties can be converted to a format we like
2016-09-27 16:23:08 +03:00
$attachments = array_filter($files, function ($f) {
return false !== strpos($f['name'], 'attachment');
2016-09-06 14:05:50 +03:00
});
// Convert resources to strings
2016-09-27 16:23:08 +03:00
$attachments = array_map(function ($f) {
2016-09-06 14:05:50 +03:00
return [
'name' => $f['name'],
2016-09-06 14:05:50 +03:00
'contents' => fread($f['contents'], 50),
'filename' => $f['filename'],
];
}, $attachments);
2017-04-08 02:24:37 +03:00
$this->assertContains(['name' => 'attachment', 'contents' => 'File content', 'filename' => 'file1.txt'], $attachments);
$this->assertContains(['name' => 'attachment', 'contents' => 'File content 2', 'filename' => 'file2.txt'], $attachments);
$this->assertContains(['name' => 'attachment', 'contents' => 'Contents of a text file', 'filename' => 'text_file.txt'], $attachments);
2016-09-06 14:05:50 +03:00
};
$attachments = [
2016-09-27 16:23:08 +03:00
['filename' => 'file1.txt', 'fileContent' => 'File content'],
['filename' => 'file2.txt', 'fileContent' => 'File content 2'],
['filePath' => './tests/TestAssets/text_file.txt', 'remoteName' => 'text_file.txt'],
2016-09-06 14:05:50 +03:00
];
$mailgun = MockedMailgun::createMock($this, 'POST', 'domain/messages', [], $fileValidator);
2016-09-06 14:05:50 +03:00
2016-09-27 16:23:08 +03:00
$mailgun->sendMessage('domain', [
'from' => 'bob@example.com',
'to' => 'alice@example.com',
'subject' => 'Foo',
'text' => 'Bar', ], [
2016-09-27 16:23:08 +03:00
'attachment' => $attachments,
]);
2016-09-06 14:05:50 +03:00
}
}