Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / simpletest / tests / src / Unit / SimpletestPhpunitRunCommandTest.php
1 <?php
2
3 namespace Drupal\Tests\simpletest\Unit;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Core\File\FileSystemInterface;
8 use PHPUnit\Framework\TestCase;
9
10 /**
11  * Tests simpletest_run_phpunit_tests() handles PHPunit fatals correctly.
12  *
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.
15  *
16  * @group simpletest
17  *
18  * @runTestsInSeparateProcesses
19  * @preserveGlobalState disabled
20  */
21 class SimpletestPhpunitRunCommandTest extends TestCase {
22
23   /**
24    * Path to the app root.
25    *
26    * @var string
27    */
28   protected static $root;
29
30   /**
31    * {@inheritdoc}
32    */
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
42     // simpletest.module.
43     include_once self::$root . '/core/modules/simpletest/simpletest.module';
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::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);
59   }
60
61   /**
62    * Data provider for testSimpletestPhpUnitRunCommand().
63    *
64    * @return array
65    *   Arrays of status codes and the label they're expected to have.
66    */
67   public function provideStatusCodes() {
68     $data = [
69       [0, 'pass'],
70       [1, 'fail'],
71       [2, 'exception'],
72     ];
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'];
78     }
79     return $data;
80   }
81
82   /**
83    * Test the round trip for PHPUnit execution status codes.
84    *
85    * @covers ::simpletest_run_phpunit_tests
86    *
87    * @dataProvider provideStatusCodes
88    */
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', [
93         'driver' => 'mysql',
94         'username' => 'test_user',
95         'password' => 'test_pass',
96         'host' => 'test_host',
97         'database' => 'test_database',
98         'port' => 3306,
99         'namespace' => 'Drupal\Core\Database\Driver\mysql',
100       ]
101     );
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));
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function tearDown() {
114     // We unset the $base_url global, since the test code sets it as a
115     // side-effect.
116     unset($GLOBALS['base_url']);
117     parent::tearDown();
118   }
119
120 }