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. 10 core/tests/Drupal/KernelTests/Core/Installer/InstallerRedirectTraitTest.php \Drupal\KernelTests\Core\Installer\InstallerRedirectTraitTest::testShouldRedirectToInstaller()
  3. 11.x 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 69

Class

InstallerRedirectTraitTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Installer%21InstallerRedirectTrait.php/trait/InstallerRedirectTrait/8.9.x" title="Provides methods for checking if Drupal is already installed." class="local">\Drupal\Core\Installer\InstallerRedirectTrait</a>

Namespace

Drupal\KernelTests\Core\Installer

Code

public function testShouldRedirectToInstaller($expected, $exception, $connection, $connection_info, $session_table_exists = TRUE) {
    try {
        throw new $exception();
    } catch (\Exception $e) {
        // Mock the trait.
        $trait = $this->getMockBuilder(InstallerRedirectTrait::class)
            ->setMethods([
            'isCli',
        ])
            ->getMockForTrait();
        // Make sure that the method thinks we are not using the cli.
        $trait->expects($this->any())
            ->method('isCli')
            ->willReturn(FALSE);
        // Un-protect the method using reflection.
        $method_ref = new \ReflectionMethod($trait, 'shouldRedirectToInstaller');
        $method_ref->setAccessible(TRUE);
        // Mock the database connection info.
        $db = $this->getMockForAbstractClass(Database::class);
        $property_ref = new \ReflectionProperty($db, 'databaseInfo');
        $property_ref->setAccessible(TRUE);
        $property_ref->setValue($db, [
            'default' => $connection_info,
        ]);
        if ($connection) {
            // Mock the database connection.
            $connection = $this->getMockBuilder(Connection::class)
                ->disableOriginalConstructor()
                ->setMethods([
                'schema',
            ])
                ->getMockForAbstractClass();
            if ($connection_info) {
                // Mock the database schema class.
                $schema = $this->getMockBuilder(Schema::class)
                    ->disableOriginalConstructor()
                    ->setMethods([
                    'tableExists',
                ])
                    ->getMockForAbstractClass();
                $schema->expects($this->any())
                    ->method('tableExists')
                    ->with('sessions')
                    ->willReturn($session_table_exists);
                $connection->expects($this->any())
                    ->method('schema')
                    ->willReturn($schema);
            }
        }
        else {
            // Set the database connection if there is none.
            $connection = NULL;
        }
        // Call shouldRedirectToInstaller.
        $this->assertSame($expected, $method_ref->invoke($trait, $e, $connection));
    }
}

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