test: more tests for UserMountCache

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2026-04-14 22:24:30 +02:00
parent ac9da6eaa0
commit cf06ebf999
No known key found for this signature in database
GPG key ID: 42B69D8A64526EFB

View file

@ -183,7 +183,9 @@ class UserMountCacheTest extends TestCase {
$this->eventDispatcher
->expects($this->exactly(2))
->method('dispatchTyped')
->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) {
->with($this->callback(function (
UserMountAddedEvent|UserMountRemovedEvent $event,
) use (&$operation) {
return match (++$operation) {
1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/asd/',
2 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/asd/',
@ -214,7 +216,9 @@ class UserMountCacheTest extends TestCase {
$this->eventDispatcher
->expects($this->exactly(3))
->method('dispatchTyped')
->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) {
->with($this->callback(function (
UserMountAddedEvent|UserMountRemovedEvent $event,
) use (&$operation) {
return match (++$operation) {
1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/bar/',
2 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/',
@ -250,7 +254,9 @@ class UserMountCacheTest extends TestCase {
$this->eventDispatcher
->expects($this->exactly(2))
->method('dispatchTyped')
->with($this->callback(function (UserMountAddedEvent|UserMountUpdatedEvent $event) use (&$operation) {
->with($this->callback(function (
UserMountAddedEvent|UserMountUpdatedEvent $event,
) use (&$operation) {
return match (++$operation) {
1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/',
2 => $event instanceof UserMountUpdatedEvent && $event->oldMountPoint->getMountId() === null && $event->newMountPoint->getMountId() === 1,
@ -611,12 +617,59 @@ class UserMountCacheTest extends TestCase {
$this->cache->flush();
$cached = $this->cache->getMountsForUser($user);
usort($cached, fn (ICachedMountInfo $a, ICachedMountInfo $b) => $a->getMountPoint() <=> $b->getMountPoint());
usort($cached, fn (ICachedMountInfo $a, ICachedMountInfo $b,
) => $a->getMountPoint() <=> $b->getMountPoint());
$mountPoints = array_map(fn (ICachedMountInfo $mountInfo) => $mountInfo->getMountPoint(), $cached);
$mountPoints = array_map(fn (ICachedMountInfo $mountInfo,
) => $mountInfo->getMountPoint(), $cached);
$this->assertEquals(['/asd/', '/asd2/'], $mountPoints);
$mountIds = array_map(fn (ICachedMountInfo $mountInfo) => $mountInfo->getMountId(), $cached);
$mountIds = array_map(fn (ICachedMountInfo $mountInfo,
) => $mountInfo->getMountId(), $cached);
$this->assertEquals([null, 1], $mountIds);
}
public function testGetMountForPath(): void {
$user = $this->userManager->get('u1');
[$storage] = $this->getStorage(10);
$mount1 = new MountPoint($storage, '/asd/');
$mount2 = new MountPoint($storage, '/asd/foo');
$this->cache->registerMounts($user, [$mount1, $mount2]);
$this->cache->flush();
$this->assertEquals('/asd/', $this->cache->getMountForPath($user, '/asd/bar/')->getMountPoint());
$this->assertEquals('/asd/', $this->cache->getMountForPath($user, '/asd/')->getMountPoint());
$this->assertEquals('/asd/foo/', $this->cache->getMountForPath($user, '/asd/foo/bar/')->getMountPoint());
$this->assertEquals('/asd/foo/', $this->cache->getMountForPath($user, '/asd/foo/')->getMountPoint());
}
public function testGetMountsInPath(): void {
$user = $this->userManager->get('u1');
[$storage] = $this->getStorage(10);
$mount1 = new MountPoint($storage, '/asd/');
$mount2 = new MountPoint($storage, '/asd/foo/');
$mount3 = new MountPoint($storage, '/asd/foo/bar/');
$this->cache->registerMounts($user, [$mount1, $mount2, $mount3]);
$this->cache->flush();
$getMountPaths = function (string $path) use ($user) {
$mountPoints = array_values(
array_map(
fn (ICachedMountInfo $mount) => $mount->getMountPoint(),
$this->cache->getMountsInPath($user, $path)
)
);
sort($mountPoints);
return $mountPoints;
};
$this->assertEquals(['/asd/foo/', '/asd/foo/bar/'], $getMountPaths('/asd/'));
$this->assertEquals([], $getMountPaths('/asd/foo/bar/'));
$this->assertEquals(['/asd/foo/bar/'], $getMountPaths('/asd/foo/'));
$this->assertEquals([], $getMountPaths('/asd/bar/'));
}
}