function EntityApiTest::testEntityStorageExceptionHandling

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::testEntityStorageExceptionHandling()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::testEntityStorageExceptionHandling()
  3. 10 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 207

Class

EntityApiTest
Tests basic CRUD functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityStorageExceptionHandling() : void {
    $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.');
    }
}

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