8e98e0749a
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
64 lines
2.6 KiB
PHP
64 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXT\ProviderLocalDocuments\Tests\Unit;
|
|
|
|
use KTXC\Db\Collection;
|
|
use KTXC\Db\Cursor;
|
|
use KTXC\Db\DataStore;
|
|
use KTXF\Files\Node\NodeType;
|
|
use KTXF\Resource\Range\RangeTally;
|
|
use KTXM\ProviderLocalDocuments\Providers\Personal\CollectionResource;
|
|
use KTXM\ProviderLocalDocuments\Providers\Personal\EntityResource;
|
|
use KTXM\ProviderLocalDocuments\Store\MetaStore;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class NodeListTest extends TestCase
|
|
{
|
|
public function testCursorIsConsumedLazilyAndRangeIsPreserved(): void
|
|
{
|
|
$consumed = 0;
|
|
$rows = (static function () use (&$consumed): \Generator {
|
|
$consumed++;
|
|
yield ['nid' => 'folder', 'cid' => 'parent', 'type' => NodeType::Collection->value];
|
|
$consumed++;
|
|
yield ['nid' => 'file', 'cid' => 'parent', 'type' => NodeType::Entity->value];
|
|
})();
|
|
$collection = $this->createMock(Collection::class);
|
|
$collection->expects(self::once())->method('find')->with(
|
|
['tid' => 'tenant', 'uid' => 'user', 'cid' => 'parent'],
|
|
self::callback(static fn(array $options): bool =>
|
|
$options['skip'] === 64 && $options['limit'] === 64 && $options['sort']['type'] === 1),
|
|
)->willReturn(new Cursor($rows));
|
|
$database = $this->createMock(DataStore::class);
|
|
$database->expects(self::once())->method('selectCollection')->willReturn($collection);
|
|
$range = new RangeTally();
|
|
$range->setPosition(64);
|
|
$range->setTally(64);
|
|
$nodes = (new MetaStore($database))->nodeList('tenant', 'user', 'parent', false, null, null, $range);
|
|
|
|
self::assertSame(0, $consumed);
|
|
self::assertTrue($nodes->valid());
|
|
self::assertSame(1, $consumed);
|
|
self::assertSame('folder', $nodes->key());
|
|
self::assertInstanceOf(CollectionResource::class, $nodes->current());
|
|
$nodes->next();
|
|
self::assertSame(2, $consumed);
|
|
self::assertSame('file', $nodes->key());
|
|
self::assertInstanceOf(EntityResource::class, $nodes->current());
|
|
$nodes->next();
|
|
self::assertFalse($nodes->valid());
|
|
}
|
|
|
|
public function testEmptyCursorProducesAnEmptyGenerator(): void
|
|
{
|
|
$collection = $this->createStub(Collection::class);
|
|
$collection->method('find')->willReturn(new Cursor(new \ArrayIterator([])));
|
|
$database = $this->createStub(DataStore::class);
|
|
$database->method('selectCollection')->willReturn($collection);
|
|
$nodes = (new MetaStore($database))->nodeList('tenant', 'user', 'parent');
|
|
self::assertFalse($nodes->valid());
|
|
}
|
|
}
|