Skip to content

Commit

Permalink
name only accepts string and Exception expectation added to test
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar Monterrey committed Aug 6, 2022
1 parent e3a4cba commit 8913ee1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/MailboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8913ee1

Please sign in to comment.