refactor: unify command complition checking

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-09-25 12:14:37 -04:00
parent 1b68c99521
commit 6a36e91cb4
21 changed files with 132 additions and 80 deletions
+58 -3
View File
@@ -7,6 +7,9 @@ namespace KTXT\ProviderImap\Tests\Unit;
use KTXM\ProviderImap\Client\{CommandFailedException, ConnectionConfig, ImapException};
use KTXM\ProviderImap\Client\Protocol\Command\{AppendCommand, CopyCommand, MoveCommand, CommandInterface};
use KTXM\ProviderImap\Client\Protocol\Command\Argument\MessageTarget;
use KTXM\ProviderImap\Client\Protocol\Command\Argument\ListReturnOptions;
use KTXM\ProviderImap\Client\Protocol\Command as Commands;
use KTXM\ProviderImap\Client\Protocol\{CompletionChecker, ProtocolReader};
use KTXM\ProviderImap\Client\Protocol\{ResponseStream, SessionContext};
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
use KTXM\ProviderImap\Client\Transport\ConnectionInterface;
@@ -28,12 +31,32 @@ final class CommandFailedExceptionTest extends TestCase
'COPY' => new CopyCommand(MessageTarget::uid(42), 'Archive'),
'MOVE' => new MoveCommand(MessageTarget::uid(42), 'Archive'),
'APPEND' => new AppendCommand('Archive', 'body'),
'CAPABILITY' => new Commands\CapabilityCommand(),
'CREATE' => new Commands\CreateCommand('Archive'),
'DELETE' => new Commands\DeleteCommand('Archive'),
'RENAME' => new Commands\RenameCommand('INBOX', 'Archive'),
'LOGIN' => new Commands\LoginCommand('user', 'password'),
'LOGOUT' => new Commands\LogoutCommand(),
'NOOP' => new Commands\NoopCommand(),
'STARTTLS' => new Commands\StartTlsCommand(),
'SELECT' => new Commands\SelectCommand('INBOX', false),
'EXAMINE' => new Commands\SelectCommand('INBOX', true),
'STATUS' => new Commands\StatusCommand('INBOX'),
'SEARCH' => new Commands\SearchCommand(),
'SORT' => new Commands\SortCommand(['DATE']),
'STORE' => new Commands\StoreCommand(MessageTarget::uid(42), ['\\Seen']),
'EXPUNGE' => new Commands\ExpungeCommand(),
'UID EXPUNGE' => new Commands\ExpungeCommand(MessageTarget::uid(42)),
'LIST' => new Commands\ListCommand(),
'LIST-STATUS' => new Commands\ListCommand(returnOptions: ListReturnOptions::status('MESSAGES')),
'FETCH' => new Commands\FetchOneCommand(42),
'FETCH-MANY' => new Commands\FetchManyCommand(),
};
}
public static function failures(): iterable
{
foreach (['COPY', 'MOVE', 'APPEND'] as $command) {
foreach (['COPY', 'MOVE', 'APPEND', 'CAPABILITY', 'CREATE', 'DELETE', 'RENAME', 'LOGIN', 'LOGOUT', 'NOOP', 'STARTTLS', 'SELECT', 'EXAMINE', 'STATUS', 'SEARCH', 'SORT', 'STORE', 'EXPUNGE', 'UID EXPUNGE', 'LIST', 'LIST-STATUS', 'FETCH', 'FETCH-MANY'] as $command) {
foreach (['NO', 'BAD'] as $status) {
foreach ([
['[trycreate] Create destination first', ['name' => 'TRYCREATE', 'arguments' => [], 'text' => 'Create destination first']],
@@ -41,7 +64,7 @@ final class CommandFailedExceptionTest extends TestCase
['Denied', null],
['[Malformed', null],
] as [$text, $code]) {
yield [$command, $status, $text, $code];
yield $command . ' ' . $status . ' ' . $text => [$command, $status, $text, $code];
}
}
}
@@ -55,9 +78,13 @@ final class CommandFailedExceptionTest extends TestCase
});
try {
$this->command($command)->handle($responses, $this->context());
$result = $this->command($command)->handle($responses, $this->context());
if ($result instanceof \Generator) {
iterator_to_array($result);
}
$this->fail('Expected server failure');
} catch (ImapException $exception) {
$command = match ($command) { 'LIST-STATUS' => 'LIST', 'FETCH-MANY' => 'FETCH', default => $command };
$this->assertInstanceOf(CommandFailedException::class, $exception);
$this->assertSame($command . ' failed: ' . $text, $exception->getMessage());
$this->assertSame($command, $exception->command());
@@ -88,4 +115,32 @@ final class CommandFailedExceptionTest extends TestCase
});
$this->assertSame(42, $this->command('APPEND')->handle($responses, $this->context()));
}
public function testDownloadFailuresUseStructuredExceptions(): void
{
foreach (['readUntilFetchLiteral', 'readToEnd'] as $method) {
foreach (['NO', 'BAD'] as $status) {
$connection = $this->createStub(ConnectionInterface::class);
$connection->method('readLine')->willReturn("A1 $status [LIMIT 10] Denied\r\n");
try {
(new ProtocolReader($connection))->$method('A1');
$this->fail('Expected download failure');
} catch (CommandFailedException $exception) {
$this->assertSame('FETCH', $exception->command());
$this->assertSame($status, $exception->status());
$this->assertSame(['name' => 'LIMIT', 'arguments' => ['10'], 'text' => 'Denied'], $exception->responseCode());
}
}
}
}
public function testCheckerAcceptsOkAndRejectsMalformedStatusAsProtocolError(): void
{
CompletionChecker::assertSuccess('NOOP', new TaggedResponse('A1', 'OK', 'Done', 'A1 OK Done'));
try {
CompletionChecker::assertSuccess('NOOP', new TaggedResponse('A1', 'INVALID', 'Bad status', 'A1 INVALID Bad status'));
$this->fail('Expected malformed status error');
} catch (ImapException $exception) {
$this->assertNotInstanceOf(CommandFailedException::class, $exception);
}
}
}