You are here

class DrupalComponentTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/DrupalComponentTest.php \Drupal\Tests\Component\DrupalComponentTest

General tests for \Drupal\Component that can't go anywhere else.

@group Component

Hierarchy

Expanded class hierarchy of DrupalComponentTest

File

core/tests/Drupal/Tests/Component/DrupalComponentTest.php, line 18
Contains \Drupal\Tests\Component\DrupalComponentTest.

Namespace

Drupal\Tests\Component
View source
class DrupalComponentTest extends UnitTestCase {

  /**
   * Tests that classes in Component do not use any Core class.
   */
  public function testNoCoreInComponent() {
    $component_path = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__))) . '/lib/Drupal/Component';
    foreach ($this
      ->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) {
    $contents = file_get_contents($class_path);
    preg_match_all('/^.*Drupal\\\\Core.*$/m', $contents, $matches);
    $matches = array_filter($matches[0], function ($line) {

      // Filter references to @see as they don't really matter.
      return strpos($line, '@see') === FALSE;
    });
    $this
      ->assertEmpty($matches, "Checking for illegal reference to 'Drupal\\Core' namespace in {$class_path}");
  }

  /**
   * Data provider for testAssertNoCoreUseage().
   *
   * @return array
   *   Data for testAssertNoCoreUseage() in the form:
   *   - TRUE if the test passes, FALSE otherwise.
   *   - File data as a string. This will be used as a virtual file.
   */
  public function providerAssertNoCoreUseage() {
    return array(
      array(
        TRUE,
        '@see \\Drupal\\Core\\Something',
      ),
      array(
        FALSE,
        '\\Drupal\\Core\\Something',
      ),
      array(
        FALSE,
        "@see \\Drupal\\Core\\Something\n" . '\\Drupal\\Core\\Something',
      ),
      array(
        FALSE,
        "\\Drupal\\Core\\Something\n" . '@see \\Drupal\\Core\\Something',
      ),
    );
  }

  /**
   * @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage
   * @dataProvider providerAssertNoCoreUseage
   */
  public function testAssertNoCoreUseage($expected_pass, $file_data) {

    // Set up a virtual file to read.
    $vfs_root = vfsStream::setup('root');
    vfsStream::newFile('Test.php')
      ->at($vfs_root)
      ->setContent($file_data);
    $file_uri = vfsStream::url('root/Test.php');
    try {
      $pass = true;
      $this
        ->assertNoCoreUsage($file_uri);
    } catch (\PHPUnit_Framework_AssertionFailedError $e) {
      $pass = false;
    }
    $this
      ->assertEquals($expected_pass, $pass, $expected_pass ? 'Test caused a false positive' : 'Test failed to detect Core usage');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalComponentTest::assertNoCoreUsage protected function Asserts that the given class is not using any class from Core namespace.
DrupalComponentTest::findPhpClasses protected function Searches a directory recursively for PHP classes.
DrupalComponentTest::providerAssertNoCoreUseage public function Data provider for testAssertNoCoreUseage().
DrupalComponentTest::testAssertNoCoreUseage public function @covers \Drupal\Tests\Component\DrupalComponentTest::assertNoCoreUsage @dataProvider providerAssertNoCoreUseage
DrupalComponentTest::testNoCoreInComponent public function Tests that classes in Component do not use any Core class.
DrupalComponentTest::testNoCoreInComponentTests public function Tests that classes in Component Tests do not use any Core class.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259