function EntityApiTest::testLazyPreLoadingAdditional
Test lazy preloading with additional entity load calls.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityApiTest.php, line 246
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testLazyPreLoadingAdditional() : void {
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$ids = [];
$entity = $storage->create([
'name' => 'test',
]);
$entity->save();
$ids[] = $entity->id();
$entity = $storage->create([
'name' => 'test2',
]);
$entity->save();
$ids[] = $entity->id();
$entity = $storage->create([
'name' => 'test3',
]);
$entity->save();
$ids[] = $entity->id();
$fiber1 = new \Fiber(function () use ($ids, $storage) {
$storage->loadMultiple([
$ids[0],
]);
return $storage->loadMultiple([
$ids[2],
]);
});
$fiber2 = new \Fiber(fn() => $storage->loadMultiple([
$ids[1],
]));
// Make sure the entity cache is empty.
$this->container
->get('entity.memory_cache')
->reset();
// Start Fiber 1, this should set the first entity to be loaded, without
// actually loading it, and then suspend.
$fiber1->start();
$this->assertTrue($fiber1->isSuspended());
$this->assertFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[0]));
// Start Fiber 2, this should set the second entity to be loaded, without
// actually loading it, and then suspend.
$fiber2->start();
$this->assertTrue($fiber2->isSuspended());
$this->assertFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[1]));
// Resume Fiber 1, this should load both entities, and set the third entity
// to be loaded, without actually loading it, then suspend again.
$fiber1->resume();
$this->assertTrue($fiber1->isSuspended());
// Now the first and second entities should be loaded.
$this->assertNotFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[0]));
$this->assertNotFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[1]));
// But the third entity should not.
$this->assertFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[2]));
$fiber2->resume();
$this->assertTrue($fiber2->isTerminated());
$return2 = $fiber2->getReturn();
$this->assertSame($return2[2]->id(), $ids[1]);
$this->assertSame(\count($return2), 1);
// All three entities should be loaded if Fiber1 is resumed again.
$fiber1->resume();
$this->assertNotFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[0]));
$this->assertNotFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[1]));
$this->assertNotFalse($this->container
->get('entity.memory_cache')
->get('values:entity_test:' . $ids[2]));
$this->assertTrue($fiber1->isTerminated());
$return1 = $fiber1->getReturn();
$this->assertSame($return1[3]->id(), $ids[2]);
$this->assertSame(\count($return1), 1);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.