You are here

function SelectTest::testSimpleSelectExpression in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectTest.php \Drupal\system\Tests\Database\SelectTest::testSimpleSelectExpression()

Tests SELECT statements with expressions.

File

core/modules/system/src/Tests/Database/SelectTest.php, line 127
Contains \Drupal\system\Tests\Database\SelectTest.

Class

SelectTest
Tests the Select query builder.

Namespace

Drupal\system\Tests\Database

Code

function testSimpleSelectExpression() {
  $query = db_select('test');
  $name_field = $query
    ->addField('test', 'name');
  $age_field = $query
    ->addExpression("age*2", 'double_age');
  $query
    ->condition('age', 27);
  $result = $query
    ->execute();

  // Check that the aliases are being created the way we want.
  $this
    ->assertEqual($name_field, 'name', 'Name field alias is correct.');
  $this
    ->assertEqual($age_field, 'double_age', 'Age field alias is correct.');

  // Ensure that we got the right record.
  $record = $result
    ->fetch();
  $this
    ->assertEqual($record->{$name_field}, 'George', 'Fetched name is correct.');
  $this
    ->assertEqual($record->{$age_field}, 27 * 2, 'Fetched age expression is correct.');
}