function EntityApiTest::testLazyPreLoading
Test lazy preloading.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityApiTest.php, line 149
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testLazyPreLoading() : 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();
$fiber1 = new \Fiber(fn() => $storage->load($ids[0]));
$fiber2 = new \Fiber(fn() => $storage->load($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 first 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]));
$fiber2->resume();
$this->assertTrue($fiber2->isTerminated());
$this->assertSame($fiber2->getReturn()
->id(), $ids[1]);
// Now both 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]));
$fiber1->resume();
$this->assertTrue($fiber1->isTerminated());
$this->assertSame($fiber1->getReturn()
->id(), $ids[0]);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.