3 namespace Drupal\Tests\Component;
5 use org\bovigo\vfs\vfsStream;
6 use PHPUnit\Framework\TestCase;
9 * General tests for \Drupal\Component that can't go anywhere else.
13 class DrupalComponentTest extends TestCase {
16 * Tests that classes in Component do not use any Core class.
18 public function testNoCoreInComponent() {
19 $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
20 foreach ($this->findPhpClasses($component_path) as $class) {
21 $this->assertNoCoreUsage($class);
26 * Tests that classes in Component Tests do not use any Core class.
28 public function testNoCoreInComponentTests() {
29 $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/tests/Drupal/Tests/Component';
30 foreach ($this->findPhpClasses($component_path) as $class) {
31 $this->assertNoCoreUsage($class);
36 * Tests LICENSE.txt is present and has the correct content.
38 * @param $component_path
39 * The path to the component.
40 * @dataProvider \Drupal\Tests\Component\DrupalComponentTest::getComponents
42 public function testComponentLicence($component_path) {
43 $this->assertFileExists($component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt');
44 $this->assertSame('e84dac1d9fbb5a4a69e38654ce644cea769aa76b', hash_file('sha1', $component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt'));
52 public function getComponents() {
53 $root_component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
54 $component_paths = [];
55 foreach (new \DirectoryIterator($root_component_path) as $file) {
56 if ($file->isDir() && !$file->isDot()) {
57 $component_paths[$file->getBasename()] = [$file->getPathname()];
60 return $component_paths;
64 * Searches a directory recursively for PHP classes.
67 * The full path to the directory that should be checked.
70 * An array of class paths.
72 protected function findPhpClasses($dir) {
74 foreach (new \DirectoryIterator($dir) as $file) {
75 if ($file->isDir() && !$file->isDot()) {
76 $classes = array_merge($classes, $this->findPhpClasses($file->getPathname()));
78 elseif ($file->getExtension() == 'php') {
79 $classes[] = $file->getPathname();
87 * Asserts that the given class is not using any class from Core namespace.
89 * @param string $class_path
90 * The full path to the class that should be checked.
92 protected function assertNoCoreUsage($class_path) {
93 $contents = file_get_contents($class_path);
94 preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches);
95 $matches = array_filter($matches[0], function ($line) {
96 // Filter references to @see as they don't really matter.
97 return strpos($line, '@see') === FALSE;
99 $this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path");
103 * Data provider for testAssertNoCoreUseage().
106 * Data for testAssertNoCoreUseage() in the form:
107 * - TRUE if the test passes, FALSE otherwise.
108 * - File data as a string. This will be used as a virtual file.
110 public function providerAssertNoCoreUseage() {
114 '@see \\Drupal\\Core\\Something',
118 '\\Drupal\\Core\\Something',
122 "@see \\Drupal\\Core\\Something\n" .
123 '\\Drupal\\Core\\Something',
127 "\\Drupal\\Core\\Something\n" .
128 '@see \\Drupal\\Core\\Something',
134 * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage
135 * @dataProvider providerAssertNoCoreUseage
137 public function testAssertNoCoreUseage($expected_pass, $file_data) {
138 // Set up a virtual file to read.
139 $vfs_root = vfsStream::setup('root');
140 vfsStream::newFile('Test.php')->at($vfs_root)->setContent($file_data);
141 $file_uri = vfsStream::url('root/Test.php');
145 $this->assertNoCoreUsage($file_uri);
147 catch (\PHPUnit_Framework_AssertionFailedError $e) {
150 $this->assertEquals($expected_pass, $pass, $expected_pass ?
151 'Test caused a false positive' :
152 'Test failed to detect Core usage');