mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-02-16 21:13:12 +03:00
Merge pull request #156 from Nyholm/travis-tests
This PR add tests to #151
This commit is contained in:
commit
f66f512724
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ build
|
|||||||
composer.lock
|
composer.lock
|
||||||
nbproject/*
|
nbproject/*
|
||||||
.idea
|
.idea
|
||||||
|
phpunit.phar
|
||||||
|
@ -14,10 +14,14 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-0": {
|
"psr-0": {
|
||||||
"Mailgun\\Tests": "tests/",
|
|
||||||
"Mailgun": "src/"
|
"Mailgun": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"Mailgun\\Tests\\": "tests/"
|
||||||
|
}
|
||||||
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<phpunit bootstrap="tests/Bootstrap.php"
|
<phpunit bootstrap="vendor/autoload.php"
|
||||||
colors="true"
|
colors="true"
|
||||||
processIsolation="false"
|
processIsolation="false"
|
||||||
stopOnFailure="false"
|
stopOnFailure="false"
|
||||||
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<testsuites>
|
<testsuites>
|
||||||
<testsuite>
|
<testsuite>
|
||||||
<directory>tests/Mailgun/Tests</directory>
|
<directory>tests</directory>
|
||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
||||||
|
@ -119,8 +119,10 @@ class RestClient
|
|||||||
foreach ($fields as $fieldName) {
|
foreach ($fields as $fieldName) {
|
||||||
if (isset($files[$fieldName])) {
|
if (isset($files[$fieldName])) {
|
||||||
if (is_array($files[$fieldName])) {
|
if (is_array($files[$fieldName])) {
|
||||||
|
$fileIndex = 0;
|
||||||
foreach ($files[$fieldName] as $file) {
|
foreach ($files[$fieldName] as $file) {
|
||||||
$postFiles[] = $this->prepareFile($fieldName, $file);
|
$postFiles[] = $this->prepareFile($fieldName, $file, $fileIndex);
|
||||||
|
$fileIndex++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]);
|
$postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]);
|
||||||
@ -251,10 +253,11 @@ class RestClient
|
|||||||
*
|
*
|
||||||
* @param string $fieldName
|
* @param string $fieldName
|
||||||
* @param string|array $filePath
|
* @param string|array $filePath
|
||||||
|
* @param integer $fileIndex
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepareFile($fieldName, $filePath)
|
protected function prepareFile($fieldName, $filePath, $fileIndex=0)
|
||||||
{
|
{
|
||||||
$filename = null;
|
$filename = null;
|
||||||
// Backward compatibility code
|
// Backward compatibility code
|
||||||
@ -268,6 +271,9 @@ class RestClient
|
|||||||
$filePath = substr($filePath, 1);
|
$filePath = substr($filePath, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add index for multiple file support
|
||||||
|
$fieldName .= '[' . $fileIndex . ']';
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => $fieldName,
|
'name' => $fieldName,
|
||||||
'contents' => fopen($filePath, 'r'),
|
'contents' => fopen($filePath, 'r'),
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
//Grab the composer Autoloader!
|
|
||||||
$autoloader = include dirname(__DIR__).'/vendor/autoload.php';
|
|
51
tests/Functional/InlineFileTest.php
Normal file
51
tests/Functional/InlineFileTest.php
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Mailgun\Tests\Functional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||||
|
*/
|
||||||
|
class InlineFileTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function testSimpleExample()
|
||||||
|
{
|
||||||
|
// Create a Closure that validates the $files parameter to RestClient::send()
|
||||||
|
$fileValidator = function($files) {
|
||||||
|
// Validate standard params
|
||||||
|
$this->assertContains(['name'=>'from', 'contents'=>'bob@example.com'], $files);
|
||||||
|
$this->assertContains(['name'=>'to', 'contents'=>'alice@example.com'], $files);
|
||||||
|
$this->assertContains(['name'=>'subject', 'contents'=>'Foo'], $files);
|
||||||
|
$this->assertContains(['name'=>'text', 'contents'=>'Bar'], $files);
|
||||||
|
|
||||||
|
$fileNames = [
|
||||||
|
['name'=>'inline[0]', 'filename'=>'foo.png'],
|
||||||
|
['name'=>'inline[1]', 'filename'=>'bar.png']
|
||||||
|
];
|
||||||
|
foreach ($fileNames as $idx => $fileName) {
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if ($file['name'] === $fileName['name'] && $file['filename'] === $fileName['filename']) {
|
||||||
|
unset ($fileNames[$idx]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEmpty($fileNames, 'Filenames could not be found');
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the mocked mailgun client. We use $this->assertEquals on $method, $uri and $body parameters.
|
||||||
|
$mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator);
|
||||||
|
|
||||||
|
$builder = $mailgun->MessageBuilder();
|
||||||
|
$builder->setFromAddress("bob@example.com");
|
||||||
|
$builder->addToRecipient("alice@example.com");
|
||||||
|
$builder->setSubject("Foo");
|
||||||
|
$builder->setTextBody("Bar");
|
||||||
|
|
||||||
|
$builder->addInlineImage("@./tests/TestAssets/mailgun_icon1.png", 'foo.png');
|
||||||
|
$builder->addInlineImage("@./tests/TestAssets/mailgun_icon2.png", 'bar.png');
|
||||||
|
|
||||||
|
$mailgun->post("domain/messages", $builder->getMessage(), $builder->getFiles());
|
||||||
|
}
|
||||||
|
}
|
40
tests/Functional/NoSamePostNameTest.php
Normal file
40
tests/Functional/NoSamePostNameTest.php
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Mailgun\Tests\Functional;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||||
|
*/
|
||||||
|
class NoSamePostNameTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* No post names should ever be the same
|
||||||
|
*/
|
||||||
|
public function testNames()
|
||||||
|
{
|
||||||
|
$fileValidator = function($files) {
|
||||||
|
$usedNames = [];
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$this->assertFalse(in_array($file['name'], $usedNames), 'No files should have the same POST name.');
|
||||||
|
$usedNames[] = $file['name'];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the mocked mailgun client. We use $this->assertEquals on $method, $uri and $body parameters.
|
||||||
|
$mailgun = MockedMailgun::create($this, 'POST', 'domain/messages', [], $fileValidator);
|
||||||
|
|
||||||
|
$builder = $mailgun->MessageBuilder();
|
||||||
|
$builder->setFromAddress("bob@example.com");
|
||||||
|
$builder->addToRecipient("alice@example.com");
|
||||||
|
$builder->setSubject("Foo");
|
||||||
|
$builder->setTextBody("Bar");
|
||||||
|
|
||||||
|
$builder->addAttachment("@./tests/TestAssets/mailgun_icon1.png", 'foo.png');
|
||||||
|
$builder->addAttachment("@./tests/TestAssets/mailgun_icon1.png", 'foo.png');
|
||||||
|
$builder->addInlineImage("@./tests/TestAssets/mailgun_icon2.png", 'bar.png');
|
||||||
|
$builder->addInlineImage("@./tests/TestAssets/mailgun_icon2.png", 'bar.png');
|
||||||
|
|
||||||
|
$mailgun->post("domain/messages", $builder->getMessage(), $builder->getFiles());
|
||||||
|
}
|
||||||
|
}
|
@ -27,5 +27,5 @@ class SendMessageTest extends \PHPUnit_Framework_TestCase
|
|||||||
'to' => 'alice@example.com',
|
'to' => 'alice@example.com',
|
||||||
'subject' => 'Foo',
|
'subject' => 'Foo',
|
||||||
'text' => 'Bar'));
|
'text' => 'Bar'));
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 85 KiB |
Binary file not shown.
Before Width: | Height: | Size: 44 KiB |
@ -1,6 +1,6 @@
|
|||||||
<?PHP
|
<?PHP
|
||||||
|
|
||||||
namespace Mailgun\Tests\Lists;
|
namespace Mailgun\Tests;
|
||||||
|
|
||||||
use Mailgun\Mailgun;
|
use Mailgun\Mailgun;
|
||||||
|
|
111
tests/Messages/ComplexMessageTest.php
Normal file
111
tests/Messages/ComplexMessageTest.php
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
<?PHP
|
||||||
|
|
||||||
|
namespace Mailgun\Tests\Messages;
|
||||||
|
|
||||||
|
use Mailgun\Tests\Mock\Mailgun;
|
||||||
|
use Mailgun\Connection\RestClient;
|
||||||
|
|
||||||
|
|
||||||
|
class mockRestClient extends RestClient{
|
||||||
|
public function send($method, $uri, $body = null, $files = [], array $headers = [])
|
||||||
|
{
|
||||||
|
$result = new \stdClass;
|
||||||
|
|
||||||
|
$result->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/TestAssets/mailgun_icon1.png'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'remoteName'=> 'mailgun_icon2.png',
|
||||||
|
'filePath' => '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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 85 KiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 85 KiB |
Loading…
x
Reference in New Issue
Block a user