Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\simpletest\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
6 use Drupal\Core\File\FileSystemInterface;
|
Chris@0
|
7 use PHPUnit\Framework\TestCase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests simpletest_run_phpunit_tests() handles PHPunit fatals correctly.
|
Chris@0
|
11 *
|
Chris@0
|
12 * We don't extend Drupal\Tests\UnitTestCase here because its $root property is
|
Chris@0
|
13 * not static and we need it to be static here.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group simpletest
|
Chris@0
|
16 *
|
Chris@0
|
17 * @runTestsInSeparateProcesses
|
Chris@0
|
18 */
|
Chris@0
|
19 class SimpletestPhpunitRunCommandTest extends TestCase {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Path to the app root.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var string
|
Chris@0
|
25 */
|
Chris@0
|
26 protected static $root;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 public static function setUpBeforeClass() {
|
Chris@0
|
32 parent::setUpBeforeClass();
|
Chris@0
|
33 // Figure out our app root.
|
Chris@0
|
34 self::$root = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
|
Chris@0
|
35 // Include the files we need for tests. The stub test we will run is
|
Chris@0
|
36 // SimpletestPhpunitRunCommandTestWillDie which is located in
|
Chris@0
|
37 // simpletest_phpunit_run_command_test.php.
|
Chris@0
|
38 include_once self::$root . '/core/modules/simpletest/tests/fixtures/simpletest_phpunit_run_command_test.php';
|
Chris@0
|
39 // Since we're testing simpletest_run_phpunit_tests(), we need to include
|
Chris@0
|
40 // simpletest.module.
|
Chris@0
|
41 include_once self::$root . '/core/modules/simpletest/simpletest.module';
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function setUp() {
|
Chris@0
|
48 parent::setUp();
|
Chris@0
|
49 // Organize our mock container.
|
Chris@0
|
50 $container = new ContainerBuilder();
|
Chris@0
|
51 $container->set('app.root', self::$root);
|
Chris@0
|
52 $file_system = $this->prophesize(FileSystemInterface::class);
|
Chris@0
|
53 // The simpletest directory wrapper will always point to /tmp.
|
Chris@0
|
54 $file_system->realpath('public://simpletest')->willReturn(sys_get_temp_dir());
|
Chris@0
|
55 $container->set('file_system', $file_system->reveal());
|
Chris@0
|
56 \Drupal::setContainer($container);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Data provider for testSimpletestPhpUnitRunCommand().
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return array
|
Chris@0
|
63 * Arrays of status codes and the label they're expected to have.
|
Chris@0
|
64 */
|
Chris@0
|
65 public function provideStatusCodes() {
|
Chris@0
|
66 $data = [
|
Chris@0
|
67 [0, 'pass'],
|
Chris@0
|
68 [1, 'fail'],
|
Chris@0
|
69 [2, 'exception'],
|
Chris@0
|
70 ];
|
Chris@0
|
71 // All status codes 3 and above should be labeled 'error'.
|
Chris@0
|
72 // @todo: The valid values here would be 3 to 127. But since the test
|
Chris@0
|
73 // touches the file system a lot, we only have 3, 4, and 127 for speed.
|
Chris@0
|
74 foreach ([3, 4, 127] as $status) {
|
Chris@0
|
75 $data[] = [$status, 'error'];
|
Chris@0
|
76 }
|
Chris@0
|
77 return $data;
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Test the round trip for PHPUnit execution status codes.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @covers ::simpletest_run_phpunit_tests
|
Chris@0
|
84 *
|
Chris@0
|
85 * @dataProvider provideStatusCodes
|
Chris@0
|
86 */
|
Chris@0
|
87 public function testSimpletestPhpUnitRunCommand($status, $label) {
|
Chris@0
|
88 $test_id = basename(tempnam(sys_get_temp_dir(), 'xxx'));
|
Chris@0
|
89 putenv('SimpletestPhpunitRunCommandTestWillDie=' . $status);
|
Chris@0
|
90 $ret = simpletest_run_phpunit_tests($test_id, [SimpletestPhpunitRunCommandTestWillDie::class]);
|
Chris@0
|
91 $this->assertSame($ret[0]['status'], $label);
|
Chris@0
|
92 putenv('SimpletestPhpunitRunCommandTestWillDie');
|
Chris@0
|
93 unlink(simpletest_phpunit_xml_filepath($test_id));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * {@inheritdoc}
|
Chris@0
|
98 */
|
Chris@0
|
99 protected function tearDown() {
|
Chris@0
|
100 // We unset the $base_url global, since the test code sets it as a
|
Chris@0
|
101 // side-effect.
|
Chris@0
|
102 unset($GLOBALS['base_url']);
|
Chris@0
|
103 parent::tearDown();
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 }
|