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