ConnectionTest.php

Same filename in this branch
  1. 11.x core/modules/sqlite/tests/src/Unit/ConnectionTest.php
  2. 11.x core/modules/mysql/tests/src/Unit/ConnectionTest.php
  3. 11.x core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php
  4. 11.x core/modules/mysqli/tests/src/Kernel/mysqli/ConnectionTest.php
  5. 11.x core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
  6. 11.x core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
  7. 11.x core/modules/pgsql/tests/src/Unit/ConnectionTest.php
Same filename and directory in other branches
  1. 10 core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php
  2. 8.9.x core/tests/Drupal/Tests/Core/Database/Driver/sqlite/ConnectionTest.php
  3. 10 core/modules/sqlite/tests/src/Unit/ConnectionTest.php
  4. 10 core/modules/mysql/tests/src/Unit/ConnectionTest.php
  5. 10 core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php
  6. 10 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
  7. 10 core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
  8. 9 core/modules/sqlite/tests/src/Unit/ConnectionTest.php
  9. 9 core/modules/mysql/tests/src/Unit/ConnectionTest.php
  10. 9 core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php
  11. 9 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
  12. 9 core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
  13. 8.9.x core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
  14. 8.9.x core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
  15. main core/modules/sqlite/tests/src/Unit/ConnectionTest.php
  16. main core/modules/mysql/tests/src/Unit/ConnectionTest.php
  17. main core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php
  18. main core/modules/mysqli/tests/src/Kernel/mysqli/ConnectionTest.php
  19. main core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php
  20. main core/tests/Drupal/Tests/Core/Database/ConnectionTest.php

Namespace

Drupal\Tests\pgsql\Kernel\pgsql

File

core/modules/pgsql/tests/src/Kernel/pgsql/ConnectionTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\pgsql\Kernel\pgsql;

use Drupal\Core\Database\Database;
use Drupal\KernelTests\Core\Database\DriverSpecificKernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
// cspell:ignore héllo wörld PGCLIENTENCODING libpq

/**
 * PostgreSQL-specific connection tests.
 */
class ConnectionTest extends DriverSpecificKernelTestBase {
  
  /**
   * Tests the client encoding is set to UTF8 when the connection is opened.
   *
   * The client encoding is set via the DSN in
   * \Drupal\pgsql\Driver\Database\pgsql\Connection::open(), so it is part of
   * the connection startup packet and no "SET NAMES" query is needed.
   */
  public function testClientEncoding() : void {
    $this->assertSame('UTF8', $this->connection
      ->query('SHOW client_encoding')
      ->fetchField());
    // Ensure multi-byte UTF-8 data round trips intact.
    $string = 'héllo wörld ✓ 漢字';
    $this->assertSame($string, $this->connection
      ->query('SELECT :string::text', [
      ':string' => $string,
    ])
      ->fetchField());
    // The client encoding in the DSN must take precedence over the
    // PGCLIENTENCODING environment variable, which libpq would otherwise use
    // as the default.
    putenv('PGCLIENTENCODING=LATIN1');
    try {
      Database::addConnectionInfo('encoding_test', 'default', Database::getConnectionInfo()['default']);
      $connection = Database::getConnection('default', 'encoding_test');
      $this->assertSame('UTF8', $connection->query('SHOW client_encoding')
        ->fetchField());
    } finally {
      putenv('PGCLIENTENCODING');
      Database::closeConnection('default', 'encoding_test');
    }
  }

}

Classes

Title Deprecated Summary
ConnectionTest PostgreSQL-specific connection tests.

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