Skip to content

Commit

Permalink
Issue #140, #246: Improved exception handling and added PHPUnit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Krätzig committed May 1, 2019
1 parent c928a0d commit 48f8616
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/PhpImap/ConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace PhpImap\Exceptions;

/**
* @see https://github.com/barbushin/php-imap
* @author Barbushin Sergey http://linkedin.com/in/barbushin
*/
class ConnectionException extends Exception {}
18 changes: 9 additions & 9 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php namespace PhpImap;
<?php
namespace PhpImap;

use stdClass;
use Exception;
use PhpImap\Exceptions\ConnectionException;

/**
* @see https://github.com/barbushin/php-imap
Expand Down Expand Up @@ -789,8 +792,13 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $m
* @param string $string
* @param string $toEncoding
* @return string Converted string if conversion was successful, or the original string if not
* @throws Exception
*/
public function decodeMimeStr($string, $toCharset = 'utf-8') {
if(empty(trim($string))) {
throw new Exception('decodeMimeStr() Can not decode an empty string!');
}

$newString = '';
foreach(imap_mime_header_decode($string) as $element) {
if(isset($element->text)) {
Expand Down Expand Up @@ -988,11 +996,3 @@ public function imap($methodShortName, $args = [], $prependConnectionAsFirstArg
return $result;
}
}

class Exception extends \Exception {

}

class ConnectionException extends Exception {

}
10 changes: 8 additions & 2 deletions tests/unit/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @author Sebastian Kraetzig <sebastian-kraetzig@gmx.de>
*/

use DateTime;
use PhpImap\Mailbox;
use PhpImap\Exceptions\ConnectionException;
use PHPUnit\Framework\TestCase;

final class MailboxTest extends TestCase
Expand Down Expand Up @@ -340,13 +340,19 @@ public function testMimeEncoding() {
'=?iso-8859-1?Q?Sebastian_Kr=E4tzig?=' => 'Sebastian Krätzig',
'sebastian.kraetzig' => 'sebastian.kraetzig',
'=?US-ASCII?Q?Keith_Moore?= <km@ab.example.edu>' => 'Keith Moore <km@ab.example.edu>',
' ' => '',
'=?ISO-8859-1?Q?Max_J=F8rn_Simsen?= <max.joern.s@example.dk>' => 'Max Jørn Simsen <max.joern.s@example.dk>',
'=?ISO-8859-1?Q?Andr=E9?= Muster <andre.muster@vm1.ulg.ac.be>' => 'André Muster <andre.muster@vm1.ulg.ac.be>',
'=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=' => 'If you can read this you understand the example.'
);

foreach($test_strings as $str => $expected) {
$this->assertEquals($this->mailbox->decodeMimeStr($str), $expected);
if(empty($expected)) {
$this->expectException(Exception::class);
$this->mailbox->decodeMimeStr($str);
} else {
$this->assertEquals($this->mailbox->decodeMimeStr($str), $expected);
}
}
}
}

0 comments on commit 48f8616

Please sign in to comment.