You are here

SQLiteDatabaseTrait.php in Drupal 7 to 8/9 Module Upgrader 8

File

tests/src/Unit/SQLiteDatabaseTrait.php
View source
<?php

namespace Drupal\Tests\drupalmoduleupgrader\Unit;

use Drupal\Core\Database\Driver\sqlite\Connection;

/**
 * A trait for tests that need a database.
 */
trait SQLiteDatabaseTrait {

  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $db;
  protected function initDB() {
    if (empty($this->db)) {

      // In-memory databases will cease to exist as soon as the connection
      // is closed, which is...convenient as hell!
      $db = new \PDO('sqlite::memory:');

      // Throw exceptions when things go awry.
      $db
        ->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
      $this->db = new Connection($db, []);
    }
  }

}

Traits

Namesort descending Description
SQLiteDatabaseTrait A trait for tests that need a database.