3 namespace Drupal\Tests\simpletest\Unit;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Core\File\FileSystemInterface;
8 use PHPUnit\Framework\TestCase;
11 * Tests simpletest_run_phpunit_tests() handles PHPunit fatals correctly.
13 * We don't extend Drupal\Tests\UnitTestCase here because its $root property is
14 * not static and we need it to be static here.
18 * @runTestsInSeparateProcesses
19 * @preserveGlobalState disabled
21 class SimpletestPhpunitRunCommandTest extends TestCase {
24 * Path to the app root.
28 protected static $root;
33 public static function setUpBeforeClass() {
34 parent::setUpBeforeClass();
35 // Figure out our app root.
36 self::$root = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
37 // Include the files we need for tests. The stub test we will run is
38 // SimpletestPhpunitRunCommandTestWillDie which is located in
39 // simpletest_phpunit_run_command_test.php.
40 include_once self::$root . '/core/modules/simpletest/tests/fixtures/simpletest_phpunit_run_command_test.php';
41 // Since we're testing simpletest_run_phpunit_tests(), we need to include
43 include_once self::$root . '/core/modules/simpletest/simpletest.module';
49 protected function setUp() {
51 // Organize our mock container.
52 $container = new ContainerBuilder();
53 $container->set('app.root', self::$root);
54 $file_system = $this->prophesize(FileSystemInterface::class);
55 // The simpletest directory wrapper will always point to /tmp.
56 $file_system->realpath('public://simpletest')->willReturn(sys_get_temp_dir());
57 $container->set('file_system', $file_system->reveal());
58 \Drupal::setContainer($container);
62 * Data provider for testSimpletestPhpUnitRunCommand().
65 * Arrays of status codes and the label they're expected to have.
67 public function provideStatusCodes() {
73 // All status codes 3 and above should be labeled 'error'.
74 // @todo: The valid values here would be 3 to 127. But since the test
75 // touches the file system a lot, we only have 3, 4, and 127 for speed.
76 foreach ([3, 4, 127] as $status) {
77 $data[] = [$status, 'error'];
83 * Test the round trip for PHPUnit execution status codes.
85 * @covers ::simpletest_run_phpunit_tests
87 * @dataProvider provideStatusCodes
89 public function testSimpletestPhpUnitRunCommand($status, $label) {
90 // Add a default database connection in order for
91 // Database::getConnectionInfoAsUrl() to return valid information.
92 Database::addConnectionInfo('default', 'default', [
94 'username' => 'test_user',
95 'password' => 'test_pass',
96 'host' => 'test_host',
97 'database' => 'test_database',
99 'namespace' => 'Drupal\Core\Database\Driver\mysql',
102 $test_id = basename(tempnam(sys_get_temp_dir(), 'xxx'));
103 putenv('SimpletestPhpunitRunCommandTestWillDie=' . $status);
104 $ret = simpletest_run_phpunit_tests($test_id, [SimpletestPhpunitRunCommandTestWillDie::class]);
105 $this->assertSame($ret[0]['status'], $label);
106 putenv('SimpletestPhpunitRunCommandTestWillDie');
107 unlink(simpletest_phpunit_xml_filepath($test_id));
113 protected function tearDown() {
114 // We unset the $base_url global, since the test code sets it as a
116 unset($GLOBALS['base_url']);