diff --git a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php index b370649..20ebd96 100644 --- a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php +++ b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php @@ -1,25 +1,27 @@ httpResponseCode = $response_code; - $this->httpResponseBody = $response_body; - } - - public function getHttpResponseCode() { - return $this->httpResponseCode; - } - - public function getHttpResponseBody() { - return $this->httpResponseBody; - } -} + protected $httpResponseCode; + protected $httpResponseBody; -?> + public function __construct($message = null, $response_code = null, $response_body = null, $code = 0, \Exception $previous = null) + { + parent::__construct($message, $code, $previous); + + $this->httpResponseCode = $response_code; + $this->httpResponseBody = $response_body; + } + + public function getHttpResponseCode() + { + return $this->httpResponseCode; + } + + public function getHttpResponseBody() + { + return $this->httpResponseBody; + } +} diff --git a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php index bfcc743..d7159d5 100644 --- a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php +++ b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php @@ -1,4 +1,7 @@ $key, + 'name' => $key, 'contents' => $subValue, ]; } } else { $postDataMultipart[] = [ - 'name' => $key, + 'name' => $key, 'contents' => $value, ]; } @@ -149,14 +152,14 @@ class RestClient * @param string $endpointUrl * @param array $queryString * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ - public function get($endpointUrl, $queryString = array()) + public function get($endpointUrl, $queryString = []) { return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); } @@ -164,12 +167,12 @@ class RestClient /** * @param string $endpointUrl * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function delete($endpointUrl) { @@ -180,12 +183,12 @@ class RestClient * @param string $endpointUrl * @param mixed $putData * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function put($endpointUrl, $putData) { @@ -195,35 +198,35 @@ class RestClient /** * @param ResponseInterface $responseObj * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function responseHandler(ResponseInterface $responseObj) { - $httpResponseCode = (int)$responseObj->getStatusCode(); + $httpResponseCode = (int) $responseObj->getStatusCode(); switch ($httpResponseCode) { - case 200: - $data = (string)$responseObj->getBody(); - $jsonResponseData = json_decode($data, false); - $result = new \stdClass(); - // return response data as json if possible, raw if not - $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; - $result->http_response_code = $httpResponseCode; + case 200: + $data = (string) $responseObj->getBody(); + $jsonResponseData = json_decode($data, false); + $result = new \stdClass(); + // return response data as json if possible, raw if not + $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; + $result->http_response_code = $httpResponseCode; - return $result; - case 400: - throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); - case 401: - throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); - case 404: - throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); - default: - throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); + return $result; + case 400: + throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS.$this->getResponseExceptionMessage($responseObj)); + case 401: + throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); + case 404: + throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj)); + default: + throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); } } @@ -266,15 +269,13 @@ class RestClient } return [ - 'name' => $fieldName, + 'name' => $fieldName, 'contents' => fopen($filePath, 'r'), 'filename' => $filename, ]; } - /** - * * @return HttpClient */ protected function getHttpClient() @@ -296,7 +297,6 @@ class RestClient return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; } - /** * @param string $apiEndpoint * @param string $apiVersion @@ -306,7 +306,7 @@ class RestClient */ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { - return ($ssl ? 'https://' : 'http://') . $apiEndpoint . '/' . $apiVersion . '/'; + return ($ssl ? 'https://' : 'http://').$apiEndpoint.'/'.$apiVersion.'/'; } /** @@ -322,7 +322,7 @@ class RestClient } /** - * @param boolean $sslEnabled + * @param bool $sslEnabled * * @return RestClient */ diff --git a/src/Mailgun/Constants/Api.php b/src/Mailgun/Constants/Api.php index 7a2908e..a3ffdfb 100644 --- a/src/Mailgun/Constants/Api.php +++ b/src/Mailgun/Constants/Api.php @@ -1,16 +1,14 @@ $recipientAddress, 'l' => $mailingList); - $encodedInnerPayload = base64_encode(json_encode($innerPayload)); + public function generateHash($mailingList, $secretAppId, $recipientAddress) + { + $innerPayload = ['r' => $recipientAddress, 'l' => $mailingList]; + $encodedInnerPayload = base64_encode(json_encode($innerPayload)); - $innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId); - $outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload); + $innerHash = hash_hmac('sha1', $encodedInnerPayload, $secretAppId); + $outerPayload = ['h' => $innerHash, 'p' => $encodedInnerPayload]; - return urlencode(base64_encode(json_encode($outerPayload))); - } + return urlencode(base64_encode(json_encode($outerPayload))); + } /** * @param string $secretAppId * @param string $uniqueHash + * * @return array|bool */ - public function validateHash($secretAppId, $uniqueHash){ - $decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true); + public function validateHash($secretAppId, $uniqueHash) + { + $decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true); - $decodedHash = $decodedOuterPayload['h']; - $innerPayload = $decodedOuterPayload['p']; + $decodedHash = $decodedOuterPayload['h']; + $innerPayload = $decodedOuterPayload['p']; - $decodedInnerPayload = json_decode(base64_decode($innerPayload), true); - $computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId); + $decodedInnerPayload = json_decode(base64_decode($innerPayload), true); + $computedInnerHash = hash_hmac('sha1', $innerPayload, $secretAppId); - if($computedInnerHash == $decodedHash){ - return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']); - } + if ($computedInnerHash == $decodedHash) { + return ['recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']]; + } - return false; - } + return false; + } } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 16b3d9a..a0e3ead 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -3,11 +3,11 @@ namespace Mailgun; use Http\Client\HttpClient; -use Mailgun\Constants\ExceptionMessages; -use Mailgun\Messages\Exceptions; use Mailgun\Connection\RestClient; -use Mailgun\Messages\BatchMessage; +use Mailgun\Constants\ExceptionMessages; use Mailgun\Lists\OptInHandler; +use Mailgun\Messages\BatchMessage; +use Mailgun\Messages\Exceptions; use Mailgun\Messages\MessageBuilder; /** @@ -16,8 +16,8 @@ use Mailgun\Messages\MessageBuilder; * * @link https://github.com/mailgun/mailgun-php/blob/master/README.md */ -class Mailgun{ - +class Mailgun +{ /** * @var RestClient */ @@ -30,8 +30,8 @@ class Mailgun{ /** * @param string|null $apiKey - * @param HttpClient $httpClient - * @param string $apiEndpoint + * @param HttpClient $httpClient + * @param string $apiEndpoint */ public function __construct( $apiKey = null, @@ -48,27 +48,28 @@ class Mailgun{ * position of the function call. * * @param string $workingDomain - * @param array $postData - * @param array $postFiles - * @return \stdClass + * @param array $postData + * @param array $postFiles + * * @throws Exceptions\MissingRequiredMIMEParameters + * + * @return \stdClass */ - public function sendMessage($workingDomain, $postData, $postFiles = array()){ - if(is_array($postFiles)){ + public function sendMessage($workingDomain, $postData, $postFiles = []) + { + if (is_array($postFiles)) { return $this->post("$workingDomain/messages", $postData, $postFiles); - } - else if(is_string($postFiles)){ - - $tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME"); - $fileHandle = fopen($tempFile, "w"); + } elseif (is_string($postFiles)) { + $tempFile = tempnam(sys_get_temp_dir(), 'MG_TMP_MIME'); + $fileHandle = fopen($tempFile, 'w'); fwrite($fileHandle, $postFiles); - $result = $this->post("$workingDomain/messages.mime", $postData, array("message" => $tempFile)); + $result = $this->post("$workingDomain/messages.mime", $postData, ['message' => $tempFile]); fclose($fileHandle); unlink($tempFile); + return $result; - } - else{ + } else { throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); } } @@ -84,59 +85,68 @@ class Mailgun{ * You should reject the request with status code 403 Forbidden. * * @param array|null $postData + * * @return bool */ - public function verifyWebhookSignature($postData = NULL) { - if($postData === null) { + public function verifyWebhookSignature($postData = null) + { + if ($postData === null) { $postData = $_POST; } - if(!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { + if (!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { return false; } - $hmac = hash_hmac('sha256', "{$postData["timestamp"]}{$postData["token"]}", $this->apiKey); + $hmac = hash_hmac('sha256', "{$postData['timestamp']}{$postData['token']}", $this->apiKey); $sig = $postData['signature']; - if(function_exists('hash_equals')) { + if (function_exists('hash_equals')) { // hash_equals is constant time, but will not be introduced until PHP 5.6 return hash_equals($hmac, $sig); - } - else { - return ($hmac === $sig); + } else { + return $hmac === $sig; } } /** * @param string $endpointUrl - * @param array $postData - * @param array $files + * @param array $postData + * @param array $files + * * @return \stdClass */ - public function post($endpointUrl, $postData = array(), $files = array()){ + public function post($endpointUrl, $postData = [], $files = []) + { return $this->restClient->post($endpointUrl, $postData, $files); } /** * @param string $endpointUrl - * @param array $queryString + * @param array $queryString + * * @return \stdClass */ - public function get($endpointUrl, $queryString = array()){ + public function get($endpointUrl, $queryString = []) + { return $this->restClient->get($endpointUrl, $queryString); } /** * @param string $endpointUrl + * * @return \stdClass */ - public function delete($endpointUrl){ + public function delete($endpointUrl) + { return $this->restClient->delete($endpointUrl); } /** * @param string $endpointUrl - * @param array $putData + * @param array $putData + * * @return \stdClass */ - public function put($endpointUrl, $putData){ + public function put($endpointUrl, $putData) + { return $this->restClient->put($endpointUrl, $putData); } @@ -153,7 +163,7 @@ class Mailgun{ } /** - * @param boolean $sslEnabled + * @param bool $sslEnabled * * @return Mailgun */ @@ -167,23 +177,27 @@ class Mailgun{ /** * @return MessageBuilder */ - public function MessageBuilder(){ + public function MessageBuilder() + { return new MessageBuilder(); } /** * @return OptInHandler */ - public function OptInHandler(){ + public function OptInHandler() + { return new OptInHandler(); } /** * @param string $workingDomain - * @param bool $autoSend + * @param bool $autoSend + * * @return BatchMessage */ - public function BatchMessage($workingDomain, $autoSend = true){ + public function BatchMessage($workingDomain, $autoSend = true) + { return new BatchMessage($this->restClient, $workingDomain, $autoSend); } } diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index 36cdff7..7085400 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -4,8 +4,8 @@ namespace Mailgun\Messages; use Mailgun\Constants\Api; use Mailgun\Constants\ExceptionMessages; -use Mailgun\Messages\Exceptions\TooManyParameters; use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; +use Mailgun\Messages\Exceptions\TooManyParameters; /** * This class is used for batch sending. See the official documentation (link below) @@ -13,134 +13,135 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; * * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md */ -class BatchMessage extends MessageBuilder{ - +class BatchMessage extends MessageBuilder +{ /** * @var array */ - private $batchRecipientAttributes; + private $batchRecipientAttributes; /** - * @var boolean + * @var bool */ - private $autoSend; + private $autoSend; /** * @var \Mailgun\Connection\RestClient */ - private $restClient; + private $restClient; /** * @var string */ - private $workingDomain; + private $workingDomain; /** * @var array */ - private $messageIds = array(); - + private $messageIds = []; + /** - * @var string + * @var string */ - private $endpointUrl; + private $endpointUrl; /** * @param \Mailgun\Connection\RestClient $restClient - * @param string $workingDomain - * @param boolean $autoSend + * @param string $workingDomain + * @param bool $autoSend */ - public function __construct($restClient, $workingDomain, $autoSend){ - $this->batchRecipientAttributes = array(); - $this->autoSend = $autoSend; - $this->restClient = $restClient; - $this->workingDomain = $workingDomain; - $this->endpointUrl = $workingDomain . "/messages"; - } + public function __construct($restClient, $workingDomain, $autoSend) + { + $this->batchRecipientAttributes = []; + $this->autoSend = $autoSend; + $this->restClient = $restClient; + $this->workingDomain = $workingDomain; + $this->endpointUrl = $workingDomain.'/messages'; + } /** * @param string $headerName * @param string $address - * @param array $variables + * @param array $variables + * * @throws MissingRequiredMIMEParameters * @throws TooManyParameters */ - protected function addRecipient($headerName, $address, $variables){ - if(array_key_exists($headerName, $this->counters['recipients'])){ - if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){ - if($this->autoSend == false){ - throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); - } - $this->sendMessage(); - } - } + protected function addRecipient($headerName, $address, $variables) + { + if (array_key_exists($headerName, $this->counters['recipients'])) { + if ($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) { + if ($this->autoSend == false) { + throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); + } + $this->sendMessage(); + } + } - $compiledAddress = $this->parseAddress($address, $variables); + $compiledAddress = $this->parseAddress($address, $variables); - if(isset($this->message[$headerName])){ - array_push($this->message[$headerName], $compiledAddress); - } - elseif($headerName == "h:reply-to"){ - $this->message[$headerName] = $compiledAddress; - } - else{ - $this->message[$headerName] = array($compiledAddress); - } + if (isset($this->message[$headerName])) { + array_push($this->message[$headerName], $compiledAddress); + } elseif ($headerName == 'h:reply-to') { + $this->message[$headerName] = $compiledAddress; + } else { + $this->message[$headerName] = [$compiledAddress]; + } - if(array_key_exists($headerName, $this->counters['recipients'])){ - $this->counters['recipients'][$headerName] += 1; - if(!array_key_exists("id", $variables)){ - $variables['id'] = $this->counters['recipients'][$headerName]; - } - } - $this->batchRecipientAttributes["$address"] = $variables; - } + if (array_key_exists($headerName, $this->counters['recipients'])) { + $this->counters['recipients'][$headerName] += 1; + if (!array_key_exists('id', $variables)) { + $variables['id'] = $this->counters['recipients'][$headerName]; + } + } + $this->batchRecipientAttributes["$address"] = $variables; + } /** * @param array $message * @param array $files + * * @throws MissingRequiredMIMEParameters */ - public function sendMessage($message = array(), $files = array()){ - if(count($message) < 1){ - $message = $this->message; - $files = $this->files; - } - if(!array_key_exists("from", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("to", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("subject", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - else{ - $message["recipient-variables"] = json_encode($this->batchRecipientAttributes); - $response = $this->restClient->post($this->endpointUrl, $message, $files); - $this->batchRecipientAttributes = array(); - $this->counters['recipients']['to'] = 0; - $this->counters['recipients']['cc'] = 0; - $this->counters['recipients']['bcc'] = 0; - unset($this->message["to"]); - array_push($this->messageIds, $response->http_response_body->id); - } - } + public function sendMessage($message = [], $files = []) + { + if (count($message) < 1) { + $message = $this->message; + $files = $this->files; + } + if (!array_key_exists('from', $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } elseif (!array_key_exists('to', $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } elseif (!array_key_exists('subject', $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } elseif ((!array_key_exists('text', $message) && !array_key_exists('html', $message))) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } else { + $message['recipient-variables'] = json_encode($this->batchRecipientAttributes); + $response = $this->restClient->post($this->endpointUrl, $message, $files); + $this->batchRecipientAttributes = []; + $this->counters['recipients']['to'] = 0; + $this->counters['recipients']['cc'] = 0; + $this->counters['recipients']['bcc'] = 0; + unset($this->message['to']); + array_push($this->messageIds, $response->http_response_body->id); + } + } /** * @throws MissingRequiredMIMEParameters */ - public function finalize(){ - $this->sendMessage(); - } + public function finalize() + { + $this->sendMessage(); + } /** * @return string[] */ - public function getMessageIds(){ - return $this->messageIds; - } + public function getMessageIds() + { + return $this->messageIds; + } } diff --git a/src/Mailgun/Messages/Exceptions/InvalidParameter.php b/src/Mailgun/Messages/Exceptions/InvalidParameter.php index 9ea1dfc..06c8612 100644 --- a/src/Mailgun/Messages/Exceptions/InvalidParameter.php +++ b/src/Mailgun/Messages/Exceptions/InvalidParameter.php @@ -1,4 +1,7 @@ array( + protected $counters = [ + 'recipients' => [ 'to' => 0, 'cc' => 0, - 'bcc' => 0 - ), - 'attributes' => array( + 'bcc' => 0, + ], + 'attributes' => [ 'attachment' => 0, 'campaign_id' => 0, 'custom_option' => 0, - 'tag' => 0 - ) - ); + 'tag' => 0, + ], + ]; /** - * @param array $params + * @param array $params * @param string $key - * @param mixed $default + * @param mixed $default + * * @return mixed */ protected function safeGet($params, $key, $default) @@ -66,23 +67,25 @@ class MessageBuilder /** * @param array $params + * * @return mixed|string */ protected function getFullName($params) { - if (array_key_exists("first", $params)) { - $first = $this->safeGet($params, "first", ""); - $last = $this->safeGet($params, "last", ""); + if (array_key_exists('first', $params)) { + $first = $this->safeGet($params, 'first', ''); + $last = $this->safeGet($params, 'last', ''); return trim("$first $last"); } - return $this->safeGet($params, "full_name", ""); + return $this->safeGet($params, 'full_name', ''); } /** * @param string $address - * @param array $variables + * @param array $variables + * * @return string */ protected function parseAddress($address, $variables) @@ -101,7 +104,7 @@ class MessageBuilder /** * @param string $headerName * @param string $address - * @param array $variables + * @param array $variables */ protected function addRecipient($headerName, $address, $variables) { @@ -109,10 +112,10 @@ class MessageBuilder if (isset($this->message[$headerName])) { array_push($this->message[$headerName], $compiledAddress); - } elseif ($headerName == "h:reply-to") { + } elseif ($headerName == 'h:reply-to') { $this->message[$headerName] = $compiledAddress; } else { - $this->message[$headerName] = array($compiledAddress); + $this->message[$headerName] = [$compiledAddress]; } if (array_key_exists($headerName, $this->counters['recipients'])) { $this->counters['recipients'][$headerName] += 1; @@ -120,85 +123,94 @@ class MessageBuilder } /** - * @param string $address + * @param string $address * @param array|null $variables - * @return mixed + * * @throws TooManyParameters + * + * @return mixed */ public function addToRecipient($address, $variables = null) { if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("to", $address, $variables); + $this->addRecipient('to', $address, $variables); return end($this->message['to']); } /** - * @param string $address + * @param string $address * @param array|null $variables - * @return mixed + * * @throws TooManyParameters + * + * @return mixed */ public function addCcRecipient($address, $variables = null) { if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("cc", $address, $variables); + $this->addRecipient('cc', $address, $variables); return end($this->message['cc']); } /** - * @param string $address + * @param string $address * @param array|null $variables - * @return mixed + * * @throws TooManyParameters + * + * @return mixed */ public function addBccRecipient($address, $variables = null) { if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("bcc", $address, $variables); + $this->addRecipient('bcc', $address, $variables); return end($this->message['bcc']); } /** - * @param string $address + * @param string $address * @param array|null $variables + * * @return mixed */ public function setFromAddress($address, $variables = null) { - $this->addRecipient("from", $address, $variables); + $this->addRecipient('from', $address, $variables); return $this->message['from']; } /** - * @param string $address + * @param string $address * @param array|null $variables + * * @return mixed */ public function setReplyToAddress($address, $variables = null) { - $this->addRecipient("h:reply-to", $address, $variables); + $this->addRecipient('h:reply-to', $address, $variables); return $this->message['h:reply-to']; } /** * @param string $subject + * * @return mixed */ - public function setSubject($subject = "") + public function setSubject($subject = '') { - if ($subject == null || $subject == "") { - $subject = " "; + if ($subject == null || $subject == '') { + $subject = ' '; } $this->message['subject'] = $subject; @@ -207,27 +219,29 @@ class MessageBuilder /** * @param string $headerName - * @param mixed $headerData + * @param mixed $headerData + * * @return mixed */ public function addCustomHeader($headerName, $headerData) { - if (!preg_match("/^h:/i", $headerName)) { - $headerName = "h:" . $headerName; + if (!preg_match('/^h:/i', $headerName)) { + $headerName = 'h:'.$headerName; } - $this->message[$headerName] = array($headerData); + $this->message[$headerName] = [$headerData]; return $this->message[$headerName]; } /** * @param string $textBody + * * @return string */ public function setTextBody($textBody) { - if ($textBody == null || $textBody == "") { - $textBody = " "; + if ($textBody == null || $textBody == '') { + $textBody = ' '; } $this->message['text'] = $textBody; @@ -236,12 +250,13 @@ class MessageBuilder /** * @param string $htmlBody + * * @return string */ public function setHtmlBody($htmlBody) { - if ($htmlBody == null || $htmlBody == "") { - $htmlBody = " "; + if ($htmlBody == null || $htmlBody == '') { + $htmlBody = ' '; } $this->message['html'] = $htmlBody; @@ -249,53 +264,55 @@ class MessageBuilder } /** - * @param string $attachmentPath + * @param string $attachmentPath * @param string|null $attachmentName + * * @return bool */ public function addAttachment($attachmentPath, $attachmentName = null) { - if (isset($this->files["attachment"])) { - $attachment = array( + if (isset($this->files['attachment'])) { + $attachment = [ 'filePath' => $attachmentPath, - 'remoteName' => $attachmentName - ); - array_push($this->files["attachment"], $attachment); + 'remoteName' => $attachmentName, + ]; + array_push($this->files['attachment'], $attachment); } else { - $this->files["attachment"] = array( - array( + $this->files['attachment'] = [ + [ 'filePath' => $attachmentPath, - 'remoteName' => $attachmentName - ) - ); + 'remoteName' => $attachmentName, + ], + ]; } return true; } /** - * @param string $inlineImagePath + * @param string $inlineImagePath * @param string|null $inlineImageName * - * @return bool|true * @throws InvalidParameter + * + * @return bool|true */ public function addInlineImage($inlineImagePath, $inlineImageName = null) { if (strpos($inlineImagePath, '@') === 0) { if (isset($this->files['inline'])) { - $inlineAttachment = array( + $inlineAttachment = [ 'filePath' => $inlineImagePath, - 'remoteName' => $inlineImageName - ); + 'remoteName' => $inlineImageName, + ]; array_push($this->files['inline'], $inlineAttachment); } else { - $this->files['inline'] = array( - array( + $this->files['inline'] = [ + [ 'filePath' => $inlineImagePath, - 'remoteName' => $inlineImageName - ) - ); + 'remoteName' => $inlineImageName, + ], + ]; } return true; @@ -305,15 +322,16 @@ class MessageBuilder } /** - * @param boolean $testMode + * @param bool $testMode + * * @return string */ public function setTestMode($testMode) { if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) { - $testMode = "yes"; + $testMode = 'yes'; } else { - $testMode = "no"; + $testMode = 'no'; } $this->message['o:testmode'] = $testMode; @@ -322,8 +340,10 @@ class MessageBuilder /** * @param string|int $campaignId - * @return string|int + * * @throws TooManyParameters + * + * @return string|int */ public function addCampaignId($campaignId) { @@ -331,7 +351,7 @@ class MessageBuilder if (isset($this->message['o:campaign'])) { array_push($this->message['o:campaign'], $campaignId); } else { - $this->message['o:campaign'] = array($campaignId); + $this->message['o:campaign'] = [$campaignId]; } $this->counters['attributes']['campaign_id'] += 1; @@ -343,6 +363,7 @@ class MessageBuilder /** * @param string $tag + * * @throws TooManyParameters */ public function addTag($tag) @@ -351,7 +372,7 @@ class MessageBuilder if (isset($this->message['o:tag'])) { array_push($this->message['o:tag'], $tag); } else { - $this->message['o:tag'] = array($tag); + $this->message['o:tag'] = [$tag]; } $this->counters['attributes']['tag'] += 1; @@ -362,31 +383,33 @@ class MessageBuilder } /** - * @param boolean $enabled + * @param bool $enabled + * * @return mixed */ public function setDkim($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; + $enabled = 'yes'; } else { - $enabled = "no"; + $enabled = 'no'; } - $this->message["o:dkim"] = $enabled; + $this->message['o:dkim'] = $enabled; - return $this->message["o:dkim"]; + return $this->message['o:dkim']; } /** - * @param boolean $enabled + * @param bool $enabled + * * @return string */ public function setOpenTracking($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; + $enabled = 'yes'; } else { - $enabled = "no"; + $enabled = 'no'; } $this->message['o:tracking-opens'] = $enabled; @@ -394,17 +417,18 @@ class MessageBuilder } /** - * @param boolean $enabled + * @param bool $enabled + * * @return string */ public function setClickTracking($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; - } elseif ($enabled == "html") { - $enabled = "html"; + $enabled = 'yes'; + } elseif ($enabled == 'html') { + $enabled = 'html'; } else { - $enabled = "no"; + $enabled = 'no'; } $this->message['o:tracking-clicks'] = $enabled; @@ -412,8 +436,9 @@ class MessageBuilder } /** - * @param string $timeDate + * @param string $timeDate * @param string|null $timeZone + * * @return string */ public function setDeliveryTime($timeDate, $timeZone = null) @@ -424,8 +449,8 @@ class MessageBuilder $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE); } - $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); - $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); + $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); + $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); $this->message['o:deliverytime'] = $formattedTimeDate; return $this->message['o:deliverytime']; @@ -433,16 +458,17 @@ class MessageBuilder /** * @param string $customName - * @param mixed $data + * @param mixed $data */ public function addCustomData($customName, $data) { - $this->message['v:' . $customName] = json_encode($data); + $this->message['v:'.$customName] = json_encode($data); } /** * @param string $parameterName - * @param mixed $data + * @param mixed $data + * * @return mixed */ public function addCustomParameter($parameterName, $data) @@ -452,7 +478,7 @@ class MessageBuilder return $this->message[$parameterName]; } else { - $this->message[$parameterName] = array($data); + $this->message[$parameterName] = [$data]; return $this->message[$parameterName]; } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index da835f1..53a3686 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,5 +1,4 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); $this->optInHandler = $this->client->OptInHandler(); } @@ -23,7 +23,7 @@ class OptInHandler extends \Mailgun\Tests\MailgunTestCase 'mysupersecretappid', 'testrecipient@example.com' ); - $knownHash = "eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D"; + $knownHash = 'eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D'; $this->assertEquals($generatedHash, $knownHash); } diff --git a/tests/Mailgun/Tests/MailgunTest.php b/tests/Mailgun/Tests/MailgunTest.php index 38a6905..204d2f5 100644 --- a/tests/Mailgun/Tests/MailgunTest.php +++ b/tests/Mailgun/Tests/MailgunTest.php @@ -1,42 +1,45 @@ setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $client = new Mailgun(); - $client->sendMessage("test.mailgun.com", "etss", 1); + $client->sendMessage('test.mailgun.com', 'etss', 1); } - public function testVerifyWebhookGood() { + public function testVerifyWebhookGood() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array( + $postData = [ 'timestamp' => '1403645220', - 'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6', + 'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', - ); + ]; assert($client->verifyWebhookSignature($postData)); } - public function testVerifyWebhookBad() { + public function testVerifyWebhookBad() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array( + $postData = [ 'timestamp' => '1403645220', - 'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr', + 'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', - ); + ]; assert(!$client->verifyWebhookSignature($postData)); } - public function testVerifyWebhookEmptyRequest() { + public function testVerifyWebhookEmptyRequest() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array(); + $postData = []; assert(!$client->verifyWebhookSignature($postData)); } } diff --git a/tests/Mailgun/Tests/Messages/BatchMessageTest.php b/tests/Mailgun/Tests/Messages/BatchMessageTest.php index 05a39a1..a1072fb 100644 --- a/tests/Mailgun/Tests/Messages/BatchMessageTest.php +++ b/tests/Mailgun/Tests/Messages/BatchMessageTest.php @@ -1,17 +1,17 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); } public function testBlankInstantiation() @@ -23,12 +23,12 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testAddRecipient() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['to']); @@ -37,78 +37,78 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testRecipientVariablesOnTo() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnCc() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("cc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['cc' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnBcc() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['bcc' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testAddMultipleBatchRecipients() { $message = $this->client->BatchMessage($this->sampleDomain); for ($i = 0; $i < 100; $i++) { - $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i")); + $message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $messageObj = $message->getMessage(); - $this->assertEquals(100, count($messageObj["to"])); + $this->assertEquals(100, count($messageObj['to'])); } public function testMaximumBatchSize() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); for ($i = 0; $i < 1001; $i++) { - $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i")); + $message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $messageObj = $message->getMessage(); - $this->assertEquals(1, count($messageObj["to"])); + $this->assertEquals(1, count($messageObj['to'])); } public function testAttributeResetOnEndBatchMessage() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); $message->finalize(); $messageObj = $message->getMessage(); $this->assertTrue(true, empty($messageObj)); @@ -117,10 +117,10 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testDefaultIDInVariables() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); $this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']); @@ -129,44 +129,44 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testgetMessageIds() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); $message->finalize(); - $this->assertEquals(array("1234"), $message->getMessageIds()); + $this->assertEquals(['1234'], $message->getMessageIds()); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array(1, 2, 3)); + $message->sendMessage([1, 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, 2, 3)); + $message->sendMessage(['from' => 1, 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, "to" => 2, 3)); + $message->sendMessage(['from' => 1, 'to' => 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, "to" => 2, "subject" => 3)); + $message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]); } } diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index 8daf2e3..d32bf78 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -1,4 +1,5 @@ client->MessageBuilder(); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $propertyValue = $property->getValue($message); $this->assertEquals(0, $propertyValue['recipients']['to']); @@ -38,34 +39,34 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testAddToRecipient() { $message = $this->client->MessageBuilder(); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); } public function testAddCcRecipient() { $message = $this->client->MessageBuilder(); - $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("cc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['cc' => ["'Test User' "]], $messageObj); } public function testAddBccRecipient() { $message = $this->client->MessageBuilder(); - $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['bcc' => ["'Test User' "]], $messageObj); } public function testToRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['to']); @@ -74,10 +75,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testCcRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addCcRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['cc']); @@ -86,10 +87,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testBccRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addBccRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['bcc']); @@ -98,89 +99,89 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testSetFromAddress() { $message = $this->client->MessageBuilder(); - $message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->setFromAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("from" => array("'Test User' ")), $messageObj); + $this->assertEquals(['from' => ["'Test User' "]], $messageObj); } public function testSetReplyTo() { $message = $this->client->MessageBuilder(); - $message->setReplyToAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->setReplyToAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("h:reply-to" => "'Test User' "), $messageObj); + $this->assertEquals(['h:reply-to' => "'Test User' "], $messageObj); } public function testSetSubject() { $message = $this->client->MessageBuilder(); - $message->setSubject("Test Subject"); + $message->setSubject('Test Subject'); $messageObj = $message->getMessage(); - $this->assertEquals(array("subject" => "Test Subject"), $messageObj); + $this->assertEquals(['subject' => 'Test Subject'], $messageObj); } public function testAddCustomHeader() { $message = $this->client->MessageBuilder(); - $message->addCustomHeader("My-Header", "123"); + $message->addCustomHeader('My-Header', '123'); $messageObj = $message->getMessage(); - $this->assertEquals(array("h:My-Header" => array("123")), $messageObj); + $this->assertEquals(['h:My-Header' => ['123']], $messageObj); } public function testSetTextBody() { $message = $this->client->MessageBuilder(); - $message->setTextBody("This is the text body!"); + $message->setTextBody('This is the text body!'); $messageObj = $message->getMessage(); - $this->assertEquals(array("text" => "This is the text body!"), $messageObj); + $this->assertEquals(['text' => 'This is the text body!'], $messageObj); } public function testSetHtmlBody() { $message = $this->client->MessageBuilder(); - $message->setHtmlBody("This is an awesome email"); + $message->setHtmlBody('This is an awesome email'); $messageObj = $message->getMessage(); - $this->assertEquals(array("html" => "This is an awesome email"), $messageObj); + $this->assertEquals(['html' => 'This is an awesome email'], $messageObj); } public function testAddAttachments() { $message = $this->client->MessageBuilder(); - $message->addAttachment("@../TestAssets/mailgun_icon.png"); - $message->addAttachment("@../TestAssets/rackspace_logo.png"); + $message->addAttachment('@../TestAssets/mailgun_icon.png'); + $message->addAttachment('@../TestAssets/rackspace_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( - 'filePath' => "@../TestAssets/mailgun_icon.png", - 'remoteName' => null - ), - array( - 'filePath' => "@../TestAssets/rackspace_logo.png", - 'remoteName' => null - ) - ), - $messageObj["attachment"] + [ + [ + 'filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => null, + ], + [ + 'filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => null, + ], + ], + $messageObj['attachment'] ); } public function testAddInlineImages() { $message = $this->client->MessageBuilder(); - $message->addInlineImage("@../TestAssets/mailgun_icon.png"); - $message->addInlineImage("@../TestAssets/rackspace_logo.png"); + $message->addInlineImage('@../TestAssets/mailgun_icon.png'); + $message->addInlineImage('@../TestAssets/rackspace_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( - 'filePath' => "@../TestAssets/mailgun_icon.png", - 'remoteName' => null - ), - array( - 'filePath' => "@../TestAssets/rackspace_logo.png", - 'remoteName' => null - ) - ), + [ + [ + 'filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => null, + ], + [ + 'filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => null, + ], + ], $messageObj['inline'] ); } @@ -192,17 +193,17 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( + [ + [ 'filePath' => '@../TestAssets/mailgun_icon.png', - 'remoteName' => 'mg_icon.png' - ), - array( + 'remoteName' => 'mg_icon.png', + ], + [ 'filePath' => '@../TestAssets/rackspace_logo.png', - 'remoteName' => 'rs_logo.png' - ) - ), - $messageObj["attachment"] + 'remoteName' => 'rs_logo.png', + ], + ], + $messageObj['attachment'] ); } @@ -213,16 +214,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( + [ + [ 'filePath' => '@../TestAssets/mailgun_icon.png', - 'remoteName' => 'mg_icon.png' - ), - array( + 'remoteName' => 'mg_icon.png', + ], + [ 'filePath' => '@../TestAssets/rackspace_logo.png', - 'remoteName' => 'rs_logo.png' - ) - ), + 'remoteName' => 'rs_logo.png', + ], + ], $messageObj['inline'] ); } @@ -232,27 +233,27 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setTestMode(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "yes"), $messageObj); + $this->assertEquals(['o:testmode' => 'yes'], $messageObj); $message->setTestMode(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "no"), $messageObj); - $message->setTestMode("yes"); + $this->assertEquals(['o:testmode' => 'no'], $messageObj); + $message->setTestMode('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "yes"), $messageObj); - $message->setTestMode("no"); + $this->assertEquals(['o:testmode' => 'yes'], $messageObj); + $message->setTestMode('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "no"), $messageObj); + $this->assertEquals(['o:testmode' => 'no'], $messageObj); } public function addCampaignId() { $message = $this->client->MessageBuilder(); - $message->addCampaignId("ABC123"); - $message->addCampaignId("XYZ987"); - $message->addCampaignId("TUV456"); - $message->addCampaignId("NONO123"); + $message->addCampaignId('ABC123'); + $message->addCampaignId('XYZ987'); + $message->addCampaignId('TUV456'); + $message->addCampaignId('NONO123'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:campaign" => array("ABC123", "XYZ987", "TUV456")), $messageObj); + $this->assertEquals(['o:campaign' => ['ABC123', 'XYZ987', 'TUV456']], $messageObj); } public function testSetDkim() @@ -260,16 +261,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setDkim(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "yes"), $messageObj); + $this->assertEquals(['o:dkim' => 'yes'], $messageObj); $message->setDkim(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "no"), $messageObj); - $message->setDkim("yes"); + $this->assertEquals(['o:dkim' => 'no'], $messageObj); + $message->setDkim('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "yes"), $messageObj); - $message->setDkim("no"); + $this->assertEquals(['o:dkim' => 'yes'], $messageObj); + $message->setDkim('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "no"), $messageObj); + $this->assertEquals(['o:dkim' => 'no'], $messageObj); } public function testSetClickTracking() @@ -277,16 +278,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setClickTracking(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); + $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj); $message->setClickTracking(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); - $message->setClickTracking("yes"); + $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj); + $message->setClickTracking('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); - $message->setClickTracking("no"); + $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj); + $message->setClickTracking('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); + $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj); } public function testSetOpenTracking() @@ -294,33 +295,33 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setOpenTracking(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); + $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj); $message->setOpenTracking(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); - $message->setOpenTracking("yes"); + $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj); + $message->setOpenTracking('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); - $message->setOpenTracking("no"); + $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj); + $message->setOpenTracking('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); + $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj); } public function testSetDeliveryTime() { $message = $this->client->MessageBuilder(); - $message->setDeliveryTime("January 15, 2014 8:00AM", "CST"); + $message->setDeliveryTime('January 15, 2014 8:00AM', 'CST'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 -0600"), $messageObj); - $message->setDeliveryTime("January 15, 2014 8:00AM", "UTC"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 -0600'], $messageObj); + $message->setDeliveryTime('January 15, 2014 8:00AM', 'UTC'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); - $message->setDeliveryTime("January 15, 2014 8:00AM"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj); + $message->setDeliveryTime('January 15, 2014 8:00AM'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); - $message->setDeliveryTime("1/15/2014 13:50:01", "CDT"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj); + $message->setDeliveryTime('1/15/2014 13:50:01', 'CDT'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 13:50:01 -0600"), $messageObj); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 13:50:01 -0600'], $messageObj); // https://github.com/mailgun/mailgun-php/pull/42 // https://github.com/mailgun/mailgun-php/issues/43 //$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT"); @@ -331,23 +332,23 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testAddCustomData() { $message = $this->client->MessageBuilder(); - $message->addCustomData("My-Super-Awesome-Data", array("What" => "Mailgun Rocks!")); + $message->addCustomData('My-Super-Awesome-Data', ['What' => 'Mailgun Rocks!']); $messageObj = $message->getMessage(); - $this->assertEquals(array("v:My-Super-Awesome-Data" => "{\"What\":\"Mailgun Rocks!\"}"), $messageObj); + $this->assertEquals(['v:My-Super-Awesome-Data' => '{"What":"Mailgun Rocks!"}'], $messageObj); } public function testAddCustomParameter() { $message = $this->client->MessageBuilder(); - $message->addCustomParameter("my-option", "yes"); - $message->addCustomParameter("o:my-other-option", "no"); + $message->addCustomParameter('my-option', 'yes'); + $message->addCustomParameter('o:my-other-option', 'no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("my-option" => array("yes"), "o:my-other-option" => array("no")), $messageObj); + $this->assertEquals(['my-option' => ['yes'], 'o:my-other-option' => ['no']], $messageObj); } public function testSetMessage() { - $message = array(1, 2, 3, 4, 5); + $message = [1, 2, 3, 4, 5]; $messageBuilder = $this->client->MessageBuilder(); $messageBuilder->setMessage($message); diff --git a/tests/Mailgun/Tests/Messages/StandardMessageTest.php b/tests/Mailgun/Tests/Messages/StandardMessageTest.php index b1f885d..b3f95c3 100644 --- a/tests/Mailgun/Tests/Messages/StandardMessageTest.php +++ b/tests/Mailgun/Tests/Messages/StandardMessageTest.php @@ -1,24 +1,24 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); } public function testSendMIMEMessage() { - $customMime = "Received: by luna.mailgun.net with SMTP mgrt 8728174999085; Mon, 10 Jun 2013 09:50:58 +0000 + $customMime = 'Received: by luna.mailgun.net with SMTP mgrt 8728174999085; Mon, 10 Jun 2013 09:50:58 +0000 Mime-Version: 1.0 - Content-Type: text/plain; charset=\"ascii\" + Content-Type: text/plain; charset="ascii" Subject: This is the Subject! From: Mailgun Testing To: test@test.mailgun.com @@ -28,21 +28,20 @@ class StandardMessageTest extends \Mailgun\Tests\MailgunTestCase Date: Mon, 10 Jun 2013 09:50:58 +0000 Sender: test@test.mailgun.com - Mailgun is testing!"; - $envelopeFields = array('to' => 'test@test.mailgun.org'); - $result = $this->client->sendMessage("test.mailgun.org", $envelopeFields, $customMime); - $this->assertEquals("test.mailgun.org/messages.mime", $result->http_endpoint_url); + Mailgun is testing!'; + $envelopeFields = ['to' => 'test@test.mailgun.org']; + $result = $this->client->sendMessage('test.mailgun.org', $envelopeFields, $customMime); + $this->assertEquals('test.mailgun.org/messages.mime', $result->http_endpoint_url); } public function testSendMessage() { - $message = array('to' => 'test@test.mailgun.org', + $message = ['to' => 'test@test.mailgun.org', 'from' => 'sender@test.mailgun.org', 'subject' => 'This is my test subject', - 'text' => 'Testing!' - ); - $result = $this->client->sendMessage("test.mailgun.org", $message); - $this->assertEquals("test.mailgun.org/messages", $result->http_endpoint_url); - + 'text' => 'Testing!', + ]; + $result = $this->client->sendMessage('test.mailgun.org', $message); + $this->assertEquals('test.mailgun.org/messages', $result->http_endpoint_url); } } diff --git a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php index b1dee7e..01d860e 100644 --- a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php +++ b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php @@ -1,4 +1,5 @@ apiKey = $apiKey; + $this->apiKey = $apiKey; $this->apiEndpoint = $apiHost; } - public function post($endpointUrl, array $postData = array(), $files = array()) + public function post($endpointUrl, array $postData = [], $files = []) { return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200); } - public function get($endpointUrl, $queryString = array()) + public function get($endpointUrl, $queryString = []) { return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200); } @@ -42,9 +43,9 @@ class TestBroker extends RestClient public function testResponseHandler($endpointUrl, $httpResponseCode = 200) { if ($httpResponseCode === 200) { - $result = new \stdClass(); + $result = new \stdClass(); $result->http_response_body = new \stdClass(); - $jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}'); + $jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}'); foreach ($jsonResponseData as $key => $value) { $result->http_response_body->$key = $value; } @@ -62,10 +63,8 @@ class TestBroker extends RestClient return false; } $result->http_response_code = $httpResponseCode; - $result->http_endpoint_url = $endpointUrl; + $result->http_endpoint_url = $endpointUrl; return $result; } - - } diff --git a/tests/Mailgun/Tests/Mock/Mailgun.php b/tests/Mailgun/Tests/Mock/Mailgun.php index ad57bcc..38e8651 100644 --- a/tests/Mailgun/Tests/Mock/Mailgun.php +++ b/tests/Mailgun/Tests/Mock/Mailgun.php @@ -10,7 +10,7 @@ class Mailgun extends Base protected $debug; protected $restClient; - public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3") + public function __construct($apiKey = null, $apiEndpoint = 'api.mailgun.net', $apiVersion = 'v3') { $this->restClient = new TestBroker($apiKey, $apiEndpoint, $apiVersion); }