DatabaseTestBase.php 2.98 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Database;
4

5
use Drupal\KernelTests\KernelTestBase;
6 7

/**
8
 * Base class for databases database tests.
9 10 11 12
 *
 * Because all database tests share the same test data, we can centralize that
 * here.
 */
13
abstract class DatabaseTestBase extends KernelTestBase {
14

15 16
  public static $modules = array('database_test');

17
  protected function setUp() {
18
    parent::setUp();
19 20 21
    $this->installSchema('database_test', array(
      'test',
      'test_people',
22
      'test_people_copy',
23 24 25 26 27
      'test_one_blob',
      'test_two_blobs',
      'test_task',
      'test_null',
      'test_serialized',
28
      'test_special_columns',
29
      'TEST_UPPERCASE',
30
    ));
31
    self::addSampleData();
32 33 34
  }

  /**
35
   * Sets up tables for NULL handling.
36 37 38
   */
  function ensureSampleDataNull() {
    db_insert('test_null')
39 40
      ->fields(array('name', 'age'))
      ->values(array(
41 42 43
      'name' => 'Kermit',
      'age' => 25,
    ))
44
      ->values(array(
45 46 47
      'name' => 'Fozzie',
      'age' => NULL,
    ))
48
      ->values(array(
49 50 51
      'name' => 'Gonzo',
      'age' => 27,
    ))
52
      ->execute();
53 54 55
  }

  /**
56
   * Sets up our sample data.
57
   */
58
  static function addSampleData() {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    // We need the IDs, so we can't use a multi-insert here.
    $john = db_insert('test')
      ->fields(array(
        'name' => 'John',
        'age' => 25,
        'job' => 'Singer',
      ))
      ->execute();

    $george = db_insert('test')
      ->fields(array(
        'name' => 'George',
        'age' => 27,
        'job' => 'Singer',
      ))
      ->execute();

76
    db_insert('test')
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      ->fields(array(
        'name' => 'Ringo',
        'age' => 28,
        'job' => 'Drummer',
      ))
      ->execute();

    $paul = db_insert('test')
      ->fields(array(
        'name' => 'Paul',
        'age' => 26,
        'job' => 'Songwriter',
      ))
      ->execute();

    db_insert('test_people')
      ->fields(array(
        'name' => 'Meredith',
        'age' => 30,
        'job' => 'Speaker',
      ))
      ->execute();

    db_insert('test_task')
      ->fields(array('pid', 'task', 'priority'))
      ->values(array(
        'pid' => $john,
        'task' => 'eat',
        'priority' => 3,
      ))
      ->values(array(
        'pid' => $john,
        'task' => 'sleep',
        'priority' => 4,
      ))
      ->values(array(
        'pid' => $john,
        'task' => 'code',
        'priority' => 1,
      ))
      ->values(array(
        'pid' => $george,
        'task' => 'sing',
        'priority' => 2,
      ))
      ->values(array(
        'pid' => $george,
        'task' => 'sleep',
        'priority' => 2,
      ))
      ->values(array(
        'pid' => $paul,
        'task' => 'found new band',
        'priority' => 1,
      ))
      ->values(array(
        'pid' => $paul,
        'task' => 'perform at superbowl',
        'priority' => 3,
      ))
      ->execute();
138 139 140 141 142 143 144

    db_insert('test_special_columns')
      ->fields(array(
        'id' => 1,
        'offset' => 'Offset value 1',
      ))
      ->execute();
145
  }
146

147
}