function MergeTest::testInvalidMerge

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testInvalidMerge()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testInvalidMerge()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/MergeTest.php \Drupal\KernelTests\Core\Database\MergeTest::testInvalidMerge()

Tests that an invalid merge query throws an exception.

File

core/tests/Drupal/KernelTests/Core/Database/MergeTest.php, line 198

Class

MergeTest
Tests the MERGE query builder.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testInvalidMerge() {
    try {
        // This query will fail because there is no key field specified.
        // Normally it would throw an exception but we are suppressing it with
        // the throw_exception option.
        $options['throw_exception'] = FALSE;
        $this->connection
            ->merge('test_people', $options)
            ->fields([
            'age' => 31,
            'name' => 'Tiffany',
        ])
            ->execute();
    } catch (InvalidMergeQueryException $e) {
        $this->fail('$options[\'throw_exception\'] is FALSE, but InvalidMergeQueryException thrown for invalid query.');
    }
    try {
        // This query will fail because there is no key field specified.
        $this->connection
            ->merge('test_people')
            ->fields([
            'age' => 31,
            'name' => 'Tiffany',
        ])
            ->execute();
        $this->fail('InvalidMergeQueryException should be thrown.');
    } catch (\Exception $e) {
        $this->assertInstanceOf(InvalidMergeQueryException::class, $e);
    }
}

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