You are here

public function EntityApiTest::testEntityStorageExceptionHandling in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityApiTest.php \Drupal\system\Tests\Entity\EntityApiTest::testEntityStorageExceptionHandling()

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

File

core/modules/system/src/Tests/Entity/EntityApiTest.php, line 128
Contains \Drupal\system\Tests\Entity\EntityApiTest.

Class

EntityApiTest
Tests basic CRUD functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testEntityStorageExceptionHandling() {
  $entity = entity_create('entity_test', array(
    '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 = entity_create('entity_test', array(
    'name' => 'test2',
  ));
  try {
    unset($GLOBALS['entity_test_throw_exception']);
    $entity
      ->save();
    $this
      ->pass('Exception presave not thrown and not caught.');
  } catch (EntityStorageException $e) {
    $this
      ->assertNotEqual($e
      ->getCode(), 1, 'Entity presave EntityStorageException caught.');
  }
  $entity = entity_create('entity_test', array(
    '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 = entity_create('entity_test', array(
    'name' => 'test4',
  ));
  $entity
    ->save();
  try {
    $entity
      ->delete();
    $this
      ->pass('Entity predelete EntityStorageException not thrown and not caught.');
  } catch (EntityStorageException $e) {
    $this
      ->assertNotEqual($e
      ->getCode(), 2, 'Entity predelete EntityStorageException thrown.');
  }
}