function EntityApiTest::testEntityStorageExceptionHandling

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

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->assertEqual($e->getcode(), 1, 'Entity presave EntityStorageException caught.');
    }
    $entity = EntityTest::create([
        'name' => 'test2',
    ]);
    try {
        unset($GLOBALS['entity_test_throw_exception']);
        $entity->save();
    } catch (EntityStorageException $e) {
        $this->assertNotEqual($e->getCode(), 1, '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->assertEqual($e->getCode(), 2, 'Entity predelete EntityStorageException caught.');
    }
    unset($GLOBALS['entity_test_throw_exception']);
    $entity = EntityTest::create([
        'name' => 'test4',
    ]);
    $entity->save();
    try {
        $entity->delete();
    } catch (EntityStorageException $e) {
        $this->assertNotEqual($e->getCode(), 2, 'Entity predelete EntityStorageException thrown.');
    }
}

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