function KernelTestBaseTest::testMethodRequiresModule

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/KernelTestBaseTest.php \Drupal\KernelTests\KernelTestBaseTest::testMethodRequiresModule()
  2. 10 core/tests/Drupal/KernelTests/KernelTestBaseTest.php \Drupal\KernelTests\KernelTestBaseTest::testMethodRequiresModule()

Tests that a test method is skipped when it requires a module not present.

In order to catch checkRequirements() regressions, we have to make a new test object and run checkRequirements() here.

@covers ::checkRequirements @covers ::checkModuleRequirements

File

core/tests/Drupal/KernelTests/KernelTestBaseTest.php, line 261

Class

KernelTestBaseTest
@coversDefaultClass <a href="/api/drupal/core%21tests%21Drupal%21KernelTests%21KernelTestBase.php/class/KernelTestBase/9" title="Base class for functional integration tests." class="local">\Drupal\KernelTests\KernelTestBase</a>

Namespace

Drupal\KernelTests

Code

public function testMethodRequiresModule() {
    require __DIR__ . '/../../fixtures/KernelMissingDependentModuleMethodTest.php';
    $stub_test = new KernelMissingDependentModuleMethodTest();
    // We have to setName() to the method name we're concerned with.
    $stub_test->setName('testRequiresModule');
    // We cannot use $this->setExpectedException() because PHPUnit would skip
    // the test before comparing the exception type.
    try {
        $stub_test->publicCheckRequirements();
        $this->fail('Missing required module throws skipped test exception.');
    } catch (SkippedTestError $e) {
        $this->assertEquals('Required modules: module_does_not_exist', $e->getMessage());
    }
}

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