mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-22 12:36:02 +03:00
Fix disordered POST parameters (#279)
This commit is contained in:
parent
ae9ee585a2
commit
0bc0a3b1a5
@ -4,7 +4,7 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": "^5.5|^7.0",
|
"php": "^5.5|^7.0",
|
||||||
"php-http/httplug": "^1.0",
|
"php-http/httplug": "^1.0",
|
||||||
"php-http/multipart-stream-builder": "^0.1 || ^0.2",
|
"php-http/multipart-stream-builder": "^0.2",
|
||||||
"php-http/message": "^1.0",
|
"php-http/message": "^1.0",
|
||||||
"php-http/client-common": "^1.1",
|
"php-http/client-common": "^1.1",
|
||||||
"php-http/discovery": "^1.0",
|
"php-http/discovery": "^1.0",
|
||||||
|
@ -39,10 +39,8 @@ class Message extends HttpApi
|
|||||||
if (!is_array($params[$fieldName])) {
|
if (!is_array($params[$fieldName])) {
|
||||||
$postDataMultipart[] = $this->prepareFile($fieldName, $params[$fieldName]);
|
$postDataMultipart[] = $this->prepareFile($fieldName, $params[$fieldName]);
|
||||||
} else {
|
} else {
|
||||||
$fileIndex = 0;
|
|
||||||
foreach ($params[$fieldName] as $file) {
|
foreach ($params[$fieldName] as $file) {
|
||||||
$postDataMultipart[] = $this->prepareFile($fieldName, $file, $fileIndex);
|
$postDataMultipart[] = $this->prepareFile($fieldName, $file);
|
||||||
++$fileIndex;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,10 +49,9 @@ class Message extends HttpApi
|
|||||||
|
|
||||||
foreach ($params as $key => $value) {
|
foreach ($params as $key => $value) {
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$index = 0;
|
|
||||||
foreach ($value as $subValue) {
|
foreach ($value as $subValue) {
|
||||||
$postDataMultipart[] = [
|
$postDataMultipart[] = [
|
||||||
'name' => sprintf('%s[%d]', $key, $index++),
|
'name' => $key,
|
||||||
'content' => $subValue,
|
'content' => $subValue,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -98,16 +95,13 @@ class Message extends HttpApi
|
|||||||
*
|
*
|
||||||
* @param string $fieldName
|
* @param string $fieldName
|
||||||
* @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar')
|
* @param array $filePath array('fileContent' => 'content') or array('filePath' => '/foo/bar')
|
||||||
* @param int $fileIndex
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*
|
*
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
private function prepareFile($fieldName, array $filePath, $fileIndex = 0)
|
private function prepareFile($fieldName, array $filePath)
|
||||||
{
|
{
|
||||||
// Add index for multiple file support
|
|
||||||
$fieldName .= '['.$fileIndex.']';
|
|
||||||
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
||||||
|
|
||||||
if (isset($filePath['fileContent'])) {
|
if (isset($filePath['fileContent'])) {
|
||||||
|
@ -150,10 +150,8 @@ 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, $fileIndex);
|
$postFiles[] = $this->prepareFile($fieldName, $file);
|
||||||
++$fileIndex;
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]);
|
$postFiles[] = $this->prepareFile($fieldName, $files[$fieldName]);
|
||||||
@ -164,10 +162,9 @@ class RestClient
|
|||||||
$postDataMultipart = [];
|
$postDataMultipart = [];
|
||||||
foreach ($postData as $key => $value) {
|
foreach ($postData as $key => $value) {
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$index = 0;
|
|
||||||
foreach ($value as $subValue) {
|
foreach ($value as $subValue) {
|
||||||
$postDataMultipart[] = [
|
$postDataMultipart[] = [
|
||||||
'name' => sprintf('%s[%d]', $key, $index++),
|
'name' => $key,
|
||||||
'contents' => $subValue,
|
'contents' => $subValue,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -285,11 +282,10 @@ class RestClient
|
|||||||
*
|
*
|
||||||
* @param string $fieldName
|
* @param string $fieldName
|
||||||
* @param string|array $filePath
|
* @param string|array $filePath
|
||||||
* @param int $fileIndex
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function prepareFile($fieldName, $filePath, $fileIndex = 0)
|
protected function prepareFile($fieldName, $filePath)
|
||||||
{
|
{
|
||||||
$filename = null;
|
$filename = null;
|
||||||
|
|
||||||
@ -314,9 +310,6 @@ class RestClient
|
|||||||
$resource = fopen($filePath, 'r');
|
$resource = fopen($filePath, 'r');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add index for multiple file support
|
|
||||||
$fieldName .= '['.$fileIndex.']';
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'name' => $fieldName,
|
'name' => $fieldName,
|
||||||
'contents' => $resource,
|
'contents' => $resource,
|
||||||
|
@ -33,9 +33,9 @@ class FileFromMemoryTest extends \PHPUnit_Framework_TestCase
|
|||||||
];
|
];
|
||||||
}, $attachments);
|
}, $attachments);
|
||||||
|
|
||||||
$this->assertContains(['name' => 'attachment[0]', 'contents' => 'File content', 'filename' => 'file1.txt'], $attachments);
|
$this->assertContains(['name' => 'attachment', 'contents' => 'File content', 'filename' => 'file1.txt'], $attachments);
|
||||||
$this->assertContains(['name' => 'attachment[1]', 'contents' => 'File content 2', 'filename' => 'file2.txt'], $attachments);
|
$this->assertContains(['name' => 'attachment', 'contents' => 'File content 2', 'filename' => 'file2.txt'], $attachments);
|
||||||
$this->assertContains(['name' => 'attachment[2]', 'contents' => 'Contents of a text file', 'filename' => 'text_file.txt'], $attachments);
|
$this->assertContains(['name' => 'attachment', 'contents' => 'Contents of a text file', 'filename' => 'text_file.txt'], $attachments);
|
||||||
};
|
};
|
||||||
|
|
||||||
$attachments = [
|
$attachments = [
|
||||||
|
@ -18,8 +18,8 @@ class InlineFileTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$fileValidator = function ($files) {
|
$fileValidator = function ($files) {
|
||||||
$fileNames = [
|
$fileNames = [
|
||||||
['name' => 'inline[0]', 'filename' => 'foo.png'],
|
['name' => 'inline', 'filename' => 'foo.png'],
|
||||||
['name' => 'inline[1]', 'filename' => 'bar.png'],
|
['name' => 'inline', 'filename' => 'bar.png'],
|
||||||
];
|
];
|
||||||
|
|
||||||
// Make sure that both files exists
|
// Make sure that both files exists
|
||||||
|
@ -18,8 +18,8 @@ class MessageBuilderHeaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$messageValidator = function ($headers) {
|
$messageValidator = function ($headers) {
|
||||||
$this->assertContains(['name' => 'h:My-Singular-Header', 'contents' => '123'], $headers);
|
$this->assertContains(['name' => 'h:My-Singular-Header', 'contents' => '123'], $headers);
|
||||||
$this->assertContains(['name' => 'h:My-Plural-Header[0]', 'contents' => '123'], $headers);
|
$this->assertContains(['name' => 'h:My-Plural-Header', 'contents' => '123'], $headers);
|
||||||
$this->assertContains(['name' => 'h:My-Plural-Header[1]', 'contents' => '456'], $headers);
|
$this->assertContains(['name' => 'h:My-Plural-Header', 'contents' => '456'], $headers);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the mocked mailgun client.
|
// Create the mocked mailgun client.
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Copyright (C) 2013-2016 Mailgun
|
|
||||||
*
|
|
||||||
* This software may be modified and distributed under the terms
|
|
||||||
* of the MIT license. See the LICENSE file for details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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::createMock($this, 'POST', 'domain/messages', [], $fileValidator);
|
|
||||||
|
|
||||||
$builder = $mailgun->MessageBuilder();
|
|
||||||
$builder->setFromAddress('bob@example.com');
|
|
||||||
$builder->addToRecipient('to1@example.com');
|
|
||||||
$builder->addToRecipient('to2@example.com');
|
|
||||||
$builder->addCcRecipient('cc1@example.com');
|
|
||||||
$builder->addCcRecipient('cc2@example.com');
|
|
||||||
$builder->addBccRecipient('bcc1@example.com');
|
|
||||||
$builder->addBccRecipient('bcc2@example.com');
|
|
||||||
$builder->addCustomParameter('foo', 'bar');
|
|
||||||
$builder->addCustomParameter('foo', 'baz');
|
|
||||||
$builder->addCampaignId('campaign0');
|
|
||||||
$builder->addCampaignId('campaign1');
|
|
||||||
$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());
|
|
||||||
}
|
|
||||||
}
|
|
@ -81,6 +81,7 @@ class ComplexMessageTest extends \Mailgun\Tests\MailgunTestCase
|
|||||||
// Start a counter, make sure all files are asserted
|
// Start a counter, make sure all files are asserted
|
||||||
$testCount = 0;
|
$testCount = 0;
|
||||||
|
|
||||||
|
$expectedFilenames = ['mailgun_icon1.png', 'mailgun_icon2.png'];
|
||||||
foreach ($result->files as $file) {
|
foreach ($result->files as $file) {
|
||||||
if ($file['name'] == 'to') {
|
if ($file['name'] == 'to') {
|
||||||
$this->assertEquals($file['contents'], 'test@test.mailgun.org');
|
$this->assertEquals($file['contents'], 'test@test.mailgun.org');
|
||||||
@ -98,12 +99,10 @@ class ComplexMessageTest extends \Mailgun\Tests\MailgunTestCase
|
|||||||
$this->assertEquals($file['contents'], 'Testing!');
|
$this->assertEquals($file['contents'], 'Testing!');
|
||||||
++$testCount;
|
++$testCount;
|
||||||
}
|
}
|
||||||
if ($file['name'] == 'inline[0]') {
|
if ($file['name'] == 'inline') {
|
||||||
$this->assertEquals($file['filename'], 'mailgun_icon1.png');
|
$expectedFilename = array_shift($expectedFilenames);
|
||||||
++$testCount;
|
$this->assertNotNull($expectedFilename);
|
||||||
}
|
$this->assertSame($expectedFilename, $file['filename']);
|
||||||
if ($file['name'] == 'inline[1]') {
|
|
||||||
$this->assertEquals($file['filename'], 'mailgun_icon2.png');
|
|
||||||
++$testCount;
|
++$testCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user