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/composer.json b/composer.json index bc541bc..0c68194 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,14 @@ }, "autoload": { "psr-0": { - "Mailgun\\Tests": "tests/", "Mailgun": "src/" } }, + "autoload-dev": { + "psr-4": { + "Mailgun\\Tests\\": "tests/" + } + }, "license": "MIT", "authors": [ { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 33cea16..8707107 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - - tests/Mailgun/Tests + tests 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/Bootstrap.php b/tests/Bootstrap.php deleted file mode 100644 index 95f8179..0000000 --- a/tests/Bootstrap.php +++ /dev/null @@ -1,4 +0,0 @@ - + */ +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()); + } +} diff --git a/tests/Mailgun/Tests/Functional/MockedMailgun.php b/tests/Functional/MockedMailgun.php similarity index 100% rename from tests/Mailgun/Tests/Functional/MockedMailgun.php rename to tests/Functional/MockedMailgun.php diff --git a/tests/Mailgun/Tests/Functional/MockedRestClient.php b/tests/Functional/MockedRestClient.php similarity index 100% rename from tests/Mailgun/Tests/Functional/MockedRestClient.php rename to tests/Functional/MockedRestClient.php diff --git a/tests/Functional/NoSamePostNameTest.php b/tests/Functional/NoSamePostNameTest.php new file mode 100644 index 0000000..0f346ff --- /dev/null +++ b/tests/Functional/NoSamePostNameTest.php @@ -0,0 +1,40 @@ + + */ +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()); + } +} diff --git a/tests/Mailgun/Tests/Functional/SendMessageTest.php b/tests/Functional/SendMessageTest.php similarity index 99% rename from tests/Mailgun/Tests/Functional/SendMessageTest.php rename to tests/Functional/SendMessageTest.php index 193a2bf..4966920 100644 --- a/tests/Mailgun/Tests/Functional/SendMessageTest.php +++ b/tests/Functional/SendMessageTest.php @@ -27,5 +27,5 @@ class SendMessageTest extends \PHPUnit_Framework_TestCase 'to' => 'alice@example.com', 'subject' => 'Foo', 'text' => 'Bar')); - } + } } diff --git a/tests/Mailgun/Tests/Lists/OptInHandlerTest.php b/tests/Lists/OptInHandlerTest.php similarity index 100% rename from tests/Mailgun/Tests/Lists/OptInHandlerTest.php rename to tests/Lists/OptInHandlerTest.php diff --git a/tests/Mailgun/Tests/TestAssets/mailgun_icon.png b/tests/Mailgun/Tests/TestAssets/mailgun_icon.png deleted file mode 100644 index 604f673..0000000 Binary files a/tests/Mailgun/Tests/TestAssets/mailgun_icon.png and /dev/null differ diff --git a/tests/Mailgun/Tests/TestAssets/rackspace_logo.jpg b/tests/Mailgun/Tests/TestAssets/rackspace_logo.jpg deleted file mode 100644 index 46360a0..0000000 Binary files a/tests/Mailgun/Tests/TestAssets/rackspace_logo.jpg and /dev/null differ diff --git a/tests/Mailgun/Tests/MailgunTest.php b/tests/MailgunTest.php similarity index 97% rename from tests/Mailgun/Tests/MailgunTest.php rename to tests/MailgunTest.php index 204d2f5..5c7e305 100644 --- a/tests/Mailgun/Tests/MailgunTest.php +++ b/tests/MailgunTest.php @@ -1,6 +1,6 @@ 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); + + } +} diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Messages/MessageBuilderTest.php similarity index 100% rename from tests/Mailgun/Tests/Messages/MessageBuilderTest.php rename to tests/Messages/MessageBuilderTest.php diff --git a/tests/Mailgun/Tests/Messages/StandardMessageTest.php b/tests/Messages/StandardMessageTest.php similarity index 100% rename from tests/Mailgun/Tests/Messages/StandardMessageTest.php rename to tests/Messages/StandardMessageTest.php diff --git a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php b/tests/Mock/Connection/TestBroker.php similarity index 100% rename from tests/Mailgun/Tests/Mock/Connection/TestBroker.php rename to tests/Mock/Connection/TestBroker.php diff --git a/tests/Mailgun/Tests/Mock/Mailgun.php b/tests/Mock/Mailgun.php similarity index 100% rename from tests/Mailgun/Tests/Mock/Mailgun.php rename to tests/Mock/Mailgun.php diff --git a/tests/Mailgun/Tests/TestAssets/mailgun_icon1.png b/tests/TestAssets/mailgun_icon1.png similarity index 100% rename from tests/Mailgun/Tests/TestAssets/mailgun_icon1.png rename to tests/TestAssets/mailgun_icon1.png diff --git a/tests/Mailgun/Tests/TestAssets/mailgun_icon2.png b/tests/TestAssets/mailgun_icon2.png similarity index 100% rename from tests/Mailgun/Tests/TestAssets/mailgun_icon2.png rename to tests/TestAssets/mailgun_icon2.png