Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Test / TestSuiteBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Test;
4
5 use Drupal\Tests\TestSuites\TestSuiteBase;
6 use org\bovigo\vfs\vfsStream;
7 use PHPUnit\Framework\TestCase;
8
9 // The test suite class is not part of the autoloader, we need to include it
10 // manually.
11 require_once __DIR__ . '/../../../../TestSuites/TestSuiteBase.php';
12
13 /**
14  * @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
15  *
16  * @group TestSuite
17  */
18 class TestSuiteBaseTest extends TestCase {
19
20   /**
21    * Helper method to set up the file system.
22    *
23    * @return array[]
24    *   A Drupal filesystem suitable for use with vfsStream.
25    */
26   protected function getFilesystem() {
27     return [
28       'core' => [
29         'modules' => [],
30         'profiles' => [],
31         'tests' => [
32           'Drupal' => [
33             'NotUnitTests' => [
34               'CoreNotUnitTest.php' => '<?php',
35             ],
36             'Tests' => [
37               'CoreUnitTest.php' => '<?php',
38               // Ensure that the following files are not found as tests.
39               'Listeners' => [
40                 'Listener.php' => '<?php',
41                 'Legacy' => [
42                   'Listener.php' => '<?php',
43                 ],
44               ],
45             ],
46           ],
47         ],
48       ],
49     ];
50   }
51
52   /**
53    * @return array[]
54    *   Test data for testAddTestsBySuiteNamespaceCore(). An array of arrays:
55    *   - A filesystem array for vfsStream.
56    *   - The sub-namespace of the test suite.
57    *   - The array of tests expected to be discovered.
58    */
59   public function provideCoreTests() {
60     $filesystem = $this->getFilesystem();
61     return [
62       'unit-tests' => [
63         $filesystem,
64         'Unit',
65         [
66           'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
67         ],
68       ],
69       'not-unit-tests' => [
70         $filesystem,
71         'NotUnit',
72         [
73           'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
74         ],
75       ],
76     ];
77   }
78
79   /**
80    * Tests for special case behavior of unit test suite namespaces in core.
81    *
82    * @covers ::addTestsBySuiteNamespace
83    *
84    * @dataProvider provideCoreTests
85    */
86   public function testAddTestsBySuiteNamespaceCore($filesystem, $suite_namespace, $expected_tests) {
87     // Set up the file system.
88     $vfs = vfsStream::setup('root');
89     vfsStream::create($filesystem, $vfs);
90
91     // Make a stub suite base to test.
92     $stub = new StubTestSuiteBase('test_me');
93
94     // Access addTestsBySuiteNamespace().
95     $ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
96     $ref_add_tests->setAccessible(TRUE);
97
98     // Invoke addTestsBySuiteNamespace().
99     $ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
100
101     // Determine if we loaded the expected test files.
102     $this->assertEquals($expected_tests, $stub->testFiles);
103   }
104
105   /**
106    * Tests the assumption that local time is in 'Australia/Sydney'.
107    */
108   public function testLocalTimeZone() {
109     // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
110     $this->assertEquals('Australia/Sydney', date_default_timezone_get());
111   }
112
113 }
114
115 /**
116  * Stub subclass of TestSuiteBase.
117  *
118  * We use this class to alter the behavior of TestSuiteBase so it can be
119  * testable.
120  */
121 class StubTestSuiteBase extends TestSuiteBase {
122
123   /**
124    * Test files discovered by addTestsBySuiteNamespace().
125    *
126    * @var string[]
127    */
128   public $testFiles = [];
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function findExtensionDirectories($root) {
134     // We have to stub findExtensionDirectories() because we can't inject a
135     // vfsStream filesystem into drupal_phpunit_find_extension_directories(),
136     // which uses \SplFileInfo->getRealPath(). getRealPath() resolves
137     // stream-based paths to an empty string. See
138     // https://github.com/mikey179/vfsStream/wiki/Known-Issues
139     return [];
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function addTestFiles($filenames) {
146     // We stub addTestFiles() because the parent implementation can't deal with
147     // vfsStream-based filesystems due to an error in
148     // stream_resolve_include_path(). See
149     // https://github.com/mikey179/vfsStream/issues/5 Here we just store the
150     // test file being added in $this->testFiles.
151     $this->testFiles = array_merge($this->testFiles, $filenames);
152   }
153
154 }