You are here

class SqlsrvConnectionTest in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 4.2.x tests/src/Unit/SqlsrvConnectionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConnectionTest
  2. 3.0.x tests/src/Unit/SqlsrvConnectionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConnectionTest
  3. 3.1.x tests/src/Unit/SqlsrvConnectionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConnectionTest
  4. 4.0.x tests/src/Unit/SqlsrvConnectionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConnectionTest
  5. 4.1.x tests/src/Unit/SqlsrvConnectionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConnectionTest

Test the behavior of the Connection class.

These tests are not expected to pass on other database drivers.

@group Database

Hierarchy

Expanded class hierarchy of SqlsrvConnectionTest

File

tests/src/Unit/SqlsrvConnectionTest.php, line 16

Namespace

Drupal\Tests\sqlsrv\Unit
View source
class SqlsrvConnectionTest extends UnitTestCase {

  /**
   * Mock PDO object for use in tests.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Tests\Core\Database\Stub\StubPDO
   */
  protected $mockPdo;

  /**
   * Mock Schema object for use in tests.
   *
   * @var \PHPUnit\Framework\MockObject\MockObject|\Drupal\Driver\Database\sqlsrv\Schema
   */
  protected $mockSchema;

  /**
   * Database connection options.
   *
   * The core test suite uses an empty array.
   * This module requires at least a value in:
   * $option['prefix']['default']
   *
   * @var array
   */
  protected $options;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->mockSchema = $this
      ->getMockBuilder('Drupal\\Driver\\Database\\sqlsrv\\Schema')
      ->setMethods([
      'getDefaultSchema',
      '__construct',
    ])
      ->setMockClassName('MockSchema')
      ->setConstructorArgs([
      NULL,
    ])
      ->disableOriginalConstructor()
      ->getMock();
    $this->mockSchema
      ->method('getDefaultSchema')
      ->willReturn('dbo');
    if (!class_exists('Drupal\\Driver\\Database\\mock\\Schema')) {
      class_alias('MockSchema', 'Drupal\\Driver\\Database\\mock\\Schema');
    }
    $this->options['namespace'] = 'Drupal\\Driver\\Database\\mock';
    $this->options['prefix']['default'] = '';
    $this->options['driver_settings'] = ConnectionSettings::instanceFromData([]);
    $this->mockPdo = $this
      ->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
  }

  /**
   * Data provider for testEscapeTable.
   *
   * @return array
   *   An indexed array of where each value is an array of arguments to pass to
   *   testEscapeField. The first value is the expected value, and the second
   *   value is the value to test.
   */
  public function providerEscapeTables() {
    return [
      [
        'nocase',
        'nocase',
      ],
    ];
  }

  /**
   * Data provider for testEscapeField.
   *
   * @return array
   *   Array of arrays with the following elements:
   *   - Expected escaped string.
   *   - String to escape.
   */
  public function providerEscapeFields() {
    return [
      [
        '[timestamp]',
        'timestamp',
      ],
    ];
  }

  /**
   * Test that tables are properly escaped.
   *
   * @dataProvider providerEscapeTables
   */
  public function testEscapeTable($expected, $name) {
    $connection = new Connection($this->mockPdo, $this->options);
    $this
      ->assertEquals($expected, $connection
      ->escapeTable($name));
  }

  /**
   * Test that fields are properly escaped.
   *
   * @dataProvider providerEscapeFields
   */
  public function testEscapeField($expected, $name) {
    $connection = new Connection($this->mockPdo, $this->options);
    $this
      ->assertEquals($expected, $connection
      ->escapeField($name));
  }

  /**
   * Test that the connection returns the correct driver string.
   */
  public function testDriverString() {
    $connection = new Connection($this->mockPdo, $this->options);
    $this
      ->assertEquals('sqlsrv', $connection
      ->driver());
  }

  /**
   * Test that the connection returns the correct database type string.
   */
  public function testDatabaseType() {
    $connection = new Connection($this->mockPdo, $this->options);
    $this
      ->assertEquals('sqlsrv', $connection
      ->databaseType());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SqlsrvConnectionTest::$mockPdo protected property Mock PDO object for use in tests.
SqlsrvConnectionTest::$mockSchema protected property Mock Schema object for use in tests.
SqlsrvConnectionTest::$options protected property Database connection options.
SqlsrvConnectionTest::providerEscapeFields public function Data provider for testEscapeField.
SqlsrvConnectionTest::providerEscapeTables public function Data provider for testEscapeTable.
SqlsrvConnectionTest::setUp protected function Overrides UnitTestCase::setUp
SqlsrvConnectionTest::testDatabaseType public function Test that the connection returns the correct database type string.
SqlsrvConnectionTest::testDriverString public function Test that the connection returns the correct driver string.
SqlsrvConnectionTest::testEscapeField public function Test that fields are properly escaped.
SqlsrvConnectionTest::testEscapeTable public function Test that tables are properly escaped.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.