From 89198125236ded1c3da17a8f41d5fa3c9c37887e Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Wed, 8 Nov 2017 13:54:38 +0100 Subject: [PATCH 01/10] uid in appended message as return --- src/Mailbox.php | 16 ++++++++++------ src/MailboxInterface.php | 4 ++-- tests/MessageTest.php | 34 ++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index 7725b596..e3c53290 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -128,7 +128,7 @@ public function setFlag(string $flag, $numbers): bool $numbers = \implode(',', $numbers); } - return \imap_setflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); + return \imap_setflag_full($this->resource->getStream(), (string)$numbers, $flag, \ST_UID); } /** @@ -145,7 +145,7 @@ public function clearFlag(string $flag, $numbers): bool $numbers = \implode(',', $numbers); } - return \imap_clearflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); + return \imap_clearflag_full($this->resource->getStream(), (string)$numbers, $flag, \ST_UID); } /** @@ -210,11 +210,14 @@ public function getIterator(): MessageIteratorInterface * * @param string $message * - * @return bool + * @return int sppended message UID */ - public function addMessage(string $message): bool + public function addMessage(string $message): int { - return \imap_append($this->resource->getStream(), $this->getFullEncodedName(), $message); + $status = $this->getStatus(SA_UIDNEXT); + if (\imap_append($this->resource->getStream(), $this->getFullEncodedName(), $message)) { + return $status->uidnext; + } } /** @@ -224,7 +227,8 @@ public function addMessage(string $message): bool */ public function getThread(): array { - \set_error_handler(function () {}); + \set_error_handler(function () { + }); $tree = \imap_thread($this->resource->getStream()); diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 24c6e24b..9703b43d 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -105,9 +105,9 @@ public function getIterator(): MessageIteratorInterface; * * @param string $message * - * @return bool + * @return int */ - public function addMessage(string $message): bool; + public function addMessage(string $message): int; /** * Returns a tree of threaded message for the current Mailbox. diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 8e8cd178..08e93f8d 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -7,6 +7,7 @@ use Ddeboer\Imap\Exception\InvalidDateHeaderException; use Ddeboer\Imap\Exception\MessageDoesNotExistException; use Ddeboer\Imap\Exception\UnsupportedCharsetException; +use Ddeboer\Imap\Mailbox; use Ddeboer\Imap\Message; use Ddeboer\Imap\Message\EmailAddress; use Ddeboer\Imap\Message\Parameters; @@ -39,20 +40,24 @@ final class MessageTest extends AbstractTest ]; private static $charsets = [ - 'ASCII' => '! "#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', - 'GB18030' => " 、。〃々〆〇〈〉《》「」『』【】〒〓〔〕〖〗〝〞〡〢〣〤〥〦〧〨〩〾一\u{200b}丁\u{200b}丂踰\u{200b}踱\u{200b}踲\u{200b}", - 'ISO-8859-6' => 'ءآأؤإئابةتثجحخدذرزسشصضطظعغـفقكلمنهوىي', - 'ISO-8859-7' => 'ΆΈΉΊ»Ό½ΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ2ΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ', - 'SJIS' => '。「」、・ヲァィゥェォャュョッBーアイウエオカキクケコサシスセソCタチツテトナニヌネノハヒフヘホマDミムメモヤユヨラリルレロワン゙゚', - 'UTF-8' => '€✔', + 'ASCII' => '! "#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', + 'GB18030' => " 、。〃々〆〇〈〉《》「」『』【】〒〓〔〕〖〗〝〞〡〢〣〤〥〦〧〨〩〾一\u{200b}丁\u{200b}丂踰\u{200b}踱\u{200b}踲\u{200b}", + 'ISO-8859-6' => 'ءآأؤإئابةتثجحخدذرزسشصضطظعغـفقكلمنهوىي', + 'ISO-8859-7' => 'ΆΈΉΊ»Ό½ΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ2ΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ', + 'SJIS' => '。「」、・ヲァィゥェォャュョッBーアイウエオカキクケコサシスセソCタチツテトナニヌネノハヒフヘホマDミムメモヤユヨラリルレロワン゙゚', + 'UTF-8' => '€✔', 'Windows-1251' => 'ЂЃѓЉЊЌЋЏђљњќћџЎўЈҐЁЄЇІіґёєјЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя', 'Windows-1252' => 'ƒŠŒŽšœžŸªºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', ]; private static $iconvOnlyCharsets = [ - 'macintosh' => '†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈«»…ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ', + 'macintosh' => '†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈«»…ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ', 'Windows-1250' => 'ŚŤŹśťźˇ˘ŁĄŞŻ˛łąşĽ˝ľż', ]; + /** + * @var Mailbox + */ + protected $mailbox; protected function setUp() { @@ -65,7 +70,7 @@ public function testCustomNonExistentMessageFetch() $messageNumber = 98765; $this->expectException(MessageDoesNotExistException::class); - $this->expectExceptionMessageRegExp(\sprintf('/E_WARNING.+%s/s', \preg_quote((string) $messageNumber))); + $this->expectExceptionMessageRegExp(\sprintf('/E_WARNING.+%s/s', \preg_quote((string)$messageNumber))); new Message($connection->getResource(), $messageNumber); } @@ -236,6 +241,12 @@ public function provideIconvCharsets(): array return $provider; } + public function testAppendedMessageUid() + { + $uid = $this->mailbox->addMessage($this->getFixture('email_address')); + $this->assertEquals(1, $uid); + } + public function testEmailAddress() { $this->mailbox->addMessage($this->getFixture('email_address')); @@ -473,8 +484,7 @@ public function testRawHeaders() { $headers = 'From: from@there.com' . "\r\n" . 'To: to@here.com' . "\n" - . "\r\n" - ; + . "\r\n"; $originalMessage = $headers . 'Content' . "\n"; $this->mailbox->addMessage($originalMessage); @@ -643,9 +653,9 @@ public function testSignedMessage() $this->assertCount(3, $attachments); $expected = [ - 'data.xml' => 'PHhtbC8+', + 'data.xml' => 'PHhtbC8+', 'postacert.eml' => 'test-content', - 'smime.p7s' => 'MQ==', + 'smime.p7s' => 'MQ==', ]; foreach ($attachments as $attachment) { From 9da10aa0ecd1ff0ee23f94cef71d6081d7778657 Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Mon, 20 Nov 2017 15:43:24 +0100 Subject: [PATCH 02/10] Mailboxes parser --- .gitignore | 2 + phpunit.xml.dist | 12 +- src/Exception/MailboxesParserException.php | 9 + src/MailboxesParser/MailboxesParser.php | 245 ++++++++++++++++++ src/MailboxesParser/MailboxesTreeParser.php | 77 ++++++ src/MailboxesParser/ParsedMailbox.php | 155 +++++++++++ .../languages/folderNames/pl/ids.php | 21 ++ .../languages/folderNames/pl/names.php | 21 ++ tests/AbstractTest.php | 2 +- tests/MailboxesParser/MailboxesParserTest.php | 85 ++++++ .../MailboxesTreeParserTest.php | 108 ++++++++ 11 files changed, 733 insertions(+), 4 deletions(-) create mode 100644 src/Exception/MailboxesParserException.php create mode 100644 src/MailboxesParser/MailboxesParser.php create mode 100644 src/MailboxesParser/MailboxesTreeParser.php create mode 100644 src/MailboxesParser/ParsedMailbox.php create mode 100644 src/MailboxesParser/languages/folderNames/pl/ids.php create mode 100644 src/MailboxesParser/languages/folderNames/pl/names.php create mode 100644 tests/MailboxesParser/MailboxesParserTest.php create mode 100644 tests/MailboxesParser/MailboxesTreeParserTest.php diff --git a/.gitignore b/.gitignore index 825c1913..afca4f96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor/ .php_cs.cache composer.lock phpunit.xml +.idea/ +phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7c4f9a0e..a8367edd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,9 @@ @@ -15,4 +15,10 @@ ./src + + + + + + diff --git a/src/Exception/MailboxesParserException.php b/src/Exception/MailboxesParserException.php new file mode 100644 index 00000000..141c244a --- /dev/null +++ b/src/Exception/MailboxesParserException.php @@ -0,0 +1,9 @@ + ['inbox',], + self::SENT => ['sent', 'sent messages', 'INBOX.Sent', '[Gmail]/Sent Mail',], + self::DRAFT => ['drafts', 'INBOX.Drafts', '[Gmail]/Drafts',], + self::SPAM => ['spam', 'INBOX.spam', '[Gmail]/spam'], + self::TRASH => ['trash', 'bin', 'INBOX.trash', '[Gmail]/trash'], + self::TEMPLATES => ['templates'], + self::ARCHIVES => ['archives',], + ]; + + protected $specialFoldersNames = [ + self::DRAFT => 'Drafts', + self::INBOX => 'Inbox', + self::SENT => 'Sent', + self::SPAM => 'Spam', + self::TRASH => 'Trash', + self::TEMPLATES => 'Templates', + self::ARCHIVES => 'Archives', + ]; + + protected $specialFoldersOrder = [ + self::INBOX => 1, + self::SENT => 2, + self::DRAFT => 3, + self::TEMPLATES => 4, + self::ARCHIVES => 10000, + self::SPAM => 20000, + self::TRASH => 30000, + ]; + + /** + * MailboxesTree constructor. + * + * @param MailboxInterface[] $mailboxes + */ + public function __construct($mailboxes) + { + $this->mailboxes = $mailboxes; + } + + public function setLanguage($lang) + { + $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'folderNames' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'names.php'; + if (!is_file($path)) { + throw new MailboxesParserException(\sprintf('File for language names %s does not exist', $path)); + } + $names = require $path; + $this->setSpecialFoldersNames($names); + + $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'folderNames' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'ids.php'; + if (!is_file($path)) { + throw new MailboxesParserException(\sprintf('File for language ids %s does not exist', $path)); + } + $ids = require $path; + foreach ($ids AS $specialFolder => $idsArray) { + foreach ($idsArray AS $id) { + $this->addSpecialFolderId($specialFolder, $id); + } + } + } + + protected function parse() + { + $this->folders = []; + usort($this->mailboxes, [$this, "sortByMailboxName"]); + foreach ($this->mailboxes AS $k => $mailbox) { + $mailboxName = $mailbox->getName(); + $folder = new ParsedMailbox(); + $folder->setMailbox($mailbox); + $folder->setMailboxName($mailboxName); + $special = $this->getSpecialFolder($mailboxName); + $folder->setSpecial($special); + $folder->setName($special ? $this->specialFoldersNames[$special] : $this->getName($mailboxName, $mailbox->getDelimiter())); + $folder->setOrder($special ? $this->specialFoldersOrder[$special] : $this->getOrder($mailboxName, $mailbox->getDelimiter())); + $folder->setLevel($this->getFolderLevel($mailboxName, $mailbox->getDelimiter())); + $folder->setDelimiter($mailbox->getDelimiter()); + $this->folders[] = $folder; + } + + usort($this->folders, [$this, "sortByOrder"]); + + return $this->folders; + } + + + protected function sortByMailboxName(MailboxInterface $a, MailboxInterface $b) + { + return ($a->getName() < $b->getName()) ? -1 : 1; + } + + protected function sortByOrder(ParsedMailbox $a, ParsedMailbox $b) + { + return ($a->getOrder() < $b->getOrder()) ? -1 : 1; + } + + /** + * @return ParsedMailbox[] + */ + public function getFolders() + { + if (!$this->folders) { + $this->parse(); + } + + return $this->folders; + } + + /** + * @return mixed + */ + public function getTreeStructure() + { + if (!$this->treeStructure) { + $treeParser = new MailboxesTreeParser(); + $this->treeStructure = $treeParser->parse($this->getFolders()); + } + + return $this->treeStructure; + } + + /** + * @param array $specialFoldersNames + */ + public function setSpecialFoldersNames(array $specialFoldersNames) + { + $this->specialFoldersNames = $specialFoldersNames; + } + + protected function getSpecialFolder($mailboxName) + { + foreach ($this->specialFoldersIds AS $specialFolderKind => $names) { + $lower = mb_strtolower($mailboxName); + foreach ($names AS $name) { + if ($lower === mb_strtolower($name)) { + return $specialFolderKind; + } + } + } + + return null; + } + + private function getName($mailboxName, $delimiter = '.') + { + $e = explode($delimiter, $mailboxName); + + return ucfirst($e[count($e) - 1]); + } + + private function getOrder($mailboxName, $delimiter) + { + if ($this->folders[$mailboxName]['special']) { + return $this->specialFoldersOrder[$this->folders[$mailboxName]['special']]; + } else { + $e = explode($delimiter, $mailboxName); + + $level = count($e) - 1; + if ($e[0] === 'INBOX' && $level === 1) { + $level = -1; + } + if ($e[1]) { + array_pop($e); + $parentMailboxName = implode($delimiter, $e); + if ($level === -1) { + $multiplier = 100; + } else { + $power = -1 * ($level * 2); + $multiplier = pow(10, ($power)); + } + $parsedParent = null; + /** @var ParsedMailbox $parsedMailbox */ + foreach ($this->folders as $parsedMailbox) { + if ($parsedMailbox->getMailboxName() === $parentMailboxName) { + $parsedParent = $parsedMailbox; + break; + } + } + if ($parsedParent) { + $parsedParent->setSubfolders($parsedParent->getSubfolders() + 1); + $order = ($parsedParent->getOrder() + ($parsedParent->getSubfolders() * $multiplier)); + } else { + $order = 10; + } + } else { + $order = 10; + } + + return $order; + } + } + + public function getMailboxNameForSpecial($special) + { + /** @var ParsedMailbox $parsedMailbox */ + foreach ($this->folders as $parsedMailbox) { + if ($parsedMailbox->getSpecial() === $special) { + return $parsedMailbox->getMailboxName(); + } + } + + return null; + } + + public function addSpecialFolderId($specialFolder, $id) + { + $this->specialFoldersIds[$specialFolder][] = $id; + } + + private function getFolderLevel($mailboxName, $delimiter) + { + $e = explode($delimiter, $mailboxName); + + return count($e); + } +} \ No newline at end of file diff --git a/src/MailboxesParser/MailboxesTreeParser.php b/src/MailboxesParser/MailboxesTreeParser.php new file mode 100644 index 00000000..a588c5fc --- /dev/null +++ b/src/MailboxesParser/MailboxesTreeParser.php @@ -0,0 +1,77 @@ +getDelimiter(), $value->getMailboxName()); + $newkey = []; + foreach ($k as $segment) { + $newkey[] = $segment; + $newkey[] = "subfolders"; + } + + return implode(".", $newkey); + }, array_keys($input), $input); + + $arrayToParse = []; + foreach ($newKeys AS $index => $value) { + $k = explode(".", $value); + $keyWithoutLast = implode('.', array_splice($k, 0, -1)); + $arrayToParse[$value] = []; + if ($input[$index]) { + /** @var ParsedMailbox $parsedMailbox */ + $parsedMailbox = $input[$index]; + $arrayToParse[$keyWithoutLast . '.mailboxName'] = $parsedMailbox->getMailboxName(); + $arrayToParse[$keyWithoutLast . '.name'] = $parsedMailbox->getName(); + } + } + + $res = []; + array_walk($arrayToParse, function ($value, $key) use (&$res) { + $this->set($res, $key, $value); + }); + + return $res; + } + + private function set(&$array, $key, $value) + { + if (is_null($key)) { + return $array = $value; + } + $keys = explode('.', $key); + while (count($keys) > 1) { + $key = array_shift($keys); + // If the key doesn't exist at this depth, we will just create an empty array + // to hold the next value, allowing us to create the arrays to hold final + // values at the correct depth. Then we'll keep digging into the array. + if (!isset($array[$key]) || !is_array($array[$key])) { + $array[$key] = []; + } + $array = &$array[$key]; + } + $array[array_shift($keys)] = $value; + + return $array; + } +} \ No newline at end of file diff --git a/src/MailboxesParser/ParsedMailbox.php b/src/MailboxesParser/ParsedMailbox.php new file mode 100644 index 00000000..1bf98514 --- /dev/null +++ b/src/MailboxesParser/ParsedMailbox.php @@ -0,0 +1,155 @@ +mailbox; + } + + /** + * @param MailboxInterface $mailbox + */ + public function setMailbox(MailboxInterface $mailbox) + { + $this->mailbox = $mailbox; + } + + /** + * @return mixed + */ + public function getOrder() + { + return $this->order; + } + + /** + * @param mixed $order + */ + public function setOrder($order) + { + $this->order = $order; + } + + /** + * @return mixed + */ + public function getMailboxName() + { + return $this->mailboxName; + } + + /** + * @param mixed $mailboxName + */ + public function setMailboxName($mailboxName) + { + $this->mailboxName = $mailboxName; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return mixed + */ + public function getSpecial() + { + return $this->special; + } + + /** + * @param mixed $special + */ + public function setSpecial($special) + { + $this->special = $special; + } + + /** + * @return string + */ + public function getDelimiter(): string + { + return $this->delimiter; + } + + /** + * @param string $delimiter + */ + public function setDelimiter(string $delimiter) + { + $this->delimiter = $delimiter; + } + + /** + * @return int + */ + public function getLevel(): int + { + return $this->level; + } + + /** + * @param int $level + */ + public function setLevel(int $level) + { + $this->level = $level; + } + + /** + * @return int + */ + public function getSubfolders(): int + { + return $this->subfolders; + } + + /** + * @param int $subfolders + */ + public function setSubfolders(int $subfolders) + { + $this->subfolders = $subfolders; + } + + +} \ No newline at end of file diff --git a/src/MailboxesParser/languages/folderNames/pl/ids.php b/src/MailboxesParser/languages/folderNames/pl/ids.php new file mode 100644 index 00000000..59aa4f82 --- /dev/null +++ b/src/MailboxesParser/languages/folderNames/pl/ids.php @@ -0,0 +1,21 @@ + ['INBOX.odebrane', 'odebrane'], + MailboxesParser::SENT => ['INBOX.Wysłane', 'elementy wysłane', 'wysłane'], + MailboxesParser::DRAFT => ['szkice',], + MailboxesParser::SPAM => [], + MailboxesParser::TRASH => ['kosz',], + MailboxesParser::TEMPLATES => ['szablony',], + MailboxesParser::ARCHIVES => ['archiwum',], +]; \ No newline at end of file diff --git a/src/MailboxesParser/languages/folderNames/pl/names.php b/src/MailboxesParser/languages/folderNames/pl/names.php new file mode 100644 index 00000000..eece9364 --- /dev/null +++ b/src/MailboxesParser/languages/folderNames/pl/names.php @@ -0,0 +1,21 @@ + 'Odebrane', + MailboxesParser::SENT => 'Wysłane', + MailboxesParser::DRAFT => 'Szkice', + MailboxesParser::SPAM => 'Spam', + MailboxesParser::TRASH => 'Kosz', + MailboxesParser::TEMPLATES => 'Szablony', + MailboxesParser::ARCHIVES => 'Archiwum', +]; \ No newline at end of file diff --git a/tests/AbstractTest.php b/tests/AbstractTest.php index 4825b141..b8a39a5e 100644 --- a/tests/AbstractTest.php +++ b/tests/AbstractTest.php @@ -37,7 +37,7 @@ final protected function createConnection(): Connection final protected function createMailbox(Connection $connection = null): Mailbox { $connection = $connection ?? $this->getConnection(); - $this->mailboxName = \uniqid('mailbox_' . self::SPECIAL_CHARS); + $this->mailboxName = \uniqid('INBOX.mailbox_' . self::SPECIAL_CHARS); return $connection->createMailbox($this->mailboxName); } diff --git a/tests/MailboxesParser/MailboxesParserTest.php b/tests/MailboxesParser/MailboxesParserTest.php new file mode 100644 index 00000000..f40a492a --- /dev/null +++ b/tests/MailboxesParser/MailboxesParserTest.php @@ -0,0 +1,85 @@ +mailboxes = [ + $this->createMailboxMock('INBOX.Drafts'), + $this->createMailboxMock('INBOX.Sent'), + $this->createMailboxMock('INBOX.Sent.sub'), + $this->createMailboxMock('INBOX.normal'), + $this->createMailboxMock('INBOX'), + $this->createMailboxMock('INBOX.normal.sub'), + $this->createMailboxMock('INBOX.trash'), + $this->createMailboxMock('Outside'), + ]; + } + + public function testParser() + { + $parser = new MailboxesParser($this->mailboxes); + $folders = $parser->getFolders(); + $this->assertCount(8, $folders); + $this->assertEquals('INBOX', $folders[0]->getMailboxName()); + $this->assertEquals('Inbox', $folders[0]->getName()); + $this->assertInstanceOf(MailboxInterface::class, $folders[0]->getMailbox()); + $order = []; + /** @var ParsedMailbox $parsedMailbox */ + foreach ($folders AS $parsedMailbox) { + $order[] = ['order' => $parsedMailbox->getOrder(), 'name' => $parsedMailbox->getMailboxName()]; + } + $expected = [ + ['order' => 1, 'name' => 'INBOX'], + ['order' => 2, 'name' => 'INBOX.Sent'], + ['order' => 2.0001, 'name' => 'INBOX.Sent.sub'], + ['order' => 3, 'name' => 'INBOX.Drafts'], + ['order' => 10, 'name' => 'Outside'], + ['order' => 101, 'name' => 'INBOX.normal'], + ['order' => 101.0001, 'name' => 'INBOX.normal.sub'], + ['order' => 30000, 'name' => 'INBOX.trash'], + ]; +// $tree = $parser->getTreeStructure(); + $this->assertEquals($expected, $order); + } + + public function testSetLanguage() + { + $this->mailboxes[] = $this->createMailboxMock('INBOX.Wysłane'); + $parser = new MailboxesParser($this->mailboxes); + $parser->setLanguage('pl'); + $folders = $parser->getFolders(); + $this->assertEquals('INBOX', $folders[0]->getMailboxName()); + $this->assertEquals('Odebrane', $folders[0]->getName()); + $this->assertEquals('INBOX.Wysłane', $folders[1]->getMailboxName()); + $this->assertEquals('Wysłane', $folders[1]->getName()); + } + + private function createMailboxMock($mailboxName) + { + $mailbox = $this->createMock(MailboxInterface::class); + $mailbox->method('getName') + ->willReturn($mailboxName); + + $mailbox->method('getDelimiter') + ->willReturn('.'); + + return $mailbox; + } +} diff --git a/tests/MailboxesParser/MailboxesTreeParserTest.php b/tests/MailboxesParser/MailboxesTreeParserTest.php new file mode 100644 index 00000000..f8be4bdb --- /dev/null +++ b/tests/MailboxesParser/MailboxesTreeParserTest.php @@ -0,0 +1,108 @@ +createMailboxMock('inbox'), + $this->createMailboxMock('inbox.first'), + $this->createMailboxMock('inbox.second'), + $this->createMailboxMock('inbox.second.other'), + $this->createMailboxMock('inbox.third.another'), + ]; + $parser = new MailboxesParser($mailboxes); + $folders = $parser->getFolders(); + $parser = new MailboxesTreeParser(); + $zwroc = $parser->parse($folders); + $spodziewane = [ + 'inbox' => [ + 'name' => 'Inbox', + 'mailboxName' => 'inbox', + 'subfolders' => [ + 'first' => [ + 'name' => 'First', + 'mailboxName' => 'inbox.first', + 'subfolders' => [], + ], + 'second' => [ + 'name' => 'Second', + 'mailboxName' => 'inbox.second', + 'subfolders' => [ + 'other' => [ + 'name' => 'Other', + 'subfolders' => [], + 'mailboxName' => 'inbox.second.other', + ], + ], + ], + 'third' => [ + 'subfolders' => [ + 'another' => [ + 'name' => 'Another', + 'subfolders' => [], + 'mailboxName' => 'inbox.third.another', + ], + ], + ], + ], + ], + ]; + $this->assertEquals($spodziewane, $zwroc); + } + +// public function testTreeParserWithDashDelimiter() +// { +// $dane = [ +// 'inbox' => ['name' => 'Inbox'], +// 'inbox|first' => ['name' => 'First'], +// 'inbox|second' => ['name' => 'Second'], +// ]; +// $parser = new MailboxesTreeParser(); +// $zwroc = $parser->parse($dane, '|'); +// $spodziewane = [ +// 'inbox' => [ +// 'name' => 'Inbox', +// 'mailboxName' => 'inbox', +// 'subfolders' => [ +// 'first' => [ +// 'name' => 'First', +// 'mailboxName' => 'inbox|first', +// 'subfolders' => [], +// ], +// 'second' => [ +// 'name' => 'Second', +// 'mailboxName' => 'inbox|second', +// 'subfolders' => [], +// ], +// ], +// ], +// ]; +// $this->assertEquals($spodziewane, $zwroc); +// } + + private function createMailboxMock($mailboxName) + { + $mailbox = $this->createMock(MailboxInterface::class); + $mailbox->method('getName') + ->willReturn($mailboxName); + + $mailbox->method('getDelimiter') + ->willReturn('.'); + + return $mailbox; + } +} From 62827ce49d740c01372414ba22a82af5480fe96c Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Mon, 20 Nov 2017 15:45:36 +0100 Subject: [PATCH 03/10] Revert "uid in appended message as return" This reverts commit 8919812 --- src/Mailbox.php | 16 ++++++---------- src/MailboxInterface.php | 4 ++-- tests/MessageTest.php | 34 ++++++++++++---------------------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index e3c53290..7725b596 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -128,7 +128,7 @@ public function setFlag(string $flag, $numbers): bool $numbers = \implode(',', $numbers); } - return \imap_setflag_full($this->resource->getStream(), (string)$numbers, $flag, \ST_UID); + return \imap_setflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); } /** @@ -145,7 +145,7 @@ public function clearFlag(string $flag, $numbers): bool $numbers = \implode(',', $numbers); } - return \imap_clearflag_full($this->resource->getStream(), (string)$numbers, $flag, \ST_UID); + return \imap_clearflag_full($this->resource->getStream(), (string) $numbers, $flag, \ST_UID); } /** @@ -210,14 +210,11 @@ public function getIterator(): MessageIteratorInterface * * @param string $message * - * @return int sppended message UID + * @return bool */ - public function addMessage(string $message): int + public function addMessage(string $message): bool { - $status = $this->getStatus(SA_UIDNEXT); - if (\imap_append($this->resource->getStream(), $this->getFullEncodedName(), $message)) { - return $status->uidnext; - } + return \imap_append($this->resource->getStream(), $this->getFullEncodedName(), $message); } /** @@ -227,8 +224,7 @@ public function addMessage(string $message): int */ public function getThread(): array { - \set_error_handler(function () { - }); + \set_error_handler(function () {}); $tree = \imap_thread($this->resource->getStream()); diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 9703b43d..24c6e24b 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -105,9 +105,9 @@ public function getIterator(): MessageIteratorInterface; * * @param string $message * - * @return int + * @return bool */ - public function addMessage(string $message): int; + public function addMessage(string $message): bool; /** * Returns a tree of threaded message for the current Mailbox. diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 08e93f8d..8e8cd178 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -7,7 +7,6 @@ use Ddeboer\Imap\Exception\InvalidDateHeaderException; use Ddeboer\Imap\Exception\MessageDoesNotExistException; use Ddeboer\Imap\Exception\UnsupportedCharsetException; -use Ddeboer\Imap\Mailbox; use Ddeboer\Imap\Message; use Ddeboer\Imap\Message\EmailAddress; use Ddeboer\Imap\Message\Parameters; @@ -40,24 +39,20 @@ final class MessageTest extends AbstractTest ]; private static $charsets = [ - 'ASCII' => '! "#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', - 'GB18030' => " 、。〃々〆〇〈〉《》「」『』【】〒〓〔〕〖〗〝〞〡〢〣〤〥〦〧〨〩〾一\u{200b}丁\u{200b}丂踰\u{200b}踱\u{200b}踲\u{200b}", - 'ISO-8859-6' => 'ءآأؤإئابةتثجحخدذرزسشصضطظعغـفقكلمنهوىي', - 'ISO-8859-7' => 'ΆΈΉΊ»Ό½ΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ2ΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ', - 'SJIS' => '。「」、・ヲァィゥェォャュョッBーアイウエオカキクケコサシスセソCタチツテトナニヌネノハヒフヘホマDミムメモヤユヨラリルレロワン゙゚', - 'UTF-8' => '€✔', + 'ASCII' => '! "#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', + 'GB18030' => " 、。〃々〆〇〈〉《》「」『』【】〒〓〔〕〖〗〝〞〡〢〣〤〥〦〧〨〩〾一\u{200b}丁\u{200b}丂踰\u{200b}踱\u{200b}踲\u{200b}", + 'ISO-8859-6' => 'ءآأؤإئابةتثجحخدذرزسشصضطظعغـفقكلمنهوىي', + 'ISO-8859-7' => 'ΆΈΉΊ»Ό½ΎΏΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ2ΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώ', + 'SJIS' => '。「」、・ヲァィゥェォャュョッBーアイウエオカキクケコサシスセソCタチツテトナニヌネノハヒフヘホマDミムメモヤユヨラリルレロワン゙゚', + 'UTF-8' => '€✔', 'Windows-1251' => 'ЂЃѓЉЊЌЋЏђљњќћџЎўЈҐЁЄЇІіґёєјЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя', 'Windows-1252' => 'ƒŠŒŽšœžŸªºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', ]; private static $iconvOnlyCharsets = [ - 'macintosh' => '†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈«»…ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ', + 'macintosh' => '†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø¿¡¬√ƒ≈«»…ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔ', 'Windows-1250' => 'ŚŤŹśťźˇ˘ŁĄŞŻ˛łąşĽ˝ľż', ]; - /** - * @var Mailbox - */ - protected $mailbox; protected function setUp() { @@ -70,7 +65,7 @@ public function testCustomNonExistentMessageFetch() $messageNumber = 98765; $this->expectException(MessageDoesNotExistException::class); - $this->expectExceptionMessageRegExp(\sprintf('/E_WARNING.+%s/s', \preg_quote((string)$messageNumber))); + $this->expectExceptionMessageRegExp(\sprintf('/E_WARNING.+%s/s', \preg_quote((string) $messageNumber))); new Message($connection->getResource(), $messageNumber); } @@ -241,12 +236,6 @@ public function provideIconvCharsets(): array return $provider; } - public function testAppendedMessageUid() - { - $uid = $this->mailbox->addMessage($this->getFixture('email_address')); - $this->assertEquals(1, $uid); - } - public function testEmailAddress() { $this->mailbox->addMessage($this->getFixture('email_address')); @@ -484,7 +473,8 @@ public function testRawHeaders() { $headers = 'From: from@there.com' . "\r\n" . 'To: to@here.com' . "\n" - . "\r\n"; + . "\r\n" + ; $originalMessage = $headers . 'Content' . "\n"; $this->mailbox->addMessage($originalMessage); @@ -653,9 +643,9 @@ public function testSignedMessage() $this->assertCount(3, $attachments); $expected = [ - 'data.xml' => 'PHhtbC8+', + 'data.xml' => 'PHhtbC8+', 'postacert.eml' => 'test-content', - 'smime.p7s' => 'MQ==', + 'smime.p7s' => 'MQ==', ]; foreach ($attachments as $attachment) { From c47ca925e74b6430d9d7beae5ee158a074b1b8c4 Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Mon, 20 Nov 2017 15:48:39 +0100 Subject: [PATCH 04/10] mailboxes parser fixes --- src/MailboxesParser/MailboxesParser.php | 4 ++-- src/MailboxesParser/languages/{folderNames => }/pl/ids.php | 0 src/MailboxesParser/languages/{folderNames => }/pl/names.php | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/MailboxesParser/languages/{folderNames => }/pl/ids.php (100%) rename src/MailboxesParser/languages/{folderNames => }/pl/names.php (100%) diff --git a/src/MailboxesParser/MailboxesParser.php b/src/MailboxesParser/MailboxesParser.php index cb827655..a7a110fb 100644 --- a/src/MailboxesParser/MailboxesParser.php +++ b/src/MailboxesParser/MailboxesParser.php @@ -70,14 +70,14 @@ public function __construct($mailboxes) public function setLanguage($lang) { - $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'folderNames' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'names.php'; + $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'names.php'; if (!is_file($path)) { throw new MailboxesParserException(\sprintf('File for language names %s does not exist', $path)); } $names = require $path; $this->setSpecialFoldersNames($names); - $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'folderNames' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'ids.php'; + $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'ids.php'; if (!is_file($path)) { throw new MailboxesParserException(\sprintf('File for language ids %s does not exist', $path)); } diff --git a/src/MailboxesParser/languages/folderNames/pl/ids.php b/src/MailboxesParser/languages/pl/ids.php similarity index 100% rename from src/MailboxesParser/languages/folderNames/pl/ids.php rename to src/MailboxesParser/languages/pl/ids.php diff --git a/src/MailboxesParser/languages/folderNames/pl/names.php b/src/MailboxesParser/languages/pl/names.php similarity index 100% rename from src/MailboxesParser/languages/folderNames/pl/names.php rename to src/MailboxesParser/languages/pl/names.php From 65c9841088b8f40f3fc6574e21e343eaa2903e23 Mon Sep 17 00:00:00 2001 From: piernik Date: Mon, 20 Nov 2017 15:50:14 +0100 Subject: [PATCH 05/10] Update phpunit.xml.dist --- phpunit.xml.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a8367edd..beef4949 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -19,6 +19,6 @@ - + From 386852700b590b7f0c3ab07e16f2479dff164c0d Mon Sep 17 00:00:00 2001 From: piernik Date: Mon, 20 Nov 2017 15:53:16 +0100 Subject: [PATCH 06/10] Update phpunit.xml.dist --- phpunit.xml.dist | 6 ------ 1 file changed, 6 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index beef4949..ef4da504 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,10 +15,4 @@ ./src - - - - - - From ebb92712270b5e3d718f28c4cd667f73ca211f5a Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Fri, 1 Dec 2017 10:31:27 +0100 Subject: [PATCH 07/10] mailboxes parser docs and interfaces --- .gitignore | 4 +- src/MailboxesParser/MailboxesParser.php | 81 ++++++++++++++++--- .../MailboxesParserInterface.php | 52 ++++++++++++ src/MailboxesParser/MailboxesTreeParser.php | 16 +++- .../MailboxesTreeParserInterface.php | 24 ++++++ src/MailboxesParser/ParsedMailbox.php | 6 +- tests/MailboxesParser/MailboxesParserTest.php | 8 +- 7 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 src/MailboxesParser/MailboxesParserInterface.php create mode 100644 src/MailboxesParser/MailboxesTreeParserInterface.php diff --git a/.gitignore b/.gitignore index afca4f96..c3820b63 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ vendor/ .php_cs.cache composer.lock phpunit.xml -.idea/ -phpunit.xml.dist + #phpstorm config folder +.idea/ \ No newline at end of file diff --git a/src/MailboxesParser/MailboxesParser.php b/src/MailboxesParser/MailboxesParser.php index a7a110fb..671405f6 100644 --- a/src/MailboxesParser/MailboxesParser.php +++ b/src/MailboxesParser/MailboxesParser.php @@ -12,7 +12,11 @@ use Ddeboer\Imap\Exception\MailboxesParserException; use Ddeboer\Imap\MailboxInterface; -class MailboxesParser +/** + * Class MailboxesParser + * @package Ddeboer\Imap\MailboxesParser + */ +final class MailboxesParser implements MailboxesParserInterface { /** @var MailboxInterface[] */ protected $mailboxes; @@ -68,7 +72,12 @@ public function __construct($mailboxes) $this->mailboxes = $mailboxes; } - public function setLanguage($lang) + /** + * Set language for parser + * + * @param string $lang + */ + public function setLanguage(string $lang) { $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'names.php'; if (!is_file($path)) { @@ -89,7 +98,10 @@ public function setLanguage($lang) } } - protected function parse() + /** + * @return ParsedMailbox[] + */ + protected function parse(): array { $this->folders = []; usort($this->mailboxes, [$this, "sortByMailboxName"]); @@ -112,21 +124,32 @@ protected function parse() return $this->folders; } - - protected function sortByMailboxName(MailboxInterface $a, MailboxInterface $b) + /** + * @param MailboxInterface $a + * @param MailboxInterface $b + * + * @return int + */ + protected function sortByMailboxName(MailboxInterface $a, MailboxInterface $b): int { - return ($a->getName() < $b->getName()) ? -1 : 1; + return ($a->getName() <=> $b->getName()); } - protected function sortByOrder(ParsedMailbox $a, ParsedMailbox $b) + /** + * @param ParsedMailbox $a + * @param ParsedMailbox $b + * + * @return int + */ + protected function sortByOrder(ParsedMailbox $a, ParsedMailbox $b): int { - return ($a->getOrder() < $b->getOrder()) ? -1 : 1; + return ($a->getOrder() <=> $b->getOrder()); } /** * @return ParsedMailbox[] */ - public function getFolders() + public function getFolders(): array { if (!$this->folders) { $this->parse(); @@ -136,9 +159,9 @@ public function getFolders() } /** - * @return mixed + * @return array */ - public function getTreeStructure() + public function getTreeStructure(): array { if (!$this->treeStructure) { $treeParser = new MailboxesTreeParser(); @@ -156,6 +179,11 @@ public function setSpecialFoldersNames(array $specialFoldersNames) $this->specialFoldersNames = $specialFoldersNames; } + /** + * @param $mailboxName + * + * @return int|null|string + */ protected function getSpecialFolder($mailboxName) { foreach ($this->specialFoldersIds AS $specialFolderKind => $names) { @@ -170,13 +198,25 @@ protected function getSpecialFolder($mailboxName) return null; } - private function getName($mailboxName, $delimiter = '.') + /** + * @param $mailboxName + * @param string $delimiter + * + * @return string + */ + private function getName($mailboxName, $delimiter = '.'): string { $e = explode($delimiter, $mailboxName); return ucfirst($e[count($e) - 1]); } + /** + * @param $mailboxName + * @param $delimiter + * + * @return float|int|mixed + */ private function getOrder($mailboxName, $delimiter) { if ($this->folders[$mailboxName]['special']) { @@ -219,6 +259,11 @@ private function getOrder($mailboxName, $delimiter) } } + /** + * @param $special + * + * @return string|null + */ public function getMailboxNameForSpecial($special) { /** @var ParsedMailbox $parsedMailbox */ @@ -231,12 +276,22 @@ public function getMailboxNameForSpecial($special) return null; } + /** + * @param string $specialFolder Name of special folder + * @param string $id Id for that special folder + */ public function addSpecialFolderId($specialFolder, $id) { $this->specialFoldersIds[$specialFolder][] = $id; } - private function getFolderLevel($mailboxName, $delimiter) + /** + * @param $mailboxName + * @param $delimiter + * + * @return int + */ + private function getFolderLevel($mailboxName, $delimiter): int { $e = explode($delimiter, $mailboxName); diff --git a/src/MailboxesParser/MailboxesParserInterface.php b/src/MailboxesParser/MailboxesParserInterface.php new file mode 100644 index 00000000..9c0eaba6 --- /dev/null +++ b/src/MailboxesParser/MailboxesParserInterface.php @@ -0,0 +1,52 @@ + 101.0001, 'name' => 'INBOX.normal.sub'], ['order' => 30000, 'name' => 'INBOX.trash'], ]; -// $tree = $parser->getTreeStructure(); $this->assertEquals($expected, $order); } public function testSetLanguage() { - $this->mailboxes[] = $this->createMailboxMock('INBOX.Wysłane'); - $parser = new MailboxesParser($this->mailboxes); + $mailboxes = [ + $this->createMailboxMock('INBOX'), + $this->createMailboxMock('INBOX.Wysłane'), + ]; + $parser = new MailboxesParser($mailboxes); $parser->setLanguage('pl'); $folders = $parser->getFolders(); $this->assertEquals('INBOX', $folders[0]->getMailboxName()); From bbd78f15bc196aed511fe4ded622c3aeac630bc3 Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Fri, 1 Dec 2017 11:29:11 +0100 Subject: [PATCH 08/10] tests fixes --- tests/MailboxesParser/MailboxesParserTest.php | 3 ++- tests/MailboxesParser/MailboxesTreeParserTest.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/MailboxesParser/MailboxesParserTest.php b/tests/MailboxesParser/MailboxesParserTest.php index 1b47c7e3..0292b350 100644 --- a/tests/MailboxesParser/MailboxesParserTest.php +++ b/tests/MailboxesParser/MailboxesParserTest.php @@ -11,8 +11,9 @@ use Ddeboer\Imap\MailboxesParser\MailboxesParser; use Ddeboer\Imap\MailboxesParser\ParsedMailbox; use Ddeboer\Imap\MailboxInterface; +use PHPUnit\Framework\TestCase; -class MailboxesParserTest extends \PHPUnit_Framework_TestCase +final class MailboxesParserTest extends TestCase { /** @var MailboxInterface[] */ protected $mailboxes; diff --git a/tests/MailboxesParser/MailboxesTreeParserTest.php b/tests/MailboxesParser/MailboxesTreeParserTest.php index f8be4bdb..557fab76 100644 --- a/tests/MailboxesParser/MailboxesTreeParserTest.php +++ b/tests/MailboxesParser/MailboxesTreeParserTest.php @@ -11,8 +11,9 @@ use Ddeboer\Imap\MailboxesParser\MailboxesParser; use Ddeboer\Imap\MailboxesParser\MailboxesTreeParser; use Ddeboer\Imap\MailboxInterface; +use PHPUnit\Framework\TestCase; -class MailboxesTreeParserTest extends \PHPUnit_Framework_TestCase +class MailboxesTreeParserTest extends TestCase { public function testTreeParser() { From 7d5bed69176c12602ef3ccb67860d9b7c914aed0 Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Fri, 1 Dec 2017 11:42:50 +0100 Subject: [PATCH 09/10] tests fixes --- src/MailboxesParser/MailboxesParser.php | 168 +++++++++--------- .../MailboxesParserInterface.php | 14 +- src/MailboxesParser/MailboxesTreeParser.php | 44 ++--- .../MailboxesTreeParserInterface.php | 10 +- src/MailboxesParser/ParsedMailbox.php | 28 ++- src/MailboxesParser/languages/pl/ids.php | 19 +- src/MailboxesParser/languages/pl/names.php | 17 +- tests/MailboxesParser/MailboxesParserTest.php | 20 ++- .../MailboxesTreeParserTest.php | 30 ++-- 9 files changed, 179 insertions(+), 171 deletions(-) diff --git a/src/MailboxesParser/MailboxesParser.php b/src/MailboxesParser/MailboxesParser.php index 671405f6..8cb81e69 100644 --- a/src/MailboxesParser/MailboxesParser.php +++ b/src/MailboxesParser/MailboxesParser.php @@ -1,28 +1,28 @@ ['inbox',], - self::SENT => ['sent', 'sent messages', 'INBOX.Sent', '[Gmail]/Sent Mail',], - self::DRAFT => ['drafts', 'INBOX.Drafts', '[Gmail]/Drafts',], - self::SPAM => ['spam', 'INBOX.spam', '[Gmail]/spam'], - self::TRASH => ['trash', 'bin', 'INBOX.trash', '[Gmail]/trash'], + private $specialFoldersIds = [ + self::INBOX => ['inbox'], + self::SENT => ['sent', 'sent messages', 'INBOX.Sent', '[Gmail]/Sent Mail'], + self::DRAFT => ['drafts', 'INBOX.Drafts', '[Gmail]/Drafts'], + self::SPAM => ['spam', 'INBOX.spam', '[Gmail]/spam'], + self::TRASH => ['trash', 'bin', 'INBOX.trash', '[Gmail]/trash'], self::TEMPLATES => ['templates'], - self::ARCHIVES => ['archives',], + self::ARCHIVES => ['archives'], ]; - protected $specialFoldersNames = [ - self::DRAFT => 'Drafts', - self::INBOX => 'Inbox', - self::SENT => 'Sent', - self::SPAM => 'Spam', - self::TRASH => 'Trash', + private $specialFoldersNames = [ + self::DRAFT => 'Drafts', + self::INBOX => 'Inbox', + self::SENT => 'Sent', + self::SPAM => 'Spam', + self::TRASH => 'Trash', self::TEMPLATES => 'Templates', - self::ARCHIVES => 'Archives', + self::ARCHIVES => 'Archives', ]; - protected $specialFoldersOrder = [ - self::INBOX => 1, - self::SENT => 2, - self::DRAFT => 3, + private $specialFoldersOrder = [ + self::INBOX => 1, + self::SENT => 2, + self::DRAFT => 3, self::TEMPLATES => 4, - self::ARCHIVES => 10000, - self::SPAM => 20000, - self::TRASH => 30000, + self::ARCHIVES => 10000, + self::SPAM => 20000, + self::TRASH => 30000, ]; /** @@ -73,26 +73,26 @@ public function __construct($mailboxes) } /** - * Set language for parser + * Set language for parser. * * @param string $lang */ public function setLanguage(string $lang) { $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'names.php'; - if (!is_file($path)) { + if (!\is_file($path)) { throw new MailboxesParserException(\sprintf('File for language names %s does not exist', $path)); } $names = require $path; $this->setSpecialFoldersNames($names); $path = __DIR__ . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $lang . DIRECTORY_SEPARATOR . 'ids.php'; - if (!is_file($path)) { + if (!\is_file($path)) { throw new MailboxesParserException(\sprintf('File for language ids %s does not exist', $path)); } $ids = require $path; - foreach ($ids AS $specialFolder => $idsArray) { - foreach ($idsArray AS $id) { + foreach ($ids as $specialFolder => $idsArray) { + foreach ($idsArray as $id) { $this->addSpecialFolderId($specialFolder, $id); } } @@ -101,11 +101,11 @@ public function setLanguage(string $lang) /** * @return ParsedMailbox[] */ - protected function parse(): array + private function parse(): array { $this->folders = []; - usort($this->mailboxes, [$this, "sortByMailboxName"]); - foreach ($this->mailboxes AS $k => $mailbox) { + \usort($this->mailboxes, [$this, 'sortByMailboxName']); + foreach ($this->mailboxes as $k => $mailbox) { $mailboxName = $mailbox->getName(); $folder = new ParsedMailbox(); $folder->setMailbox($mailbox); @@ -119,7 +119,7 @@ protected function parse(): array $this->folders[] = $folder; } - usort($this->folders, [$this, "sortByOrder"]); + \usort($this->folders, [$this, 'sortByOrder']); return $this->folders; } @@ -130,9 +130,9 @@ protected function parse(): array * * @return int */ - protected function sortByMailboxName(MailboxInterface $a, MailboxInterface $b): int + private function sortByMailboxName(MailboxInterface $a, MailboxInterface $b): int { - return ($a->getName() <=> $b->getName()); + return $a->getName() <=> $b->getName(); } /** @@ -141,9 +141,9 @@ protected function sortByMailboxName(MailboxInterface $a, MailboxInterface $b): * * @return int */ - protected function sortByOrder(ParsedMailbox $a, ParsedMailbox $b): int + private function sortByOrder(ParsedMailbox $a, ParsedMailbox $b): int { - return ($a->getOrder() <=> $b->getOrder()); + return $a->getOrder() <=> $b->getOrder(); } /** @@ -182,20 +182,20 @@ public function setSpecialFoldersNames(array $specialFoldersNames) /** * @param $mailboxName * - * @return int|null|string + * @return null|int|string */ - protected function getSpecialFolder($mailboxName) + private function getSpecialFolder($mailboxName) { - foreach ($this->specialFoldersIds AS $specialFolderKind => $names) { - $lower = mb_strtolower($mailboxName); - foreach ($names AS $name) { - if ($lower === mb_strtolower($name)) { + foreach ($this->specialFoldersIds as $specialFolderKind => $names) { + $lower = \mb_strtolower($mailboxName); + foreach ($names as $name) { + if ($lower === \mb_strtolower($name)) { return $specialFolderKind; } } } - return null; + return; } /** @@ -206,9 +206,9 @@ protected function getSpecialFolder($mailboxName) */ private function getName($mailboxName, $delimiter = '.'): string { - $e = explode($delimiter, $mailboxName); + $e = \explode($delimiter, $mailboxName); - return ucfirst($e[count($e) - 1]); + return \ucfirst($e[\count($e) - 1]); } /** @@ -221,48 +221,48 @@ private function getOrder($mailboxName, $delimiter) { if ($this->folders[$mailboxName]['special']) { return $this->specialFoldersOrder[$this->folders[$mailboxName]['special']]; - } else { - $e = explode($delimiter, $mailboxName); + } + $e = \explode($delimiter, $mailboxName); - $level = count($e) - 1; - if ($e[0] === 'INBOX' && $level === 1) { - $level = -1; + $level = \count($e) - 1; + if ('INBOX' === $e[0] && 1 === $level) { + $level = -1; + } + if ($e[1]) { + \array_pop($e); + $parentMailboxName = \implode($delimiter, $e); + if ($level === -1) { + $multiplier = 100; + } else { + $power = -1 * ($level * 2); + $multiplier = 10 ** ($power); } - if ($e[1]) { - array_pop($e); - $parentMailboxName = implode($delimiter, $e); - if ($level === -1) { - $multiplier = 100; - } else { - $power = -1 * ($level * 2); - $multiplier = pow(10, ($power)); - } - $parsedParent = null; - /** @var ParsedMailbox $parsedMailbox */ - foreach ($this->folders as $parsedMailbox) { - if ($parsedMailbox->getMailboxName() === $parentMailboxName) { - $parsedParent = $parsedMailbox; - break; - } - } - if ($parsedParent) { - $parsedParent->setSubfolders($parsedParent->getSubfolders() + 1); - $order = ($parsedParent->getOrder() + ($parsedParent->getSubfolders() * $multiplier)); - } else { - $order = 10; + $parsedParent = null; + /** @var ParsedMailbox $parsedMailbox */ + foreach ($this->folders as $parsedMailbox) { + if ($parsedMailbox->getMailboxName() === $parentMailboxName) { + $parsedParent = $parsedMailbox; + + break; } + } + if ($parsedParent) { + $parsedParent->setSubfolders($parsedParent->getSubfolders() + 1); + $order = ($parsedParent->getOrder() + ($parsedParent->getSubfolders() * $multiplier)); } else { $order = 10; } - - return $order; + } else { + $order = 10; } + + return $order; } /** * @param $special * - * @return string|null + * @return null|string */ public function getMailboxNameForSpecial($special) { @@ -273,7 +273,7 @@ public function getMailboxNameForSpecial($special) } } - return null; + return; } /** @@ -293,8 +293,8 @@ public function addSpecialFolderId($specialFolder, $id) */ private function getFolderLevel($mailboxName, $delimiter): int { - $e = explode($delimiter, $mailboxName); + $e = \explode($delimiter, $mailboxName); - return count($e); + return \count($e); } -} \ No newline at end of file +} diff --git a/src/MailboxesParser/MailboxesParserInterface.php b/src/MailboxesParser/MailboxesParserInterface.php index 9c0eaba6..f9e22fd4 100644 --- a/src/MailboxesParser/MailboxesParserInterface.php +++ b/src/MailboxesParser/MailboxesParserInterface.php @@ -1,22 +1,22 @@ getDelimiter(), $value->getMailboxName()); + $k = \explode($value->getDelimiter(), $value->getMailboxName()); $newkey = []; foreach ($k as $segment) { $newkey[] = $segment; - $newkey[] = "subfolders"; + $newkey[] = 'subfolders'; } - return implode(".", $newkey); - }, array_keys($input), $input); + return \implode('.', $newkey); + }, + \array_keys($input), + $input + ); $arrayToParse = []; - foreach ($newKeys AS $index => $value) { - $k = explode(".", $value); - $keyWithoutLast = implode('.', array_splice($k, 0, -1)); + foreach ($newKeys as $index => $value) { + $k = \explode('.', $value); + $keyWithoutLast = \implode('.', \array_splice($k, 0, -1)); $arrayToParse[$value] = []; if ($input[$index]) { /** @var ParsedMailbox $parsedMailbox */ @@ -50,7 +54,7 @@ function ($key, $value) { } $res = []; - array_walk($arrayToParse, function ($value, $key) use (&$res) { + \array_walk($arrayToParse, function ($value, $key) use (&$res) { $this->set($res, $key, $value); }); @@ -66,22 +70,22 @@ function ($key, $value) { */ private function set(&$array, $key, $value) { - if (is_null($key)) { + if (null === $key) { return $array = $value; } - $keys = explode('.', $key); - while (count($keys) > 1) { - $key = array_shift($keys); + $keys = \explode('.', $key); + while (\count($keys) > 1) { + $key = \array_shift($keys); // If the key doesn't exist at this depth, we will just create an empty array // to hold the next value, allowing us to create the arrays to hold final // values at the correct depth. Then we'll keep digging into the array. - if (!isset($array[$key]) || !is_array($array[$key])) { + if (!isset($array[$key]) || !\is_array($array[$key])) { $array[$key] = []; } $array = &$array[$key]; } - $array[array_shift($keys)] = $value; + $array[\array_shift($keys)] = $value; return $array; } -} \ No newline at end of file +} diff --git a/src/MailboxesParser/MailboxesTreeParserInterface.php b/src/MailboxesParser/MailboxesTreeParserInterface.php index db780f07..2bff983b 100644 --- a/src/MailboxesParser/MailboxesTreeParserInterface.php +++ b/src/MailboxesParser/MailboxesTreeParserInterface.php @@ -1,17 +1,17 @@ subfolders = $subfolders; } - - -} \ No newline at end of file +} diff --git a/src/MailboxesParser/languages/pl/ids.php b/src/MailboxesParser/languages/pl/ids.php index 59aa4f82..b7544413 100644 --- a/src/MailboxesParser/languages/pl/ids.php +++ b/src/MailboxesParser/languages/pl/ids.php @@ -1,7 +1,8 @@ ['INBOX.odebrane', 'odebrane'], - MailboxesParser::SENT => ['INBOX.Wysłane', 'elementy wysłane', 'wysłane'], - MailboxesParser::DRAFT => ['szkice',], - MailboxesParser::SPAM => [], - MailboxesParser::TRASH => ['kosz',], - MailboxesParser::TEMPLATES => ['szablony',], - MailboxesParser::ARCHIVES => ['archiwum',], -]; \ No newline at end of file + MailboxesParser::INBOX => ['INBOX.odebrane', 'odebrane'], + MailboxesParser::SENT => ['INBOX.Wysłane', 'elementy wysłane', 'wysłane'], + MailboxesParser::DRAFT => ['szkice'], + MailboxesParser::SPAM => [], + MailboxesParser::TRASH => ['kosz'], + MailboxesParser::TEMPLATES => ['szablony'], + MailboxesParser::ARCHIVES => ['archiwum'], +]; diff --git a/src/MailboxesParser/languages/pl/names.php b/src/MailboxesParser/languages/pl/names.php index eece9364..bc9d7468 100644 --- a/src/MailboxesParser/languages/pl/names.php +++ b/src/MailboxesParser/languages/pl/names.php @@ -1,7 +1,8 @@ 'Odebrane', - MailboxesParser::SENT => 'Wysłane', - MailboxesParser::DRAFT => 'Szkice', - MailboxesParser::SPAM => 'Spam', - MailboxesParser::TRASH => 'Kosz', + MailboxesParser::INBOX => 'Odebrane', + MailboxesParser::SENT => 'Wysłane', + MailboxesParser::DRAFT => 'Szkice', + MailboxesParser::SPAM => 'Spam', + MailboxesParser::TRASH => 'Kosz', MailboxesParser::TEMPLATES => 'Szablony', - MailboxesParser::ARCHIVES => 'Archiwum', -]; \ No newline at end of file + MailboxesParser::ARCHIVES => 'Archiwum', +]; diff --git a/tests/MailboxesParser/MailboxesParserTest.php b/tests/MailboxesParser/MailboxesParserTest.php index 0292b350..25a5c0d5 100644 --- a/tests/MailboxesParser/MailboxesParserTest.php +++ b/tests/MailboxesParser/MailboxesParserTest.php @@ -1,9 +1,11 @@ mailboxes); $folders = $parser->getFolders(); $this->assertCount(8, $folders); - $this->assertEquals('INBOX', $folders[0]->getMailboxName()); - $this->assertEquals('Inbox', $folders[0]->getName()); + $this->assertSame('INBOX', $folders[0]->getMailboxName()); + $this->assertSame('Inbox', $folders[0]->getName()); $this->assertInstanceOf(MailboxInterface::class, $folders[0]->getMailbox()); $order = []; /** @var ParsedMailbox $parsedMailbox */ - foreach ($folders AS $parsedMailbox) { + foreach ($folders as $parsedMailbox) { $order[] = ['order' => $parsedMailbox->getOrder(), 'name' => $parsedMailbox->getMailboxName()]; } $expected = [ @@ -56,7 +58,7 @@ public function testParser() ['order' => 101.0001, 'name' => 'INBOX.normal.sub'], ['order' => 30000, 'name' => 'INBOX.trash'], ]; - $this->assertEquals($expected, $order); + $this->assertSame($expected, $order); } public function testSetLanguage() @@ -68,10 +70,10 @@ public function testSetLanguage() $parser = new MailboxesParser($mailboxes); $parser->setLanguage('pl'); $folders = $parser->getFolders(); - $this->assertEquals('INBOX', $folders[0]->getMailboxName()); - $this->assertEquals('Odebrane', $folders[0]->getName()); - $this->assertEquals('INBOX.Wysłane', $folders[1]->getMailboxName()); - $this->assertEquals('Wysłane', $folders[1]->getName()); + $this->assertSame('INBOX', $folders[0]->getMailboxName()); + $this->assertSame('Odebrane', $folders[0]->getName()); + $this->assertSame('INBOX.Wysłane', $folders[1]->getMailboxName()); + $this->assertSame('Wysłane', $folders[1]->getName()); } private function createMailboxMock($mailboxName) diff --git a/tests/MailboxesParser/MailboxesTreeParserTest.php b/tests/MailboxesParser/MailboxesTreeParserTest.php index 557fab76..44b9a6e1 100644 --- a/tests/MailboxesParser/MailboxesTreeParserTest.php +++ b/tests/MailboxesParser/MailboxesTreeParserTest.php @@ -1,9 +1,11 @@ parse($folders); $spodziewane = [ 'inbox' => [ - 'name' => 'Inbox', + 'name' => 'Inbox', 'mailboxName' => 'inbox', - 'subfolders' => [ - 'first' => [ - 'name' => 'First', + 'subfolders' => [ + 'first' => [ + 'name' => 'First', 'mailboxName' => 'inbox.first', - 'subfolders' => [], + 'subfolders' => [], ], 'second' => [ - 'name' => 'Second', + 'name' => 'Second', 'mailboxName' => 'inbox.second', - 'subfolders' => [ + 'subfolders' => [ 'other' => [ - 'name' => 'Other', - 'subfolders' => [], + 'name' => 'Other', + 'subfolders' => [], 'mailboxName' => 'inbox.second.other', ], ], ], - 'third' => [ + 'third' => [ 'subfolders' => [ 'another' => [ - 'name' => 'Another', - 'subfolders' => [], + 'name' => 'Another', + 'subfolders' => [], 'mailboxName' => 'inbox.third.another', ], ], @@ -62,7 +64,7 @@ public function testTreeParser() ], ], ]; - $this->assertEquals($spodziewane, $zwroc); + $this->assertSame($spodziewane, $zwroc); } // public function testTreeParserWithDashDelimiter() From bb972bc24fe53283500e6b4993bfa7c153ca450b Mon Sep 17 00:00:00 2001 From: Lukasz Piernikarczyk Date: Fri, 1 Dec 2017 11:48:09 +0100 Subject: [PATCH 10/10] tests fixes --- .../MailboxesParser/MailboxesTreeParserTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/MailboxesParser/MailboxesTreeParserTest.php b/tests/MailboxesParser/MailboxesTreeParserTest.php index 44b9a6e1..e635441c 100644 --- a/tests/MailboxesParser/MailboxesTreeParserTest.php +++ b/tests/MailboxesParser/MailboxesTreeParserTest.php @@ -33,35 +33,35 @@ public function testTreeParser() $zwroc = $parser->parse($folders); $spodziewane = [ 'inbox' => [ - 'name' => 'Inbox', - 'mailboxName' => 'inbox', 'subfolders' => [ 'first' => [ - 'name' => 'First', - 'mailboxName' => 'inbox.first', 'subfolders' => [], + 'mailboxName' => 'inbox.first', + 'name' => 'First', ], 'second' => [ - 'name' => 'Second', - 'mailboxName' => 'inbox.second', 'subfolders' => [ 'other' => [ - 'name' => 'Other', 'subfolders' => [], 'mailboxName' => 'inbox.second.other', + 'name' => 'Other', ], ], + 'mailboxName' => 'inbox.second', + 'name' => 'Second', ], 'third' => [ 'subfolders' => [ 'another' => [ - 'name' => 'Another', 'subfolders' => [], 'mailboxName' => 'inbox.third.another', + 'name' => 'Another', ], ], ], ], + 'mailboxName' => 'inbox', + 'name' => 'Inbox', ], ]; $this->assertSame($spodziewane, $zwroc);