TestSuiteBaseTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Drupal\Tests\Core\Test;
  3. use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion;
  4. use Drupal\Tests\TestSuites\TestSuiteBase;
  5. use org\bovigo\vfs\vfsStream;
  6. use PHPUnit\Framework\TestCase;
  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. // In order to manage different method signatures between PHPUnit versions, we
  11. // dynamically load a compatibility trait dependent on the PHPUnit runner
  12. // version.
  13. if (!trait_exists(PhpunitVersionDependentStubTestSuiteBaseTrait::class, FALSE)) {
  14. class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit" . RunnerVersion::getMajor() . "\StubTestSuiteBaseTrait", PhpunitVersionDependentStubTestSuiteBaseTrait::class);
  15. }
  16. /**
  17. * @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
  18. *
  19. * @group TestSuite
  20. */
  21. class TestSuiteBaseTest extends TestCase {
  22. /**
  23. * Helper method to set up the file system.
  24. *
  25. * @return array[]
  26. * A Drupal filesystem suitable for use with vfsStream.
  27. */
  28. protected function getFilesystem() {
  29. return [
  30. 'core' => [
  31. 'modules' => [],
  32. 'profiles' => [],
  33. 'tests' => [
  34. 'Drupal' => [
  35. 'NotUnitTests' => [
  36. 'CoreNotUnitTest.php' => '<?php',
  37. ],
  38. 'Tests' => [
  39. 'CoreUnitTest.php' => '<?php',
  40. // Ensure that the following files are not found as tests.
  41. 'Listeners' => [
  42. 'Listener.php' => '<?php',
  43. 'Legacy' => [
  44. 'Listener.php' => '<?php',
  45. ],
  46. ],
  47. ],
  48. ],
  49. ],
  50. ],
  51. ];
  52. }
  53. /**
  54. * @return array[]
  55. * Test data for testAddTestsBySuiteNamespaceCore(). An array of arrays:
  56. * - A filesystem array for vfsStream.
  57. * - The sub-namespace of the test suite.
  58. * - The array of tests expected to be discovered.
  59. */
  60. public function provideCoreTests() {
  61. $filesystem = $this->getFilesystem();
  62. return [
  63. 'unit-tests' => [
  64. $filesystem,
  65. 'Unit',
  66. [
  67. 'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
  68. ],
  69. ],
  70. 'not-unit-tests' => [
  71. $filesystem,
  72. 'NotUnit',
  73. [
  74. 'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
  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. // Make a stub suite base to test.
  91. $stub = new StubTestSuiteBase('test_me');
  92. // Access addTestsBySuiteNamespace().
  93. $ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
  94. $ref_add_tests->setAccessible(TRUE);
  95. // Invoke addTestsBySuiteNamespace().
  96. $ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
  97. // Determine if we loaded the expected test files.
  98. $this->assertEquals($expected_tests, $stub->testFiles);
  99. }
  100. /**
  101. * Tests the assumption that local time is in 'Australia/Sydney'.
  102. */
  103. public function testLocalTimeZone() {
  104. // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
  105. $this->assertEquals('Australia/Sydney', date_default_timezone_get());
  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. use PhpunitVersionDependentStubTestSuiteBaseTrait;
  116. /**
  117. * Test files discovered by addTestsBySuiteNamespace().
  118. *
  119. * @var string[]
  120. */
  121. public $testFiles = [];
  122. /**
  123. * {@inheritdoc}
  124. */
  125. protected function findExtensionDirectories($root) {
  126. // We have to stub findExtensionDirectories() because we can't inject a
  127. // vfsStream filesystem into drupal_phpunit_find_extension_directories(),
  128. // which uses \SplFileInfo->getRealPath(). getRealPath() resolves
  129. // stream-based paths to an empty string. See
  130. // https://github.com/mikey179/vfsStream/wiki/Known-Issues
  131. return [];
  132. }
  133. }