From c2e4d723fa065bc6750a1169e9af48ef27ecddc2 Mon Sep 17 00:00:00 2001 From: OmarMonterrey Date: Sat, 6 Aug 2022 01:11:35 -0400 Subject: [PATCH] Mailbox: add renaming capability (#540) Fixes #446 --- src/Exception/RenameMailboxException.php | 9 +++++++++ src/Mailbox.php | 18 ++++++++++++++++++ src/MailboxInterface.php | 5 +++++ tests/AbstractTest.php | 2 ++ tests/MailboxTest.php | 19 +++++++++++++++++++ 5 files changed, 53 insertions(+) create mode 100755 src/Exception/RenameMailboxException.php diff --git a/src/Exception/RenameMailboxException.php b/src/Exception/RenameMailboxException.php new file mode 100755 index 00000000..2b221e77 --- /dev/null +++ b/src/Exception/RenameMailboxException.php @@ -0,0 +1,9 @@ +name; } + public function renameTo(string $name): bool + { + $encodedName = \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8'); + $oldFullName = $this->getFullEncodedName(); + $newFullName = \preg_replace('/' . \preg_quote(\mb_convert_encoding($this->name, 'UTF7-IMAP', 'UTF-8')) . '$/', $encodedName, $oldFullName); + \assert(null !== $newFullName); + + $return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName); + if (false === $return) { + throw new RenameMailboxException('Could not rename mailbox'); + } + $this->name = $name; + $this->info->name = $newFullName; + + return true; + } + public function getEncodedName(): string { /** @var string $name */ diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 067bdc28..7c623667 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -20,6 +20,11 @@ interface MailboxInterface extends \Countable, \IteratorAggregate */ public function getName(): string; + /** + * Set new mailbox name. + */ + public function renameTo(string $name): bool; + /** * Get mailbox encoded path. */ diff --git a/tests/AbstractTest.php b/tests/AbstractTest.php index d180052b..25dd4c21 100644 --- a/tests/AbstractTest.php +++ b/tests/AbstractTest.php @@ -18,6 +18,7 @@ abstract class AbstractTest extends TestCase public const SPECIAL_CHARS = 'A_\\|!"£$%&()=?àèìòùÀÈÌÒÙ<>-@#[]_ß_б_π_€_✔_你_يد_Z_'; protected ?string $mailboxName; + protected ?string $altName; final protected function getConnection(): ConnectionInterface { @@ -40,6 +41,7 @@ final protected function createMailbox(ConnectionInterface $connection = null): { $connection = $connection ?? $this->getConnection(); $this->mailboxName = \uniqid('mailbox_' . self::SPECIAL_CHARS); + $this->altName = \uniqid('mailbox_' . self::SPECIAL_CHARS); return $connection->createMailbox($this->mailboxName); } diff --git a/tests/MailboxTest.php b/tests/MailboxTest.php index 63bf03a3..60ca79bb 100644 --- a/tests/MailboxTest.php +++ b/tests/MailboxTest.php @@ -9,6 +9,7 @@ use Ddeboer\Imap\Exception\MessageCopyException; use Ddeboer\Imap\Exception\MessageDoesNotExistException; use Ddeboer\Imap\Exception\MessageMoveException; +use Ddeboer\Imap\Exception\RenameMailboxException; use Ddeboer\Imap\Exception\ReopenMailboxException; use Ddeboer\Imap\MailboxInterface; use Ddeboer\Imap\MessageIterator; @@ -39,6 +40,24 @@ public function testGetName(): void static::assertSame($this->mailboxName, $this->mailbox->getName()); } + public function testRenameTo(): void + { + static::assertNotSame($this->mailboxName, $this->altName); + + /** @var string $altName */ + $altName = $this->altName; + static::assertTrue($this->mailbox->renameTo($altName)); + static::assertSame($this->altName, $this->mailbox->getName()); + + /** @var string $mailboxName */ + $mailboxName = $this->mailboxName; + static::assertTrue($this->mailbox->renameTo($mailboxName)); + static::assertSame($this->mailboxName, $this->mailbox->getName()); + + static::expectException(RenameMailboxException::class); + $this->mailbox->renameTo($mailboxName); + } + public function testGetFullEncodedName(): void { static::assertIsString($this->mailboxName);