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