DatabaseTestBase.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Base class for databases database tests.
  7. *
  8. * Because all database tests share the same test data, we can centralize that
  9. * here.
  10. */
  11. abstract class DatabaseTestBase extends KernelTestBase {
  12. public static $modules = ['database_test'];
  13. /**
  14. * The database connection for testing.
  15. *
  16. * @var \Drupal\Core\Database\Connection
  17. */
  18. protected $connection;
  19. protected function setUp() {
  20. parent::setUp();
  21. $this->connection = Database::getConnection();
  22. $this->installSchema('database_test', [
  23. 'test',
  24. 'test_classtype',
  25. 'test_people',
  26. 'test_people_copy',
  27. 'test_one_blob',
  28. 'test_two_blobs',
  29. 'test_task',
  30. 'test_null',
  31. 'test_serialized',
  32. 'test_special_columns',
  33. 'TEST_UPPERCASE',
  34. 'virtual',
  35. ]);
  36. self::addSampleData();
  37. }
  38. /**
  39. * Sets up tables for NULL handling.
  40. */
  41. public function ensureSampleDataNull() {
  42. $this->connection->insert('test_null')
  43. ->fields(['name', 'age'])
  44. ->values([
  45. 'name' => 'Kermit',
  46. 'age' => 25,
  47. ])
  48. ->values([
  49. 'name' => 'Fozzie',
  50. 'age' => NULL,
  51. ])
  52. ->values([
  53. 'name' => 'Gonzo',
  54. 'age' => 27,
  55. ])
  56. ->execute();
  57. }
  58. /**
  59. * Sets up our sample data.
  60. */
  61. public static function addSampleData() {
  62. $connection = Database::getConnection();
  63. // We need the IDs, so we can't use a multi-insert here.
  64. $john = $connection->insert('test')
  65. ->fields([
  66. 'name' => 'John',
  67. 'age' => 25,
  68. 'job' => 'Singer',
  69. ])
  70. ->execute();
  71. $george = $connection->insert('test')
  72. ->fields([
  73. 'name' => 'George',
  74. 'age' => 27,
  75. 'job' => 'Singer',
  76. ])
  77. ->execute();
  78. $connection->insert('test')
  79. ->fields([
  80. 'name' => 'Ringo',
  81. 'age' => 28,
  82. 'job' => 'Drummer',
  83. ])
  84. ->execute();
  85. $paul = $connection->insert('test')
  86. ->fields([
  87. 'name' => 'Paul',
  88. 'age' => 26,
  89. 'job' => 'Songwriter',
  90. ])
  91. ->execute();
  92. $connection->insert('test_classtype')
  93. ->fields([
  94. 'classname' => 'Drupal\Tests\system\Functional\Database\FakeRecord',
  95. 'name' => 'Kay',
  96. 'age' => 26,
  97. 'job' => 'Web Developer',
  98. ])
  99. ->execute();
  100. $connection->insert('test_people')
  101. ->fields([
  102. 'name' => 'Meredith',
  103. 'age' => 30,
  104. 'job' => 'Speaker',
  105. ])
  106. ->execute();
  107. $connection->insert('test_task')
  108. ->fields(['pid', 'task', 'priority'])
  109. ->values([
  110. 'pid' => $john,
  111. 'task' => 'eat',
  112. 'priority' => 3,
  113. ])
  114. ->values([
  115. 'pid' => $john,
  116. 'task' => 'sleep',
  117. 'priority' => 4,
  118. ])
  119. ->values([
  120. 'pid' => $john,
  121. 'task' => 'code',
  122. 'priority' => 1,
  123. ])
  124. ->values([
  125. 'pid' => $george,
  126. 'task' => 'sing',
  127. 'priority' => 2,
  128. ])
  129. ->values([
  130. 'pid' => $george,
  131. 'task' => 'sleep',
  132. 'priority' => 2,
  133. ])
  134. ->values([
  135. 'pid' => $paul,
  136. 'task' => 'found new band',
  137. 'priority' => 1,
  138. ])
  139. ->values([
  140. 'pid' => $paul,
  141. 'task' => 'perform at superbowl',
  142. 'priority' => 3,
  143. ])
  144. ->execute();
  145. $connection->insert('test_special_columns')
  146. ->fields([
  147. 'id' => 1,
  148. 'offset' => 'Offset value 1',
  149. 'function' => 'Function value 1',
  150. ])
  151. ->execute();
  152. $connection->insert('virtual')
  153. ->fields([
  154. 'id' => 1,
  155. 'function' => 'Function value 1',
  156. ])
  157. ->execute();
  158. }
  159. }