You are here

public function EntityStorageBaseTest::testLoadMultiple in Drupal 10

Test loadMultiple().

Does not cover statically-cached results.

@covers ::loadMultiple

@dataProvider providerLoadMultiple

File

core/tests/Drupal/Tests/Core/Entity/EntityStorageBaseTest.php, line 140

Class

EntityStorageBaseTest
@coversDefaultClass \Drupal\Core\Entity\EntityStorageBase @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testLoadMultiple($expected, $load_multiple, $query) {

  // Make our EntityStorageBase mock.
  $mock_base = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageBase')
    ->disableOriginalConstructor()
    ->onlyMethods([
    'doLoadMultiple',
    'postLoad',
  ])
    ->getMockForAbstractClass();

  // For all non-cached queries, we call doLoadMultiple().
  $mock_base
    ->expects($this
    ->once())
    ->method('doLoadMultiple')
    ->with($query)
    ->willReturn($load_multiple);

  // Make our EntityTypeInterface mock so that we can turn off static caching.
  $mock_entity_type = $this
    ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeInterface')
    ->onlyMethods([
    'isStaticallyCacheable',
  ])
    ->getMockForAbstractClass();

  // Disallow caching.
  $mock_entity_type
    ->expects($this
    ->any())
    ->method('isStaticallyCacheable')
    ->willReturn(FALSE);

  // Add the EntityTypeInterface to the storage object.
  $ref_entity_type = new \ReflectionProperty($mock_base, 'entityType');
  $ref_entity_type
    ->setAccessible(TRUE);
  $ref_entity_type
    ->setValue($mock_base, $mock_entity_type);

  // Set up expectations for postLoad(), which we only call if there are
  // results from loadMultiple().
  $mock_base
    ->expects($this
    ->exactly(empty($load_multiple) ? 0 : 1))
    ->method('postLoad');
  $this
    ->assertEquals($expected, $mock_base
    ->loadMultiple($query));
}