Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Relation/BulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ public function __construct(

public function collect(object ...$entities): RelationLoaderInterface
{
$entities === [] and throw new \InvalidArgumentException('At least one entity must be provided.');
if ($entities === []) {
return new class implements RelationLoaderInterface {
public function load(string $relation, array $options = []): static
{
return $this;
}

public function run(): void {}
};
}
$entities = \array_values($entities);

// Validate entity roles
Expand Down Expand Up @@ -112,7 +121,7 @@ public function run(): void
* @param non-empty-array<non-empty-string> $keys
* @param non-empty-array<non-empty-string> $data
*/
public function indexEntity(Node $node, array $keys, array $data, object $entity): void
private function indexEntity(Node $node, array $keys, array $data, object $entity): void
{
$pool = &$this->index;
foreach ($keys as $k) {
Expand Down
2 changes: 2 additions & 0 deletions src/Relation/BulkLoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ interface BulkLoaderInterface
/**
* Collect entities for bulk relation loading.
*
* If no entities are provided, a no-op loader is returned.
*
* @param object ...$entities Entities to collect
* @psalm-immutable
*/
Expand Down
11 changes: 5 additions & 6 deletions tests/ORM/Unit/Relation/BulkLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cycle\ORM\Mapper\Mapper;
use Cycle\ORM\ORM;
use Cycle\ORM\Relation\BulkLoader;
use Cycle\ORM\Relation\RelationLoaderInterface;
use Cycle\ORM\Schema;
use Cycle\ORM\Tests\Fixtures\User;
use Cycle\ORM\Tests\Fixtures\Profile;
Expand Down Expand Up @@ -130,14 +131,12 @@ public function testChainingMultipleCollectCalls(): void
*/
public function testCollectWithEmptyArrayUnpacking(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('At least one entity must be provided.');

$orm = $this->createORM();
$entities = [];
$loader = (new BulkLoader($orm))->collect();

$loader = new BulkLoader($orm);
$loader->collect(...$entities);
self::assertInstanceOf(RelationLoaderInterface::class, $loader);
$loader->load('profile');
$loader->run();
}

/**
Expand Down
Loading