fix: entityExtant()

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-09-26 21:57:27 -04:00
parent ec6bdcf7fd
commit 54a24c9892
3 changed files with 68 additions and 4 deletions
+10 -4
View File
@@ -596,11 +596,17 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
{ {
$this->initialize(); $this->initialize();
$allUids = $this->remoteService->entityList((string) $collection); // only positive integers are valid UIDs; anything else cannot exist
$uidSet = array_flip($allUids); // int[] → [uid => index] $uids = array_values(array_filter(
$extant = []; array_map(static fn (string|int $id): int => (int) $id, $identifiers),
static fn (int $uid): bool => $uid > 0,
));
$existing = array_flip($this->remoteService->entityExtant((string) $collection, ...$uids));
$extant = [];
foreach ($identifiers as $id) { foreach ($identifiers as $id) {
$extant[$id] = isset($uidSet[(int) $id]); $extant[$id] = isset($existing[(int) $id]) && (string) (int) $id === (string) $id;
} }
return $extant; return $extant;
} }
+19
View File
@@ -298,6 +298,25 @@ class RemoteMailService
return $this->entityApplyRange($uids, $range); return $this->entityApplyRange($uids, $range);
} }
/**
* Determine which of the given UIDs exist in a mailbox.
*
* @return int[] the subset of the given UIDs that exist
*/
public function entityExtant(string $collection, int ...$uids): array
{
if ($uids === []) {
return [];
}
$this->imapClient()->perform(new SelectCommand($collection, true));
return $this->imapClient()->perform(new SearchCommand(
SearchCriteriaBuilder::create()->uid(SequenceSet::items(...$uids)),
IdentifierMode::Uid,
))->matches();
}
/** /**
* Retrieve a list of messages in a mailbox matching the given filter, sorted and paginated as requested. * Retrieve a list of messages in a mailbox matching the given filter, sorted and paginated as requested.
* *
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace KTXT\ProviderImap\Tests\Unit;
use KTXM\ProviderImap\Providers\Service;
use KTXM\ProviderImap\Service\Remote\RemoteMailService;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
final class ServiceTest extends TestCase
{
public function testEntityExtantReportsExistenceOfEachIdentifier(): void
{
$service = new Service();
$remote = new ServiceTestRemoteStub($this->createStub(Service::class));
$remote->existing = [5];
(new ReflectionProperty(Service::class, 'remoteService'))->setValue($service, $remote);
$result = $service->entityExtant('INBOX', 5, '6', 'abc', 0);
$this->assertSame([5 => true, 6 => false, 'abc' => false, 0 => false], $result);
$this->assertSame([['INBOX', [5, 6]]], $remote->extantCalls);
}
}
final class ServiceTestRemoteStub extends RemoteMailService
{
/** @var int[] */
public array $existing = [];
public array $extantCalls = [];
public function entityExtant(string $collection, int ...$uids): array
{
$this->extantCalls[] = [$collection, $uids];
return array_values(array_intersect($uids, $this->existing));
}
}