3 namespace Drupal\Tests\simpletest\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\File\FileSystemInterface;
7 use PHPUnit\Framework\TestCase;
10 * Tests simpletest_run_phpunit_tests() handles PHPunit fatals correctly.
12 * We don't extend Drupal\Tests\UnitTestCase here because its $root property is
13 * not static and we need it to be static here.
17 * @runTestsInSeparateProcesses
19 class SimpletestPhpunitRunCommandTest extends TestCase {
22 * Path to the app root.
26 protected static $root;
31 public static function setUpBeforeClass() {
32 parent::setUpBeforeClass();
33 // Figure out our app root.
34 self::$root = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
35 // Include the files we need for tests. The stub test we will run is
36 // SimpletestPhpunitRunCommandTestWillDie which is located in
37 // simpletest_phpunit_run_command_test.php.
38 include_once self::$root . '/core/modules/simpletest/tests/fixtures/simpletest_phpunit_run_command_test.php';
39 // Since we're testing simpletest_run_phpunit_tests(), we need to include
41 include_once self::$root . '/core/modules/simpletest/simpletest.module';
47 protected function setUp() {
49 // Organize our mock container.
50 $container = new ContainerBuilder();
51 $container->set('app.root', self::$root);
52 $file_system = $this->prophesize(FileSystemInterface::class);
53 // The simpletest directory wrapper will always point to /tmp.
54 $file_system->realpath('public://simpletest')->willReturn(sys_get_temp_dir());
55 $container->set('file_system', $file_system->reveal());
56 \Drupal::setContainer($container);
60 * Data provider for testSimpletestPhpUnitRunCommand().
63 * Arrays of status codes and the label they're expected to have.
65 public function provideStatusCodes() {
71 // All status codes 3 and above should be labeled 'error'.
72 // @todo: The valid values here would be 3 to 127. But since the test
73 // touches the file system a lot, we only have 3, 4, and 127 for speed.
74 foreach ([3, 4, 127] as $status) {
75 $data[] = [$status, 'error'];
81 * Test the round trip for PHPUnit execution status codes.
83 * @covers ::simpletest_run_phpunit_tests
85 * @dataProvider provideStatusCodes
87 public function testSimpletestPhpUnitRunCommand($status, $label) {
88 $test_id = basename(tempnam(sys_get_temp_dir(), 'xxx'));
89 putenv('SimpletestPhpunitRunCommandTestWillDie=' . $status);
90 $ret = simpletest_run_phpunit_tests($test_id, [SimpletestPhpunitRunCommandTestWillDie::class]);
91 $this->assertSame($ret[0]['status'], $label);
92 putenv('SimpletestPhpunitRunCommandTestWillDie');
93 unlink(simpletest_phpunit_xml_filepath($test_id));
99 protected function tearDown() {
100 // We unset the $base_url global, since the test code sets it as a
102 unset($GLOBALS['base_url']);