From 5207e126806f11ce629a96e563cc707909c29698 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Tue, 2 Aug 2022 21:19:56 -0400 Subject: [PATCH 1/8] setName method added to Mailbox class --- src/Exception/RenameMailboxException.php | 9 +++++++++ src/Mailbox.php | 15 +++++++++++++++ src/MailboxInterface.php | 5 +++++ 3 files changed, 29 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 setName(string $name): bool + { + $oldFullName = $this->getFullEncodedName(); + $newFullName = str_replace($this->name, $name, $oldFullName); + + $return = \imap_renamemailbox( $this->resource->getStream(), $oldFullName, $newFullName ); + if( $return === false ){ + 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..5eb9d029 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 setName(string $name): bool; + /** * Get mailbox encoded path. */ From 31f4babf5c5e7bab3c7c7f42cfe8da537be463ec Mon Sep 17 00:00:00 2001 From: OmarMonterrey Date: Wed, 3 Aug 2022 21:43:28 -0400 Subject: [PATCH 2/8] Update src/MailboxInterface.php Co-authored-by: Filippo Tessarotto --- src/MailboxInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 5eb9d029..c23f7955 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -23,7 +23,7 @@ public function getName(): string; /** * Set new mailbox name */ - public function setName(string $name): bool; + public function renameTo(string $name): bool; /** * Get mailbox encoded path. From 3cb71fb0d59fe5644d15bcefcbd8b850990cbca2 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Wed, 3 Aug 2022 21:44:41 -0400 Subject: [PATCH 3/8] Renamed method according to interface --- src/Mailbox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index d0116fde..ea4dc7cd 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -42,7 +42,7 @@ public function getName(): string return $this->name; } - public function setName(string $name): bool + public function renameTo(string $name): bool { $oldFullName = $this->getFullEncodedName(); $newFullName = str_replace($this->name, $name, $oldFullName); From 4b501676d8c1305ed5e948a3d50f938f47c83fdd Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Thu, 4 Aug 2022 20:29:12 +0000 Subject: [PATCH 4/8] Build command (make) ran --- src/Mailbox.php | 9 +++++---- src/MailboxInterface.php | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index ea4dc7cd..96bbceaa 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -45,14 +45,15 @@ public function getName(): string public function renameTo(string $name): bool { $oldFullName = $this->getFullEncodedName(); - $newFullName = str_replace($this->name, $name, $oldFullName); + $newFullName = \str_replace($this->name, $name, $oldFullName); - $return = \imap_renamemailbox( $this->resource->getStream(), $oldFullName, $newFullName ); - if( $return === false ){ + $return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName); + if (false === $return) { throw new RenameMailboxException('Could not rename mailbox'); } - $this->name = $name; + $this->name = $name; $this->info->name = $newFullName; + return true; } diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index c23f7955..7c623667 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -21,7 +21,7 @@ interface MailboxInterface extends \Countable, \IteratorAggregate public function getName(): string; /** - * Set new mailbox name + * Set new mailbox name. */ public function renameTo(string $name): bool; From 4c976d95cc029e0df64fe2487f3c8c0f97c596c9 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Thu, 4 Aug 2022 20:48:10 +0000 Subject: [PATCH 5/8] Made name parameter nullable on renameTo --- src/Mailbox.php | 5 ++++- src/MailboxInterface.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index 96bbceaa..d48336fd 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -42,8 +42,11 @@ public function getName(): string return $this->name; } - public function renameTo(string $name): bool + public function renameTo(string $name = null): bool { + if (null === $name) { + return false; + } $oldFullName = $this->getFullEncodedName(); $newFullName = \str_replace($this->name, $name, $oldFullName); diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 7c623667..687b8b6e 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -23,7 +23,7 @@ public function getName(): string; /** * Set new mailbox name. */ - public function renameTo(string $name): bool; + public function renameTo(string $name = null): bool; /** * Get mailbox encoded path. From 14bfce70395ede5439d1478754af6a491e79ce50 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Thu, 4 Aug 2022 20:48:58 +0000 Subject: [PATCH 6/8] Test written for renameTo method --- tests/AbstractTest.php | 2 ++ tests/MailboxTest.php | 8 ++++++++ 2 files changed, 10 insertions(+) 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..72dbe8d8 100644 --- a/tests/MailboxTest.php +++ b/tests/MailboxTest.php @@ -39,6 +39,14 @@ public function testGetName(): void static::assertSame($this->mailboxName, $this->mailbox->getName()); } + public function testRenameTo(): void + { + $this->mailbox->renameTo($this->altName); + static::assertSame($this->altName, $this->mailbox->getName()); + $this->mailbox->renameTo($this->mailboxName); + static::assertSame($this->mailboxName, $this->mailbox->getName()); + } + public function testGetFullEncodedName(): void { static::assertIsString($this->mailboxName); From e3a4cba9982a058d308b740a2ac1b4d0d9d94787 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Thu, 4 Aug 2022 23:15:54 +0000 Subject: [PATCH 7/8] Fixed encoding for rename method, improved test --- src/Mailbox.php | 6 +++++- tests/MailboxTest.php | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index d48336fd..f3cc43a6 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -47,8 +47,12 @@ public function renameTo(string $name = null): bool if (null === $name) { return false; } + $encodedName = \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8'); $oldFullName = $this->getFullEncodedName(); - $newFullName = \str_replace($this->name, $name, $oldFullName); + $newFullName = \preg_replace('/' . \preg_quote(\mb_convert_encoding($this->name, 'UTF7-IMAP', 'UTF-8')) . '$/', $encodedName, $oldFullName); + if (null === $newFullName) { + return false; + } $return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName); if (false === $return) { diff --git a/tests/MailboxTest.php b/tests/MailboxTest.php index 72dbe8d8..e8ed6e89 100644 --- a/tests/MailboxTest.php +++ b/tests/MailboxTest.php @@ -41,9 +41,12 @@ public function testGetName(): void public function testRenameTo(): void { - $this->mailbox->renameTo($this->altName); + static::assertNotSame($this->mailboxName, $this->altName); + + static::assertTrue($this->mailbox->renameTo($this->altName)); static::assertSame($this->altName, $this->mailbox->getName()); - $this->mailbox->renameTo($this->mailboxName); + + static::assertTrue($this->mailbox->renameTo($this->mailboxName)); static::assertSame($this->mailboxName, $this->mailbox->getName()); } From 8913ee18667790c4fb88518d0f6b1ed47ac8c7f1 Mon Sep 17 00:00:00 2001 From: Omar Monterrey Date: Sat, 6 Aug 2022 02:55:27 +0000 Subject: [PATCH 8/8] name only accepts string and Exception expectation added to test --- src/Mailbox.php | 9 ++------- src/MailboxInterface.php | 2 +- tests/MailboxTest.php | 12 ++++++++++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Mailbox.php b/src/Mailbox.php index f3cc43a6..51d9a7c3 100644 --- a/src/Mailbox.php +++ b/src/Mailbox.php @@ -42,17 +42,12 @@ public function getName(): string return $this->name; } - public function renameTo(string $name = null): bool + public function renameTo(string $name): bool { - if (null === $name) { - return false; - } $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); - if (null === $newFullName) { - return false; - } + \assert(null !== $newFullName); $return = \imap_renamemailbox($this->resource->getStream(), $oldFullName, $newFullName); if (false === $return) { diff --git a/src/MailboxInterface.php b/src/MailboxInterface.php index 687b8b6e..7c623667 100644 --- a/src/MailboxInterface.php +++ b/src/MailboxInterface.php @@ -23,7 +23,7 @@ public function getName(): string; /** * Set new mailbox name. */ - public function renameTo(string $name = null): bool; + public function renameTo(string $name): bool; /** * Get mailbox encoded path. diff --git a/tests/MailboxTest.php b/tests/MailboxTest.php index e8ed6e89..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; @@ -43,11 +44,18 @@ public function testRenameTo(): void { static::assertNotSame($this->mailboxName, $this->altName); - static::assertTrue($this->mailbox->renameTo($this->altName)); + /** @var string $altName */ + $altName = $this->altName; + static::assertTrue($this->mailbox->renameTo($altName)); static::assertSame($this->altName, $this->mailbox->getName()); - static::assertTrue($this->mailbox->renameTo($this->mailboxName)); + /** @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