You are here

public function EntityApiTest::testEntityStorageExceptionHandling in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::testEntityStorageExceptionHandling()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::testEntityStorageExceptionHandling()

Tests that exceptions are thrown when saving or deleting an entity.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php, line 206

Class

EntityApiTest
Tests basic CRUD functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityStorageExceptionHandling() {
  $entity = EntityTest::create([
    'name' => 'test',
  ]);
  try {
    $GLOBALS['entity_test_throw_exception'] = TRUE;
    $entity
      ->save();
    $this
      ->fail('Entity presave EntityStorageException thrown but not caught.');
  } catch (EntityStorageException $e) {
    $this
      ->assertEquals(1, $e
      ->getcode(), 'Entity presave EntityStorageException caught.');
  }
  $entity = EntityTest::create([
    'name' => 'test2',
  ]);
  try {
    unset($GLOBALS['entity_test_throw_exception']);
    $entity
      ->save();
  } catch (EntityStorageException $e) {
    $this
      ->assertNotEquals(1, $e
      ->getCode(), 'Entity presave EntityStorageException caught.');
  }
  $entity = EntityTest::create([
    'name' => 'test3',
  ]);
  $entity
    ->save();
  try {
    $GLOBALS['entity_test_throw_exception'] = TRUE;
    $entity
      ->delete();
    $this
      ->fail('Entity predelete EntityStorageException not thrown.');
  } catch (EntityStorageException $e) {
    $this
      ->assertEquals(2, $e
      ->getCode(), 'Entity predelete EntityStorageException caught.');
  }
  unset($GLOBALS['entity_test_throw_exception']);
  $entity = EntityTest::create([
    'name' => 'test4',
  ]);
  $entity
    ->save();
  try {
    $entity
      ->delete();
  } catch (EntityStorageException $e) {
    $this
      ->assertNotEquals(2, $e
      ->getCode(), 'Entity predelete EntityStorageException thrown.');
  }
}