Files
provider_imap/tests/php/Support/FakeLiveMailService.php
T
2026-10-07 22:15:34 -04:00

104 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXT\ProviderImap\Tests\Support;
use Generator;
use KTXF\Resource\Filter\IFilter;
use KTXF\Resource\Range\IRange;
use KTXF\Resource\Sort\ISort;
use KTXM\ProviderImap\Client\Mailbox;
use KTXM\ProviderImap\Client\Protocol\Command\Argument\FetchOptions;
use KTXM\ProviderImap\Client\Protocol\Parser\FetchMessageParser;
use KTXM\ProviderImap\Providers\CollectionResource;
use KTXM\ProviderImap\Service\Live\LiveMailService;
/**
* LiveMailService without a server: mailboxes, UIDs and flags are set by the test.
*/
final class FakeLiveMailService extends LiveMailService
{
public ?int $uidValidity = null;
public ?int $uidNext = null;
/** @var array<int, list<string>> */
public array $flags = [];
/** @var int[] */
public array $unfetchable = [];
public array $fetchBatches = [];
public int $selects = 0;
public ?int $bodyTextLimit = null;
/** @var Mailbox[] */
public array $mailboxList = [];
/** @var string[] */
public array $failing = [];
/** @var list<array{0: string, 1: array}> */
public array $calls = [];
public function collectionList(?string $location = null, ?IFilter $filter = null, ?ISort $sort = null): array
{
$this->calls[] = ['collectionList', [$location, $filter]];
$list = [];
foreach ($this->mailboxList as $mailbox) {
$list[$mailbox->name()] = (new CollectionResource('imap', 'svc'))->fromImap($mailbox);
}
return $list;
}
public function mailboxFetch(string $identifier): ?Mailbox
{
if (in_array($identifier, $this->failing, true)) {
return null;
}
$this->selects++;
return new Mailbox($identifier, '/', [], count($this->flags), 0, $this->uidValidity, 0, [], true, $this->uidNext);
}
public function entityFlags(string $collection): Generator
{
yield from $this->flags;
}
public function messageFetch(string $collection, ?FetchOptions $options = null, int ...$uids): Generator
{
$this->fetchBatches[] = $uids;
if (preg_match('/BODY\.PEEK\[TEXT\]<0\.(\d+)>/', (string) $options?->toCommand(), $matches) === 1) {
$this->bodyTextLimit = (int) $matches[1];
}
foreach ($uids as $uid) {
if (in_array($uid, $this->unfetchable, true)) {
continue;
}
$flags = implode(' ', $this->flags[$uid] ?? []);
yield $uid => FetchMessageParser::parse("* {$uid} FETCH (UID {$uid} FLAGS ({$flags}))");
}
}
/**
* Every message of the mailbox in UID order; filter / sort / range are not evaluated.
*/
public function messageList(string $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null): Generator
{
$this->calls[] = ['messageList', [$collection]];
$uids = array_keys($this->flags);
sort($uids);
yield from $this->messageFetch($collection, null, ...$uids);
}
/** @var int[] UIDs entityFind() returns */
public array $found = [];
public function entityFind(string $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null): array
{
$this->calls[] = ['entityFind', [$collection, $filter]];
return $this->found;
}
public function entityPatch(string $collection, array $flagsToAdd = [], array $flagsToRemove = [], int ...$uids): void
{
$this->calls[] = ['entityPatch', [$collection, $flagsToAdd, $flagsToRemove, $uids]];
}
}