function EntityApiTest::testLazyPreLoadingMultiple
Test lazy preloading.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityApiTest.php, line 195
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testLazyPreLoadingMultiple() : 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->loadMultiple([
$ids[0],
]));
$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 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());
$return2 = $fiber2->getReturn();
$this->assertSame($return2[2]->id(), $ids[1]);
$this->assertSame(\count($return2), 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());
$return1 = $fiber1->getReturn();
$this->assertSame($return1[1]->id(), $ids[0]);
$this->assertSame(\count($return1), 1);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.