You are here

public static function EntityUpdateTestHelper::checkFieldList in Entity Update 2.0.x

Same name and namespace in other branches
  1. 8 modules/entity_update_tests/src/EntityUpdateTestHelper.php \Drupal\entity_update_tests\EntityUpdateTestHelper::checkFieldList()

Check a database table fields list and match with privided list.

Return value

bool|array TRUE if has correct fields list. FALSE if exception. Array if different.

4 calls to EntityUpdateTestHelper::checkFieldList()
EntityUpdateFunctionsTest::testEntityUpdateAll in tests/src/Functional/EntityUpdateFunctionsTest.php
Entity update function : all.
EntityUpdateFunctionsTest::testEntityUpdateBasic in tests/src/Functional/EntityUpdateFunctionsTest.php
Entity update function : basic.
EntityUpdateFunctionsTest::testEntityUpdateClean in tests/src/Functional/EntityUpdateFunctionsTest.php
Entity update function : clean.
EntityUpdateFunctionsTest::testEntityUpdateSel in tests/src/Functional/EntityUpdateFunctionsTest.php
Update a selected entity type.

File

tests/modules/entity_update_tests/src/EntityUpdateTestHelper.php, line 76

Class

EntityUpdateTestHelper
EntityUpdateTest Helper functions.

Namespace

Drupal\entity_update_tests

Code

public static function checkFieldList($table, array $fields) {

  // Combine fields.
  $fields_must = array_combine($fields, $fields);

  // Current database fields list.
  $fields_curr = [];

  // Get Database connection.
  $con = Database::getConnection();

  // Add table prefix.
  if ($table_prefix = $con
    ->tablePrefix()) {
    $table = $table_prefix . $table;
  }
  try {
    $fields_list = $con
      ->query("DESCRIBE `{$table}`")
      ->fetchAll();
    foreach ($fields_list as $field) {
      $fields_curr[$field->Field] = $field->Field;
    }

    // Check differents.
    $array_diff = array_diff($fields_must, $fields_curr);
    if (empty($array_diff)) {
      $array_diff = array_diff($fields_curr, $fields_must);

      // Tables matched.
      if (empty($array_diff)) {
        return TRUE;
      }
    }

    // Two tables are different.
    return $array_diff;
  } catch (\Exception $ex) {
    return FALSE;
  }
  return FALSE;
}