Issue #3443535 by longwave: Change @dataprovider to static in SqlContentEntityStorageSchemaTest

merge-requests/7845/head
catch 2024-04-30 07:35:34 +01:00
parent d7bcfdf6d7
commit 3ee7654bb5
1 changed files with 73 additions and 93 deletions

View File

@ -1153,46 +1153,30 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
); );
} }
public function providerTestRequiresEntityDataMigration() { public static function providerTestRequiresEntityDataMigration(): \Generator {
$updated_entity_type_definition = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$updated_entity_type_definition->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
$original_entity_type_definition = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
$original_entity_type_definition_other_nonexisting = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition_other_nonexisting->expects($this->any())
->method('getStorageClass')
->willReturn('bar');
$original_entity_type_definition_other_existing = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition_other_existing->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
return [
// Case 1: same storage class, ::hasData() === TRUE. // Case 1: same storage class, ::hasData() === TRUE.
[$updated_entity_type_definition, $original_entity_type_definition, TRUE, TRUE, TRUE], yield [SqlContentEntityStorageSchema::class, TRUE, TRUE, TRUE];
// Case 2: same storage class, ::hasData() === FALSE. // Case 2: same storage class, ::hasData() === FALSE.
[$updated_entity_type_definition, $original_entity_type_definition, FALSE, TRUE, FALSE], yield [SqlContentEntityStorageSchema::class, FALSE, TRUE, FALSE];
// Case 3: different storage class, original storage class does not exist. // Case 3: different storage class, original storage class does not exist.
[$updated_entity_type_definition, $original_entity_type_definition_other_nonexisting, NULL, TRUE, TRUE], yield ['bar', NULL, TRUE, TRUE];
// Case 4: different storage class, original storage class exists, // Case 4: different storage class, original storage class exists,
// ::hasData() === TRUE. // ::hasData() === TRUE.
[$updated_entity_type_definition, $original_entity_type_definition_other_existing, TRUE, TRUE, TRUE], yield [SqlContentEntityStorageSchemaTest::class, TRUE, TRUE, TRUE];
// Case 5: different storage class, original storage class exists, // Case 5: different storage class, original storage class exists,
// ::hasData() === FALSE. // ::hasData() === FALSE.
[$updated_entity_type_definition, $original_entity_type_definition_other_existing, FALSE, TRUE, FALSE], yield [SqlContentEntityStorageSchemaTest::class, FALSE, TRUE, FALSE];
// Case 6: same storage class, ::hasData() === TRUE, no structure changes. // Case 6: same storage class, ::hasData() === TRUE, no structure changes.
[$updated_entity_type_definition, $original_entity_type_definition, TRUE, FALSE, FALSE], yield [SqlContentEntityStorageSchema::class, TRUE, FALSE, FALSE];
// Case 7: different storage class, original storage class exists, // Case 7: different storage class, original storage class exists,
// ::hasData() === TRUE, no structure changes. // ::hasData() === TRUE, no structure changes.
[$updated_entity_type_definition, $original_entity_type_definition_other_existing, TRUE, FALSE, FALSE], yield [SqlContentEntityStorageSchemaTest::class, TRUE, FALSE, FALSE];
];
} }
/** /**
@ -1200,7 +1184,7 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
* *
* @dataProvider providerTestRequiresEntityDataMigration * @dataProvider providerTestRequiresEntityDataMigration
*/ */
public function testRequiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition, $original_storage_has_data, $shared_table_structure_changed, $migration_required) { public function testRequiresEntityDataMigration(string $storage_class, bool|null $original_storage_has_data, bool $shared_table_structure_changed, bool $migration_required): void {
$this->entityType = new ContentEntityType([ $this->entityType = new ContentEntityType([
'id' => 'entity_test', 'id' => 'entity_test',
'entity_keys' => ['id' => 'id'], 'entity_keys' => ['id' => 'id'],
@ -1228,6 +1212,17 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
->onlyMethods(['installedStorageSchema', 'hasSharedTableStructureChange']) ->onlyMethods(['installedStorageSchema', 'hasSharedTableStructureChange'])
->getMock(); ->getMock();
$updated_entity_type_definition = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$updated_entity_type_definition->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
$original_entity_type_definition = $this->createMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition->expects($this->any())
->method('getStorageClass')
->willReturn($storage_class);
$this->storageSchema->expects($this->any()) $this->storageSchema->expects($this->any())
->method('hasSharedTableStructureChange') ->method('hasSharedTableStructureChange')
->with($updated_entity_type_definition, $original_entity_type_definition) ->with($updated_entity_type_definition, $original_entity_type_definition)
@ -1239,67 +1234,21 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
/** /**
* Data provider for ::testRequiresEntityStorageSchemaChanges(). * Data provider for ::testRequiresEntityStorageSchemaChanges().
*/ */
public function providerTestRequiresEntityStorageSchemaChanges() { public static function providerTestRequiresEntityStorageSchemaChanges(): \Generator {
$cases = [];
$updated_entity_type_definition = $this->createMock('\Drupal\Core\Entity\ContentEntityTypeInterface');
$original_entity_type_definition = $this->createMock('\Drupal\Core\Entity\ContentEntityTypeInterface');
$updated_entity_type_definition->expects($this->any())
->method('id')
->willReturn('entity_test');
$updated_entity_type_definition->expects($this->any())
->method('getKey')
->willReturn('id');
$original_entity_type_definition->expects($this->any())
->method('id')
->willReturn('entity_test');
$original_entity_type_definition->expects($this->any())
->method('getKey')
->willReturn('id');
// Storage class changes should not impact this at all, and should not be
// checked.
$updated = clone $updated_entity_type_definition;
$original = clone $original_entity_type_definition;
$updated->expects($this->never())
->method('getStorageClass');
$original->expects($this->never())
->method('getStorageClass');
// Case 1: No shared table changes should not require change. // Case 1: No shared table changes should not require change.
$cases[] = [$updated, $original, FALSE, FALSE, FALSE]; yield [FALSE, FALSE, FALSE];
// Case 2: A change in the entity schema should result in required changes. // Case 2: A change in the entity schema should result in required changes.
$cases[] = [$updated, $original, TRUE, TRUE, FALSE]; yield [TRUE, TRUE, FALSE];
// Case 3: Has shared table changes should result in required changes. // Case 3: Has shared table changes should result in required changes.
$cases[] = [$updated, $original, TRUE, FALSE, TRUE]; yield [TRUE, FALSE, TRUE];
// Case 4: Changing translation should result in required changes. // Case 4: Changing translation should result in required changes.
$updated = clone $updated_entity_type_definition; yield [TRUE, FALSE, FALSE, 'isTranslatable'];
$updated->expects($this->once())
->method('isTranslatable')
->willReturn(FALSE);
$original = clone $original_entity_type_definition;
$original->expects($this->once())
->method('isTranslatable')
->willReturn(TRUE);
$cases[] = [$updated, $original, TRUE, FALSE, FALSE];
// Case 5: Changing revisionable should result in required changes. // Case 5: Changing revisionable should result in required changes.
$updated = clone $updated_entity_type_definition; yield [TRUE, FALSE, FALSE, 'isRevisionable'];
$updated->expects($this->once())
->method('isRevisionable')
->willReturn(FALSE);
$original = clone $original_entity_type_definition;
$original->expects($this->once())
->method('isRevisionable')
->willReturn(TRUE);
$cases[] = [$updated, $original, TRUE, FALSE, FALSE];
return $cases;
} }
/** /**
@ -1307,7 +1256,38 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
* *
* @dataProvider providerTestRequiresEntityStorageSchemaChanges * @dataProvider providerTestRequiresEntityStorageSchemaChanges
*/ */
public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterface $updated, ContentEntityTypeInterface $original, $requires_change, $change_schema, $change_shared_table) { public function testRequiresEntityStorageSchemaChanges(bool $requires_change, bool $change_schema, bool $change_shared_table, ?string $method = NULL): void {
$original = $this->createMock(ContentEntityTypeInterface::class);
$original->expects($this->any())
->method('id')
->willReturn('entity_test');
$original->expects($this->any())
->method('getKey')
->willReturn('id');
$updated = $this->createMock(ContentEntityTypeInterface::class);
$updated->expects($this->any())
->method('id')
->willReturn('entity_test');
$updated->expects($this->any())
->method('getKey')
->willReturn('id');
if ($method) {
$original->expects($this->once())
->method($method)
->willReturn(TRUE);
$updated->expects($this->once())
->method($method)
->willReturn(FALSE);
}
// Storage class changes should not impact this at all, and should not be
// checked.
$original->expects($this->never())
->method('getStorageClass');
$updated->expects($this->never())
->method('getStorageClass');
$this->entityType = new ContentEntityType([ $this->entityType = new ContentEntityType([
'id' => 'entity_test', 'id' => 'entity_test',