Files
provider_imap/tests/php/Unit/SelectCommandTest.php
T
2026-10-05 20:34:02 -04:00

70 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXT\ProviderImap\Tests\Unit;
use KTXM\ProviderImap\Client\{Client, ConnectionConfig};
use KTXM\ProviderImap\Client\Protocol\Command\SelectCommand;
use KTXM\ProviderImap\Client\Transport\{ConnectionInterface, ConnectionFactoryInterface};
use PHPUnit\Framework\TestCase;
final class SelectCommandTest extends TestCase
{
public function testResponseCodesAreParsed(): void
{
$mailbox = $this->client([
"* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n",
"* OK [PERMANENTFLAGS ()] Read-only mailbox\r\n",
"* 172 EXISTS\r\n",
"* 1 RECENT\r\n",
"* OK [UIDVALIDITY 3857529045] UIDs valid\r\n",
"* OK [UIDNEXT 4392] Predicted next UID\r\n",
"* OK [HIGHESTMODSEQ 715194045007] Highest\r\n",
])->perform(new SelectCommand('INBOX', true));
$this->assertSame(172, $mailbox->messages());
$this->assertSame(1, $mailbox->recent());
$this->assertSame(3857529045, $mailbox->uidValidity());
$this->assertSame(4392, $mailbox->uidNext());
$this->assertSame(715194045007, $mailbox->highestModSeq());
}
public function testMissingResponseCodesAreNull(): void
{
$mailbox = $this->client(["* 0 EXISTS\r\n"])->perform(new SelectCommand('INBOX', true));
$this->assertNull($mailbox->uidValidity());
$this->assertNull($mailbox->uidNext());
$this->assertNull($mailbox->highestModSeq());
}
/**
* @param string[] $untagged untagged lines the server sends before completing SELECT
*/
private function client(array $untagged): Client
{
$lines = ["* PREAUTH Ready\r\n"];
$connection = $this->createStub(ConnectionInterface::class);
$connection->method('readLine')->willReturnCallback(static function () use (&$lines): string {
return array_shift($lines) ?? throw new \RuntimeException('Unexpected response read');
});
$connection->method('write')->willReturnCallback(static function (string $wire) use (&$lines, $untagged): void {
[$tag, $command] = explode(' ', trim($wire), 2);
if (str_starts_with($command, 'CAPABILITY')) {
$lines[] = "* CAPABILITY IMAP4rev1\r\n";
}
if (str_starts_with($command, 'EXAMINE') || str_starts_with($command, 'SELECT')) {
array_push($lines, ...$untagged);
}
$lines[] = "$tag OK Completed\r\n";
});
$factory = $this->createStub(ConnectionFactoryInterface::class);
$factory->method('create')->willReturn($connection);
$client = new Client($factory);
$client->connect(new ConnectionConfig('localhost'));
return $client;
}
}