comparison core/tests/Drupal/Tests/Component/DrupalComponentTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\Component;
4
5 use org\bovigo\vfs\vfsStream;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9 * General tests for \Drupal\Component that can't go anywhere else.
10 *
11 * @group Component
12 */
13 class DrupalComponentTest extends TestCase {
14
15 /**
16 * Tests that classes in Component do not use any Core class.
17 */
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);
22 }
23 }
24
25 /**
26 * Tests that classes in Component Tests do not use any Core class.
27 */
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);
32 }
33 }
34
35 /**
36 * Tests LICENSE.txt is present and has the correct content.
37 *
38 * @param $component_path
39 * The path to the component.
40 * @dataProvider \Drupal\Tests\Component\DrupalComponentTest::getComponents
41 */
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'));
45 }
46
47 /**
48 * Data provider.
49 *
50 * @return array
51 */
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()];
58 }
59 }
60 return $component_paths;
61 }
62
63 /**
64 * Searches a directory recursively for PHP classes.
65 *
66 * @param string $dir
67 * The full path to the directory that should be checked.
68 *
69 * @return array
70 * An array of class paths.
71 */
72 protected function findPhpClasses($dir) {
73 $classes = [];
74 foreach (new \DirectoryIterator($dir) as $file) {
75 if ($file->isDir() && !$file->isDot()) {
76 $classes = array_merge($classes, $this->findPhpClasses($file->getPathname()));
77 }
78 elseif ($file->getExtension() == 'php') {
79 $classes[] = $file->getPathname();
80 }
81 }
82
83 return $classes;
84 }
85
86 /**
87 * Asserts that the given class is not using any class from Core namespace.
88 *
89 * @param string $class_path
90 * The full path to the class that should be checked.
91 */
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;
98 });
99 $this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path");
100 }
101
102 /**
103 * Data provider for testAssertNoCoreUseage().
104 *
105 * @return array
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.
109 */
110 public function providerAssertNoCoreUseage() {
111 return [
112 [
113 TRUE,
114 '@see \\Drupal\\Core\\Something',
115 ],
116 [
117 FALSE,
118 '\\Drupal\\Core\\Something',
119 ],
120 [
121 FALSE,
122 "@see \\Drupal\\Core\\Something\n" .
123 '\\Drupal\\Core\\Something',
124 ],
125 [
126 FALSE,
127 "\\Drupal\\Core\\Something\n" .
128 '@see \\Drupal\\Core\\Something',
129 ],
130 ];
131 }
132
133 /**
134 * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage
135 * @dataProvider providerAssertNoCoreUseage
136 */
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');
142
143 try {
144 $pass = TRUE;
145 $this->assertNoCoreUsage($file_uri);
146 }
147 catch (\PHPUnit_Framework_AssertionFailedError $e) {
148 $pass = FALSE;
149 }
150 $this->assertEquals($expected_pass, $pass, $expected_pass ?
151 'Test caused a false positive' :
152 'Test failed to detect Core usage');
153 }
154
155 }