diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 21d57da5..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "tools/phptools"] - path = tools/phptools - url = git://github.com/ralphschindler/PHPTools.git diff --git a/.travis/run-tests.sh b/.travis/run-tests.sh deleted file mode 100755 index a84e0ba2..00000000 --- a/.travis/run-tests.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -travisdir=$(dirname "$0") -testdir="$travisdir/../tests" -testedcomponents=(`cat "$travisdir/tested-components"`) -result=0 - -for tested in "${testedcomponents[@]}" - do - echo "$tested:" - phpunit -c $testdir/phpunit.xml.dist $testdir/$tested - result=$(($result || $?)) -done - -exit $result diff --git a/.travis/skipped-components b/.travis/skipped-components deleted file mode 100644 index 171dfe9d..00000000 --- a/.travis/skipped-components +++ /dev/null @@ -1,7 +0,0 @@ -Zend/Amf -Zend/Date -Zend/Dojo -Zend/Queue -Zend/Service -Zend/Test -Zend/Wildfire diff --git a/.travis/tested-components b/.travis/tested-components deleted file mode 100644 index b0b94380..00000000 --- a/.travis/tested-components +++ /dev/null @@ -1,61 +0,0 @@ -Zend/Acl -Zend/Authentication -Zend/Barcode -Zend/Cache -Zend/Captcha -Zend/Cloud -Zend/Code -Zend/Config -Zend/Console -Zend/Crypt -Zend/Currency -Zend/Db -Zend/Di -Zend/DocBook -Zend/Dojo -Zend/Dom -Zend/EventManager -Zend/Feed -Zend/File -Zend/Filter -Zend/Form -Zend/GData -Zend/Http -Zend/InfoCard -Zend/InputFilter -Zend/Json -Zend/Ldap -Zend/Loader -Zend/Locale -Zend/Log -Zend/Mail -Zend/Markup -Zend/Math -Zend/Measure -Zend/Memory -Zend/Mime -Zend/ModuleManager -Zend/Mvc -Zend/Navigation -Zend/OAuth -Zend/OpenId -Zend/Paginator -Zend/Pdf -Zend/ProgressBar -Zend/RegistryTest.php -Zend/Rest -Zend/Search -Zend/Serializer -Zend/Server -Zend/Session -Zend/Soap -Zend/Stdlib -Zend/Tag -Zend/Text -Zend/TimeSync -Zend/Translator -Zend/Uri -Zend/Validator -Zend/VersionTest.php -Zend/View -Zend/XmlRpc diff --git a/composer.json b/composer.json index 2a3d0f3b..5694d107 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "zendframework/zend-mail", - "description": "Zend\\Mail component", + "description": "provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages", "license": "BSD-3-Clause", "keywords": [ "zf2", @@ -9,11 +9,11 @@ "homepage": "https://github.com/zendframework/zend-mail", "autoload": { "psr-4": { - "Zend\\Mail\\": "src/" + "Zend\\Mail": "src/" } }, "require": { - "php": ">=5.3.23", + "php": ">=5.3.3", "zendframework/zend-crypt": "self.version", "zendframework/zend-loader": "self.version", "zendframework/zend-mime": "self.version", @@ -27,7 +27,8 @@ "phpunit/PHPUnit": "~4.0" }, "suggest": { - "zendframework/zend-servicemanager": "Zend\\ServiceManager component" + "zendframework/zend-servicemanager": "Zend\\ServiceManager component", + "zendframework/zend-validator": "Zend\\Validator component" }, "extra": { "branch-alias": { diff --git a/src/Address.php b/src/Address.php index d3bdb369..397afb00 100644 --- a/src/Address.php +++ b/src/Address.php @@ -1,21 +1,11 @@ setEncoding('UTF-8'); + } // split value on "," $fieldValue = str_replace(Headers::FOLDING, ' ', $fieldValue); $values = explode(',', $fieldValue); @@ -97,8 +78,6 @@ public static function fromString($headerLine) } if (empty($name)) { $name = null; - } else { - $name = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR); } if (isset($matches['namedEmail'])) { @@ -112,26 +91,15 @@ public static function fromString($headerLine) // populate address list $addressList->add($email, $name); } - return $header; } - /** - * Get field name of this header - * - * @return string - */ public function getFieldName() { return $this->fieldName; } - /** - * Get field value of this header - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { $emails = array(); $encoding = $this->getEncoding(); @@ -145,33 +113,24 @@ public function getFieldValue() $name = sprintf('"%s"', $name); } - if ('ASCII' !== $encoding) { - $name = HeaderWrap::mimeEncodeValue($name, $encoding, false); + if ($format == HeaderInterface::FORMAT_ENCODED + && 'ASCII' !== $encoding + ) { + $name = HeaderWrap::mimeEncodeValue($name, $encoding); } $emails[] = sprintf('%s <%s>', $name, $email); } } - $string = implode(',' . Headers::FOLDING, $emails); - return $string; + + return implode(',' . Headers::FOLDING, $emails); } - /** - * Set header encoding - * - * @param string $encoding - * @return AbstractAddressList - */ public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { return $this->encoding; @@ -200,15 +159,10 @@ public function getAddressList() return $this->addressList; } - /** - * Serialize to string - * - * @return string - */ public function toString() { $name = $this->getFieldName(); - $value = $this->getFieldValue(); + $value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED); return (empty($value)) ? '' : sprintf('%s: %s', $name, $value); } } diff --git a/src/Header/Bcc.php b/src/Header/Bcc.php index 6df7fa3e..673353a7 100644 --- a/src/Header/Bcc.php +++ b/src/Header/Bcc.php @@ -1,22 +1,11 @@ addParameter($key, $value); } } - + return $header; } - /** - * Get header name - * - * @return string - */ public function getFieldName() { return 'Content-Type'; } - /** - * Get header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { $prepared = $this->type; if (empty($this->parameters)) { @@ -110,37 +73,21 @@ public function getFieldValue() foreach ($this->parameters as $attribute => $value) { $values[] = sprintf('%s="%s"', $attribute, $value); } - $value = implode(';' . Headers::FOLDING, $values); - return $value; + + return implode(';' . Headers::FOLDING, $values); } - /** - * Set header encoding - * - * @param string $encoding - * @return ContentType - */ - public function setEncoding($encoding) + public function setEncoding($encoding) { - $this->encoding = $encoding; + // This header must be always in US-ASCII return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { - return $this->encoding; + return 'ASCII'; } - /** - * Serialize header to string - * - * @return string - */ public function toString() { return 'Content-Type: ' . $this->getFieldValue(); @@ -168,7 +115,7 @@ public function setType($type) /** * Retrieve the content type - * + * * @return string */ public function getType() @@ -178,9 +125,9 @@ public function getType() /** * Add a parameter pair - * - * @param string $name - * @param string $value + * + * @param string $name + * @param string $value * @return ContentType */ public function addParameter($name, $value) @@ -192,7 +139,7 @@ public function addParameter($name, $value) /** * Get all parameters - * + * * @return array */ public function getParameters() @@ -202,8 +149,8 @@ public function getParameters() /** * Get a parameter by name - * - * @param string $name + * + * @param string $name * @return null|string */ public function getParameter($name) @@ -217,8 +164,8 @@ public function getParameter($name) /** * Remove a named parameter - * - * @param string $name + * + * @param string $name * @return bool */ public function removeParameter($name) diff --git a/src/Header/Date.php b/src/Header/Date.php index b4f7c771..494d9817 100644 --- a/src/Header/Date.php +++ b/src/Header/Date.php @@ -1,22 +1,11 @@ value= $value; - + return $header; } - /** - * Get the header name - * - * @return string - */ public function getFieldName() { return 'Date'; } - /** - * Get the header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { return $this->value; } - /** - * Set header encoding - * - * @param string $encoding - * @return AbstractAddressList - */ - public function setEncoding($encoding) + public function setEncoding($encoding) { - $this->encoding = $encoding; + // This header must be always in US-ASCII return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { - return $this->encoding; + return 'ASCII'; } - /** - * Serialize header to string - * - * @return string - */ public function toString() { return 'Date: ' . $this->getFieldValue(); diff --git a/src/Header/Exception/BadMethodCallException.php b/src/Header/Exception/BadMethodCallException.php index 80292958..5589c51e 100644 --- a/src/Header/Exception/BadMethodCallException.php +++ b/src/Header/Exception/BadMethodCallException.php @@ -1,22 +1,11 @@ setEncoding('UTF-8'); + } return $header; } /** * Constructor - * + * * @param string $fieldName Optional * @param string $fieldValue Optional */ @@ -108,11 +92,6 @@ public function setFieldName($fieldName) return $this; } - /** - * Retrieve header name - * - * @return string - */ public function getFieldName() { return $this->fieldName; @@ -120,7 +99,7 @@ public function getFieldName() /** * Set header value - * + * * @param string $fieldValue * @return GenericHeader */ @@ -136,49 +115,30 @@ public function setFieldValue($fieldValue) return $this; } - /** - * Retrieve header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { + if (HeaderInterface::FORMAT_ENCODED) { + return HeaderWrap::wrap($this->fieldValue, $this); + } + return $this->fieldValue; } - /** - * Set header encoding - * - * @param string $encoding - * @return GenericHeader - */ - public function setEncoding($encoding) + public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { return $this->encoding; } - /** - * Cast to string - * - * Returns in form of "NAME: VALUE" - * - * @return string - */ public function toString() { $name = $this->getFieldName(); - $value = $this->getFieldValue(); + $value = $this->getFieldValue(HeaderInterface::FORMAT_ENCODED); return $name. ': ' . $value; } diff --git a/src/Header/GenericMultiHeader.php b/src/Header/GenericMultiHeader.php index a7938783..033a079e 100644 --- a/src/Header/GenericMultiHeader.php +++ b/src/Header/GenericMultiHeader.php @@ -1,22 +1,11 @@ setEncoding($encoding); + } return $headers; } else { $header = new static($fieldName, $fieldValue); + if ($decodedLine != $headerLine) { + $header->setEncoding('UTF-8'); + } return $header; } } @@ -69,14 +56,14 @@ public static function fromString($headerLine) public function toStringMultipleHeaders(array $headers) { $name = $this->getFieldName(); - $values = array($this->getFieldValue()); + $values = array($this->getFieldValue(HeaderInterface::FORMAT_ENCODED)); foreach ($headers as $header) { if (!$header instanceof static) { throw new Exception\InvalidArgumentException( 'This method toStringMultipleHeaders was expecting an array of headers of the same type' ); } - $values[] = $header->getFieldValue(); + $values[] = $header->getFieldValue(HeaderInterface::FORMAT_ENCODED); } return $name. ': ' . implode(',', $values); } diff --git a/src/Header/HeaderInterface.php b/src/Header/HeaderInterface.php index c0f59531..4f82f10f 100644 --- a/src/Header/HeaderInterface.php +++ b/src/Header/HeaderInterface.php @@ -1,22 +1,11 @@ 'Zend\Mail\Header\ContentType', 'date' => 'Zend\Mail\Header\Date', 'from' => 'Zend\Mail\Header\From', + 'message-id' => 'Zend\Mail\Header\MessageId', 'mimeversion' => 'Zend\Mail\Header\MimeVersion', 'mime_version' => 'Zend\Mail\Header\MimeVersion', 'mime-version' => 'Zend\Mail\Header\MimeVersion', @@ -45,4 +44,4 @@ class HeaderLoader extends PluginClassLoader 'subject' => 'Zend\Mail\Header\Subject', 'to' => 'Zend\Mail\Header\To', ); -} \ No newline at end of file +} diff --git a/src/Header/HeaderWrap.php b/src/Header/HeaderWrap.php index 86f57fb2..27802246 100644 --- a/src/Header/HeaderWrap.php +++ b/src/Header/HeaderWrap.php @@ -1,27 +1,17 @@ getEncoding(); + if ($encoding == 'ASCII') { + return wordwrap($value, 78, Headers::FOLDING); + } + return static::mimeEncodeValue($value, $encoding, 78); } /** * Wrap a structured header line - * - * @param string $value - * @param HeaderInterface $header + * + * @param string $value + * @param StructuredInterface $header * @return string */ - protected static function wrapStructuredHeader($value, HeaderInterface $header) + protected static function wrapStructuredHeader($value, StructuredInterface $header) { $delimiter = $header->getDelimiter(); @@ -92,34 +85,16 @@ protected static function wrapStructuredHeader($value, HeaderInterface $header) /** * MIME-encode a value * - * Performs quoted-printable encoding on a value, setting maximum - * line-length to 998. - * - * @param string $value - * @param string $encoding - * @param bool $splitWords Whether or not to split the $value on whitespace - * and encode each word separately. - * @return string + * Performs quoted-printable encoding on a value, setting maximum + * line-length to 998. + * + * @param string $value + * @param string $encoding + * @param int $lineLength maximum line-length, by default 998 + * @return string Returns the mime encode value without the last line ending */ - public static function mimeEncodeValue($value, $encoding, $splitWords = false) + public static function mimeEncodeValue($value, $encoding, $lineLength = 998) { - if ($splitWords) { - $words = array_map(function($word) use ($encoding) { - $header = iconv_mime_encode('Header', $word, array( - 'scheme' => 'Q', - 'line-length' => 78, - 'output-charset' => $encoding, - )); - return str_replace('Header: ', '', $header); - }, explode(' ', $value)); - return implode(Headers::FOLDING, $words); - } - - $header = iconv_mime_encode('Header', $value, array( - 'scheme' => 'Q', - 'line-length' => 998, - 'output-charset' => $encoding, - )); - return str_replace('Header: ', '', $header); + return Mime::encodeQuotedPrintableHeader($value, $encoding, $lineLength, Headers::EOL); } } diff --git a/src/Header/MessageId.php b/src/Header/MessageId.php new file mode 100644 index 00000000..9a8f2823 --- /dev/null +++ b/src/Header/MessageId.php @@ -0,0 +1,121 @@ +setId($value); + + return $header; + } + + public function getFieldName() + { + return 'Message-ID'; + } + + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) + { + return $this->messageId; + } + + public function setEncoding($encoding) + { + // This header must be always in US-ASCII + return $this; + } + + public function getEncoding() + { + return 'ASCII'; + } + + public function toString() + { + return 'Message-ID: ' . $this->getFieldValue(); + } + + /** + * Set the message id + * + * @return MessageId + */ + public function setId($id = null) + { + if ($id === null) { + $id = $this->createMessageId(); + } + + $id = sprintf('<%s>', $id); + $this->messageId = $id; + return $this; + } + + /** + * Retrieve the message id + * + * @return string + */ + public function getId() + { + return $this->messageId; + } + + /** + * Creates the Message-ID + * + * @return string + */ + public function createMessageId() { + + $time = time(); + + if (isset($_SERVER['REMOTE_ADDR'])) { + $user = $_SERVER['REMOTE_ADDR']; + } else { + $user = getmypid(); + } + + $rand = mt_rand(); + + if (isset($_SERVER["SERVER_NAME"])) { + $hostName = $_SERVER["SERVER_NAME"]; + } else { + $hostName = php_uname('n'); + } + + return sha1($time . $user . $rand) . '@' . $hostName; + } + +} diff --git a/src/Header/MimeVersion.php b/src/Header/MimeVersion.php index 3f3a6091..5bd57b09 100644 --- a/src/Header/MimeVersion.php +++ b/src/Header/MimeVersion.php @@ -1,22 +1,11 @@ version; } - /** - * Set character encoding - * - * @param string $encoding - */ public function setEncoding($encoding) { - // irrelevant to this implementation + // This header must be always in US-ASCII + return $this; } - /** - * Get character encoding - * - */ public function getEncoding() { - // irrelevant to this implementation + return 'ASCII'; } - /** - * Serialize to string - * - * @return string - */ public function toString() { - return 'Mime-Version: ' . $this->getFieldValue(); + return 'MIME-Version: ' . $this->getFieldValue(); } /** diff --git a/src/Header/MultipleHeadersInterface.php b/src/Header/MultipleHeadersInterface.php index b7c56c69..6e8e228d 100644 --- a/src/Header/MultipleHeadersInterface.php +++ b/src/Header/MultipleHeadersInterface.php @@ -1,22 +1,11 @@ value= $value; - + return $header; } - /** - * Get header name - * - * @return string - */ public function getFieldName() { return 'Received'; } - /** - * Get header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { return $this->value; } - /** - * Set header encoding - * - * @param string $encoding - * @return AbstractAddressList - */ - public function setEncoding($encoding) + public function setEncoding($encoding) { - $this->encoding = $encoding; + // This header must be always in US-ASCII return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { - return $this->encoding; + return 'ASCII'; } - /** - * Serialize to string - * - * @return string - */ public function toString() { return 'Received: ' . $this->getFieldValue(); diff --git a/src/Header/ReplyTo.php b/src/Header/ReplyTo.php index 09583291..945f49fa 100644 --- a/src/Header/ReplyTo.php +++ b/src/Header/ReplyTo.php @@ -1,22 +1,11 @@ setEncoding('UTF-8'); + } // Check for address, and set if found if (preg_match('^(?P.*?)<(?P[^>]+)>$', $value, $matches)) { @@ -69,7 +52,7 @@ public static function fromString($headerLine) if (empty($name)) { $name = null; } else { - $name = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR); + $name = iconv_mime_decode($name, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8'); } $header->setAddress($matches['email'], $name); } @@ -77,22 +60,12 @@ public static function fromString($headerLine) return $header; } - /** - * Get header name - * - * @return string - */ public function getFieldName() { return 'Sender'; } - /** - * Get header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { if (!$this->address instanceof Mail\Address\AddressInterface) { return ''; @@ -102,44 +75,30 @@ public function getFieldValue() $name = $this->address->getName(); if (!empty($name)) { $encoding = $this->getEncoding(); - if ('ASCII' !== $encoding) { - $name = HeaderWrap::mimeEncodeValue($name, $encoding, false); + if ($format == HeaderInterface::FORMAT_ENCODED + && 'ASCII' !== $encoding + ) { + $name = HeaderWrap::mimeEncodeValue($name, $encoding); } $email = sprintf('%s %s', $name, $email); } return $email; } - /** - * Set header encoding - * - * @param string $encoding - * @return Sender - */ public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { return $this->encoding; } - /** - * Serialize to string - * - * @return string - */ public function toString() { - return 'Sender: ' . $this->getFieldValue(); + return 'Sender: ' . $this->getFieldValue(HeaderInterface::FORMAT_ENCODED); } /** diff --git a/src/Header/StructuredInterface.php b/src/Header/StructuredInterface.php index a49d2f2b..15a140cb 100644 --- a/src/Header/StructuredInterface.php +++ b/src/Header/StructuredInterface.php @@ -1,22 +1,11 @@ setEncoding('UTF-8'); + } $header->setSubject($value); - + return $header; } - /** - * Get the header name - * - * @return string - */ public function getFieldName() { return 'Subject'; } - /** - * Get the header value - * - * @return string - */ - public function getFieldValue() + public function getFieldValue($format = HeaderInterface::FORMAT_RAW) { - $encoding = $this->getEncoding(); - if ($encoding == 'ASCII') { + if (HeaderInterface::FORMAT_ENCODED) { return HeaderWrap::wrap($this->subject, $this); } - return HeaderWrap::mimeEncodeValue($this->subject, $encoding, true); + + return $this->subject; } - /** - * Set header encoding - * - * @param string $encoding - * @return Subject - */ - public function setEncoding($encoding) + public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } - /** - * Get header encoding - * - * @return string - */ public function getEncoding() { return $this->encoding; } - /** - * Set the value of the header - * - * @param string $subject - * @return Subject - */ public function setSubject($subject) { $this->subject = (string) $subject; return $this; } - /** - * String representation of header - * - * @return string - */ public function toString() { - return 'Subject: ' . $this->getFieldValue(); + return 'Subject: ' . $this->getFieldValue(HeaderInterface::FORMAT_ENCODED); } } diff --git a/src/Header/To.php b/src/Header/To.php index b2126e05..bd0624eb 100644 --- a/src/Header/To.php +++ b/src/Header/To.php @@ -1,22 +1,11 @@ [^()><@,;:\"\\/\[\]?=}{ \t]+):.*$/', $line, $matches)) { if ($currentLine) { @@ -222,12 +210,15 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null) } if ($fieldValue === null) { - $header = Header\GenericHeader::fromString($headerFieldNameOrLine); + $this->addHeader(Header\GenericHeader::fromString($headerFieldNameOrLine)); + } elseif (is_array($fieldValue)) { + foreach($fieldValue as $i) { + $this->addHeader(new Header\GenericMultiHeader($headerFieldNameOrLine, $i)); + } } else { - $header = new Header\GenericHeader($headerFieldNameOrLine, $fieldValue); + $this->addHeader(new Header\GenericHeader($headerFieldNameOrLine, $fieldValue)); } - $this->addHeader($header); return $this; } @@ -240,10 +231,11 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null) public function addHeader(Header\HeaderInterface $header) { $key = $this->normalizeFieldName($header->getFieldName()); - $this->headersKeys[] = $key; $this->headers[] = $header; - $header->setEncoding($this->getEncoding()); + if ($this->getEncoding() !== 'ASCII') { + $header->setEncoding($this->getEncoding()); + } return $this; } @@ -292,7 +284,11 @@ public function get($name) $results = array(); foreach (array_keys($this->headersKeys, $key) as $index) { - $results[] = $this->headers[$index]; + if ($this->headers[$index] instanceof Header\GenericHeader) { + $results[] = $this->lazyLoadHeader($index); + } else { + $results[] = $this->headers[$index]; + } } switch(count($results)) { @@ -453,7 +449,7 @@ protected function lazyLoadHeader($index) /* @var $class Header\HeaderInterface */ $class = ($this->getPluginClassLoader()->load($key)) ?: 'Zend\Mail\Header\GenericHeader'; - $encoding = $this->getEncoding(); + $encoding = $current->getEncoding(); $headers = $class::fromString($current->toString()); if (is_array($headers)) { $current = array_shift($headers); diff --git a/src/Message.php b/src/Message.php index 153ed7f2..c0f151a1 100644 --- a/src/Message.php +++ b/src/Message.php @@ -1,21 +1,11 @@ from(); + $from = $this->getFrom(); if (!$from instanceof AddressList) { return false; } @@ -70,20 +58,20 @@ public function isValid() /** * Set the message encoding - * - * @param string $encoding + * + * @param string $encoding * @return Message */ public function setEncoding($encoding) { $this->encoding = $encoding; - $this->headers()->setEncoding($encoding); + $this->getHeaders()->setEncoding($encoding); return $this; } /** * Get the message encoding - * + * * @return string */ public function getEncoding() @@ -93,8 +81,8 @@ public function getEncoding() /** * Compose headers - * - * @param Headers $headers + * + * @param Headers $headers * @return Message */ public function setHeaders(Headers $headers) @@ -108,10 +96,10 @@ public function setHeaders(Headers $headers) * Access headers collection * * Lazy-loads if not already attached. - * + * * @return Headers */ - public function headers() + public function getHeaders() { if (null === $this->headers) { $this->setHeaders(new Headers()); @@ -122,9 +110,9 @@ public function headers() /** * Set (overwrite) From addresses - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList - * @param string|null $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList + * @param string|null $name * @return Message */ public function setFrom($emailOrAddressList, $name = null) @@ -135,33 +123,33 @@ public function setFrom($emailOrAddressList, $name = null) /** * Add a "From" address - * - * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList - * @param string|null $name + * + * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList + * @param string|null $name * @return Message */ public function addFrom($emailOrAddressOrList, $name = null) { - $addressList = $this->from(); + $addressList = $this->getFrom(); $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__); return $this; } /** * Retrieve list of From senders - * + * * @return AddressList */ - public function from() + public function getFrom() { return $this->getAddressListFromHeader('from', __NAMESPACE__ . '\Header\From'); } /** * Overwrite the address list in the To recipients - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList - * @param null|string $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList + * @param null|string $name * @return Message */ public function setTo($emailOrAddressList, $name = null) @@ -174,33 +162,33 @@ public function setTo($emailOrAddressList, $name = null) * Add one or more addresses to the To recipients * * Appends to the list. - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList - * @param null|string $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList + * @param null|string $name * @return Message */ public function addTo($emailOrAddressOrList, $name = null) { - $addressList = $this->to(); + $addressList = $this->getTo(); $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__); return $this; } /** * Access the address list of the To header - * + * * @return AddressList */ - public function to() + public function getTo() { return $this->getAddressListFromHeader('to', __NAMESPACE__ . '\Header\To'); } /** * Set (overwrite) CC addresses - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList - * @param string|null $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList + * @param string|null $name * @return Message */ public function setCc($emailOrAddressList, $name = null) @@ -211,33 +199,33 @@ public function setCc($emailOrAddressList, $name = null) /** * Add a "Cc" address - * - * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList - * @param string|null $name + * + * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList + * @param string|null $name * @return Message */ public function addCc($emailOrAddressOrList, $name = null) { - $addressList = $this->cc(); + $addressList = $this->getCc(); $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__); return $this; } /** * Retrieve list of CC recipients - * + * * @return AddressList */ - public function cc() + public function getCc() { return $this->getAddressListFromHeader('cc', __NAMESPACE__ . '\Header\Cc'); } /** * Set (overwrite) BCC addresses - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList - * @param string|null $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList + * @param string|null $name * @return Message */ public function setBcc($emailOrAddressList, $name = null) @@ -248,33 +236,33 @@ public function setBcc($emailOrAddressList, $name = null) /** * Add a "Bcc" address - * - * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList - * @param string|null $name + * + * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList + * @param string|null $name * @return Message */ public function addBcc($emailOrAddressOrList, $name = null) { - $addressList = $this->bcc(); + $addressList = $this->getBcc(); $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__); return $this; } /** * Retrieve list of BCC recipients - * + * * @return AddressList */ - public function bcc() + public function getBcc() { return $this->getAddressListFromHeader('bcc', __NAMESPACE__ . '\Header\Bcc'); } /** * Overwrite the address list in the Reply-To recipients - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList - * @param null|string $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList + * @param null|string $name * @return Message */ public function setReplyTo($emailOrAddressList, $name = null) @@ -287,62 +275,62 @@ public function setReplyTo($emailOrAddressList, $name = null) * Add one or more addresses to the Reply-To recipients * * Appends to the list. - * - * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList - * @param null|string $name + * + * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList + * @param null|string $name * @return Message */ public function addReplyTo($emailOrAddressOrList, $name = null) { - $addressList = $this->replyTo(); + $addressList = $this->getReplyTo(); $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__); return $this; } /** * Access the address list of the Reply-To header - * + * * @return AddressList */ - public function replyTo() + public function getReplyTo() { return $this->getAddressListFromHeader('reply-to', __NAMESPACE__ . '\Header\ReplyTo'); } /** - * setSender - * - * @param mixed $emailOrAddress - * @param mixed $name + * setSender + * + * @param mixed $emailOrAddress + * @param mixed $name * @return Message */ public function setSender($emailOrAddress, $name = null) { - $header = $this->getHeader('sender', __NAMESPACE__ . '\Header\Sender'); + $header = $this->getHeaderByName('sender', __NAMESPACE__ . '\Header\Sender'); $header->setAddress($emailOrAddress, $name); return $this; } /** * Retrieve the sender address, if any - * + * * @return null|Address\AddressInterface */ public function getSender() { - $header = $this->getHeader('sender', __NAMESPACE__ . '\Header\Sender'); + $header = $this->getHeaderByName('sender', __NAMESPACE__ . '\Header\Sender'); return $header->getAddress(); } /** * Set the message subject header value - * - * @param string $subject + * + * @param string $subject * @return Message */ public function setSubject($subject) { - $headers = $this->headers(); + $headers = $this->getHeaders(); if (!$headers->has('subject')) { $header = new Header\Subject(); $headers->addHeader($header); @@ -355,17 +343,17 @@ public function setSubject($subject) /** * Get the message subject header value - * + * * @return null|string */ public function getSubject() { - $headers = $this->headers(); + $headers = $this->getHeaders(); if (!$headers->has('subject')) { return null; } $header = $headers->get('subject'); - return $header->getFieldValue(); + return $header->getFieldValue(Header\HeaderInterface::FORMAT_ENCODED); } /** @@ -402,13 +390,13 @@ public function setBody($body) } // Get headers, and set Mime-Version header - $headers = $this->headers(); - $this->getHeader('mime-version', __NAMESPACE__ . '\Header\MimeVersion'); + $headers = $this->getHeaders(); + $this->getHeaderByName('mime-version', __NAMESPACE__ . '\Header\MimeVersion'); // Multipart content headers if ($this->body->isMultiPart()) { $mime = $this->body->getMime(); - $header = $this->getHeader('content-type', __NAMESPACE__ . '\Header\ContentType'); + $header = $this->getHeaderByName('content-type', __NAMESPACE__ . '\Header\ContentType'); $header->setType('multipart/mixed'); $header->addParameter('boundary', $mime->boundary()); return $this; @@ -425,7 +413,7 @@ public function setBody($body) /** * Return the currently set message body - * + * * @return object */ public function getBody() @@ -435,7 +423,7 @@ public function getBody() /** * Get the string-serialized message body text - * + * * @return string */ public function getBodyText() @@ -451,14 +439,14 @@ public function getBodyText() * Retrieve a header by name * * If not found, instantiates one based on $headerClass. - * - * @param string $headerName - * @param string $headerClass + * + * @param string $headerName + * @param string $headerClass * @return \Zend\Mail\Header\HeaderInterface */ - protected function getHeader($headerName, $headerClass) + protected function getHeaderByName($headerName, $headerClass) { - $headers = $this->headers(); + $headers = $this->getHeaders(); if ($headers->has($headerName)) { $header = $headers->get($headerName); } else { @@ -470,12 +458,12 @@ protected function getHeader($headerName, $headerClass) /** * Clear a header by name - * - * @param string $headerName + * + * @param string $headerName */ protected function clearHeaderByName($headerName) { - $this->headers()->removeHeader($headerName); + $this->getHeaders()->removeHeader($headerName); } /** @@ -491,7 +479,7 @@ protected function clearHeaderByName($headerName) */ protected function getAddressListFromHeader($headerName, $headerClass) { - $header = $this->getHeader($headerName, $headerClass); + $header = $this->getHeaderByName($headerName, $headerClass); if (!$header instanceof Header\AbstractAddressList) { throw new Exception\DomainException(sprintf( 'Cannot grab address list from header of type "%s"; not an AbstractAddressList implementation', @@ -536,12 +524,12 @@ protected function updateAddressList(AddressList $addressList, $emailOrAddressOr /** * Serialize to string - * + * * @return string */ public function toString() { - $headers = $this->headers(); + $headers = $this->getHeaders(); return $headers->toString() . Headers::EOL . $this->getBodyText(); diff --git a/src/Protocol/AbstractProtocol.php b/src/Protocol/AbstractProtocol.php index b723e6a5..1741e046 100644 --- a/src/Protocol/AbstractProtocol.php +++ b/src/Protocol/AbstractProtocol.php @@ -1,23 +1,11 @@ _maximumLog = (int) $maximumLog; } - - + + /** - * Get the maximum log size - * + * Get the maximum log size + * * @return int the maximum log size */ public function getMaximumLog() { return $this->_maximumLog; } - + /** * Create a connection to the remote host diff --git a/src/Protocol/Exception/ExceptionInterface.php b/src/Protocol/Exception/ExceptionInterface.php index 821fb0dc..d701f870 100644 --- a/src/Protocol/Exception/ExceptionInterface.php +++ b/src/Protocol/Exception/ExceptionInterface.php @@ -1,21 +1,11 @@ connect($host, $port, $ssl); @@ -118,7 +105,7 @@ public function connect($host, $port = null, $ssl = false) */ protected function _nextLine() { - $line = @fgets($this->_socket); + $line = fgets($this->_socket); if ($line === false) { throw new Exception\RuntimeException('cannot read - connection closed?'); } @@ -322,7 +309,7 @@ public function sendRequest($command, $tokens = array(), &$tag = null) foreach ($tokens as $token) { if (is_array($token)) { - if (@fwrite($this->_socket, $line . ' ' . $token[0] . "\r\n") === false) { + if (fwrite($this->_socket, $line . ' ' . $token[0] . "\r\n") === false) { throw new Exception\RuntimeException('cannot write - connection closed?'); } if (!$this->_assumedNextLine('+ ')) { @@ -334,7 +321,7 @@ public function sendRequest($command, $tokens = array(), &$tag = null) } } - if (@fwrite($this->_socket, $line . "\r\n") === false) { + if (fwrite($this->_socket, $line . "\r\n") === false) { throw new Exception\RuntimeException('cannot write - connection closed?'); } } @@ -537,9 +524,9 @@ public function fetch($items, $from, $to = null) { if (is_array($from)) { $set = implode(',', $from); - } else if ($to === null) { + } elseif ($to === null) { $set = (int)$from; - } else if ($to === INF) { + } elseif ($to === INF) { $set = (int)$from . ':*'; } else { $set = (int)$from . ':' . (int)$to; diff --git a/src/Protocol/Pop3.php b/src/Protocol/Pop3.php index 99cb30bc..02d60ca8 100644 --- a/src/Protocol/Pop3.php +++ b/src/Protocol/Pop3.php @@ -1,22 +1,11 @@ username = $username; return $this; } - + /** * Get username * @@ -134,7 +121,7 @@ public function setPassword($password) $this->password = $password; return $this; } - + /** * Get password * diff --git a/src/Protocol/Smtp/Auth/Login.php b/src/Protocol/Smtp/Auth/Login.php index 5468882c..5666a6f5 100644 --- a/src/Protocol/Smtp/Auth/Login.php +++ b/src/Protocol/Smtp/Auth/Login.php @@ -1,22 +1,11 @@ username = $username; return $this; } - + /** * Get username * @@ -135,7 +122,7 @@ public function setPassword($password) $this->password = $password; return $this; } - + /** * Get password * diff --git a/src/Protocol/Smtp/Auth/Plain.php b/src/Protocol/Smtp/Auth/Plain.php index 7540ef04..ac1ebc1d 100644 --- a/src/Protocol/Smtp/Auth/Plain.php +++ b/src/Protocol/Smtp/Auth/Plain.php @@ -1,25 +1,15 @@ username = $username; return $this; } - + /** * Get username * @@ -132,7 +120,7 @@ public function setPassword($password) $this->password = $password; return $this; } - + /** * Get password * diff --git a/src/Protocol/SmtpBroker.php b/src/Protocol/SmtpBroker.php deleted file mode 100644 index e2fa27d6..00000000 --- a/src/Protocol/SmtpBroker.php +++ /dev/null @@ -1,58 +0,0 @@ - 'Zend\Mail\Protocol\Smtp\Auth\Crammd5', - 'login' => 'Zend\Mail\Protocol\Smtp\Auth\Login', - 'plain' => 'Zend\Mail\Protocol\Smtp\Auth\Plain', - 'smtp' => 'Zend\Mail\Protocol\Smtp', - ); -} diff --git a/src/Protocol/SmtpPluginManager.php b/src/Protocol/SmtpPluginManager.php new file mode 100644 index 00000000..544b8160 --- /dev/null +++ b/src/Protocol/SmtpPluginManager.php @@ -0,0 +1,61 @@ + 'Zend\Mail\Protocol\Smtp\Auth\Crammd5', + 'login' => 'Zend\Mail\Protocol\Smtp\Auth\Login', + 'plain' => 'Zend\Mail\Protocol\Smtp\Auth\Plain', + 'smtp' => 'Zend\Mail\Protocol\Smtp', + ); + + /** + * Validate the plugin + * + * Checks that the extension loaded is an instance of Smtp. + * + * @param mixed $plugin + * @return void + * @throws Exception\InvalidArgumentException if invalid + */ + public function validatePlugin($plugin) + { + if ($plugin instanceof Smtp) { + // we're okay + return; + } + + throw new Exception\InvalidArgumentException(sprintf( + 'Plugin of type %s is invalid; must extend %s\Smtp', + (is_object($plugin) ? get_class($plugin) : gettype($plugin)), + __NAMESPACE__ + )); + } +} diff --git a/src/Storage.php b/src/Storage.php index ca8962c6..80da6847 100644 --- a/src/Storage.php +++ b/src/Storage.php @@ -1,21 +1,11 @@ \Zend\Mail\Storage\Folder folder) diff --git a/src/Storage/Folder/FolderInterface.php b/src/Storage/Folder/FolderInterface.php index 27bf6c35..613f05e2 100644 --- a/src/Storage/Folder/FolderInterface.php +++ b/src/Storage/Folder/FolderInterface.php @@ -1,22 +1,11 @@ _rootFolder = new Storage\Folder('/', '/', false); $this->_rootFolder->INBOX = new Storage\Folder('INBOX', 'INBOX', true); - $dh = @opendir($this->_rootdir); + ErrorHandler::start(E_WARNING); + $dh = opendir($this->_rootdir); + ErrorHandler::stop(); if (!$dh) { throw new Exception\RuntimeException("can't read folders in maildir"); } @@ -139,7 +129,7 @@ protected function _buildFolderTree() array_push($folderStack, $parentFolder); $parentFolder = $folder; break; - } else if ($stack) { + } elseif ($stack) { $parent = array_pop($stack); $parentFolder = array_pop($folderStack); } @@ -169,9 +159,11 @@ public function getFolders($rootFolder = null) } $currentFolder = $this->_rootFolder; $subname = trim($rootFolder, $this->_delim); - + while ($currentFolder) { - @list($entry, $subname) = @explode($this->_delim, $subname, 2); + ErrorHandler::start(E_NOTICE); + list($entry, $subname) = explode($this->_delim, $subname, 2); + ErrorHandler::stop(); $currentFolder = $currentFolder->$entry; if (!$subname) { break; diff --git a/src/Storage/Folder/Mbox.php b/src/Storage/Folder/Mbox.php index 42b46964..e5053c32 100644 --- a/src/Storage/Folder/Mbox.php +++ b/src/Storage/Folder/Mbox.php @@ -1,35 +1,23 @@ _rootFolder; } - $dh = @opendir($currentDir); + ErrorHandler::start(E_WARNING); + $dh = opendir($currentDir); + ErrorHandler::stop(); if (!$dh) { throw new Exception\InvalidArgumentException("can't read dir $currentDir"); } @@ -145,7 +135,9 @@ public function getFolders($rootFolder = null) $currentFolder = $this->_rootFolder; $subname = trim($rootFolder, DIRECTORY_SEPARATOR); while ($currentFolder) { - @list($entry, $subname) = @explode(DIRECTORY_SEPARATOR, $subname, 2); + ErrorHandler::start(E_NOTICE); + list($entry, $subname) = explode(DIRECTORY_SEPARATOR, $subname, 2); + ErrorHandler::stop(); $currentFolder = $currentFolder->$entry; if (!$subname) { break; diff --git a/src/Storage/Imap.php b/src/Storage/Imap.php index 7e70577a..d8c6731b 100644 --- a/src/Storage/Imap.php +++ b/src/Storage/Imap.php @@ -1,22 +1,11 @@ getGlobalName() . '/' . $name; - } else if ($parentFolder != null) { + } elseif ($parentFolder != null) { $folder = $parentFolder . '/' . $name; } else { $folder = $name; diff --git a/src/Storage/Maildir.php b/src/Storage/Maildir.php index 788cca3f..40928fa6 100644 --- a/src/Storage/Maildir.php +++ b/src/Storage/Maildir.php @@ -1,34 +1,22 @@ close(); } - $dh = @opendir($dirname . '/cur/'); + ErrorHandler::start(E_WARNING); + $dh = opendir($dirname . '/cur/'); + ErrorHandler::stop(); if (!$dh) { throw new Exception\RuntimeException('cannot open maildir'); } $this->_getMaildirFiles($dh, $dirname . '/cur/'); closedir($dh); - $dh = @opendir($dirname . '/new/'); + ErrorHandler::start(E_WARNING); + $dh = opendir($dirname . '/new/'); + ErrorHandler::stop(); if ($dh) { $this->_getMaildirFiles($dh, $dirname . '/new/', array(Mail\Storage::FLAG_RECENT)); closedir($dh); - } else if (file_exists($dirname . '/new/')) { + } elseif (file_exists($dirname . '/new/')) { throw new Exception\RuntimeException('cannot read recent mails in maildir'); } } @@ -308,15 +300,20 @@ protected function _getMaildirFiles($dh, $dirname, $default_flags = array()) continue; } - @list($uniq, $info) = explode(':', $entry, 2); - @list(,$size) = explode(',', $uniq, 2); + ErrorHandler::start(E_NOTICE); + list($uniq, $info) = explode(':', $entry, 2); + list(,$size) = explode(',', $uniq, 2); + ErrorHandler::stop(); if ($size && $size[0] == 'S' && $size[1] == '=') { $size = substr($size, 2); } if (!ctype_digit($size)) { $size = null; } - @list($version, $flags) = explode(',', $info, 2); + + ErrorHandler::start(E_NOTICE); + list($version, $flags) = explode(',', $info, 2); + ErrorHandler::stop(); if ($version != 2) { $flags = ''; } diff --git a/src/Storage/Mbox.php b/src/Storage/Mbox.php index 72b4b397..d109ebbf 100644 --- a/src/Storage/Mbox.php +++ b/src/Storage/Mbox.php @@ -1,32 +1,21 @@ _filemtime = filemtime($this->_filename); if (!$this->_isMboxFile($this->_fh, false)) { - @fclose($this->_fh); + ErrorHandler::start(E_WARNING); + fclose($this->_fh); + ErrorHandler::stop(); throw new Exception\InvalidArgumentException('file is not a valid mbox format'); } @@ -294,7 +289,9 @@ protected function _openMboxFile($filename) */ public function close() { - @fclose($this->_fh); + ErrorHandler::start(E_WARNING); + fclose($this->_fh); + ErrorHandler::stop(); $this->_positions = array(); } diff --git a/src/Storage/Message.php b/src/Storage/Message.php index b5ce4812..a749bd47 100644 --- a/src/Storage/Message.php +++ b/src/Storage/Message.php @@ -1,22 +1,11 @@ value) or string, if a content part is found it's used as toplines * - noToplines ignore content found after headers in param 'headers' * - content content as string + * - strict strictly parse raw content * * @param array $params full message with or without headers * @throws Exception\InvalidArgumentException @@ -108,20 +98,23 @@ public function __construct(array $params) $this->_mail = $params['handler']; $this->_messageNum = $params['id']; } + + $params['strict'] = isset($params['strict']) ? $params['strict'] : false; if (isset($params['raw'])) { - Mime\Decode::splitMessage($params['raw'], $this->_headers, $this->_content); - } else if (isset($params['headers'])) { + Mime\Decode::splitMessage($params['raw'], $this->_headers, $this->_content, Mime\Mime::LINEEND, $params['strict']); + } elseif (isset($params['headers'])) { if (is_array($params['headers'])) { - $this->_headers = $params['headers']; + $this->_headers = new Headers(); + $this->_headers->addHeaders($params['headers']); } else { - $body = null; // "Declare" variable since it's passed by reference - if (!empty($params['noToplines'])) { - Mime\Decode::splitMessage($params['headers'], $this->_headers, $body); - } else { + if (empty($params['noToplines'])) { Mime\Decode::splitMessage($params['headers'], $this->_headers, $this->_topLines); + } else { + $this->_headers = Headers::fromString($params['headers']); } } + if (isset($params['content'])) { $this->_content = $params['content']; } @@ -159,9 +152,9 @@ public function getContent() if ($this->_mail) { return $this->_mail->getRawContent($this->_messageNum); - } else { - throw new Exception\RuntimeException('no content'); } + + throw new Exception\RuntimeException('no content'); } /** @@ -171,7 +164,7 @@ public function getContent() * * @return int size */ - public function getSize() + public function getSize() { return strlen($this->getContent()); } @@ -267,24 +260,21 @@ public function countParts() return $this->_countParts; } - /** - * Get all headers + * Access headers collection * - * The returned headers are as saved internally. All names are lowercased. The value is a string or an array - * if a header with the same name occurs more than once. + * Lazy-loads if not already attached. * - * @return array headers as array(name => value) + * @return Headers */ public function getHeaders() { - if ($this->_headers === null) { - if (!$this->_mail) { - $this->_headers = array(); - } else { + if (null === $this->_headers) { + if ($this->_mail) { $part = $this->_mail->getRawHeader($this->_messageNum); - $body = null; // "Declare" variable since it's passed by reference - Mime\Decode::splitMessage($part, $this->_headers, $body); + $this->_headers = Headers::fromString($part); + } else { + $this->_headers = new Headers(); } } @@ -300,55 +290,49 @@ public function getHeaders() * @param string $name name of header, matches case-insensitive, but camel-case is replaced with dashes * @param string $format change type of return value to 'string' or 'array' * @throws Exception\InvalidArgumentException - * @return string|array value of header in wanted or internal format + * @return string|array|HeaderInterface|\ArrayIterator value of header in wanted or internal format */ public function getHeader($name, $format = null) { - if ($this->_headers === null) { - $this->getHeaders(); - } - - $lowerName = strtolower($name); - - if ($this->headerExists($name) == false) { + $header = $this->getHeaders()->get($name); + if ($header === false) { $lowerName = strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $name)); - if ($this->headerExists($lowerName) == false) { - throw new Exception\InvalidArgumentException("no Header with Name $name or $lowerName found"); + $header = $this->getHeaders()->get($lowerName); + if ($header === false) { + throw new Exception\InvalidArgumentException( + "Header with Name $name or $lowerName not found" + ); } } - $name = $lowerName; - - $header = $this->_headers[$name]; switch ($format) { case 'string': - if (is_array($header)) { - $header = implode(Mime\Mime::LINEEND, $header); + if ($header instanceof HeaderInterface) { + $return = $header->getFieldValue(HeaderInterface::FORMAT_RAW); + } else { + $return = ''; + foreach ($header as $h) { + $return .= $h->getFieldValue(HeaderInterface::FORMAT_RAW) + . Mime\Mime::LINEEND; + } + $return = trim($return, Mime\Mime::LINEEND); } break; case 'array': - $header = (array)$header; + if ($header instanceof HeaderInterface) { + $return = array($header->getFieldValue()); + } else { + $return = array(); + foreach ($header as $h) { + $return[] = $h->getFieldValue(HeaderInterface::FORMAT_RAW); + } + } + break; default: - // do nothing + $return = $header; } - return $header; - } - - /** - * Check whether the Mail part has a specific header. - * - * @param string $name - * @return boolean - */ - public function headerExists($name) - { - $name = strtolower($name); - if (isset($this->_headers[$name])) { - return true; - } else { - return false; - } + return $return; } /** @@ -400,7 +384,7 @@ public function __get($name) */ public function __isset($name) { - return $this->headerExists($name); + return $this->getHeaders()->has($name); } /** diff --git a/src/Storage/Part/Exception/ExceptionInterface.php b/src/Storage/Part/Exception/ExceptionInterface.php index 644e6b34..0f9ab2da 100644 --- a/src/Storage/Part/Exception/ExceptionInterface.php +++ b/src/Storage/Part/Exception/ExceptionInterface.php @@ -1,23 +1,11 @@ _headers, $body); + $this->_headers = Headers::fromString($header); $this->_contentPos[0] = ftell($this->_fh); if ($endPos !== null) { @@ -114,7 +101,7 @@ public function __construct(array $params) $this->_partPos[] = $part; } $part = array($pos); - } else if ($line == '--' . $boundary . '--') { + } elseif ($line == '--' . $boundary . '--') { $part[1] = $lastPos; $this->_partPos[] = $part; break; @@ -150,7 +137,7 @@ public function getContent($stream = null) * * @return int size */ - public function getSize() + public function getSize() { return $this->_contentPos[1] - $this->_contentPos[0]; } diff --git a/src/Storage/Part/PartInterface.php b/src/Storage/Part/PartInterface.php index 7f679a3c..c6296f9f 100644 --- a/src/Storage/Part/PartInterface.php +++ b/src/Storage/Part/PartInterface.php @@ -1,22 +1,11 @@ value) + * @return \Zend\Mail\Headers */ public function getHeaders(); @@ -92,7 +79,7 @@ public function getHeaders(); * * @param string $name name of header, matches case-insensitive, but camel-case is replaced with dashes * @param string $format change type of return value to 'string' or 'array' - * @return string|array value of header in wanted or internal format + * @return string|array|\Zend\Mail\Header\HeaderInterface|\ArrayIterator value of header in wanted or internal format * @throws Exception\ExceptionInterface */ public function getHeader($name, $format = null); diff --git a/src/Storage/Pop3.php b/src/Storage/Pop3.php index a79c53a7..994df5f7 100644 --- a/src/Storage/Pop3.php +++ b/src/Storage/Pop3.php @@ -1,22 +1,11 @@ getGlobalName() . $this->_delim . $name; - } else if ($parentFolder != null) { + } elseif ($parentFolder != null) { $folder = rtrim($parentFolder, $this->_delim) . $this->_delim . $name; } else { $folder = $name; @@ -485,7 +473,10 @@ public function appendMessage($message, $folder = null, $flags = null, $recent = if (!link($temp_file['filename'], $new_filename)) { $exception = new StorageException\RuntimeException('cannot link message file to final dir'); } - @unlink($temp_file['filename']); + + ErrorHandler::start(E_WARNING); + unlink($temp_file['filename']); + ErrorHandler::stop(); if ($exception) { throw $exception; @@ -544,10 +535,13 @@ public function copyMessage($id, $folder) if (!copy($old_file, $temp_file['filename'])) { $exception = new StorageException\RuntimeException('cannot copy message file'); - } else if (!link($temp_file['filename'], $new_file)) { + } elseif (!link($temp_file['filename'], $new_file)) { $exception = new StorageException\RuntimeException('cannot link message file to final dir'); } - @unlink($temp_file['filename']); + + ErrorHandler::start(E_WARNING); + unlink($temp_file['filename']); + ErrorHandler::stop(); if ($exception) { throw $exception; @@ -613,7 +607,10 @@ public function moveMessage($id, $folder) if (!rename($old_file, $new_file)) { $exception = new StorageException\RuntimeException('cannot move message file'); } - @unlink($temp_file['filename']); + + ErrorHandler::start(E_WARNING); + unlink($temp_file['filename']); + ErrorHandler::stop(); if ($exception) { throw $exception; @@ -704,7 +701,9 @@ public function setQuota($value) public function getQuota($fromStorage = false) { if ($fromStorage) { - $fh = @fopen($this->_rootdir . 'maildirsize', 'r'); + ErrorHandler::start(E_WARNING); + $fh = fopen($this->_rootdir . 'maildirsize', 'r'); + ErrorHandler::stop(); if (!$fh) { throw new StorageException\RuntimeException('cannot open maildirsize'); } diff --git a/src/Storage/Writable/WritableInterface.php b/src/Storage/Writable/WritableInterface.php index 7d2a73a2..fcb47135 100644 --- a/src/Storage/Writable/WritableInterface.php +++ b/src/Storage/Writable/WritableInterface.php @@ -1,22 +1,11 @@ headers(); + $headers = $message->getHeaders(); if (!$headers->has('to')) { throw new Exception\RuntimeException('Invalid email; contains no "To" header'); @@ -172,7 +159,7 @@ protected function prepareRecipients(Mail\Message $message) // If not on Windows, return normal string if (!$this->isWindowsOs()) { - return $to->getFieldValue(); + return $to->getFieldValue(HeaderInterface::FORMAT_ENCODED); } // Otherwise, return list of emails @@ -186,7 +173,7 @@ protected function prepareRecipients(Mail\Message $message) /** * Prepare the subject line string - * + * * @param \Zend\Mail\Message $message * @return string */ @@ -197,7 +184,7 @@ protected function prepareSubject(Mail\Message $message) /** * Prepare the body string - * + * * @param \Zend\Mail\Message $message * @return string */ @@ -216,7 +203,7 @@ protected function prepareBody(Mail\Message $message) /** * Prepare the textual representation of headers - * + * * @param \Zend\Mail\Message $message * @return string */ @@ -224,11 +211,11 @@ protected function prepareHeaders(Mail\Message $message) { // On Windows, simply return verbatim if ($this->isWindowsOs()) { - return $message->headers()->toString(); + return $message->getHeaders()->toString(); } // On *nix platforms, strip the "to" header - $headers = clone $message->headers(); + $headers = clone $message->getHeaders(); $headers->removeHeader('To'); $headers->removeHeader('Subject'); return $headers->toString(); @@ -237,9 +224,9 @@ protected function prepareHeaders(Mail\Message $message) /** * Prepare additional_parameters argument * - * Basically, overrides the MAIL FROM envelope with either the Sender or + * Basically, overrides the MAIL FROM envelope with either the Sender or * From address. - * + * * @param \Zend\Mail\Message $message * @return string */ @@ -257,7 +244,7 @@ protected function prepareParameters(Mail\Message $message) return $parameters; } - $from = $message->from(); + $from = $message->getFrom(); if (count($from)) { $from->rewind(); $sender = $from->current(); @@ -315,7 +302,7 @@ public function handleMailErrors($errno, $errstr, $errfile = null, $errline = nu /** * Is this a windows OS? - * + * * @return bool */ protected function isWindowsOs() diff --git a/src/Transport/Smtp.php b/src/Transport/Smtp.php index 47a8eb18..15d4b48f 100644 --- a/src/Transport/Smtp.php +++ b/src/Transport/Smtp.php @@ -1,32 +1,20 @@ broker = $broker; + $this->plugins = $plugins; return $this; } - + /** - * Get broker for loading SMTP protocol connection + * Get plugin manager for loading SMTP protocol connection * - * @return Protocol\SmtpBroker + * @return Protocol\SmtpPluginManager */ - public function getBroker() + public function getPluginManager() { - if (null === $this->broker) { - $this->setBroker(new Protocol\SmtpBroker()); + if (null === $this->plugins) { + $this->setPluginManager(new Protocol\SmtpPluginManager()); } - return $this->broker; + return $this->plugins; } + /** * Set the automatic disconnection when destruct - * + * * @param boolean $flag * @return Smtp */ - public function setAutoDisconnect($flag) + public function setAutoDisconnect($flag) { $this->autoDisconnect = (bool) $flag; return $this; } + /** * Get the automatic disconnection value - * + * * @return boolean */ public function getAutoDisconnect() { return $this->autoDisconnect; } + /** * Return an SMTP connection - * - * @param string $name - * @param array|null $options + * + * @param string $name + * @param array|null $options * @return Protocol\Smtp */ public function plugin($name, array $options = null) { - return $this->getBroker()->load($name, $options); + return $this->getPluginManager()->get($name, $options); } /** @@ -173,7 +155,7 @@ public function __destruct() } if ($this->autoDisconnect) { $this->connection->disconnect(); - } + } } } @@ -197,9 +179,10 @@ public function getConnection() { return $this->connection; } + /** * Disconnect the connection protocol instance - * + * * @return void */ public function disconnect() @@ -208,6 +191,7 @@ public function disconnect() $this->connection->disconnect(); } } + /** * Send an email via the SMTP connection protocol * @@ -269,7 +253,7 @@ protected function prepareFromAddress(Message $message) return $sender->getEmail(); } - $from = $message->from(); + $from = $message->getFrom(); if (!count($from)) { // Per RFC 2822 3.6 throw new Exception\RuntimeException(sprintf( '%s transport expects either a Sender or at least one From address in the Message; none provided', @@ -291,13 +275,13 @@ protected function prepareFromAddress(Message $message) protected function prepareRecipients(Message $message) { $recipients = array(); - foreach ($message->to() as $address) { + foreach ($message->getTo() as $address) { $recipients[] = $address->getEmail(); } - foreach ($message->cc() as $address) { + foreach ($message->getCc() as $address) { $recipients[] = $address->getEmail(); } - foreach ($message->bcc() as $address) { + foreach ($message->getBcc() as $address) { $recipients[] = $address->getEmail(); } $recipients = array_unique($recipients); @@ -312,7 +296,7 @@ protected function prepareRecipients(Message $message) */ protected function prepareHeaders(Message $message) { - $headers = clone $message->headers(); + $headers = clone $message->getHeaders(); $headers->removeHeader('Bcc'); return $headers->toString(); } @@ -330,17 +314,17 @@ protected function prepareBody(Message $message) /** * Lazy load the connection, and pass it helo - * + * * @return Protocol\Smtp */ protected function lazyLoadConnection() { // Check if authentication is required and determine required class - $options = $this->getOptions(); - $host = $options->getHost(); - $port = $options->getPort(); - $config = $options->getConnectionConfig(); - $connection = $this->plugin($options->getConnectionClass(), array($host, $port, $config)); + $options = $this->getOptions(); + $config = $options->getConnectionConfig(); + $config['host'] = $options->getHost(); + $config['port'] = $options->getPort(); + $connection = $this->plugin($options->getConnectionClass(), $config); $this->connection = $connection; $this->connection->connect(); $this->connection->helo($options->getName()); diff --git a/src/Transport/SmtpOptions.php b/src/Transport/SmtpOptions.php index e6b26c19..68098b3d 100644 --- a/src/Transport/SmtpOptions.php +++ b/src/Transport/SmtpOptions.php @@ -1,37 +1,24 @@ connectionClass; } @@ -111,7 +98,7 @@ public function getConnectionClass() * @throws \Zend\Mail\Exception\InvalidArgumentException * @return SmtpOptions */ - public function setConnectionClass($connectionClass) + public function setConnectionClass($connectionClass) { if (!is_string($connectionClass) && $connectionClass !== null) { throw new Exception\InvalidArgumentException(sprintf( @@ -125,7 +112,7 @@ public function setConnectionClass($connectionClass) /** * Get connection configuration array - * + * * @return array */ public function getConnectionConfig() @@ -135,8 +122,8 @@ public function getConnectionConfig() /** * Set connection configuration array - * - * @param array $connectionConfig + * + * @param array $connectionConfig * @return SmtpOptions */ public function setConnectionConfig(array $connectionConfig) @@ -147,7 +134,7 @@ public function setConnectionConfig(array $connectionConfig) /** * Get the host name - * + * * @return string */ public function getHost() @@ -157,9 +144,9 @@ public function getHost() /** * Set the SMTP host - * + * * @todo hostname/IP validation - * @param string $host + * @param string $host * @return SmtpOptions */ public function setHost($host) diff --git a/src/Transport/TransportInterface.php b/src/Transport/TransportInterface.php index 5162b02b..2f1c4c69 100644 --- a/src/Transport/TransportInterface.php +++ b/src/Transport/TransportInterface.php @@ -1,22 +1,11 @@ assertContains("Content-Type: application/x-unit-test;\r\n", $string); $this->assertContains(";\r\n charset=\"us-ascii\"", $string); } - + } diff --git a/test/Header/HeaderWrapTest.php b/test/Header/HeaderWrapTest.php new file mode 100644 index 00000000..cca75a97 --- /dev/null +++ b/test/Header/HeaderWrapTest.php @@ -0,0 +1,66 @@ +getMock('Zend\Mail\Header\UnstructuredInterface'); + $header->expects($this->any()) + ->method('getEncoding') + ->will($this->returnValue('ASCII')); + $expected = wordwrap($string, 78, "\r\n "); + + $test = HeaderWrap::wrap($string, $header); + $this->assertEquals($expected, $test); + } + + /** + * @group ZF2-258 + */ + public function testWrapUnstructuredHeaderMime() + { + $string = str_repeat('foobarblahblahblah baz bat', 3); + $header = $this->getMock('Zend\Mail\Header\UnstructuredInterface'); + $header->expects($this->any()) + ->method('getEncoding') + ->will($this->returnValue('UTF-8')); + $expected = "=?UTF-8?Q?foobarblahblahblah=20baz=20batfoobarblahblahblah=20baz=20?=\r\n" + . " =?UTF-8?Q?batfoobarblahblahblah=20baz=20bat?="; + + $test = HeaderWrap::wrap($string, $header); + $this->assertEquals($expected, $test); + $this->assertEquals($string, iconv_mime_decode($test, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8')); + } + + /** + * @group ZF2-359 + */ + public function testMimeEncoding() + { + $string = 'Umlauts: ä'; + $expected = '=?UTF-8?Q?Umlauts:=20=C3=A4?='; + + $test = HeaderWrap::mimeEncodeValue($string, 'UTF-8', 78); + $this->assertEquals($expected, $test); + $this->assertEquals($string, iconv_mime_decode($test, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8')); + } +} diff --git a/test/Header/MessageIdTest.php b/test/Header/MessageIdTest.php new file mode 100644 index 00000000..9443ba94 --- /dev/null +++ b/test/Header/MessageIdTest.php @@ -0,0 +1,40 @@ +setId($id); + + $expected = sprintf('<%s>', $id); + $this->assertEquals($expected, $messageid->getFieldValue()); + } + + public function testAutoGeneration() + { + $messageid = new Header\MessageId(); + $messageid->setId(); + + $this->assertContains('@', $messageid->getFieldValue()); + } +} diff --git a/test/Header/ReceivedTest.php b/test/Header/ReceivedTest.php index eb795011..826feb30 100644 --- a/test/Header/ReceivedTest.php +++ b/test/Header/ReceivedTest.php @@ -1,22 +1,11 @@ assertFalse($headers->has('foobar')); $this->assertTrue($headers->has('foo')); $this->assertTrue($headers->has('Foo')); - $this->assertSame($f, $headers->get('foo')); + $this->assertEquals('bar', $headers->get('foo')->getFieldValue()); } public function testHeadersAggregatesHeaderObjects() @@ -123,7 +110,7 @@ public function testHeadersAggregatesHeaderObjects() $headers = new Mail\Headers(); $headers->addHeader($fakeHeader); $this->assertEquals(1, $headers->count()); - $this->assertSame($fakeHeader, $headers->get('Fake')); + $this->assertEquals('bar', $headers->get('Fake')->getFieldValue()); } public function testHeadersAggregatesHeaderThroughAddHeader() @@ -317,7 +304,8 @@ public function testDefaultPluginLoaderIsSeededWithHeaders($plugin, $class) $this->assertEquals($class, $test); } - public function testClone() { + public function testClone() + { $headers = new Mail\Headers(); $headers->addHeader(new Header\Bcc()); $headers2 = clone($headers); diff --git a/test/MessageTest.php b/test/MessageTest.php index 1f72db03..2cc7e1a2 100644 --- a/test/MessageTest.php +++ b/test/MessageTest.php @@ -1,42 +1,29 @@ message->headers(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('date')); $header = $headers->get('date'); @@ -75,22 +62,22 @@ public function testAddingFromAddressMarksAsValid() public function testHeadersMethodReturnsHeadersObject() { - $headers = $this->message->headers(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); } public function testToMethodReturnsAddressListObject() { $this->message->addTo('zf-devteam@zend.com'); - $to = $this->message->to(); + $to = $this->message->getTo(); $this->assertInstanceOf('Zend\Mail\AddressList', $to); } public function testToAddressListLivesInHeaders() { $this->message->addTo('zf-devteam@zend.com'); - $to = $this->message->to(); - $headers = $this->message->headers(); + $to = $this->message->getTo(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('to')); $header = $headers->get('to'); @@ -100,15 +87,15 @@ public function testToAddressListLivesInHeaders() public function testFromMethodReturnsAddressListObject() { $this->message->addFrom('zf-devteam@zend.com'); - $from = $this->message->from(); + $from = $this->message->getFrom(); $this->assertInstanceOf('Zend\Mail\AddressList', $from); } public function testFromAddressListLivesInHeaders() { $this->message->addFrom('zf-devteam@zend.com'); - $from = $this->message->from(); - $headers = $this->message->headers(); + $from = $this->message->getFrom(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('from')); $header = $headers->get('from'); @@ -118,15 +105,15 @@ public function testFromAddressListLivesInHeaders() public function testCcMethodReturnsAddressListObject() { $this->message->addCc('zf-devteam@zend.com'); - $cc = $this->message->cc(); + $cc = $this->message->getCc(); $this->assertInstanceOf('Zend\Mail\AddressList', $cc); } public function testCcAddressListLivesInHeaders() { $this->message->addCc('zf-devteam@zend.com'); - $cc = $this->message->cc(); - $headers = $this->message->headers(); + $cc = $this->message->getCc(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('cc')); $header = $headers->get('cc'); @@ -136,15 +123,15 @@ public function testCcAddressListLivesInHeaders() public function testBccMethodReturnsAddressListObject() { $this->message->addBcc('zf-devteam@zend.com'); - $bcc = $this->message->bcc(); + $bcc = $this->message->getBcc(); $this->assertInstanceOf('Zend\Mail\AddressList', $bcc); } public function testBccAddressListLivesInHeaders() { $this->message->addBcc('zf-devteam@zend.com'); - $bcc = $this->message->bcc(); - $headers = $this->message->headers(); + $bcc = $this->message->getBcc(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('bcc')); $header = $headers->get('bcc'); @@ -154,15 +141,15 @@ public function testBccAddressListLivesInHeaders() public function testReplyToMethodReturnsAddressListObject() { $this->message->addReplyTo('zf-devteam@zend.com'); - $replyTo = $this->message->replyTo(); + $replyTo = $this->message->getReplyTo(); $this->assertInstanceOf('Zend\Mail\AddressList', $replyTo); } public function testReplyToAddressListLivesInHeaders() { $this->message->addReplyTo('zf-devteam@zend.com'); - $replyTo = $this->message->replyTo(); - $headers = $this->message->headers(); + $replyTo = $this->message->getReplyTo(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('reply-to')); $header = $headers->get('reply-to'); @@ -200,7 +187,7 @@ public function testCanProvideAddressObjectWhenSettingSender() public function testSenderAccessorsProxyToSenderHeader() { $header = new Header\Sender(); - $this->message->headers()->addHeader($header); + $this->message->getHeaders()->addHeader($header); $address = new Address('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->setSender($address); $this->assertSame($address, $header->getAddress()); @@ -209,7 +196,7 @@ public function testSenderAccessorsProxyToSenderHeader() public function testCanAddFromAddressUsingName() { $this->message->addFrom('zf-devteam@zend.com', 'ZF DevTeam'); - $addresses = $this->message->from(); + $addresses = $this->message->getFrom(); $this->assertEquals(1, count($addresses)); $address = $addresses->current(); $this->assertEquals('zf-devteam@zend.com', $address->getEmail()); @@ -221,7 +208,7 @@ public function testCanAddFromAddressUsingAddressObject() $address = new Address('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->addFrom($address); - $addresses = $this->message->from(); + $addresses = $this->message->getFrom(); $this->assertEquals(1, count($addresses)); $test = $addresses->current(); $this->assertSame($address, $test); @@ -236,7 +223,7 @@ public function testCanAddManyFromAddressesUsingArray() ); $this->message->addFrom($addresses); - $from = $this->message->from(); + $from = $this->message->getFrom(); $this->assertEquals(3, count($from)); $this->assertTrue($from->has('zf-devteam@zend.com')); @@ -251,7 +238,7 @@ public function testCanAddManyFromAddressesUsingAddressListObject() $this->message->addFrom('fw-announce@lists.zend.com'); $this->message->addFrom($list); - $from = $this->message->from(); + $from = $this->message->getFrom(); $this->assertEquals(2, count($from)); $this->assertTrue($from->has('fw-announce@lists.zend.com')); $this->assertTrue($from->has('zf-devteam@zend.com')); @@ -264,7 +251,7 @@ public function testCanSetFromListFromAddressList() $this->message->addFrom('fw-announce@lists.zend.com'); $this->message->setFrom($list); - $from = $this->message->from(); + $from = $this->message->getFrom(); $this->assertEquals(1, count($from)); $this->assertFalse($from->has('fw-announce@lists.zend.com')); $this->assertTrue($from->has('zf-devteam@zend.com')); @@ -273,7 +260,7 @@ public function testCanSetFromListFromAddressList() public function testCanAddCcAddressUsingName() { $this->message->addCc('zf-devteam@zend.com', 'ZF DevTeam'); - $addresses = $this->message->cc(); + $addresses = $this->message->getCc(); $this->assertEquals(1, count($addresses)); $address = $addresses->current(); $this->assertEquals('zf-devteam@zend.com', $address->getEmail()); @@ -285,7 +272,7 @@ public function testCanAddCcAddressUsingAddressObject() $address = new Address('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->addCc($address); - $addresses = $this->message->cc(); + $addresses = $this->message->getCc(); $this->assertEquals(1, count($addresses)); $test = $addresses->current(); $this->assertSame($address, $test); @@ -300,7 +287,7 @@ public function testCanAddManyCcAddressesUsingArray() ); $this->message->addCc($addresses); - $cc = $this->message->cc(); + $cc = $this->message->getCc(); $this->assertEquals(3, count($cc)); $this->assertTrue($cc->has('zf-devteam@zend.com')); @@ -315,7 +302,7 @@ public function testCanAddManyCcAddressesUsingAddressListObject() $this->message->addCc('fw-announce@lists.zend.com'); $this->message->addCc($list); - $cc = $this->message->cc(); + $cc = $this->message->getCc(); $this->assertEquals(2, count($cc)); $this->assertTrue($cc->has('fw-announce@lists.zend.com')); $this->assertTrue($cc->has('zf-devteam@zend.com')); @@ -328,7 +315,7 @@ public function testCanSetCcListFromAddressList() $this->message->addCc('fw-announce@lists.zend.com'); $this->message->setCc($list); - $cc = $this->message->cc(); + $cc = $this->message->getCc(); $this->assertEquals(1, count($cc)); $this->assertFalse($cc->has('fw-announce@lists.zend.com')); $this->assertTrue($cc->has('zf-devteam@zend.com')); @@ -337,7 +324,7 @@ public function testCanSetCcListFromAddressList() public function testCanAddBccAddressUsingName() { $this->message->addBcc('zf-devteam@zend.com', 'ZF DevTeam'); - $addresses = $this->message->bcc(); + $addresses = $this->message->getBcc(); $this->assertEquals(1, count($addresses)); $address = $addresses->current(); $this->assertEquals('zf-devteam@zend.com', $address->getEmail()); @@ -349,7 +336,7 @@ public function testCanAddBccAddressUsingAddressObject() $address = new Address('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->addBcc($address); - $addresses = $this->message->bcc(); + $addresses = $this->message->getBcc(); $this->assertEquals(1, count($addresses)); $test = $addresses->current(); $this->assertSame($address, $test); @@ -364,7 +351,7 @@ public function testCanAddManyBccAddressesUsingArray() ); $this->message->addBcc($addresses); - $bcc = $this->message->bcc(); + $bcc = $this->message->getBcc(); $this->assertEquals(3, count($bcc)); $this->assertTrue($bcc->has('zf-devteam@zend.com')); @@ -379,7 +366,7 @@ public function testCanAddManyBccAddressesUsingAddressListObject() $this->message->addBcc('fw-announce@lists.zend.com'); $this->message->addBcc($list); - $bcc = $this->message->bcc(); + $bcc = $this->message->getBcc(); $this->assertEquals(2, count($bcc)); $this->assertTrue($bcc->has('fw-announce@lists.zend.com')); $this->assertTrue($bcc->has('zf-devteam@zend.com')); @@ -392,7 +379,7 @@ public function testCanSetBccListFromAddressList() $this->message->addBcc('fw-announce@lists.zend.com'); $this->message->setBcc($list); - $bcc = $this->message->bcc(); + $bcc = $this->message->getBcc(); $this->assertEquals(1, count($bcc)); $this->assertFalse($bcc->has('fw-announce@lists.zend.com')); $this->assertTrue($bcc->has('zf-devteam@zend.com')); @@ -401,7 +388,7 @@ public function testCanSetBccListFromAddressList() public function testCanAddReplyToAddressUsingName() { $this->message->addReplyTo('zf-devteam@zend.com', 'ZF DevTeam'); - $addresses = $this->message->replyTo(); + $addresses = $this->message->getReplyTo(); $this->assertEquals(1, count($addresses)); $address = $addresses->current(); $this->assertEquals('zf-devteam@zend.com', $address->getEmail()); @@ -413,7 +400,7 @@ public function testCanAddReplyToAddressUsingAddressObject() $address = new Address('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->addReplyTo($address); - $addresses = $this->message->replyTo(); + $addresses = $this->message->getReplyTo(); $this->assertEquals(1, count($addresses)); $test = $addresses->current(); $this->assertSame($address, $test); @@ -428,7 +415,7 @@ public function testCanAddManyReplyToAddressesUsingArray() ); $this->message->addReplyTo($addresses); - $replyTo = $this->message->replyTo(); + $replyTo = $this->message->getReplyTo(); $this->assertEquals(3, count($replyTo)); $this->assertTrue($replyTo->has('zf-devteam@zend.com')); @@ -443,7 +430,7 @@ public function testCanAddManyReplyToAddressesUsingAddressListObject() $this->message->addReplyTo('fw-announce@lists.zend.com'); $this->message->addReplyTo($list); - $replyTo = $this->message->replyTo(); + $replyTo = $this->message->getReplyTo(); $this->assertEquals(2, count($replyTo)); $this->assertTrue($replyTo->has('fw-announce@lists.zend.com')); $this->assertTrue($replyTo->has('zf-devteam@zend.com')); @@ -456,7 +443,7 @@ public function testCanSetReplyToListFromAddressList() $this->message->addReplyTo('fw-announce@lists.zend.com'); $this->message->setReplyTo($list); - $replyTo = $this->message->replyTo(); + $replyTo = $this->message->getReplyTo(); $this->assertEquals(1, count($replyTo)); $this->assertFalse($replyTo->has('fw-announce@lists.zend.com')); $this->assertTrue($replyTo->has('zf-devteam@zend.com')); @@ -477,7 +464,7 @@ public function testSubjectIsMutable() public function testSettingSubjectProxiesToHeader() { $this->message->setSubject('test subject'); - $headers = $this->message->headers(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('subject')); $header = $headers->get('subject'); @@ -545,7 +532,7 @@ public function testSettingBodyFromSinglePartMimeMessageSetsAppropriateHeaders() $body->addPart($part); $this->message->setBody($body); - $headers = $this->message->headers(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('mime-version')); @@ -570,7 +557,7 @@ public function testSettingBodyFromMultiPartMimeMessageSetsAppropriateHeaders() $body->addPart($html); $this->message->setBody($body); - $headers = $this->message->headers(); + $headers = $this->message->getHeaders(); $this->assertInstanceOf('Zend\Mail\Headers', $headers); $this->assertTrue($headers->has('mime-version')); @@ -617,10 +604,6 @@ public function testEncodingIsMutable() public function testSettingNonAsciiEncodingForcesMimeEncodingOfSomeHeaders() { - if (!function_exists('iconv_mime_encode')) { - $this->markTestSkipped('Encoding relies on iconv extension'); - } - $this->message->addTo('zf-devteam@zend.com', 'ZF DevTeam'); $this->message->addFrom('matthew@zend.com', "Matthew Weier O'Phinney"); $this->message->addCc('zf-contributors@lists.zend.com', 'ZF Contributors List'); @@ -628,40 +611,25 @@ public function testSettingNonAsciiEncodingForcesMimeEncodingOfSomeHeaders() $this->message->setSubject('This is a subject'); $this->message->setEncoding('UTF-8'); - $test = $this->message->headers()->toString(); + $test = $this->message->getHeaders()->toString(); - $expected = $this->encodeString('ZF DevTeam', 'UTF-8'); + $expected = '=?UTF-8?Q?ZF=20DevTeam?='; $this->assertContains($expected, $test); $this->assertContains('', $test); - $expected = $this->encodeString("Matthew Weier O'Phinney", 'UTF-8'); + $expected = "=?UTF-8?Q?Matthew=20Weier=20O'Phinney?="; $this->assertContains($expected, $test, $test); $this->assertContains('', $test); - $expected = $this->encodeString("ZF Contributors List", 'UTF-8'); + $expected = '=?UTF-8?Q?ZF=20Contributors=20List?='; $this->assertContains($expected, $test); $this->assertContains('', $test); - $expected = $this->encodeString("ZF CR Team", 'UTF-8'); + $expected = '=?UTF-8?Q?ZF=20CR=20Team?='; $this->assertContains($expected, $test); $this->assertContains('', $test); - $self = $this; - $words = array_map(function($word) use ($self) { - return $self->encodeString($word, 'UTF-8'); - }, explode(' ', 'This is a subject')); - $expected = 'Subject: ' . implode("\r\n ", $words); - $this->assertContains($expected, $test, $test); - } - - public function encodeString($string, $charset) - { - $encoded = iconv_mime_encode('Header', $string, array( - 'scheme' => 'Q', - 'output-charset' => $charset, - 'line-length' => 998, - )); - $encoded = str_replace('Header: ', '', $encoded); - return $encoded; + $expected = 'Subject: =?UTF-8?Q?This=20is=20a=20subject?='; + $this->assertContains($expected, $test); } } diff --git a/test/Protocol/SmtpTest.php b/test/Protocol/SmtpTest.php index 60a926f8..80ca4f4d 100644 --- a/test/Protocol/SmtpTest.php +++ b/test/Protocol/SmtpTest.php @@ -1,38 +1,25 @@ transport->setConnection($this->connection); } - public function testSendMinimalMail() { + public function testSendMinimalMail() + { $headers = new Headers(); $headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200'); $message = new Message(); diff --git a/test/Storage/ImapTest.php b/test/Storage/ImapTest.php index 05eba3fd..cd14503a 100644 --- a/test/Storage/ImapTest.php +++ b/test/Storage/ImapTest.php @@ -1,28 +1,17 @@ _params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to imap server'); - } + new Storage\Imap($this->_params); } public function testConnectConfig() { - try { - $mail = new Storage\Imap(new Config\Config($this->_params)); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to imap server'); - } + new Storage\Imap(new Config\Config($this->_params)); } public function testConnectFailure() { $this->_params['host'] = 'example.example'; - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - // I can only hope noone installs a imap server there - $this->fail('no exception raised while connecting to example.example'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testNoParams() { - try { - $mail = new Storage\Imap(array()); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with empty params'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap(array()); } @@ -150,11 +118,8 @@ public function testConnectSSL() } $this->_params['ssl'] = 'SSL'; - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to imap server with SSL'); - } + new Storage\Imap($this->_params); + } public function testConnectTLS() @@ -164,51 +129,29 @@ public function testConnectTLS() } $this->_params['ssl'] = 'TLS'; - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to imap server with TLS'); - } + new Storage\Imap($this->_params); } public function testInvalidService() { $this->_params['port'] = TESTS_ZEND_MAIL_IMAP_INVALID_PORT; - - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while connection to invalid port'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testWrongService() { $this->_params['port'] = TESTS_ZEND_MAIL_IMAP_WRONG_PORT; - - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while connection to wrong port'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testWrongUsername() { // this also triggers ...{chars}token for coverage $this->_params['user'] = "there is no\nnobody"; - - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while using wrong username'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testWithInstanceConstruction() @@ -216,56 +159,36 @@ public function testWithInstanceConstruction() $protocol = new Protocol\Imap($this->_params['host']); $protocol->login($this->_params['user'], $this->_params['password']); // if $protocol is invalid the constructor fails while selecting INBOX - $mail = new Storage\Imap($protocol); + new Storage\Imap($protocol); } public function testWithNotConnectedInstance() { $protocol = new Protocol\Imap(); - try { - $mail = new Storage\Imap($protocol); - } catch (ProtocolException\ExceptionInterface $e) { - return; // test ok - } - - $this->fail('no exception while using not connected low-level class'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($protocol); } public function testWithNotLoggedInstance() { $protocol = new Protocol\Imap($this->_params['host']); - try { - $mail = new Storage\Imap($protocol); - } catch (Exception\ExceptionInterface $e) { - return; // test ok - } - - $this->fail('no exception while using not logged in low-level class'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($protocol); } public function testWrongFolder() { $this->_params['folder'] = 'this folder does not exist on your server'; - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception with not existing folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testClose() { $mail = new Storage\Imap($this->_params); - - try { - $mail->close(); - } catch (\Exception $e) { - $this->fail('exception raised while closing imap connection'); - } + $mail->close(); } /* currently imap has no top @@ -287,12 +210,7 @@ public function testHasCreate() public function testNoop() { $mail = new Storage\Imap($this->_params); - - try { - $mail->noop(); - } catch (\Exception $e) { - $this->fail('exception raised while doing nothing (noop)'); - } + $mail->noop(); } public function testCount() @@ -372,35 +290,21 @@ public function testTooLateCount() $mail->close(); // after closing we can't count messages - try { - $mail->countMessages(); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while counting messages on closed connection'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->countMessages(); } public function testLoadUnkownFolder() { $this->_params['folder'] = 'UnknownFolder'; - try { - $mail = new Storage\Imap($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Imap($this->_params); } public function testChangeFolder() { $mail = new Storage\Imap($this->_params); - try { - $mail->selectFolder('subfolder/test'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder'); - } + $mail->selectFolder('subfolder/test'); $this->assertEquals($mail->getCurrentFolder(), 'subfolder/test'); } @@ -408,34 +312,20 @@ public function testChangeFolder() public function testUnknownFolder() { $mail = new Storage\Imap($this->_params); - try { - $mail->selectFolder('/Unknown/Folder/'); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while selecting unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('/Unknown/Folder/'); } public function testGlobalName() { $mail = new Storage\Imap($this->_params); - try { - // explicit call of __toString() needed for PHP < 5.2 - $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder and getting global name'); - } + $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder'); } public function testLocalName() { $mail = new Storage\Imap($this->_params); - try { - $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); - } catch (MailException\ExceptionInterface $e) { - $this->fail('exception raised while selecting existing folder and getting local name'); - } + $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); } public function testKeyLocalName() @@ -548,13 +438,8 @@ public function testUniqueId() public function testWrongUniqueId() { $mail = new Storage\Imap($this->_params); - try { - $mail->getNumberByUniqueId('this_is_an_invalid_id'); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while getting number for invalid id'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getNumberByUniqueId('this_is_an_invalid_id'); } public function testCreateFolder() @@ -564,26 +449,17 @@ public function testCreateFolder() $mail->createFolder('test2', 'subfolder'); $mail->createFolder('test3', $mail->getFolders()->subfolder); - try { - $mail->getFolders()->subfolder->test1; - $mail->getFolders()->subfolder->test2; - $mail->getFolders()->subfolder->test3; - } catch (\Exception $e) { - $this->fail('could not get new folders'); - } + $mail->getFolders()->subfolder->test1; + $mail->getFolders()->subfolder->test2; + $mail->getFolders()->subfolder->test3; } public function testCreateExistingFolder() { $mail = new Storage\Imap($this->_params); - try { - $mail->createFolder('subfolder/test'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to create existing folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('subfolder/test'); } public function testRemoveFolderName() @@ -591,12 +467,8 @@ public function testRemoveFolderName() $mail = new Storage\Imap($this->_params); $mail->removeFolder('subfolder/test'); - try { - $mail->getFolders()->subfolder->test; - } catch (\Exception $e) { - return; // ok - } - $this->fail('folder still exists'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getFolders()->subfolder->test; } public function testRemoveFolderInstance() @@ -604,42 +476,27 @@ public function testRemoveFolderInstance() $mail = new Storage\Imap($this->_params); $mail->removeFolder($mail->getFolders()->subfolder->test); - try { - $mail->getFolders()->subfolder->test; - } catch (\Exception $e) { - return; // ok - } - $this->fail('folder still exists'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getFolders()->subfolder->test; } public function testRemoveInvalidFolder() { $mail = new Storage\Imap($this->_params); - try { - $mail->removeFolder('thisFolderDoestNotExist'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while removing invalid folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeFolder('thisFolderDoestNotExist'); } public function testRenameFolder() { $mail = new Storage\Imap($this->_params); - try { - $mail->renameFolder('subfolder/test', 'subfolder/test1'); - $mail->renameFolder($mail->getFolders()->subfolder->test1, 'subfolder/test'); - } catch (\Exception $e) { - $this->fail('renaming failed'); - } - try { - $mail->renameFolder('subfolder/test', 'INBOX'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while renaming folder to INBOX'); + $mail->renameFolder('subfolder/test', 'subfolder/test1'); + $mail->renameFolder($mail->getFolders()->subfolder->test1, 'subfolder/test'); + + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->renameFolder('subfolder/test', 'INBOX'); } public function testAppend() @@ -658,12 +515,8 @@ public function testAppend() $this->assertEquals($count + 1, $mail->countMessages()); $this->assertEquals($mail->getMessage($count + 1)->subject, 'append test'); - try { - $mail->appendMessage(''); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while appending empty message'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->appendMessage(''); } public function testCopy() @@ -682,12 +535,8 @@ public function testCopy() $this->assertEquals($mail->getMessage($count + 1)->from, $message->from); $this->assertEquals($mail->getMessage($count + 1)->to, $message->to); - try { - $mail->copyMessage(1, 'justARandomFolder'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while copying to wrong folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->copyMessage(1, 'justARandomFolder'); } public function testSetFlags() @@ -715,12 +564,8 @@ public function testSetFlags() $this->assertFalse($message->hasFlag(Storage::FLAG_FLAGGED)); $this->assertTrue($message->hasFlag('myflag')); - try { - $mail->setFlags(1, array(Storage::FLAG_RECENT)); - } catch (\Exception $e) { - return; // ok - } - $this->fail('should not be able to set recent flag'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->setFlags(1, array(Storage::FLAG_RECENT)); } public function testCapability() @@ -757,12 +602,8 @@ public function testClosedSocketNewlineToken() $protocol->login($this->_params['user'], $this->_params['password']); $protocol->logout(); - try { - $protocol->select("foo\nbar"); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no exception while using procol with closed socket'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $protocol->select("foo\nbar"); } public function testEscaping() @@ -796,12 +637,8 @@ public function testFetch() $this->assertTrue(is_array($v['FLAGS'])); } - try { - $protocol->fetch('UID', 99); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no exception while fetching message'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $protocol->fetch('UID', 99); } public function testStore() diff --git a/test/Storage/MaildirFolderTest.php b/test/Storage/MaildirFolderTest.php index ff4f4997..72c3bf11 100644 --- a/test/Storage/MaildirFolderTest.php +++ b/test/Storage/MaildirFolderTest.php @@ -1,22 +1,11 @@ _params); - } catch (\Exception $e) { - $this->fail('exception raised while loading Maildir folder'); - } + new Folder\Maildir($this->_params); } public function testLoadConfig() { - try { - $mail = new Folder\Maildir(new Config\Config($this->_params)); - } catch (\Exception $e) { - $this->fail('exception raised while loading Maildir folder'); - } + new Folder\Maildir(new Config\Config($this->_params)); } public function testNoParams() { - try { - $mail = new Folder\Maildir(array()); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with empty params'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Maildir(array()); } public function testLoadFailure() { - try { - $mail = new Folder\Maildir(array('dirname' => 'This/Folder/Does/Not/Exist')); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown dirname'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Maildir(array('dirname' => 'This/Folder/Does/Not/Exist')); } public function testLoadUnkownFolder() { $this->_params['folder'] = 'UnknownFolder'; - try { - $mail = new Folder\Maildir($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Maildir($this->_params); } public function testChangeFolder() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); - try { - $mail->selectFolder('subfolder.test'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder'); - } + + $mail->selectFolder('subfolder.test'); $this->assertEquals($mail->getCurrentFolder(), 'subfolder.test'); } @@ -187,41 +148,30 @@ public function testChangeFolder() public function testUnknownFolder() { $mail = new Folder\Maildir($this->_params); - try { - $mail->selectFolder('/Unknown/Folder/'); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception raised while selecting unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('/Unknown/Folder/'); } public function testGlobalName() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); - try { - // explicit call of __toString() needed for PHP < 5.2 - $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder and getting global name'); - } + + $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder'); } public function testLocalName() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); - try { - $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder and getting local name'); - } + + $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); } public function testIterator() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $iterator = new \RecursiveIteratorIterator($mail->getFolders(), \RecursiveIteratorIterator::SELF_FIRST); // we search for this folder because we can't assume a order while iterating @@ -244,7 +194,7 @@ public function testIterator() public function testKeyLocalName() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $iterator = new \RecursiveIteratorIterator($mail->getFolders(), \RecursiveIteratorIterator::SELF_FIRST); // we search for this folder because we can't assume a order while iterating @@ -267,7 +217,7 @@ public function testKeyLocalName() public function testInboxEquals() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $iterator = new \RecursiveIteratorIterator($mail->getFolders('INBOX.subfolder'), \RecursiveIteratorIterator::SELF_FIRST); // we search for this folder because we can't assume a order while iterating @@ -299,7 +249,7 @@ public function testSelectable() public function testCount() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $count = $mail->countMessages(); @@ -312,7 +262,7 @@ public function testCount() public function testSize() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $shouldSizes = array(1 => 397, 89, 694, 452, 497); @@ -326,7 +276,7 @@ public function testSize() public function testFetchHeader() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Folder\Maildir($this->_params); $subject = $mail->getMessage(1)->subject; @@ -339,7 +289,7 @@ public function testFetchHeader() public function testNotReadableFolder() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $stat = stat($this->_params['dirname'] . '.subfolder'); chmod($this->_params['dirname'] . '.subfolder', 0); clearstatcache(); @@ -398,13 +348,8 @@ public function testGetInvalidFolder() $root = $mail->getFolders(); $root->foobar = new Folder('foobar', DIRECTORY_SEPARATOR . 'foobar'); - try { - $mail->selectFolder('foobar'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('no error while getting invalid folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('foobar'); } public function testGetVanishedFolder() @@ -413,13 +358,8 @@ public function testGetVanishedFolder() $root = $mail->getFolders(); $root->foobar = new Folder('foobar', 'foobar'); - try { - $mail->selectFolder('foobar'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('no error while getting vanished folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('foobar'); } public function testGetNotSelectableFolder() @@ -428,13 +368,8 @@ public function testGetNotSelectableFolder() $root = $mail->getFolders(); $root->foobar = new Folder('foobar', 'foobar', false); - try { - $mail->selectFolder('foobar'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('no error while getting not selectable folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('foobar'); } public function testWithAdditionalFolder() diff --git a/test/Storage/MaildirMessageOldTest.php b/test/Storage/MaildirMessageOldTest.php index 607dfc68..ac0d7cbc 100644 --- a/test/Storage/MaildirMessageOldTest.php +++ b/test/Storage/MaildirMessageOldTest.php @@ -1,22 +1,11 @@ $this->_maildir)); - } catch (\Exception $e) { - $this->fail('exception raised while loading maildir'); - } + new Storage\Maildir(array('dirname' => $this->_maildir)); } public function testLoadConfig() { - try { - $mail = new Storage\Maildir(new Config\Config(array('dirname' => $this->_maildir))); - } catch (\Exception $e) { - $this->fail('exception raised while loading maildir'); - } + new Storage\Maildir(new Config\Config(array('dirname' => $this->_maildir))); } public function testLoadFailure() { - try { - $mail = new Storage\Maildir(array('dirname' => '/This/Dir/Does/Not/Exist')); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown dir'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Maildir(array('dirname' => '/This/Dir/Does/Not/Exist')); } public function testLoadInvalid() { - try { - $mail = new Storage\Maildir(array('dirname' => __DIR__)); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while loading invalid dir'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Maildir(array('dirname' => __DIR__)); } public function testClose() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->close(); - } catch (\Exception $e) { - $this->fail('exception raised while closing maildir'); - } + $mail->close(); } public function testHasTop() @@ -169,11 +134,7 @@ public function testNoop() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->noop(); - } catch (\Exception $e) { - $this->fail('exception raised while doing nothing (noop)'); - } + $mail->noop(); } public function testCount() @@ -240,39 +201,24 @@ public function testFetchWrongSize() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->getSize(0); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while getting size for message 0'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getSize(0); } public function testFetchWrongMessageBody() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->getMessage(0); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while fetching message 0'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getMessage(0); } public function testFailedRemove() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->removeMessage(1); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while deleting message (maildir is read-only)'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeMessage(1); } public function testHasFlag() @@ -316,13 +262,9 @@ public function testUniqueId() public function testWrongUniqueId() { $mail = new Storage\Maildir(array('dirname' => $this->_maildir)); - try { - $mail->getNumberByUniqueId('this_is_an_invalid_id'); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception while getting number for invalid id'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getNumberByUniqueId('this_is_an_invalid_id'); } public function isFileTest($dir) diff --git a/test/Storage/MaildirWritableTest.php b/test/Storage/MaildirWritableTest.php index 87951d3a..48b7a2cc 100644 --- a/test/Storage/MaildirWritableTest.php +++ b/test/Storage/MaildirWritableTest.php @@ -1,22 +1,11 @@ markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->createFolder('subfolder.test1'); $mail->createFolder('test2', 'INBOX.subfolder'); $mail->createFolder('test3', $mail->getFolders()->subfolder); $mail->createFolder('foo.bar'); - try { - $mail->selectFolder($mail->getFolders()->subfolder->test1); - $mail->selectFolder($mail->getFolders()->subfolder->test2); - $mail->selectFolder($mail->getFolders()->subfolder->test3); - $mail->selectFolder($mail->getFolders()->foo->bar); - } catch (\Exception $e) { - $this->fail('could not get new folders'); - } + $mail->selectFolder($mail->getFolders()->subfolder->test1); + $mail->selectFolder($mail->getFolders()->subfolder->test2); + $mail->selectFolder($mail->getFolders()->subfolder->test3); + $mail->selectFolder($mail->getFolders()->foo->bar); // to tear down $this->_subdirs[] = '.subfolder.test1'; @@ -155,177 +138,116 @@ public function testCreateFolder() public function testCreateFolderEmtpyPart() { $mail = new Writable\Maildir($this->_params); - try { - $mail->createFolder('foo..bar'); - } catch (\Exception $e) { - return; //ok - } - - $this->fail('no exception while creating folder with empty part name'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('foo..bar'); } public function testCreateFolderSlash() { $mail = new Writable\Maildir($this->_params); - try { - $mail->createFolder('foo/bar'); - } catch (\Exception $e) { - return; //ok - } - - $this->fail('no exception while creating folder with slash'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('foo/bar'); } public function testCreateFolderDirectorySeparator() { $mail = new Writable\Maildir($this->_params); - try { - $mail->createFolder('foo' . DIRECTORY_SEPARATOR . 'bar'); - } catch (\Exception $e) { - return; //ok - } - - $this->fail('no exception while creating folder with DIRECTORY_SEPARATOR'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('foo' . DIRECTORY_SEPARATOR . 'bar'); } public function testCreateFolderExistingDir() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); unset($mail->getFolders()->subfolder->test); - try { - $mail->createFolder('subfolder.test'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to create existing folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('subfolder.test'); } public function testCreateExistingFolder() { $mail = new Writable\Maildir($this->_params); - try { - $mail->createFolder('subfolder.test'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to create existing folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->createFolder('subfolder.test'); } public function testRemoveFolderName() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->removeFolder('INBOX.subfolder.test'); - try { - $mail->selectFolder($mail->getFolders()->subfolder->test); - } catch (\Exception $e) { - return; // ok - } - $this->fail('folder still exists'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder($mail->getFolders()->subfolder->test); } public function testRemoveFolderInstance() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->removeFolder($mail->getFolders()->subfolder->test); - try { - $mail->selectFolder($mail->getFolders()->subfolder->test); - } catch (\Exception $e) { - return; // ok - } - $this->fail('folder still exists'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder($mail->getFolders()->subfolder->test); } public function testRemoveFolderWithChildren() { $mail = new Writable\Maildir($this->_params); - try { - $mail->removeFolder($mail->getFolders()->subfolder); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to remove a folder with children'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeFolder($mail->getFolders()->subfolder); } public function testRemoveSelectedFolder() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->selectFolder('subfolder.test'); - try { - $mail->removeFolder('subfolder.test'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while removing selected folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeFolder('subfolder.test'); } public function testRemoveInvalidFolder() { $mail = new Writable\Maildir($this->_params); - try { - $mail->removeFolder('thisFolderDoestNotExist'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while removing invalid folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeFolder('thisFolderDoestNotExist'); } public function testRenameFolder() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); - try { - $mail->renameFolder('INBOX.subfolder', 'INBOX.foo'); - $mail->renameFolder($mail->getFolders()->foo, 'subfolder'); - } catch (\Exception $e) { - $this->fail('renaming failed'); - } - try { - $mail->renameFolder('INBOX', 'foo'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while renaming INBOX'); + $mail->renameFolder('INBOX.subfolder', 'INBOX.foo'); + $mail->renameFolder($mail->getFolders()->foo, 'subfolder'); + + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->renameFolder('INBOX', 'foo'); } public function testRenameSelectedFolder() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->selectFolder('subfolder.test'); - try { - $mail->renameFolder('subfolder.test', 'foo'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while renaming selected folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->renameFolder('subfolder.test', 'foo'); } public function testRenameToChild() { $mail = new Writable\Maildir($this->_params); - try { - $mail->renameFolder('subfolder.test', 'subfolder.test.foo'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while renaming folder to child of old'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->renameFolder('subfolder.test', 'subfolder.test.foo'); } public function testAppend() @@ -347,7 +269,7 @@ public function testAppend() public function testCopy() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->selectFolder('subfolder.test'); @@ -362,12 +284,8 @@ public function testCopy() $this->assertEquals($mail->getMessage($count + 1)->from, $message->from); $this->assertEquals($mail->getMessage($count + 1)->to, $message->to); - try { - $mail->copyMessage(1, 'justARandomFolder'); - } catch (\Exception $e) { - return; // ok - } - $this->fail('no error while copying to wrong folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->copyMessage(1, 'justARandomFolder'); } public function testSetFlags() @@ -389,12 +307,8 @@ public function testSetFlags() $this->assertFalse($message->hasFlag(Storage::FLAG_SEEN)); $this->assertTrue($message->hasFlag(Storage::FLAG_FLAGGED)); - try { - $mail->setFlags(1, array(Storage::FLAG_RECENT)); - } catch (\Exception $e) { - return; // ok - } - $this->fail('should not be able to set recent flag'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->setFlags(1, array(Storage::FLAG_RECENT)); } public function testSetFlagsRemovedFile() @@ -402,13 +316,7 @@ public function testSetFlagsRemovedFile() $mail = new Writable\Maildir($this->_params); unlink($this->_params['dirname'] . 'cur/1000000000.P1.example.org:2,S'); - try { - $mail->setFlags(1, array(Storage::FLAG_FLAGGED)); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to set flags with removed file'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); } public function testRemove() @@ -428,13 +336,8 @@ public function testRemoveRemovedFile() $mail = new Writable\Maildir($this->_params); unlink($this->_params['dirname'] . 'cur/1000000000.P1.example.org:2,S'); - try { - $mail->removeMessage(1); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('should not be able to remove message which is already removed in fs'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->removeMessage(1); } public function testCheckQuota() @@ -497,13 +400,8 @@ public function testMissingMaildirsize() $this->assertNull($mail->getQuota()); - try { - $mail->getQuota(true); - } catch(MailException\ExceptionInterface $e) { - // ok - return; - } - $this->fail('get quota from file should fail if file is missing'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getQuota(true); } public function testMissingMaildirsizeWithFixedQuota() @@ -529,7 +427,7 @@ public function testMissingMaildirsizeWithFixedQuota() public function testAppendMessage() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->setQuota(array('size' => 3000, 'count' => 6, 'X' => 0)); $this->assertFalse($mail->checkQuota(false, true)); @@ -548,26 +446,19 @@ public function testAppendMessage() $mail->setQuota(false); $this->assertTrue($mail->checkQuota()); - try { - $mail->appendMessage("Subject: test\r\n\r\n"); - } catch(MailException\ExceptionInterface $e) { - $this->fail('appending should not fail if quota check is not active'); - } + + $mail->appendMessage("Subject: test\r\n\r\n"); $mail->setQuota(true); $this->assertTrue($mail->checkQuota()); - try { - $mail->appendMessage("Subject: test\r\n\r\n"); - } catch(MailException\ExceptionInterface $e) { - // ok - return; - } - $this->fail('appending after being over quota should fail'); + + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->appendMessage("Subject: test\r\n\r\n"); } public function testRemoveMessage() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->setQuota(array('size' => 3000, 'count' => 5, 'X' => 0)); $this->assertTrue($mail->checkQuota(false, true)); @@ -578,7 +469,7 @@ public function testRemoveMessage() public function testCopyMessage() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $mail->setQuota(array('size' => 3000, 'count' => 6, 'X' => 0)); $this->assertFalse($mail->checkQuota(false, true)); @@ -610,7 +501,7 @@ public function testAppendStream() public function testMove() { - $this->markTestIncomplete("Fail"); + $this->markTestIncomplete("Fail"); $mail = new Writable\Maildir($this->_params); $target = $mail->getFolders()->subfolder->test; $mail->selectFolder($target); @@ -641,11 +532,8 @@ public function testInit() $e = null; try { $mail = new Writable\Maildir($this->_params); - } catch (\Exception $e) { - } - - if ($e === null) { $this->fail('empty maildir should not be accepted'); + } catch (\Exception $e) { } Writable\Maildir::initMaildir($this->_params['dirname']); @@ -661,11 +549,8 @@ public function testCreate() $e = null; try { $mail = new Writable\Maildir($this->_params); - } catch (\Exception $e) { - } - - if ($e === null) { $this->fail('empty maildir should not be accepted'); + } catch (\Exception $e) { } $this->_params['create'] = true; diff --git a/test/Storage/MboxFolderTest.php b/test/Storage/MboxFolderTest.php index 08b6be12..7fb035c3 100644 --- a/test/Storage/MboxFolderTest.php +++ b/test/Storage/MboxFolderTest.php @@ -1,22 +1,11 @@ _params); - } catch (\Exception $e) { - $this->fail('exception raised while loading mbox folder'); - } + new Folder\Mbox($this->_params); } public function testLoadConfig() { - try { - $mail = new Folder\Mbox(new Config\Config($this->_params)); - } catch (\Exception $e) { - $this->fail('exception raised while loading mbox folder'); - } + new Folder\Mbox(new Config\Config($this->_params)); } public function testNoParams() { - try { - $mail = new Folder\Mbox(array()); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with empty params'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Mbox(array()); } public function testFilenameParam() { - try { - // filename is not allowed in this subclass - $mail = new Folder\Mbox(array('filename' => 'foobar')); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with filename as param'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + // filename is not allowed in this subclass + new Folder\Mbox(array('filename' => 'foobar')); } public function testLoadFailure() { - try { - $mail = new Folder\Mbox(array('dirname' => 'This/Folder/Does/Not/Exist')); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown dirname'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Mbox(array('dirname' => 'This/Folder/Does/Not/Exist')); } public function testLoadUnkownFolder() { $this->_params['folder'] = 'UnknownFolder'; - try { - $mail = new Folder\Mbox($this->_params); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception raised while loading unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Folder\Mbox($this->_params); } public function testChangeFolder() { $mail = new Folder\Mbox($this->_params); - try { - $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder'); - } + + $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test'); $this->assertEquals($mail->getCurrentFolder(), DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test'); } @@ -182,46 +139,29 @@ public function testChangeFolder() public function testChangeFolderUnselectable() { $mail = new Folder\Mbox($this->_params); - try { - $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder'); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while selecting unselectable folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException'); + $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder'); } public function testUnknownFolder() { $mail = new Folder\Mbox($this->_params); - try { - $mail->selectFolder('/Unknown/Folder/'); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while selecting unknown folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->selectFolder('/Unknown/Folder/'); } public function testGlobalName() { $mail = new Folder\Mbox($this->_params); - try { - // explicit call of __toString() needed for PHP < 5.2 - $this->assertEquals($mail->getFolders()->subfolder->__toString(), DIRECTORY_SEPARATOR . 'subfolder'); - } catch (MailException\ExceptionInterface $e) { - $this->fail('exception raised while selecting existing folder and getting global name'); - } + + $this->assertEquals($mail->getFolders()->subfolder->__toString(), DIRECTORY_SEPARATOR . 'subfolder'); } public function testLocalName() { $mail = new Folder\Mbox($this->_params); - try { - $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); - } catch (\Exception $e) { - $this->fail('exception raised while selecting existing folder and getting local name'); - } + + $this->assertEquals($mail->getFolders()->subfolder->key(), 'test'); } public function testIterator() @@ -341,13 +281,8 @@ public function testNotMboxFile() touch($this->_params['dirname'] . 'foobar'); $mail = new Folder\Mbox($this->_params); - try { - $mail->getFolders()->foobar; - } catch (\Exception $e) { - return; // ok - } - - $this->fail('file, which is not mbox, got parsed'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getFolders()->foobar; } public function testNotReadableFolder() @@ -385,13 +320,8 @@ public function testGetInvalidFolder() $mail = new Folder\Mbox($this->_params); $root = $mail->getFolders(); $root->foobar = new Folder('x', 'x'); - try { - $mail->getFolders('foobar'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('no error while getting invalid folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getFolders('foobar'); } public function testGetVanishedFolder() @@ -400,12 +330,7 @@ public function testGetVanishedFolder() $root = $mail->getFolders(); $root->foobar = new Folder('foobar', DIRECTORY_SEPARATOR . 'foobar'); - try { - $mail->selectFolder('foobar'); - } catch (\Exception $e) { - return; // ok - } - - $this->fail('no error while getting vanished folder'); + $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException'); + $mail->selectFolder('foobar'); } } diff --git a/test/Storage/MboxInterfaceTest.php b/test/Storage/MboxInterfaceTest.php index 182ab7f1..cb67b203 100644 --- a/test/Storage/MboxInterfaceTest.php +++ b/test/Storage/MboxInterfaceTest.php @@ -1,22 +1,11 @@ $this->_mboxFile)); - try { - $list[1] = 'test'; - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception thrown while writing to array access'); + $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException'); + $list[1] = 'test'; } public function testIterationKey() @@ -145,25 +127,16 @@ public function testFallback() { $list = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $result = $list->noop(); - $this->assertTrue($result); - } catch (\Exception $e) { - $this->fail('exception raised while calling noop thru fallback'); - } + $result = $list->noop(); + $this->assertTrue($result); } public function testWrongVariable() { $list = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $list->thisdoesnotexist; - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception thrown while reading wrong variable (via __get())'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $list->thisdoesnotexist; } public function testGetHeaders() @@ -177,12 +150,7 @@ public function testWrongHeader() { $list = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $list[1]->thisdoesnotexist; - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception thrown while reading wrong header'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $list[1]->thisdoesnotexist; } } diff --git a/test/Storage/MboxMessageOldTest.php b/test/Storage/MboxMessageOldTest.php index df248fe8..84d4e3a3 100644 --- a/test/Storage/MboxMessageOldTest.php +++ b/test/Storage/MboxMessageOldTest.php @@ -1,22 +1,11 @@ $this->_mboxFile)); - } catch (\Exception $e) { - $this->fail('exception raised while loading mbox file'); - } + new Storage\Mbox(array('filename' => $this->_mboxFile)); } public function testLoadConfig() { - try { - $mail = new Storage\Mbox(new Config\Config(array('filename' => $this->_mboxFile))); - } catch (\Exception $e) { - $this->fail('exception raised while loading mbox folder'); - } + new Storage\Mbox(new Config\Config(array('filename' => $this->_mboxFile))); } public function testNoParams() { - try { - $mail = new Storage\Mbox(array()); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with empty params'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Mbox(array()); } public function testLoadFailure() { - try { - $mail = new Storage\Mbox(array('filename' => 'ThisFileDoesNotExist')); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while loading unknown file'); + $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException'); + new Storage\Mbox(array('filename' => 'ThisFileDoesNotExist')); } public function testLoadInvalid() { - try { - $mail = new Storage\Mbox(array('filename' => __FILE__)); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while loading invalid file'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Mbox(array('filename' => __FILE__)); } public function testClose() { $mail = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $mail->close(); - } catch (\Exception $e) { - $this->fail('exception raised while closing mbox file'); - } + $mail->close(); } public function testHasTop() @@ -152,11 +112,7 @@ public function testNoop() { $mail = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $mail->noop(); - } catch (\Exception $e) { - $this->fail('exception raised while doing nothing (noop)'); - } + $mail->noop(); } public function testCount() @@ -224,13 +180,8 @@ public function testFailedRemove() { $mail = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $mail->removeMessage(1); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while deleting message (mbox is read-only)'); + $this->setExpectedException('Zend\Mail\Storage\Exception\RuntimeException'); + $mail->removeMessage(1); } public function testCapa() @@ -254,13 +205,8 @@ public function testOutOfBounds() { $mail = new Storage\Mbox(array('filename' => $this->_mboxFile)); - try { - $mail->seek(INF); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised while seeking to not invalid id'); + $this->setExpectedException('Zend\Mail\Storage\Exception\OutOfBoundsException'); + $mail->seek(INF); } public function testSleepWake() diff --git a/test/Storage/MessageTest.php b/test/Storage/MessageTest.php index 88852fbe..9b4b8754 100644 --- a/test/Storage/MessageTest.php +++ b/test/Storage/MessageTest.php @@ -1,22 +1,11 @@ $this->_file)); - $this->assertEquals($message->from, iconv('UTF-8', iconv_get_encoding('internal_encoding'), - '"Peter Müller" ')); + $this->assertEquals('"Peter Müller" ', $message->from); } public function testGetHeaderAsArray() @@ -141,9 +127,11 @@ public function testMultipleHeader() $raw = "sUBject: test\nSubJect: test2\n" . $raw; $message = new Message(array('raw' => $raw)); - $this->assertEquals($message->getHeader('subject', 'string'), - 'test' . Mime\Mime::LINEEND . 'test2' . Mime\Mime::LINEEND . 'multipart'); - $this->assertEquals($message->getHeader('subject'), array('test', 'test2', 'multipart')); + $this->assertEquals('test' . Mime\Mime::LINEEND . 'test2' . Mime\Mime::LINEEND . 'multipart', + $message->getHeader('subject', 'string')); + + $this->assertEquals(array('test', 'test2', 'multipart'), + $message->getHeader('subject', 'array')); } public function testContentTypeDecode() @@ -211,9 +199,7 @@ public function testIterator() public function testDecodeString() { $is = Mime\Decode::decodeQuotedPrintable('=?UTF-8?Q?"Peter M=C3=BCller"?= '); - $should = iconv('UTF-8', iconv_get_encoding('internal_encoding'), - '"Peter Müller" '); - $this->assertEquals($is, $should); + $this->assertEquals('"Peter Müller" ', $is); } public function testSplitHeader() @@ -244,14 +230,14 @@ public function testSplitMessage() $body = 'body'; $newlines = array("\r\n", "\n\r", "\n", "\r"); - $decoded_body = null; // "Declare" variable before first "read" usage to avoid IDEs warning - $decoded_header = null; // "Declare" variable before first "read" usage to avoid IDEs warning + $decoded_body = null; // "Declare" variable before first "read" usage to avoid IDEs warning + $decoded_headers = null; // "Declare" variable before first "read" usage to avoid IDEs warning foreach ($newlines as $contentEOL) { foreach ($newlines as $decodeEOL) { $content = $header . $contentEOL . $contentEOL . $body; - $decoded = Mime\Decode::splitMessage($content, $decoded_header, $decoded_body, $decodeEOL); - $this->assertEquals(array('test' => 'test'), $decoded_header); + Mime\Decode::splitMessage($content, $decoded_headers, $decoded_body, $decodeEOL); + $this->assertEquals(array('Test' => 'test'), $decoded_headers->toArray()); $this->assertEquals($body, $decoded_body); } } @@ -279,18 +265,13 @@ public function testNoContent() public function testEmptyHeader() { $message = new Message(array()); - $this->assertEquals(array(), $message->getHeaders()); + $this->assertEquals(array(), $message->getHeaders()->toArray()); $message = new Message(array()); $subject = null; - try { - $subject = $message->subject; - } catch (Exception\InvalidArgumentException $e) { - // ok - } - if ($subject) { - $this->fail('no exception raised while getting header from empty message'); - } + + $this->setExpectedException('Zend\\Mail\\Exception\\InvalidArgumentException'); + $message->subject; } public function testEmptyBody() @@ -317,11 +298,11 @@ public function testCheckingHasHeaderFunctionality() { $message = new Message(array('headers' => array('subject' => 'foo'))); - $this->assertTrue( $message->headerExists('subject')); + $this->assertTrue( $message->getHeaders()->has('subject')); $this->assertTrue( isset($message->subject) ); - $this->assertTrue( $message->headerExists('SuBject')); + $this->assertTrue( $message->getHeaders()->has('SuBject')); $this->assertTrue( isset($message->suBjeCt) ); - $this->assertFalse($message->headerExists('From')); + $this->assertFalse($message->getHeaders()->has('From')); } public function testWrongMultipart() @@ -441,4 +422,16 @@ public function testSpaceInFieldName() $this->assertEquals(Mime\Decode::splitHeaderField($header, 'foo'), 'bar'); $this->assertEquals(Mime\Decode::splitHeaderField($header, 'baz'), 42); } + + /** + * @group ZF2-372 + */ + public function testStrictParseMessage() + { + $this->setExpectedException('Zend\\Mail\\Exception\\RuntimeException'); + + $raw = file_get_contents($this->_file); + $raw = "From foo@example.com Sun Jan 01 00:00:00 2000\n" . $raw; + $message = new Message(array('raw' => $raw, 'strict' => true)); + } } diff --git a/test/Storage/Pop3Test.php b/test/Storage/Pop3Test.php index bc604469..41c9300e 100644 --- a/test/Storage/Pop3Test.php +++ b/test/Storage/Pop3Test.php @@ -1,22 +1,11 @@ _params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to pop3 server'); - } + new Storage\Pop3($this->_params); } public function testConnectConfig() { - try { - $mail = new Storage\Pop3(new Config\Config($this->_params)); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to pop3 server'); - } + new Storage\Pop3(new Config\Config($this->_params)); } public function testConnectFailure() { $this->_params['host'] = 'example.example'; - try { - $mail = new Storage\Pop3($this->_params); - } catch (\Exception $e) { - return; // test ok - } - // I can only hope noone installs a POP3 server there - $this->fail('no exception raised while connecting to example.example'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Pop3($this->_params); } public function testNoParams() { - try { - $mail = new Storage\Pop3(array()); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception raised with empty params'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Pop3(array()); } public function testConnectSSL() @@ -149,11 +118,8 @@ public function testConnectSSL() } $this->_params['ssl'] = 'SSL'; - try { - $mail = new Storage\Pop3($this->_params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to pop3 server with SSL'); - } + + new Storage\Pop3($this->_params); } public function testConnectTLS() @@ -163,48 +129,31 @@ public function testConnectTLS() } $this->_params['ssl'] = 'TLS'; - try { - $mail = new Storage\Pop3($this->_params); - } catch (\Exception $e) { - $this->fail('exception raised while loading connection to pop3 server with TLS'); - } + + new Storage\Pop3($this->_params); } public function testInvalidService() { $this->_params['port'] = TESTS_ZEND_MAIL_POP3_INVALID_PORT; - try { - $mail = new Storage\Pop3($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while connection to invalid port'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Pop3($this->_params); } public function testWrongService() { $this->_params['port'] = TESTS_ZEND_MAIL_POP3_WRONG_PORT; - try { - $mail = new Storage\Pop3($this->_params); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while connection to wrong port'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + new Storage\Pop3($this->_params); } public function testClose() { $mail = new Storage\Pop3($this->_params); - try { - $mail->close(); - } catch (\Exception $e) { - $this->fail('exception raised while closing pop3 connection'); - } + $mail->close(); } public function testHasTop() @@ -225,11 +174,7 @@ public function testNoop() { $mail = new Storage\Pop3($this->_params); - try { - $mail->noop(); - } catch (\Exception $e) { - $this->fail('exception raised while doing nothing (noop)'); - } + $mail->noop(); } public function testCount() @@ -312,27 +257,19 @@ public function testWithInstanceConstruction() { $protocol = new Protocol\Pop3($this->_params['host']); $mail = new Storage\Pop3($protocol); - try { - // because we did no login this has to throw an exception - $mail->getMessage(1); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception raised while fetching with wrong transport'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + // because we did no login this has to throw an exception + $mail->getMessage(1); } public function testRequestAfterClose() { $mail = new Storage\Pop3($this->_params); $mail->close(); - try { - $mail->getMessage(1); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception raised while requesting after closing connection'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getMessage(1); } public function testServerCapa() @@ -386,13 +323,9 @@ public function testUniqueId() public function testWrongUniqueId() { $mail = new Storage\Pop3($this->_params); - try { - $mail->getNumberByUniqueId('this_is_an_invalid_id'); - } catch (\Exception $e) { - return; // test ok - } - $this->fail('no exception while getting number for invalid id'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $mail->getNumberByUniqueId('this_is_an_invalid_id'); } public function testReadAfterClose() @@ -400,13 +333,8 @@ public function testReadAfterClose() $protocol = new Protocol\Pop3($this->_params['host']); $protocol->logout(); - try { - $protocol->readResponse(); - } catch (\Exception $e) { - return; // test ok - } - - $this->fail('no exception while reading from closed socket'); + $this->setExpectedException('Zend\Mail\Storage\Exception\InvalidArgumentException'); + $protocol->readResponse(); } public function testRemove() diff --git a/test/TestAsset/SmtpProtocolSpy.php b/test/TestAsset/SmtpProtocolSpy.php index 6d349675..d92f2d58 100644 --- a/test/TestAsset/SmtpProtocolSpy.php +++ b/test/TestAsset/SmtpProtocolSpy.php @@ -1,22 +1,11 @@ _addLog($request . self::EOL); } - protected function _expect($code, $timeout = null) { + protected function _expect($code, $timeout = null) + { return ''; } /** * Are we connected? - * + * * @return bool */ public function isConnected() @@ -105,7 +93,7 @@ public function isConnected() /** * Get server name we opened a connection with - * + * * @return null|string */ public function getHelo() @@ -115,7 +103,7 @@ public function getHelo() /** * Get value of mail property - * + * * @return null|string */ public function getMail() @@ -125,11 +113,11 @@ public function getMail() /** * Get recipients - * + * * @return array */ public function getRecipients() { return $this->rcpt; } -} \ No newline at end of file +} diff --git a/test/TestAsset/StringSerializableObject.php b/test/TestAsset/StringSerializableObject.php index 532d2002..35c9fde9 100644 --- a/test/TestAsset/StringSerializableObject.php +++ b/test/TestAsset/StringSerializableObject.php @@ -1,4 +1,12 @@ cleanup($this->tempDir); } - + $fileOptions = new FileOptions(array( 'path' => $this->tempDir, )); @@ -76,7 +63,7 @@ public function getMessage() ->setSender('ralph.schindler@zend.com', 'Ralph Schindler') ->setSubject('Testing Zend\Mail\Transport\Sendmail') ->setBody('This is only a test.'); - $message->headers()->addHeaders(array( + $message->getHeaders()->addHeaders(array( 'X-Foo-Bar' => 'Matthew', )); return $message; diff --git a/test/Transport/SendmailTest.php b/test/Transport/SendmailTest.php index d49d3e99..a7bf9a42 100644 --- a/test/Transport/SendmailTest.php +++ b/test/Transport/SendmailTest.php @@ -1,35 +1,22 @@ setSender('ralph.schindler@zend.com', 'Ralph Schindler') ->setSubject('Testing Zend\Mail\Transport\Sendmail') ->setBody('This is only a test.'); - $message->headers()->addHeaders(array( + $message->getHeaders()->addHeaders(array( 'X-Foo-Bar' => 'Matthew', )); return $message; diff --git a/test/Transport/SmtpTest.php b/test/Transport/SmtpTest.php index 19a84c20..d2b9735b 100644 --- a/test/Transport/SmtpTest.php +++ b/test/Transport/SmtpTest.php @@ -1,38 +1,25 @@ setSender('ralph.schindler@zend.com', 'Ralph Schindler') ->setSubject('Testing Zend\Mail\Transport\Sendmail') ->setBody('This is only a test.'); - $message->headers()->addHeaders(array( + $message->getHeaders()->addHeaders(array( 'X-Foo-Bar' => 'Matthew', )); return $message; @@ -71,7 +58,8 @@ public function getMessage() /** * Per RFC 2822 3.6 */ - public function testSendMailWithoutMinimalHeaders() { + public function testSendMailWithoutMinimalHeaders() + { $this->setExpectedException( 'Zend\Mail\Transport\Exception\RuntimeException', 'transport expects either a Sender or at least one From address in the Message; none provided' @@ -84,7 +72,8 @@ public function testSendMailWithoutMinimalHeaders() { * Per RFC 2821 3.3 (page 18) * - RCPT (recipient) must be called before DATA (headers or body) */ - public function testSendMailWithoutRecipient() { + public function testSendMailWithoutRecipient() + { $this->setExpectedException( 'Zend\Mail\Transport\Exception\RuntimeException', 'at least one recipient if the message has at least one header or body' @@ -94,7 +83,8 @@ public function testSendMailWithoutRecipient() { $this->transport->send($message); } - public function testSendMinimalMail() { + public function testSendMinimalMail() + { $headers = new Headers(); $headers->addHeaderLine('Date', 'Sun, 10 Jun 2012 20:07:24 +0200'); $message = new Message(); @@ -136,40 +126,40 @@ public function testReceivesMailArtifacts() $this->assertContains("\r\n\r\nThis is only a test.", $data, $data); } - public function testCanUseAuthenticationExtensionsViaPluginBroker() + public function testCanUseAuthenticationExtensionsViaPluginManager() { $options = new SmtpOptions(array( 'connection_class' => 'login', )); $transport = new Smtp($options); - $connection = $transport->plugin($options->getConnectionClass(), array(array( + $connection = $transport->plugin($options->getConnectionClass(), array( 'username' => 'matthew', 'password' => 'password', 'host' => 'localhost', - ))); + )); $this->assertInstanceOf('Zend\Mail\Protocol\Smtp\Auth\Login', $connection); $this->assertEquals('matthew', $connection->getUsername()); $this->assertEquals('password', $connection->getPassword()); } - + public function testSetAutoDisconnect() { $this->transport->setAutoDisconnect(false); $this->assertFalse($this->transport->getAutoDisconnect()); } - + public function testGetDefaultAutoDisconnectValue() { $this->assertTrue($this->transport->getAutoDisconnect()); } - + public function testAutoDisconnectTrue() { $this->connection->connect(); unset($this->transport); $this->assertFalse($this->connection->isConnected()); } - + public function testAutoDisconnectFalse() { $this->connection->connect(); @@ -177,7 +167,7 @@ public function testAutoDisconnectFalse() unset($this->transport); $this->assertTrue($this->connection->isConnected()); } - + public function testDisconnect() { $this->connection->connect(); diff --git a/test/_files/mail.txt b/test/_files/mail.txt index 5bba2339..52587bac 100644 --- a/test/_files/mail.txt +++ b/test/_files/mail.txt @@ -3,6 +3,7 @@ Subject: multipart Date: Sun, 01 Jan 2000 00:00:00 +0000 From: =?UTF-8?Q?"Peter M=C3=BCller"?= ContENT-type: multipart/alternative; boUNDary="crazy-multipart" +Message-ID: MIME-version: 1.0 multipart message