generated from Nodarx/template
feat: add CachedService with cached collections and delta
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXT\ProviderImap\Tests\Support;
|
||||
|
||||
use Generator;
|
||||
use KTXF\Resource\Filter\IFilter;
|
||||
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}))");
|
||||
}
|
||||
}
|
||||
|
||||
public function entityPatch(string $collection, array $flagsToAdd = [], array $flagsToRemove = [], int ...$uids): void
|
||||
{
|
||||
$this->calls[] = ['entityPatch', [$collection, $flagsToAdd, $flagsToRemove, $uids]];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user