You are here

class PhpUnitTestRunnerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php \Drupal\Tests\Core\Test\PhpUnitTestRunnerTest
  2. 10 core/tests/Drupal/Tests/Core/Test/PhpUnitTestRunnerTest.php \Drupal\Tests\Core\Test\PhpUnitTestRunnerTest

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

Hierarchy

Expanded class hierarchy of PhpUnitTestRunnerTest

See also

Drupal\Tests\simpletest\Unit\SimpletestPhpunitRunCommandTest

File

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

Namespace

Drupal\Tests\Core\Test
View source
class PhpUnitTestRunnerTest extends UnitTestCase {

  /**
   * Test an error in the test running phase.
   *
   * @covers ::runTests
   */
  public function testRunTestsError() {
    $test_id = 23;
    $log_path = 'test_log_path';

    // Create a mock runner.
    $runner = $this
      ->getMockBuilder(PhpUnitTestRunner::class)
      ->disableOriginalConstructor()
      ->setMethods([
      '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;
    });

    // The runTests() method expects $status by reference, so we initialize it
    // to some value we don't expect back.
    $status = -1;
    $results = $runner
      ->runTests($test_id, [
      '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);
  }

  /**
   * @covers ::phpUnitCommand
   */
  public function testPhpUnitCommand() {
    $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
    $this
      ->assertRegExp('/phpunit/', $runner
      ->phpUnitCommand());
  }

  /**
   * @covers ::xmlLogFilePath
   */
  public function testXmlLogFilePath() {
    $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
    $this
      ->assertStringEndsWith('phpunit-23.xml', $runner
      ->xmlLogFilePath(23));
  }
  public function providerTestSummarizeResults() {
    return [
      [
        [
          [
            'test_class' => static::class,
            'status' => 'pass',
          ],
        ],
        '#pass',
      ],
      [
        [
          [
            'test_class' => static::class,
            'status' => 'fail',
          ],
        ],
        '#fail',
      ],
      [
        [
          [
            'test_class' => static::class,
            'status' => 'exception',
          ],
        ],
        '#exception',
      ],
      [
        [
          [
            'test_class' => static::class,
            'status' => 'debug',
          ],
        ],
        '#debug',
      ],
    ];
  }

  /**
   * @dataProvider providerTestSummarizeResults
   * @covers ::summarizeResults
   */
  public function testSummarizeResults($results, $has_status) {
    $runner = new PhpUnitTestRunner($this->root, sys_get_temp_dir());
    $summary = $runner
      ->summarizeResults($results);
    $this
      ->assertArrayHasKey(static::class, $summary);
    $this
      ->assertEquals(1, $summary[static::class][$has_status]);
    foreach (array_diff([
      '#pass',
      '#fail',
      '#exception',
      '#debug',
    ], [
      $has_status,
    ]) as $should_be_zero) {
      $this
        ->assertSame(0, $summary[static::class][$should_be_zero]);
    }
  }

}

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.
PhpUnitTestRunnerTest::providerTestSummarizeResults public function
PhpUnitTestRunnerTest::testPhpUnitCommand public function @covers ::phpUnitCommand
PhpUnitTestRunnerTest::testRunTestsError public function Test an error in the test running phase.
PhpUnitTestRunnerTest::testSummarizeResults public function @dataProvider providerTestSummarizeResults @covers ::summarizeResults
PhpUnitTestRunnerTest::testXmlLogFilePath public function @covers ::xmlLogFilePath
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.
UnitTestCase::setUp protected function 340