annotate core/tests/Drupal/Tests/Component/DrupalComponentTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\Component;
Chris@0 4
Chris@0 5 use org\bovigo\vfs\vfsStream;
Chris@0 6 use PHPUnit\Framework\TestCase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * General tests for \Drupal\Component that can't go anywhere else.
Chris@0 10 *
Chris@0 11 * @group Component
Chris@0 12 */
Chris@0 13 class DrupalComponentTest extends TestCase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Tests that classes in Component do not use any Core class.
Chris@0 17 */
Chris@0 18 public function testNoCoreInComponent() {
Chris@0 19 $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
Chris@0 20 foreach ($this->findPhpClasses($component_path) as $class) {
Chris@0 21 $this->assertNoCoreUsage($class);
Chris@0 22 }
Chris@0 23 }
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Tests that classes in Component Tests do not use any Core class.
Chris@0 27 */
Chris@0 28 public function testNoCoreInComponentTests() {
Chris@0 29 $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/tests/Drupal/Tests/Component';
Chris@0 30 foreach ($this->findPhpClasses($component_path) as $class) {
Chris@0 31 $this->assertNoCoreUsage($class);
Chris@0 32 }
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@14 36 * Tests LICENSE.txt is present and has the correct content.
Chris@14 37 *
Chris@14 38 * @param $component_path
Chris@14 39 * The path to the component.
Chris@14 40 * @dataProvider \Drupal\Tests\Component\DrupalComponentTest::getComponents
Chris@14 41 */
Chris@14 42 public function testComponentLicence($component_path) {
Chris@14 43 $this->assertFileExists($component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt');
Chris@14 44 $this->assertSame('e84dac1d9fbb5a4a69e38654ce644cea769aa76b', hash_file('sha1', $component_path . DIRECTORY_SEPARATOR . 'LICENSE.txt'));
Chris@14 45 }
Chris@14 46
Chris@14 47 /**
Chris@14 48 * Data provider.
Chris@14 49 *
Chris@14 50 * @return array
Chris@14 51 */
Chris@14 52 public function getComponents() {
Chris@14 53 $root_component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
Chris@14 54 $component_paths = [];
Chris@14 55 foreach (new \DirectoryIterator($root_component_path) as $file) {
Chris@14 56 if ($file->isDir() && !$file->isDot()) {
Chris@14 57 $component_paths[$file->getBasename()] = [$file->getPathname()];
Chris@14 58 }
Chris@14 59 }
Chris@14 60 return $component_paths;
Chris@14 61 }
Chris@14 62
Chris@14 63 /**
Chris@0 64 * Searches a directory recursively for PHP classes.
Chris@0 65 *
Chris@0 66 * @param string $dir
Chris@0 67 * The full path to the directory that should be checked.
Chris@0 68 *
Chris@0 69 * @return array
Chris@0 70 * An array of class paths.
Chris@0 71 */
Chris@0 72 protected function findPhpClasses($dir) {
Chris@0 73 $classes = [];
Chris@0 74 foreach (new \DirectoryIterator($dir) as $file) {
Chris@0 75 if ($file->isDir() && !$file->isDot()) {
Chris@0 76 $classes = array_merge($classes, $this->findPhpClasses($file->getPathname()));
Chris@0 77 }
Chris@0 78 elseif ($file->getExtension() == 'php') {
Chris@0 79 $classes[] = $file->getPathname();
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 return $classes;
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * Asserts that the given class is not using any class from Core namespace.
Chris@0 88 *
Chris@0 89 * @param string $class_path
Chris@0 90 * The full path to the class that should be checked.
Chris@0 91 */
Chris@0 92 protected function assertNoCoreUsage($class_path) {
Chris@0 93 $contents = file_get_contents($class_path);
Chris@0 94 preg_match_all('/^.*Drupal\\\Core.*$/m', $contents, $matches);
Chris@0 95 $matches = array_filter($matches[0], function ($line) {
Chris@0 96 // Filter references to @see as they don't really matter.
Chris@0 97 return strpos($line, '@see') === FALSE;
Chris@0 98 });
Chris@0 99 $this->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in $class_path");
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Data provider for testAssertNoCoreUseage().
Chris@0 104 *
Chris@0 105 * @return array
Chris@0 106 * Data for testAssertNoCoreUseage() in the form:
Chris@0 107 * - TRUE if the test passes, FALSE otherwise.
Chris@0 108 * - File data as a string. This will be used as a virtual file.
Chris@0 109 */
Chris@0 110 public function providerAssertNoCoreUseage() {
Chris@0 111 return [
Chris@0 112 [
Chris@0 113 TRUE,
Chris@0 114 '@see \\Drupal\\Core\\Something',
Chris@0 115 ],
Chris@0 116 [
Chris@0 117 FALSE,
Chris@0 118 '\\Drupal\\Core\\Something',
Chris@0 119 ],
Chris@0 120 [
Chris@0 121 FALSE,
Chris@0 122 "@see \\Drupal\\Core\\Something\n" .
Chris@0 123 '\\Drupal\\Core\\Something',
Chris@0 124 ],
Chris@0 125 [
Chris@0 126 FALSE,
Chris@0 127 "\\Drupal\\Core\\Something\n" .
Chris@0 128 '@see \\Drupal\\Core\\Something',
Chris@0 129 ],
Chris@0 130 ];
Chris@0 131 }
Chris@0 132
Chris@0 133 /**
Chris@0 134 * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage
Chris@0 135 * @dataProvider providerAssertNoCoreUseage
Chris@0 136 */
Chris@0 137 public function testAssertNoCoreUseage($expected_pass, $file_data) {
Chris@0 138 // Set up a virtual file to read.
Chris@0 139 $vfs_root = vfsStream::setup('root');
Chris@0 140 vfsStream::newFile('Test.php')->at($vfs_root)->setContent($file_data);
Chris@0 141 $file_uri = vfsStream::url('root/Test.php');
Chris@0 142
Chris@0 143 try {
Chris@0 144 $pass = TRUE;
Chris@0 145 $this->assertNoCoreUsage($file_uri);
Chris@0 146 }
Chris@0 147 catch (\PHPUnit_Framework_AssertionFailedError $e) {
Chris@0 148 $pass = FALSE;
Chris@0 149 }
Chris@0 150 $this->assertEquals($expected_pass, $pass, $expected_pass ?
Chris@0 151 'Test caused a false positive' :
Chris@0 152 'Test failed to detect Core usage');
Chris@0 153 }
Chris@0 154
Chris@0 155 }