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