Commit 765aa9e7 authored by alexpott's avatar alexpott

Issue #2499239 by Mile23, neclimdul, alexpott, jhedstrom, Mixologic, Crell,...

Issue #2499239 by Mile23, neclimdul, alexpott, jhedstrom, Mixologic, Crell, Xano, dawehner: Use test suite classes to discover different test types under phpunit, allow contrib harmony with run-tests
parent bae3e31e
......@@ -11,7 +11,7 @@
/**
* Defines a base class for Views kernel testing.
*/
class ViewsKernelTestBase extends KernelTestBase {
abstract class ViewsKernelTestBase extends KernelTestBase {
use ViewResultAssertionTrait;
......
......@@ -28,48 +28,16 @@
</php>
<testsuites>
<testsuite name="unit">
<directory>./tests/Drupal/Tests</directory>
<directory>./modules/*/tests/src/Unit</directory>
<directory>../modules/*/tests/src/Unit</directory>
<directory>../profiles/*/tests/src/Unit</directory>
<directory>../sites/*/modules/*/tests/src/Unit</directory>
<!-- Exclude Composer's vendor directory so we don't run tests there. -->
<exclude>./vendor</exclude>
<!-- Exclude Drush tests. -->
<exclude>./drush/tests</exclude>
<file>./tests/TestSuites/UnitTestSuite.php</file>
</testsuite>
<testsuite name="kernel">
<directory>./tests/Drupal/KernelTests</directory>
<directory>./modules/*/tests/src/Kernel</directory>
<directory>../modules/*/tests/src/Kernel</directory>
<directory>../profiles/*/tests/src/Kernel</directory>
<directory>../sites/*/modules/*/tests/src/Kernel</directory>
<!-- Exclude Composer's vendor directory so we don't run tests there. -->
<exclude>./vendor</exclude>
<!-- Exclude Drush tests. -->
<exclude>./drush/tests</exclude>
<file>./tests/TestSuites/KernelTestSuite.php</file>
</testsuite>
<testsuite name="functional">
<directory>./tests/Drupal/FunctionalTests</directory>
<directory>./modules/*/tests/src/Functional</directory>
<directory>../modules/*/tests/src/Functional</directory>
<directory>../profiles/*/tests/src/Functional</directory>
<directory>../sites/*/modules/*/tests/src/Functional</directory>
<!-- Exclude Composer's vendor directory so we don't run tests there. -->
<exclude>./vendor</exclude>
<!-- Exclude Drush tests. -->
<exclude>./drush/tests</exclude>
<file>./tests/TestSuites/FunctionalTestSuite.php</file>
</testsuite>
<testsuite name="functional-javascript">
<directory>./tests/Drupal/FunctionalJavascriptTests</directory>
<directory>./modules/*/tests/src/FunctionalJavascript</directory>
<directory>../modules/*/tests/src/FunctionalJavascript</directory>
<directory>../profiles/*/tests/src/FunctionalJavascript</directory>
<directory>../sites/*/modules/*/tests/src/FunctionalJavascript</directory>
<!-- Exclude Composer's vendor directory so we don't run tests there. -->
<exclude>./vendor</exclude>
<!-- Exclude Drush tests. -->
<exclude>./drush/tests</exclude>
<file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
</testsuite>
</testsuites>
<listeners>
......
......@@ -22,7 +22,7 @@
/**
* Base class for the actual unit tests testing \Drupal\Core\Render\Renderer.
*/
class RendererTestBase extends UnitTestCase {
abstract class RendererTestBase extends UnitTestCase {
/**
* The tested renderer.
......
<?php
namespace Drupal\Tests\TestSuites;
use org\bovigo\vfs\vfsStream;
/**
* @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
*
* @group TestSuite
*/
class TestSuiteBaseTest extends \PHPUnit_Framework_TestCase {
/**
* Helper method to set up the file system.
*
* @return array[]
* A Drupal filesystem suitable for use with vfsStream.
*/
protected function getFilesystem() {
return [
'core' => [
'modules' => [],
'profiles' => [],
'tests' => [
'Drupal' => [
'NotUnitTests' => [
'CoreNotUnitTest.php' => '<?php',
],
'Tests' => [
'CoreUnitTest.php' => '<?php',
],
],
],
],
];
}
/**
* @return array[]
* Test data for testAddTestsBySuiteNamespaceCore(). An array of arrays:
* - A filesystem array for vfsStream.
* - The sub-namespace of the test suite.
* - The array of tests expected to be discovered.
*/
public function provideCoreTests() {
$filesystem = $this->getFilesystem();
return [
'unit-tests' => [
$filesystem,
'Unit',
[
'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
],
],
'not-unit-tests' => [
$filesystem,
'NotUnit',
[
'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
],
],
];
}
/**
* Tests for special case behavior of unit test suite namespaces in core.
*
* @covers ::addTestsBySuiteNamespace
*
* @dataProvider provideCoreTests
*/
public function testAddTestsBySuiteNamespaceCore($filesystem, $suite_namespace, $expected_tests) {
// Set up the file system.
$vfs = vfsStream::setup('root');
vfsStream::create($filesystem, $vfs);
// Make a stub suite base to test.
$stub = new StubTestSuiteBase('test_me');
// Access addTestsBySuiteNamespace().
$ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
$ref_add_tests->setAccessible(TRUE);
// Invoke addTestsBySuiteNamespace().
$ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
// Determine if we loaded the expected test files.
$this->assertNotEmpty($stub->testFiles);
$this->assertEmpty(array_diff_assoc($expected_tests, $stub->testFiles));
}
}
/**
* Stub subclass of TestSuiteBase.
*
* We use this class to alter the behavior of TestSuiteBase so it can be
* testable.
*/
class StubTestSuiteBase extends TestSuiteBase {
/**
* Test files discovered by addTestsBySuiteNamespace().
*
* @var string[]
*/
public $testFiles = [];
/**
* {@inheritdoc}
*/
protected function findExtensionDirectories($root) {
// We have to stub findExtensionDirectories() because we can't inject a
// vfsStream filesystem into drupal_phpunit_find_extension_directories(),
// which uses \SplFileInfo->getRealPath(). getRealPath() resolves
// stream-based paths to an empty string. See
// https://github.com/mikey179/vfsStream/wiki/Known-Issues
return [];
}
/**
* {@inheritdoc}
*/
public function addTestFiles($filenames) {
// We stub addTestFiles() because the parent implementation can't deal with
// vfsStream-based filesystems due to an error in
// stream_resolve_include_path(). See
// https://github.com/mikey179/vfsStream/issues/5 Here we just store the
// test file being added in $this->testFiles.
$this->testFiles = array_merge($this->testFiles, $filenames);
}
}
<?php
namespace Drupal\Tests\TestSuites;
require_once __DIR__ . '/TestSuiteBase.php';
/**
* Discovers tests for the functional-javascript test suite.
*/
class FunctionalJavascriptTestSuite extends TestSuiteBase {
/**
* Factory method which loads up a suite with all functional javascript tests.
*
* @return static
* The test suite.
*/
public static function suite() {
$root = dirname(dirname(dirname(__DIR__)));
$suite = new static('functional-javascript');
$suite->addTestsBySuiteNamespace($root, 'FunctionalJavascript');
return $suite;
}
}
<?php
namespace Drupal\Tests\TestSuites;
require_once __DIR__ . '/TestSuiteBase.php';
/**
* Discovers tests for the functional test suite.
*/
class FunctionalTestSuite extends TestSuiteBase {
/**
* Factory method which loads up a suite with all functional tests.
*
* @return static
* The test suite.
*/
public static function suite() {
$root = dirname(dirname(dirname(__DIR__)));
$suite = new static('functional');
$suite->addTestsBySuiteNamespace($root, 'Functional');
return $suite;
}
}
<?php
namespace Drupal\Tests\TestSuites;
require_once __DIR__ . '/TestSuiteBase.php';
/**
* Discovers tests for the kernel test suite.
*/
class KernelTestSuite extends TestSuiteBase {
/**
* Factory method which loads up a suite with all kernel tests.
*
* @return static
* The test suite.
*/
public static function suite() {
$root = dirname(dirname(dirname(__DIR__)));
$suite = new static('kernel');
$suite->addTestsBySuiteNamespace($root, 'Kernel');
return $suite;
}
}
<?php
namespace Drupal\Tests\TestSuites;
use Drupal\simpletest\TestDiscovery;
/**
* Base class for Drupal test suites.
*/
abstract class TestSuiteBase extends \PHPUnit_Framework_TestSuite {
/**
* Finds extensions in a Drupal installation.
*
* An extension is defined as a directory with an *.info.yml file in it.
*
* @param string $root
* Path to the root of the Drupal installation.
*
* @return string[]
* Associative array of extension paths, with extension name as keys.
*/
protected function findExtensionDirectories($root) {
$extension_roots = \drupal_phpunit_contrib_extension_directory_roots($root);
$extension_directories = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
return array_reduce($extension_directories, 'array_merge', array());
}
/**
* Find and add tests to the suite for core and any extensions.
*
* @param string $root
* Path to the root of the Drupal installation.
* @param string $suite_namespace
* SubNamespace used to separate test suite. Examples: Unit, Functional.
*/
protected function addTestsBySuiteNamespace($root, $suite_namespace) {
// Core's tests are in the namespace Drupal\${suite_namespace}Tests\ and are
// always inside of core/tests/Drupal/${suite_namespace}Tests. The exception
// to this is Unit tests for historical reasons.
if ($suite_namespace == 'Unit') {
$this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\Tests\\", "$root/core/tests/Drupal/Tests"));
}
else {
$this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\${suite_namespace}Tests\\", "$root/core/tests/Drupal/${suite_namespace}Tests"));
}
// Extensions' tests will always be in the namespace
// Drupal\Tests\$extension_name\$suite_namespace\ and be in the
// $extension_path/tests/src/$suite_namespace directory. Not all extensions
// will have all kinds of tests.
foreach ($this->findExtensionDirectories($root) as $extension_name => $dir) {
$test_path = "$dir/tests/src/$suite_namespace";
if (is_dir($test_path)) {
$this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\Tests\\$extension_name\\$suite_namespace\\", $test_path));
}
}
}
}
<?php
namespace Drupal\Tests\TestSuites;
require_once __DIR__ . '/TestSuiteBase.php';
/**
* Discovers tests for the unit test suite.
*/
class UnitTestSuite extends TestSuiteBase {
/**
* Factory method which loads up a suite with all unit tests.
*
* @return static
* The test suite.
*/
public static function suite() {
$root = dirname(dirname(dirname(__DIR__)));
$suite = new static('unit');
$suite->addTestsBySuiteNamespace($root, 'Unit');
return $suite;
}
}
......@@ -23,7 +23,9 @@ function drupal_phpunit_find_extension_directories($scan_directory) {
$dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($scan_directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
foreach ($dirs as $dir) {
if (strpos($dir->getPathname(), '.info.yml') !== FALSE) {
// Cut off ".info.yml" from the filename for use as the extension name.
// Cut off ".info.yml" from the filename for use as the extension name. We
// use getRealPath() so that we can scan extensions represented by
// directory aliases.
$extensions[substr($dir->getFilename(), 0, -9)] = $dir->getPathInfo()
->getRealPath();
}
......@@ -34,11 +36,16 @@ function drupal_phpunit_find_extension_directories($scan_directory) {
/**
* Returns directories under which contributed extensions may exist.
*
* @param string $root
* (optional) Path to the root of the Drupal installation.
*
* @return array
* An array of directories under which contributed extensions may exist.
*/
function drupal_phpunit_contrib_extension_directory_roots() {
$root = dirname(dirname(__DIR__));
function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
if ($root === NULL) {
$root = dirname(dirname(__DIR__));
}
$paths = array(
$root . '/core/modules',
$root . '/core/profiles',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment