function EntityTypeTest::testIsSerializable

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTypeTest.php \Drupal\KernelTests\Core\Entity\EntityTypeTest::testIsSerializable()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTypeTest.php \Drupal\KernelTests\Core\Entity\EntityTypeTest::testIsSerializable()
  3. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php \Drupal\Tests\Core\Entity\EntityTypeTest::testIsSerializable()
  4. main core/tests/Drupal/KernelTests/Core/Entity/EntityTypeTest.php \Drupal\KernelTests\Core\Entity\EntityTypeTest::testIsSerializable()

Tests that the EntityType object can be serialized.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityTypeTest.php, line 39

Class

EntityTypeTest
Tests general features of entity types.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testIsSerializable() : void {
  $entity_type = $this->setUpEntityType([]);
  $translation_service = new class  extends TranslationManager {
    
    /**
     * Constructs a UnserializableTranslationManager object.
     */
    public function __construct() {
    }
    
    /**
     * Always throw an exception.
     */
    public function __serialize() : array {
      throw new \Exception();
    }

};
  $this->container
    ->set('bar', $translation_service);
  $entity_type->setStringTranslation($this->container
    ->get('string_translation'));
  // This should not throw an exception.
  $tmp = serialize($entity_type);
  $entity_type = unserialize($tmp);
  // And this should have the correct id.
  $this->assertEquals('example_entity_type', $entity_type->id());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.