function FetchTest::testQueryFetchFieldEdgeCases

Tests ::fetchField() for edge values returned.

File

core/tests/Drupal/KernelTests/Core/Database/FetchTest.php, line 236

Class

FetchTest
Tests the Database system's various fetch capabilities.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testQueryFetchFieldEdgeCases() : void {
    $this->connection
        ->insert('test_null')
        ->fields([
        'name' => 'Foo',
        'age' => 0,
    ])
        ->execute();
    $this->connection
        ->insert('test_null')
        ->fields([
        'name' => 'Bar',
        'age' => NULL,
    ])
        ->execute();
    $this->connection
        ->insert('test_null')
        ->fields([
        'name' => 'Qux',
        'age' => (int) FALSE,
    ])
        ->execute();
    $statement = $this->connection
        ->select('test_null')
        ->fields('test_null', [
        'age',
    ])
        ->orderBy('id')
        ->execute();
    // First fetch returns '0' since an existing value is always a string.
    $this->assertSame('0', $statement->fetchField());
    // Second fetch returns NULL since NULL was inserted.
    $this->assertNull($statement->fetchField());
    // Third fetch returns '0' since a FALSE bool cast to int was inserted.
    $this->assertSame('0', $statement->fetchField());
    // Fourth fetch returns FALSE since no row was available.
    $this->assertFalse($statement->fetchField());
}

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