3 namespace Drupal\Tests\Component;
5 use Drupal\Tests\UnitTestCase;
6 use org\bovigo\vfs\vfsStream;
9 * General tests for \Drupal\Component that can't go anywhere else.
13 class DrupalComponentTest extends UnitTestCase {
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 * Searches a directory recursively for PHP classes.
39 * The full path to the directory that should be checked.
42 * An array of class paths.
44 protected function findPhpClasses($dir) {
46 foreach (new \DirectoryIterator($dir) as $file) {
47 if ($file->isDir() && !$file->isDot()) {
48 $classes = array_merge($classes, $this->findPhpClasses($file->getPathname()));
50 elseif ($file->getExtension() == 'php') {
51 $classes[] = $file->getPathname();
59 * Asserts that the given class is not using any class from Core namespace.
61 * @param string $class_path
62 * The full path to the class that should be checked.
64 protected function assertNoCoreUsage($class_path) {
65 $contents = file_get_contents($class_path);
66 preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches);
67 $matches = array_filter($matches[0], function($line) {
68 // Filter references to @see as they don't really matter.
69 return strpos($line, '@see') === FALSE;
71 $this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path");
75 * Data provider for testAssertNoCoreUseage().
78 * Data for testAssertNoCoreUseage() in the form:
79 * - TRUE if the test passes, FALSE otherwise.
80 * - File data as a string. This will be used as a virtual file.
82 public function providerAssertNoCoreUseage() {
86 '@see \\Drupal\\Core\\Something',
90 '\\Drupal\\Core\\Something',
94 "@see \\Drupal\\Core\\Something\n" .
95 '\\Drupal\\Core\\Something',
99 "\\Drupal\\Core\\Something\n" .
100 '@see \\Drupal\\Core\\Something',
106 * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage
107 * @dataProvider providerAssertNoCoreUseage
109 public function testAssertNoCoreUseage($expected_pass, $file_data) {
110 // Set up a virtual file to read.
111 $vfs_root = vfsStream::setup('root');
112 vfsStream::newFile('Test.php')->at($vfs_root)->setContent($file_data);
113 $file_uri = vfsStream::url('root/Test.php');
117 $this->assertNoCoreUsage($file_uri);
119 catch (\PHPUnit_Framework_AssertionFailedError $e) {
122 $this->assertEquals($expected_pass, $pass, $expected_pass ?
123 'Test caused a false positive' :
124 'Test failed to detect Core usage');