3 namespace Drupal\Tests\TestSuites;
5 use org\bovigo\vfs\vfsStream;
6 use PHPUnit\Framework\TestCase;
8 // The test suite class is not part of the autoloader, we need to include it
10 require_once __DIR__ . '/../../../TestSuites/TestSuiteBase.php';
13 * @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
17 class TestSuiteBaseTest extends TestCase {
20 * Helper method to set up the file system.
23 * A Drupal filesystem suitable for use with vfsStream.
25 protected function getFilesystem() {
33 'CoreNotUnitTest.php' => '<?php',
36 'CoreUnitTest.php' => '<?php',
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.
51 public function provideCoreTests() {
52 $filesystem = $this->getFilesystem();
58 'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
65 'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
72 * Tests for special case behavior of unit test suite namespaces in core.
74 * @covers ::addTestsBySuiteNamespace
76 * @dataProvider provideCoreTests
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);
83 // Make a stub suite base to test.
84 $stub = new StubTestSuiteBase('test_me');
86 // Access addTestsBySuiteNamespace().
87 $ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
88 $ref_add_tests->setAccessible(TRUE);
90 // Invoke addTestsBySuiteNamespace().
91 $ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
93 // Determine if we loaded the expected test files.
94 $this->assertNotEmpty($stub->testFiles);
95 $this->assertEmpty(array_diff_assoc($expected_tests, $stub->testFiles));
99 * Tests the assumption that local time is in 'Australia/Sydney'.
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());
109 * Stub subclass of TestSuiteBase.
111 * We use this class to alter the behavior of TestSuiteBase so it can be
114 class StubTestSuiteBase extends TestSuiteBase {
117 * Test files discovered by addTestsBySuiteNamespace().
121 public $testFiles = [];
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
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);