3 namespace Drupal\Tests\Core\Test;
5 use Drupal\Tests\TestSuites\TestSuiteBase;
6 use org\bovigo\vfs\vfsStream;
7 use PHPUnit\Framework\TestCase;
9 // The test suite class is not part of the autoloader, we need to include it
11 require_once __DIR__ . '/../../../../TestSuites/TestSuiteBase.php';
14 * @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
18 class TestSuiteBaseTest extends TestCase {
21 * Helper method to set up the file system.
24 * A Drupal filesystem suitable for use with vfsStream.
26 protected function getFilesystem() {
34 'CoreNotUnitTest.php' => '<?php',
37 'CoreUnitTest.php' => '<?php',
38 // Ensure that the following files are not found as tests.
40 'Listener.php' => '<?php',
42 'Listener.php' => '<?php',
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.
59 public function provideCoreTests() {
60 $filesystem = $this->getFilesystem();
66 'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
73 'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
80 * Tests for special case behavior of unit test suite namespaces in core.
82 * @covers ::addTestsBySuiteNamespace
84 * @dataProvider provideCoreTests
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);
91 // Make a stub suite base to test.
92 $stub = new StubTestSuiteBase('test_me');
94 // Access addTestsBySuiteNamespace().
95 $ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
96 $ref_add_tests->setAccessible(TRUE);
98 // Invoke addTestsBySuiteNamespace().
99 $ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
101 // Determine if we loaded the expected test files.
102 $this->assertEquals($expected_tests, $stub->testFiles);
106 * Tests the assumption that local time is in 'Australia/Sydney'.
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());
116 * Stub subclass of TestSuiteBase.
118 * We use this class to alter the behavior of TestSuiteBase so it can be
121 class StubTestSuiteBase extends TestSuiteBase {
124 * Test files discovered by addTestsBySuiteNamespace().
128 public $testFiles = [];
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
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);