PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/Tests/Component/DrupalComponentTest.php

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