You are here

function DatabaseInsertTestCase::testInsertSelect in SimpleTest 7

Test that the INSERT INTO ... SELECT ... syntax works.

File

tests/database_test.test, line 533

Class

DatabaseInsertTestCase
Test the insert builder.

Code

function testInsertSelect() {
  $query = db_select('test_people', 'tp');

  // The query builder will always append expressions after fields.
  // Add the expression first to test that the insert fields are correctly
  // re-ordered.
  $query
    ->addExpression('tp.age', 'age');
  $query
    ->fields('tp', array(
    'name',
    'job',
  ))
    ->condition('tp.name', 'Meredith');

  // The resulting query should be equivalent to:
  // INSERT INTO test (age, name, job)
  // SELECT tp.age AS age, tp.name AS name, tp.job AS job
  // FROM test_people tp
  // WHERE tp.name = 'Meredith'
  db_insert('test')
    ->from($query)
    ->execute();
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Meredith',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '30', t('Can retrieve after inserting.'));
}