public function EntityApiTest::testEntityStorageExceptionHandling in Drupal 9
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php \Drupal\KernelTests\Core\Entity\EntityApiTest::testEntityStorageExceptionHandling()
- 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 204
Class
- EntityApiTest
- Tests basic CRUD functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
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.');
}
}