generated from Nodarx/template
refactor: split service into ServiceBase and LiveService
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -6,7 +6,9 @@ namespace KTXT\ProviderImap\Tests\Unit;
|
||||
|
||||
use KTXM\ProviderImap\Client\{Client, ConnectionConfig};
|
||||
use KTXM\ProviderImap\Client\Transport\{ConnectionInterface, ConnectionFactoryInterface};
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||
use KTXM\ProviderImap\Providers\ServiceBase;
|
||||
use KTXM\ProviderImap\Providers\LiveService;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -16,7 +18,7 @@ final class CollectionFetchTest extends TestCase
|
||||
|
||||
public function testWithListStatusUsesOneRoundTrip(): void
|
||||
{
|
||||
$mailbox = $this->service(listStatus: true)->collectionFetch('INBOX');
|
||||
$mailbox = $this->service(listStatus: true)->mailboxFetch('INBOX');
|
||||
|
||||
$this->assertCount(1, $this->commands);
|
||||
$this->assertStringContainsString('RETURN (STATUS', $this->commands[0]);
|
||||
@@ -27,7 +29,7 @@ final class CollectionFetchTest extends TestCase
|
||||
|
||||
public function testWithoutListStatusFallsBackToStatus(): void
|
||||
{
|
||||
$mailbox = $this->service(listStatus: false)->collectionFetch('INBOX');
|
||||
$mailbox = $this->service(listStatus: false)->mailboxFetch('INBOX');
|
||||
|
||||
$this->assertCount(2, $this->commands);
|
||||
$this->assertStringNotContainsString('RETURN', $this->commands[0]);
|
||||
@@ -39,7 +41,36 @@ final class CollectionFetchTest extends TestCase
|
||||
|
||||
public function testUnknownMailboxIsNull(): void
|
||||
{
|
||||
$this->assertNull($this->service(listStatus: false, exists: false)->collectionFetch('Missing'));
|
||||
$this->assertNull($this->service(listStatus: false, exists: false)->mailboxFetch('Missing'));
|
||||
}
|
||||
|
||||
public function testCollectionsAreResourcesKeyedByName(): void
|
||||
{
|
||||
$service = $this->service(listStatus: true);
|
||||
|
||||
$list = $service->collectionList();
|
||||
$collection = $service->collectionFetch('INBOX');
|
||||
|
||||
$this->assertSame(['INBOX'], array_keys($list));
|
||||
$this->assertInstanceOf(CollectionResource::class, $list['INBOX']);
|
||||
$this->assertInstanceOf(CollectionResource::class, $collection);
|
||||
$this->assertSame('INBOX', $collection?->identifier());
|
||||
$this->assertNull($this->service(listStatus: true, exists: false)->collectionFetch('Missing'));
|
||||
}
|
||||
|
||||
public function testDelimiterIsReadWithoutListingMailboxes(): void
|
||||
{
|
||||
$this->assertSame('/', $this->service(listStatus: false)->collectionDelimiter());
|
||||
$this->assertSame(['LIST "" ""'], $this->commands);
|
||||
}
|
||||
|
||||
public function testCollectionExtantReportsMissingMailboxes(): void
|
||||
{
|
||||
$service = new LiveService();
|
||||
$live = $this->service(listStatus: true, exists: false);
|
||||
(new \ReflectionProperty(LiveService::class, 'mailService'))->setValue($service, $live);
|
||||
|
||||
$this->assertSame(['Missing' => false], $service->collectionExtant('Missing'));
|
||||
}
|
||||
|
||||
private function service(bool $listStatus, bool $exists = true): LiveMailService
|
||||
@@ -57,6 +88,11 @@ final class CollectionFetchTest extends TestCase
|
||||
} else {
|
||||
$this->commands[] = $command;
|
||||
}
|
||||
if ($operation === 'LIST' && $command === 'LIST "" ""') {
|
||||
$lines[] = "* LIST (\\Noselect) \"/\" \"\"\r\n";
|
||||
$lines[] = "$tag OK Completed\r\n";
|
||||
return;
|
||||
}
|
||||
if ($operation === 'LIST') {
|
||||
if (!$listStatus && str_contains($command, 'RETURN')) {
|
||||
$lines[] = "$tag BAD Unknown RETURN option\r\n";
|
||||
@@ -80,8 +116,8 @@ final class CollectionFetchTest extends TestCase
|
||||
$client = new Client($factory);
|
||||
$client->connect(new ConnectionConfig('localhost'));
|
||||
|
||||
return new class($this->createStub(Service::class), $client) extends LiveMailService {
|
||||
public function __construct(Service $service, private readonly Client $client)
|
||||
return new class($this->createStub(ServiceBase::class), $client) extends LiveMailService {
|
||||
public function __construct(ServiceBase $service, private readonly Client $client)
|
||||
{
|
||||
parent::__construct($service);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use KTXM\ProviderImap\Client\{Client, ConnectionConfig, ImapException};
|
||||
use KTXM\ProviderImap\Client\Protocol\Command\CreateCommand;
|
||||
use KTXM\ProviderImap\Client\Result\CommandCompletion;
|
||||
use KTXM\ProviderImap\Client\Transport\{ConnectionInterface, ConnectionFactoryInterface};
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\ServiceBase;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -48,8 +48,8 @@ final class CommandCompletionTest extends TestCase
|
||||
|
||||
private function service(Client $client): LiveMailService
|
||||
{
|
||||
return new class($this->createStub(Service::class), $client) extends LiveMailService {
|
||||
public function __construct(Service $service, private readonly Client $client)
|
||||
return new class($this->createStub(ServiceBase::class), $client) extends LiveMailService {
|
||||
public function __construct(ServiceBase $service, private readonly Client $client)
|
||||
{
|
||||
parent::__construct($service);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use KTXF\Resource\Filter\IFilter;
|
||||
use KTXF\Resource\Sort\ISort;
|
||||
use KTXM\ProviderImap\Client\Mailbox;
|
||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\LiveService;
|
||||
use KTXM\ProviderImap\Service\Cache\HarmonizationService;
|
||||
use KTXM\ProviderImap\Service\Cache\MessageIngestor;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
@@ -146,7 +146,7 @@ final class HarmonizationServiceTest extends TestCase
|
||||
$this->calls[] = ['files.deleteByMailbox', $tid, $sid, $name];
|
||||
});
|
||||
|
||||
$service = (new Service())->fromStore(['tid' => 'tenant', 'sid' => 'svc']);
|
||||
$service = (new LiveService())->fromStore(['tid' => 'tenant', 'sid' => 'svc']);
|
||||
$live = new HarmonizationServiceTestLiveStub($service);
|
||||
$live->mailboxes = array_map(static fn (string $name): Mailbox => new Mailbox($name, '/', []), $remote);
|
||||
|
||||
@@ -166,10 +166,12 @@ final class HarmonizationServiceTestLiveStub extends LiveMailService
|
||||
/** @var Mailbox[] */
|
||||
public array $mailboxes = [];
|
||||
|
||||
public function collectionList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, string $depth = '*'): Generator
|
||||
public function collectionList(?string $location = null, ?IFilter $filter = null, ?ISort $sort = null): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->mailboxes as $mailbox) {
|
||||
yield $mailbox->name() => $mailbox;
|
||||
$list[$mailbox->name()] = (new CollectionResource('imap', 'svc'))->fromImap($mailbox);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXT\ProviderImap\Tests\Unit;
|
||||
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\ServiceBase;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
@@ -12,7 +12,7 @@ use RuntimeException;
|
||||
final class LiveMailServiceTest extends TestCase {
|
||||
|
||||
public function testEntityReplaceReturnsAuthoritativeReplacementUid(): void {
|
||||
$service = new LiveMailServiceStub($this->createStub(Service::class));
|
||||
$service = new LiveMailServiceStub($this->createStub(ServiceBase::class));
|
||||
$service->appendUid = 202;
|
||||
$service->destroyResults = [[101 => true]];
|
||||
|
||||
@@ -28,7 +28,7 @@ final class LiveMailServiceTest extends TestCase {
|
||||
}
|
||||
|
||||
public function testEntityReplaceRemovesReplacementWhenOldUidCannotBeDeleted(): void {
|
||||
$service = new LiveMailServiceStub($this->createStub(Service::class));
|
||||
$service = new LiveMailServiceStub($this->createStub(ServiceBase::class));
|
||||
$service->appendUid = 202;
|
||||
$service->destroyResults = [
|
||||
[101 => false],
|
||||
|
||||
@@ -4,19 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace KTXT\ProviderImap\Tests\Unit;
|
||||
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\ServiceBase;
|
||||
use KTXM\ProviderImap\Providers\LiveService;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionProperty;
|
||||
|
||||
final class ServiceTest extends TestCase
|
||||
final class LiveServiceTest extends TestCase
|
||||
{
|
||||
public function testEntityExtantReportsExistenceOfEachIdentifier(): void
|
||||
{
|
||||
$service = new Service();
|
||||
$live = new ServiceTestLiveStub($this->createStub(Service::class));
|
||||
$service = new LiveService();
|
||||
$live = new LiveServiceTestLiveStub($this->createStub(ServiceBase::class));
|
||||
$live->existing = [5];
|
||||
(new ReflectionProperty(Service::class, 'liveService'))->setValue($service, $live);
|
||||
(new ReflectionProperty(LiveService::class, 'mailService'))->setValue($service, $live);
|
||||
|
||||
$result = $service->entityExtant('INBOX', 5, '6', 'abc', 0);
|
||||
|
||||
@@ -25,7 +26,7 @@ final class ServiceTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
final class ServiceTestLiveStub extends LiveMailService
|
||||
final class LiveServiceTestLiveStub extends LiveMailService
|
||||
{
|
||||
/** @var int[] */
|
||||
public array $existing = [];
|
||||
@@ -6,7 +6,7 @@ namespace KTXT\ProviderImap\Tests\Unit;
|
||||
|
||||
use KTXM\ProviderImap\Client\{Client, ConnectionConfig, ImapException};
|
||||
use KTXM\ProviderImap\Client\Transport\{ConnectionInterface, ConnectionFactoryInterface};
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\ServiceBase;
|
||||
use KTXM\ProviderImap\Service\Live\LiveMailService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -101,8 +101,8 @@ final class MailboxSelectionTest extends TestCase
|
||||
$client = new Client($factory);
|
||||
$client->connect(new ConnectionConfig('localhost'));
|
||||
|
||||
return new class($this->createStub(Service::class), $client) extends LiveMailService {
|
||||
public function __construct(Service $service, private readonly Client $client)
|
||||
return new class($this->createStub(ServiceBase::class), $client) extends LiveMailService {
|
||||
public function __construct(ServiceBase $service, private readonly Client $client)
|
||||
{
|
||||
parent::__construct($service);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ use KTXM\ProviderImap\Client\Protocol\Command\Argument\FetchOptions;
|
||||
use KTXM\ProviderImap\Client\Protocol\Parser\FetchMessageParser;
|
||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||
use KTXM\ProviderImap\Providers\EntityResource;
|
||||
use KTXM\ProviderImap\Providers\Service;
|
||||
use KTXM\ProviderImap\Providers\LiveService;
|
||||
use KTXM\ProviderImap\Service\Cache\HarmonizationService;
|
||||
use KTXM\ProviderImap\Service\Cache\MessageDeltaService;
|
||||
use KTXM\ProviderImap\Service\Cache\MessageIngestor;
|
||||
@@ -40,7 +40,7 @@ final class MessageHarmonizationTest extends TestCase
|
||||
$this->messages = new FakeMessageStore($dataStore);
|
||||
$this->files = new FakeMessageFileStore('/nonexistent');
|
||||
|
||||
$service = (new Service())->fromStore(['tid' => 'tenant', 'sid' => 'svc']);
|
||||
$service = (new LiveService())->fromStore(['tid' => 'tenant', 'sid' => 'svc']);
|
||||
$this->live = new MessageHarmonizationLiveStub($service);
|
||||
$this->live->uidValidity = 7;
|
||||
$this->live->uidNext = 100;
|
||||
@@ -286,14 +286,16 @@ final class MessageHarmonizationLiveStub extends LiveMailService
|
||||
/** @var string[] */
|
||||
public array $failing = [];
|
||||
|
||||
public function collectionList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, string $depth = '*'): Generator
|
||||
public function collectionList(?string $location = null, ?IFilter $filter = null, ?ISort $sort = null): array
|
||||
{
|
||||
$list = [];
|
||||
foreach ($this->mailboxList as $mailbox) {
|
||||
yield $mailbox->name() => $mailbox;
|
||||
$list[$mailbox->name()] = (new CollectionResource('imap', 'svc'))->fromImap($mailbox);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function collectionFetch(string $identifier): ?Mailbox
|
||||
public function mailboxFetch(string $identifier): ?Mailbox
|
||||
{
|
||||
if (in_array($identifier, $this->failing, true)) {
|
||||
return null;
|
||||
@@ -307,7 +309,7 @@ final class MessageHarmonizationLiveStub extends LiveMailService
|
||||
yield from $this->flags;
|
||||
}
|
||||
|
||||
public function entityFetch(string $collection, ?FetchOptions $options = null, int ...$uids): Generator
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user