From 914b78de24a048f45768a27f1c9d17833131761c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 3 Nov 2014 10:47:06 +0000 Subject: [PATCH] Issue #2282673 by stevepurkiss, ParisLiakos, mgifford: Add a PHPunit test for not using Drupal\Core code in Drupal\Component. --- .../Drupal/Component/Annotation/PluginID.php | 2 +- .../Exception/InvalidDecoratedMethod.php | 2 +- .../Drupal/Component/Utility/SortArray.php | 10 +-- .../Drupal/Component/Utility/UrlHelper.php | 5 +- .../Tests/Component/DrupalComponentTest.php | 78 +++++++++++++++++++ .../PhpStorage/PhpStorageTestBase.php | 17 ---- .../Tests/Component/Plugin/PluginBaseTest.php | 1 - .../Drupal/Tests/Component/Uuid/UuidTest.php | 1 - 8 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 core/tests/Drupal/Tests/Component/DrupalComponentTest.php diff --git a/core/lib/Drupal/Component/Annotation/PluginID.php b/core/lib/Drupal/Component/Annotation/PluginID.php index fe8e599c9ea..ab5e1938178 100644 --- a/core/lib/Drupal/Component/Annotation/PluginID.php +++ b/core/lib/Drupal/Component/Annotation/PluginID.php @@ -24,7 +24,7 @@ class PluginID extends AnnotationBase { public $value; /** - * Implements \Drupal\Core\Annotation\AnnotationInterface::get(). + * {@inheritdoc} */ public function get() { return array( diff --git a/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php b/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php index a810063bf1f..57fd8359e5f 100644 --- a/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php +++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidDecoratedMethod.php @@ -1,7 +1,7 @@ findPhpClasses($component_path) as $class) { + $this->assertNoCoreUsage($class); + } + } + + /** + * Tests that classes in Component Tests do not use any Core class. + */ + public function testNoCoreInComponentTests() { + $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/tests/Drupal/Tests/Component'; + foreach ($this->findPhpClasses($component_path) as $class) { + $this->assertNoCoreUsage($class); + } + } + + /** + * Searches a directory recursively for PHP classes. + * + * @param string $dir + * The full path to the directory that should be checked. + * + * @return array + * An array of class paths. + */ + protected function findPhpClasses($dir) { + $classes = array(); + foreach (new \DirectoryIterator($dir) as $file) { + if ($file->isDir() && !$file->isDot()) { + $classes = array_merge($classes, $this->findPhpClasses($file->getPathname())); + } + elseif ($file->getExtension() == 'php') { + $classes[] = $file->getPathname(); + } + } + + return $classes; + } + + /** + * Asserts that the given class is not using any class from Core namespace. + * + * @param string $class_path + * The full path to the class that should be checked. + */ + protected function assertNoCoreUsage($class_path) { + foreach (new \SplFileObject($class_path) as $line_number => $line) { + // Allow linking to Core files with @see docs. Its harmless and boosts DX + // because even outside projects can treat those links as examples. + if ($line && (strpos($line, '@see ') === FALSE)) { + $this->assertSame(FALSE, strpos($line, 'Drupal\\Core'), "Illegal reference to 'Drupal\\Core' namespace in $class_path at line $line_number"); + } + } + } + +} diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php index 2c179a81bd1..aa54598c4c1 100644 --- a/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php +++ b/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php @@ -8,29 +8,12 @@ namespace Drupal\Tests\Component\PhpStorage; use Drupal\Tests\UnitTestCase; -use Drupal\Core\PhpStorage\PhpStorageFactory; /** * Base test for PHP storages. */ abstract class PhpStorageTestBase extends UnitTestCase { - /** - * The storage factory object. - * - * @var \Drupal\Component\PhpStorage\PhpStorageFactory - */ - protected $storageFactory; - - /** - * {@inheritdoc} - */ - protected function setUp() { - parent::setUp(); - - $this->storageFactory = new PhpStorageFactory(); - } - /** * Assert that a PHP storage's load/save/delete operations work. */ diff --git a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php index 97e4275acf9..585bd37b5ca 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php @@ -7,7 +7,6 @@ namespace Drupal\Tests\Component\Plugin; -use Drupal\Core\PhpStorage\PhpStorageFactory; use Drupal\Tests\UnitTestCase; /** diff --git a/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php index 1f008196328..353c3520a82 100644 --- a/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php +++ b/core/tests/Drupal/Tests/Component/Uuid/UuidTest.php @@ -12,7 +12,6 @@ use Drupal\Component\Uuid\UuidInterface; use Drupal\Component\Uuid\Com; use Drupal\Component\Uuid\Pecl; use Drupal\Component\Uuid\Php; -use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /**