function PhpUnitTestRunnerTest::testRunTestsError

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php \Drupal\Tests\Core\Test\PhpUnitTestRunnerTest::testRunTestsError()
  2. 8.9.x core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php \Drupal\Tests\Core\Test\PhpUnitTestRunnerTest::testRunTestsError()
  3. 11.x core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php \Drupal\Tests\Core\Test\PhpUnitTestRunnerTest::testRunTestsError()

Tests an error in the test running phase.

@covers ::execute

File

core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php, line 26

Class

PhpUnitTestRunnerTest
@coversDefaultClass \Drupal\Core\Test\PhpUnitTestRunner @group Test

Namespace

Drupal\Tests\Core\Test

Code

public function testRunTestsError() : void {
    $test_id = 23;
    $log_path = 'test_log_path';
    // Create a mock test run storage.
    $storage = $this->getMockBuilder(SimpletestTestRunResultsStorage::class)
        ->disableOriginalConstructor()
        ->onlyMethods([
        'createNew',
    ])
        ->getMock();
    // Set some expectations for createNew().
    $storage->expects($this->once())
        ->method('createNew')
        ->willReturn($test_id);
    // Create a mock runner.
    $runner = $this->getMockBuilder(PhpUnitTestRunner::class)
        ->disableOriginalConstructor()
        ->onlyMethods([
        'xmlLogFilepath',
        'runCommand',
    ])
        ->getMock();
    // Set some expectations for xmlLogFilepath().
    $runner->expects($this->once())
        ->method('xmlLogFilepath')
        ->willReturn($log_path);
    // We mark a failure by having runCommand() deliver a serious status code.
    $runner->expects($this->once())
        ->method('runCommand')
        ->willReturnCallback(function ($unescaped_test_classnames, $phpunit_file, &$status) {
        $status = TestStatus::EXCEPTION;
        return ' ';
    });
    // The execute() method expects $status by reference, so we initialize it
    // to some value we don't expect back.
    $status = -1;
    $test_run = TestRun::createNew($storage);
    $results = $runner->execute($test_run, [
        'SomeTest',
    ], $status);
    // Make sure our status code made the round trip.
    $this->assertEquals(TestStatus::EXCEPTION, $status);
    // A serious error in runCommand() should give us a fixed set of results.
    $row = reset($results);
    $fail_row = [
        'test_id' => $test_id,
        'test_class' => 'SomeTest',
        'status' => TestStatus::label(TestStatus::EXCEPTION),
        'message' => 'PHPUnit Test failed to complete; Error: ',
        'message_group' => 'Other',
        'function' => 'SomeTest',
        'line' => '0',
        'file' => $log_path,
    ];
    $this->assertEquals($fail_row, $row);
}

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