Merge pull request #156 from Nyholm/travis-tests

This PR add tests to #151
This commit is contained in:
Tobias Nyholm 2016-08-10 17:18:07 +02:00 committed by GitHub
commit f66f512724
23 changed files with 220 additions and 11 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ build
composer.lock
nbproject/*
.idea
phpunit.phar

View File

@ -14,10 +14,14 @@
},
"autoload": {
"psr-0": {
"Mailgun\\Tests": "tests/",
"Mailgun": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mailgun\\Tests\\": "tests/"
}
},
"license": "MIT",
"authors": [
{

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/Bootstrap.php"
<phpunit bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
@ -11,7 +11,7 @@
<testsuites>
<testsuite>
<directory>tests/Mailgun/Tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

View File

@ -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'),

View File

@ -1,4 +0,0 @@
<?php
//Grab the composer Autoloader!
$autoloader = include dirname(__DIR__).'/vendor/autoload.php';

View 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());
}
}

View 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());
}
}

View File

@ -27,5 +27,5 @@ class SendMessageTest extends \PHPUnit_Framework_TestCase
'to' => 'alice@example.com',
'subject' => 'Foo',
'text' => 'Bar'));
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

View File

@ -1,6 +1,6 @@
<?PHP
namespace Mailgun\Tests\Lists;
namespace Mailgun\Tests;
use Mailgun\Mailgun;

View 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);
}
}

View File

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB

View File

Before

Width:  |  Height:  |  Size: 85 KiB

After

Width:  |  Height:  |  Size: 85 KiB