class EntityStorageBaseTest
Same name in other branches
- 10 core/tests/Drupal/Tests/Core/Entity/EntityStorageBaseTest.php \Drupal\Tests\Core\Entity\EntityStorageBaseTest
- 11.x core/tests/Drupal/Tests/Core/Entity/EntityStorageBaseTest.php \Drupal\Tests\Core\Entity\EntityStorageBaseTest
@coversDefaultClass \Drupal\Core\Entity\EntityStorageBase @group Entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait
- class \Drupal\Tests\Core\Entity\EntityStorageBaseTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityStorageBaseTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityStorageBaseTest.php, line 11
Namespace
Drupal\Tests\Core\EntityView source
class EntityStorageBaseTest extends UnitTestCase {
/**
* Generate a mocked entity object.
*
* @param string $id
* ID value for this entity.
*
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit\Framework\MockObject\MockObject
* The mocked entity.
*/
public function generateEntityInterface($id) {
$mock_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityInterface')
->onlyMethods([
'id',
])
->getMockForAbstractClass();
$mock_entity->expects($this->any())
->method('id')
->willReturn((string) $id);
return $mock_entity;
}
/**
* Data provider for testLoad().
*
* @return array
* - Expected output of load().
* - A fixture of entities to query against. Suitable return value for
* loadMultiple().
* - The ID we'll query.
*/
public function providerLoad() {
$data = [];
// Data set for a matching value.
$entity = $this->generateEntityInterface('1');
$data['matching-value'] = [
$entity,
[
'1' => $entity,
],
'1',
];
// Data set for no matching value.
$data['no-matching-value'] = [
NULL,
[],
'0',
];
return $data;
}
/**
* @covers ::load
*
* @dataProvider providerLoad
*/
public function testLoad($expected, $entity_fixture, $query) {
$mock_base = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageBase')
->disableOriginalConstructor()
->onlyMethods([
'loadMultiple',
])
->getMockForAbstractClass();
// load() always calls loadMultiple().
$mock_base->expects($this->once())
->method('loadMultiple')
->with([
$query,
])
->willReturn($entity_fixture);
$this->assertEquals($expected, $mock_base->load($query));
}
/**
* Data provider for testLoadMultiple.
*
* @return array
* - The expected result.
* - Results for doLoadMultiple(), called internally by loadMultiple().
* - The query, an array of IDs.
*/
public function providerLoadMultiple() {
// Create a fixture of entity objects.
$fixture = [];
foreach (range(1, 10) as $index) {
$fixture[(string) $index] = $this->generateEntityInterface($index);
}
$data = [];
// Data set for NULL ID parameter.
$data['null-id-parameter'] = [
$fixture,
$fixture,
NULL,
];
// Data set for no results.
$data['no-results'] = [
[],
[],
[
'11',
],
];
// Data set for 0 results for multiple IDs.
$data['no-results-multiple-ids'] = [
[],
[],
[
'11',
'12',
'13',
],
];
// Data set for 1 result for 1 ID.
$data['1-result-for-1-id'] = [
[
'1' => $fixture['1'],
],
[
'1' => $fixture['1'],
],
[
'1',
],
];
// Data set for results for all IDs.
$ids = [
'1',
'2',
'3',
];
foreach ($ids as $id) {
$expectation[$id] = $fixture[$id];
$load_multiple[$id] = $fixture[$id];
}
$data['results-for-all-ids'] = [
$expectation,
$load_multiple,
$ids,
];
// Data set for partial results for multiple IDs.
$ids = [
'1',
'2',
'3',
];
foreach ($ids as $id) {
$expectation[$id] = $fixture[$id];
$load_multiple[$id] = $fixture[$id];
}
$ids = array_merge($ids, [
'11',
'12',
]);
$data['partial-results-for-multiple-ids'] = [
$expectation,
$load_multiple,
$ids,
];
return $data;
}
/**
* Test loadMultiple().
*
* Does not cover statically-cached results.
*
* @covers ::loadMultiple
*
* @dataProvider providerLoadMultiple
*/
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));
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|---|
EntityStorageBaseTest::generateEntityInterface | public | function | Generate a mocked entity object. | ||
EntityStorageBaseTest::providerLoad | public | function | Data provider for testLoad(). | ||
EntityStorageBaseTest::providerLoadMultiple | public | function | Data provider for testLoadMultiple. | ||
EntityStorageBaseTest::testLoad | public | function | @covers ::load | ||
EntityStorageBaseTest::testLoadMultiple | public | function | Test loadMultiple(). | ||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | ||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | ||
UnitTestCase::$randomGenerator | protected | property | The random generator. | ||
UnitTestCase::$root | protected | property | The app root. | 1 | |
UnitTestCase::assertArrayEquals | Deprecated | protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | ||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
UnitTestCase::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
UnitTestCase::randomMachineName | public | function | Generates a unique random string containing letters and numbers. | ||
UnitTestCase::setUp | protected | function | 338 | ||
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.