Commit fa2a7a56 authored by alexpott's avatar alexpott

Issue #2499835 by phenaproxima, chx, benjy: Remove broken Fake DB driver

parent 68e606c3
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\ConditionResolver.
*/
namespace Drupal\Core\Database\Driver\fake;
use Drupal\Core\Database\Query\Condition;
class ConditionResolver {
/**
* Match a row against a group of conditions.
*
* @param \Drupal\Core\Database\Driver\fake\DatabaseRowInterface $row
* The database row object.
* @param \Drupal\Core\Database\Query\Condition $condition_group
* The condition group object.
*
* @return bool
* TRUE if there is a match.
*/
public static function matchGroup(DatabaseRowInterface $row, Condition $condition_group) {
$conditions = $condition_group->conditions();
$and = $conditions['#conjunction'] == 'AND';
unset($conditions['#conjunction']);
$match = TRUE;
foreach ($conditions as $condition) {
$match = $condition['field'] instanceof Condition ? static::matchGroup($row, $condition['field']) : static::matchSingle($row, $condition);
// For AND, finish matching on the first fail. For OR, finish on first
// success.
if ($and != $match) {
break;
}
}
return $match;
}
/**
* Match a single row and its condition.
*
* @param \Drupal\migrate\tests\DatabaseRowInterface $row
* The row to match.
*
* @param array $condition
* An array representing a single condition.
*
* @return bool
* TRUE if the condition matches.
*
* @throws \Exception
*/
protected static function matchSingle(DatabaseRowInterface $row, array $condition) {
$row_value = $row->getValue($condition['field']);
switch ($condition['operator']) {
case '=':
return $row_value == $condition['value'];
case '<=':
return $row_value <= $condition['value'];
case '>=':
return $row_value >= $condition['value'];
case '!=':
return $row_value != $condition['value'];
case '<>':
return $row_value != $condition['value'];
case '<':
return $row_value < $condition['value'];
case '>':
return $row_value > $condition['value'];
case 'IN':
return in_array($row_value, $condition['value']);
case 'IS NULL':
return !isset($row_value);
case 'IS NOT NULL':
return isset($row_value);
default:
throw new \Exception(sprintf('operator %s is not supported', $condition['operator']));
}
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\DatabaseRow.
*/
namespace Drupal\Core\Database\Driver\fake;
class DatabaseRow implements DatabaseRowInterface {
/**
* Construct a new row.
*
* @param array $row
* The row data.
*/
public function __construct(array $row) {
$this->row = $row;
}
/**
* {@inheritdoc}
*/
public function getValue($field) {
return $this->row[$field];
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\DatabaseRowInterface.
*/
namespace Drupal\Core\Database\Driver\fake;
interface DatabaseRowInterface {
/**
* Get the field value from the row.
*
* @param mixed $field
* The field to get the value of.
*
* @return mixed
* The field value from the row.
*/
public function getValue($field);
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\DatabaseRowSelect.
*/
namespace Drupal\Core\Database\Driver\fake;
class DatabaseRowSelect extends DatabaseRow {
/**
* Construct a new database row.
*
* @param array $row
* The row data.
* @param array $fields_with_table
* The fields with a table.
* @param array $fields
* The fields.
*/
public function __construct(array $row, array $fields_with_table, array $fields) {
$this->fieldsWithTable = $fields_with_table;
$this->fields = $fields;
parent::__construct($row);
}
/**
* {@inheritdoc}
*/
public function getValue($field) {
$field_info = isset($this->fieldsWithTable[$field]) ? $this->fieldsWithTable[$field] : $this->fields[$field];
if (array_key_exists($field_info['field'], $this->row[$field_info['table']]['result'])) {
$index = 'result';
}
else {
$index = 'all';
}
return $this->row[$field_info['table']][$index][$field_info['field']];
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\FakeConnection.
*/
namespace Drupal\Core\Database\Driver\fake;
use Drupal\Core\Database\Connection;
/**
* Defines a fake connection for use during testing.
*/
class FakeConnection extends Connection {
/**
* Table prefix on the database.
*
* @var string
*/
protected $tablePrefix;
/**
* Connection options for the database.
*
* @var array
*/
protected $connectionOptions;
/**
* Constructs a FakeConnection.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows.
* @param array $connection_options
* (optional) The array of connection options for the database.
* @param string $prefix
* (optional) The table prefix on the database.
*/
public function __construct(array $database_contents, $connection_options = array(), $prefix = '') {
$this->databaseContents = $database_contents;
$this->connectionOptions = $connection_options;
$this->tablePrefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function getConnectionOptions() {
return $this->connectionOptions;
}
/**
* {@inheritdoc}
*/
public function tablePrefix($table = 'default') {
return $this->tablePrefix;
}
/**
* {@inheritdoc}
*/
public function select($table, $alias = NULL, array $options = array()) {
return new FakeSelect($this->databaseContents, $table, $alias);
}
/**
* {@inheritdoc}
*/
public function insert($table, array $options = array()) {
return new FakeInsert($this->databaseContents, $table);
}
/**
* {@inheritdoc}
*/
public function merge($table, array $options = array()) {
return new FakeMerge($this->databaseContents, $table);
}
/**
* {@inheritdoc}
*/
public function update($table, array $options = array()) {
return new FakeUpdate($this->databaseContents, $table);
}
/**
* {@inheritdoc}
*/
public function delete($table, array $options = array()) {
return new FakeDelete($this->databaseContents, $table);
}
/**
* {@inheritdoc}
*/
public function truncate($table, array $options = array()) {
return new FakeTruncate($this->databaseContents, $table);
}
/**
* {@inheritdoc}
*/
public function schema() {
return new FakeDatabaseSchema($this->databaseContents);
}
/**
* {@inheritdoc}
*/
public function query($query, array $args = array(), $options = array()) {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function queryTemporary($query, array $args = array(), array $options = array()) {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function driver() {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function databaseType() {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function createDatabase($database) {
// There is nothing to do.
}
/**
* {@inheritdoc}
*/
public function mapConditionOperator($operator) {
throw new \Exception('Method not supported');
}
/**
* {@inheritdoc}
*/
public function nextId($existing_id = 0) {
throw new \Exception('Method not supported');
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\FakeDatabaseSchema.
*/
namespace Drupal\Core\Database\Driver\fake;
use Drupal\Core\Database\Schema;
class FakeDatabaseSchema extends Schema {
/**
* As set on MigrateSqlSourceTestCase::databaseContents.
*
* @var array
*/
public $databaseContents;
/**
* Constructs a fake database schema.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows.
*/
public function __construct(array &$database_contents) {
$this->uniqueIdentifier = uniqid('', TRUE);
// @todo Maybe we can generate an internal representation.
$this->databaseContents = &$database_contents;
}
/**
* {@inheritdoc}
*/
public function tableExists($table) {
return in_array($table, array_keys($this->databaseContents));
}
/**
* {@inheritdoc}
*/
public function prefixNonTable($table) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function addField($table, $field, $spec, $keys_new = array()) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function addIndex($table, $name, $fields) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function addPrimaryKey($table, $fields) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function addUniqueKey($table, $name, $fields) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function changeField($table, $field, $field_new, $spec, $keys_new = array()) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* PHP magic __clone() method.
*/
public function __clone() {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function createTable($name, $table) {
#throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function dropField($table, $field) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function dropIndex($table, $name) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function dropPrimaryKey($table) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function dropTable($table) {
unset($this->databaseContents[$table]);
}
/**
* {@inheritdoc}
*/
public function dropUniqueKey($table, $name) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function fieldExists($table, $column) {
if (!empty($this->databaseContents[$table])) {
$row = reset($this->databaseContents[$table]);
return isset($row[$column]);
}
else {
throw new \Exception("Can't determine whether field exists with an empty / nonexistent table.");
}
}
/**
* {@inheritdoc}
*/
public function fieldNames($fields) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function fieldSetDefault($table, $field, $default) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function fieldSetNoDefault($table, $field) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function findTables($table_expression) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function getFieldTypeMap() {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function indexExists($table, $name) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function nextPlaceholder() {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function prepareComment($comment, $length = NULL) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function renameTable($table, $new_name) {
throw new \Exception(sprintf('Unsupported method "%s"', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function uniqueIdentifier() {
return $this->uniqueIdentifier;
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\FakeDelete.
*/
namespace Drupal\Core\Database\Driver\fake;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\Delete;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Defines FakeDelete for use in database tests.
*/
class FakeDelete extends Delete {
/**
* The database table to delete from.
*
* @var string
*/
protected $table;
/**
* The database contents.
*
* @var array
*/
protected $databaseContents;
/**
* Constructs a FakeDelete object.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows.
* @param string $table
* The table to delete from.
*/
public function __construct(array &$database_contents, $table) {
$this->databaseContents = &$database_contents;
$this->table = $table;
$this->condition = new Condition('AND');
}
/**
* {@inheritdoc}
*/
public function execute() {
$affected = 0;
if (isset($this->databaseContents[$this->table])) {
$original_row_count = count($this->databaseContents[$this->table]);
$condition = $this->condition;
$this->databaseContents[$this->table] = array_filter($this->databaseContents[$this->table], function ($row_array) use ($condition) {
$row = new DatabaseRow($row_array);
return !ConditionResolver::matchGroup($row, $condition);
});
$affected = $original_row_count - count($this->databaseContents[$this->table]);
}
return $affected;
}
/**
* {@inheritdoc}
*/
public function exists(SelectInterface $select) {
throw new \Exception(sprintf('Method "%s" is not supported', __METHOD__));
}
/**
* {@inheritdoc}
*/
public function where($snippet, $args = array()) {
throw new \Exception(sprintf('Method "%s" is not supported', __METHOD__));
}
}
<?php
/**
* @file
* Contains Drupal\Core\Database\Driver\fake\FakeInsert.
*/
namespace Drupal\Core\Database\Driver\fake;
use Drupal\Core\Database\Query\Insert;
use Drupal\Core\Database\Query\SelectInterface;
/**
* Defines FakeInsert for use in database tests.
*/
class FakeInsert extends Insert {
/**
* The database contents.
*
* @var array
*/
protected $databaseContents;
/**
* The database table to insert into.
*
* @var string
*/
protected $table;
/**
* Constructs a fake insert object.
*
* @param array $database_contents
* The database contents faked as an array. Each key is a table name, each
* value is a list of table rows.
* @param string $table