function InstallerRedirectTraitTest::testShouldRedirectToInstaller

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php \Drupal\KernelTests\Core\Installer\InstallerRedirectTraitTest::testShouldRedirectToInstaller()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php \Drupal\KernelTests\Core\Installer\InstallerRedirectTraitTest::testShouldRedirectToInstaller()
  3. 10 core/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php \Drupal\KernelTests\Core\Installer\InstallerRedirectTraitTest::testShouldRedirectToInstaller()

@covers ::shouldRedirectToInstaller
@dataProvider providerShouldRedirectToInstaller

File

core/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php, line 70

Class

InstallerRedirectTraitTest
@coversDefaultClass \Drupal\Core\Installer\InstallerRedirectTrait[[api-linebreak]]

Namespace

Drupal\KernelTests\Core\Installer

Code

public function testShouldRedirectToInstaller(bool $expected, string $exception, bool $connection, bool $connection_info, bool $sequences_table_exists = TRUE) : void {
  // Mock the trait.
  $trait = $this->getMockBuilder(InstallerRedirectTraitMockableClass::class)
    ->onlyMethods([
    'isCli',
  ])
    ->getMock();
  // Make sure that the method thinks we are not using the cli.
  $trait->expects($this->any())
    ->method('isCli')
    ->willReturn(FALSE);
  // If testing no connection info, we need to make the 'default' key not
  // visible.
  if (!$connection_info) {
    Database::renameConnection('default', __METHOD__);
  }
  if ($connection) {
    // Mock the database connection.
    $connection = $this->getMockBuilder(StubConnection::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'schema',
    ])
      ->getMock();
    if ($connection_info) {
      // Mock the database schema class.
      $schema = $this->getMockBuilder(StubSchema::class)
        ->disableOriginalConstructor()
        ->onlyMethods([
        'tableExists',
      ])
        ->getMock();
      $schema->expects($this->any())
        ->method('tableExists')
        ->with('sequences')
        ->willReturn($sequences_table_exists);
      $connection->expects($this->any())
        ->method('schema')
        ->willReturn($schema);
    }
  }
  else {
    // Set the database connection if there is none.
    $connection = NULL;
  }
  try {
    throw new $exception();
  } catch (\Exception $e) {
    // Call shouldRedirectToInstaller.
    $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
    $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));
  }
  if (!$connection_info) {
    Database::renameConnection(__METHOD__, 'default');
  }
}

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