trait DatabaseTestSchemaDataTrait

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/DatabaseTestSchemaDataTrait.php \Drupal\KernelTests\Core\Database\DatabaseTestSchemaDataTrait
  2. 11.x core/tests/Drupal/KernelTests/Core/Database/DatabaseTestSchemaDataTrait.php \Drupal\KernelTests\Core\Database\DatabaseTestSchemaDataTrait

Trait to manage data samples for test tables.

Hierarchy

2 files declare their use of DatabaseTestSchemaDataTrait
DatabaseTestBase.php in core/modules/system/tests/src/Functional/Database/DatabaseTestBase.php
NonPublicSchemaTest.php in core/modules/pgsql/tests/src/Kernel/pgsql/NonPublicSchemaTest.php

File

core/tests/Drupal/KernelTests/Core/Database/DatabaseTestSchemaDataTrait.php, line 12

Namespace

Drupal\KernelTests\Core\Database
View source
trait DatabaseTestSchemaDataTrait {
    
    /**
     * Sets up our sample data.
     */
    protected function addSampleData() : void {
        // We need the IDs, so we can't use a multi-insert here.
        $john = $this->connection
            ->insert('test')
            ->fields([
            'name' => 'John',
            'age' => 25,
            'job' => 'Singer',
        ])
            ->execute();
        $george = $this->connection
            ->insert('test')
            ->fields([
            'name' => 'George',
            'age' => 27,
            'job' => 'Singer',
        ])
            ->execute();
        $this->connection
            ->insert('test')
            ->fields([
            'name' => 'Ringo',
            'age' => 28,
            'job' => 'Drummer',
        ])
            ->execute();
        $paul = $this->connection
            ->insert('test')
            ->fields([
            'name' => 'Paul',
            'age' => 26,
            'job' => 'Songwriter',
        ])
            ->execute();
        $this->connection
            ->insert('test_classtype')
            ->fields([
            'classname' => FakeRecord::class,
            'name' => 'Kay',
            'age' => 26,
            'job' => 'Web Developer',
        ])
            ->execute();
        $this->connection
            ->insert('test_people')
            ->fields([
            'name' => 'Meredith',
            'age' => 30,
            'job' => 'Speaker',
        ])
            ->execute();
        $this->connection
            ->insert('test_task')
            ->fields([
            'pid',
            'task',
            'priority',
        ])
            ->values([
            'pid' => $john,
            'task' => 'eat',
            'priority' => 3,
        ])
            ->values([
            'pid' => $john,
            'task' => 'sleep',
            'priority' => 4,
        ])
            ->values([
            'pid' => $john,
            'task' => 'code',
            'priority' => 1,
        ])
            ->values([
            'pid' => $george,
            'task' => 'sing',
            'priority' => 2,
        ])
            ->values([
            'pid' => $george,
            'task' => 'sleep',
            'priority' => 2,
        ])
            ->values([
            'pid' => $paul,
            'task' => 'found new band',
            'priority' => 1,
        ])
            ->values([
            'pid' => $paul,
            'task' => 'perform at superbowl',
            'priority' => 3,
        ])
            ->execute();
        $this->connection
            ->insert('select')
            ->fields([
            'id' => 1,
            'update' => 'Update value 1',
        ])
            ->execute();
        $this->connection
            ->insert('virtual')
            ->fields([
            'id' => 1,
            'function' => 'Function value 1',
        ])
            ->execute();
    }

}

Members

Title Sort descending Modifiers Object type Summary
DatabaseTestSchemaDataTrait::addSampleData protected function Sets up our sample data.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.