Skip to content

Commit

Permalink
Merge pull request #535 from bapcltd/issue/526
Browse files Browse the repository at this point in the history
empty array is falsey and triggers exception
  • Loading branch information
Sebbo94BY authored Nov 16, 2020
2 parents 293fd8f + f60647b commit da6068c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/PhpImap/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ public static function sort(
);
}

if (!$result) {
if (false === $result) {
throw new UnexpectedValueException('Could not sort messages!', 0, self::HandleErrors(\imap_errors(), 'imap_sort'));
}

Expand Down
63 changes: 1 addition & 62 deletions tests/unit/AbstractLiveMailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,7 @@
*/
abstract class AbstractLiveMailboxTest extends TestCase
{
/**
* Provides constructor arguments for a live mailbox.
*
* @psalm-return MAILBOX_ARGS[]
*/
public function MailBoxProvider(): array
{
$sets = [];

$imapPath = \getenv('PHPIMAP_IMAP_PATH');
$login = \getenv('PHPIMAP_LOGIN');
$password = \getenv('PHPIMAP_PASSWORD');

if (\is_string($imapPath) && \is_string($login) && \is_string($password)) {
$sets['CI ENV'] = [new HiddenString($imapPath), new HiddenString($login), new HiddenString($password, true, true), \sys_get_temp_dir()];
}

return $sets;
}
use LiveMailboxTestingTrait;

/**
* @psalm-return Generator<int, array{0:COMPOSE_ENVELOPE, 1:COMPOSE_BODY, 2:string}, mixed, void>
Expand Down Expand Up @@ -193,49 +175,6 @@ public function testAppend(
}
}

/**
* Get instance of Mailbox, pre-set to a random mailbox.
*
* @param string $attachmentsDir
* @param string $serverEncoding
*
* @return mixed[]
*
* @psalm-return array{0:Mailbox, 1:string, 2:HiddenString}
*/
protected function getMailbox(HiddenString $imapPath, HiddenString $login, HiddenString $password, $attachmentsDir, $serverEncoding = 'UTF-8')
{
$mailbox = new Mailbox($imapPath->getString(), $login->getString(), $password->getString(), $attachmentsDir, $serverEncoding);

$random = 'test-box-'.\date('c').\bin2hex(\random_bytes(4));

$mailbox->createMailbox($random);

$mailbox->switchMailbox($random, false);

return [$mailbox, $random, $imapPath];
}

/**
* @psalm-param MAILBOX_ARGS $mailbox_args
*
* @return mixed[]
*
* @psalm-return array{0:Mailbox, 1:string, 2:HiddenString}
*/
protected function getMailboxFromArgs(array $mailbox_args): array
{
list($path, $username, $password, $attachments_dir) = $mailbox_args;

return $this->getMailbox(
$path,
$username,
$password,
$attachments_dir,
isset($mailbox_args[4]) ? $mailbox_args[4] : 'UTF-8'
);
}

/**
* Get subject search criteria and subject.
*
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/ImapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
use Generator;
use ParagonIE\HiddenString\HiddenString;
use PHPUnit\Framework\TestCase as Base;
use const SORTARRIVAL;
use Throwable;
use UnexpectedValueException;

/**
* @psalm-type MAILBOX_ARGS = array{
* 0:HiddenString,
* 1:HiddenString,
* 2:HiddenString,
* 3:string,
* 4?:string
* }
* @psalm-type PSALM_OPEN_ARGS = array{
* 0:HiddenString,
* 1:HiddenString,
Expand All @@ -24,6 +32,8 @@
*/
class ImapTest extends Base
{
use LiveMailboxTestingTrait;

/**
* @psalm-return Generator<int|string, array{
* 0:class-string<Throwable>,
Expand Down Expand Up @@ -97,4 +107,51 @@ public function testOpenFailure(
$args[5]
);
}

/**
* @dataProvider MailBoxProvider
*
* @group live
*/
public function testSortEmpty(
HiddenString $path,
HiddenString $login,
HiddenString $password
): void {
list($mailbox, $remove_mailbox, $path) = $this->getMailboxFromArgs([
$path,
$login,
$password,
\sys_get_temp_dir(),
]);

/** @var Throwable|null */
$exception = null;

$mailboxDeleted = false;

try {
$this->assertSame(
[],
Imap::sort(
$mailbox->getImapStream(),
SORTARRIVAL,
false,
0
)
);
} catch (Throwable $ex) {
$exception = $ex;
} finally {
$mailbox->switchMailbox($path->getString());
if (!$mailboxDeleted) {
$mailbox->deleteMailbox($remove_mailbox);
}
$mailbox->disconnect();
}

if (null !== $exception) {
throw $exception;
}
}
}
88 changes: 88 additions & 0 deletions tests/unit/LiveMailboxTestingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Live Mailbox - PHPUnit tests.
*
* Runs tests on a live mailbox
*
* @author BAPCLTD-Marv
*/
declare(strict_types=1);

namespace PhpImap;

use ParagonIE\HiddenString\HiddenString;

/**
* @psalm-type MAILBOX_ARGS = array{
* 0:HiddenString,
* 1:HiddenString,
* 2:HiddenString,
* 3:string,
* 4?:string
* }
*/
trait LiveMailboxTestingTrait
{
/**
* Provides constructor arguments for a live mailbox.
*
* @psalm-return MAILBOX_ARGS[]
*/
public function MailBoxProvider(): array
{
$sets = [];

$imapPath = \getenv('PHPIMAP_IMAP_PATH');
$login = \getenv('PHPIMAP_LOGIN');
$password = \getenv('PHPIMAP_PASSWORD');

if (\is_string($imapPath) && \is_string($login) && \is_string($password)) {
$sets['CI ENV'] = [new HiddenString($imapPath), new HiddenString($login), new HiddenString($password, true, true), \sys_get_temp_dir()];
}

return $sets;
}

/**
* Get instance of Mailbox, pre-set to a random mailbox.
*
* @param string $attachmentsDir
* @param string $serverEncoding
*
* @return mixed[]
*
* @psalm-return array{0:Mailbox, 1:string, 2:HiddenString}
*/
protected function getMailbox(HiddenString $imapPath, HiddenString $login, HiddenString $password, $attachmentsDir, $serverEncoding = 'UTF-8')
{
$mailbox = new Mailbox($imapPath->getString(), $login->getString(), $password->getString(), $attachmentsDir, $serverEncoding);

$random = 'test-box-'.\date('c').\bin2hex(\random_bytes(4));

$mailbox->createMailbox($random);

$mailbox->switchMailbox($random, false);

return [$mailbox, $random, $imapPath];
}

/**
* @psalm-param MAILBOX_ARGS $mailbox_args
*
* @return mixed[]
*
* @psalm-return array{0:Mailbox, 1:string, 2:HiddenString}
*/
protected function getMailboxFromArgs(array $mailbox_args): array
{
list($path, $username, $password, $attachments_dir) = $mailbox_args;

return $this->getMailbox(
$path,
$username,
$password,
$attachments_dir,
isset($mailbox_args[4]) ? $mailbox_args[4] : 'UTF-8'
);
}
}

0 comments on commit da6068c

Please sign in to comment.