You are here

class LinePatternDetectorTest in Libraries API 8.3

Tests the line pattern version detector.

@group libraries

@coversDefaultClass \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector

Hierarchy

Expanded class hierarchy of LinePatternDetectorTest

File

tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php, line 19

Namespace

Drupal\Tests\libraries\Unit\Plugin\libraries\VersionDetector
View source
class LinePatternDetectorTest extends UnitTestCase {
  protected $libraryId = 'test_library';

  /**
   * Tests that version detection fails for a non-local library.
   *
   * @covers ::detectVersion
   */
  public function testDetectVersionNonLocal() {
    $this
      ->expectException(UnknownLibraryVersionException::class);
    $library = $this
      ->prophesize(VersionedLibraryInterface::class);
    $detector = $this
      ->setupDetector();
    $detector
      ->detectVersion($library
      ->reveal());
  }

  /**
   * Tests that version detection fails for a missing file.
   *
   * @covers ::detectVersion
   */
  public function testDetectVersionMissingFile() {
    $this
      ->expectException(UnknownLibraryVersionException::class);
    $library = $this
      ->setupLibrary();
    $detector = $this
      ->setupDetector([
      'file' => 'CHANGELOG.txt',
    ]);
    $detector
      ->detectVersion($library
      ->reveal());
  }

  /**
   * Tests that version detection fails without a version in the file.
   *
   * @dataProvider providerTestDetectVersionNoVersion
   *
   * @covers ::detectVersion
   */
  public function testDetectVersionNoVersion($configuration, $file_contents) {
    $library = $this
      ->setupLibrary();
    $detector = $this
      ->setupDetector($configuration);
    $this
      ->setupFile($configuration['file'], $file_contents);
    $library
      ->setVersion()
      ->shouldNotBeCalled();
    $detector
      ->detectVersion($library
      ->reveal());
  }

  /**
   * @return array
   */
  public function providerTestDetectVersionNoVersion() {
    $test_cases = [];
    $configuration = [
      'file' => 'CHANGELOG.txt',
      'pattern' => '/@version (\\d+\\.\\d+\\.\\d+)/',
    ];
    $test_cases['empty_file'] = [
      $configuration,
      '',
    ];
    $test_cases['no_version'] = [
      $configuration,
      <<<EOF
This is a file with
multiple lines that does
not contain a version.
EOF
,
    ];
    $configuration['lines'] = 3;
    $test_cases['long_file'] = [
      $configuration,
      <<<EOF
This is a file that
contains the version after
the maximum number of lines
to test has been surpassed.

@version 1.2.3
EOF
,
    ];
    $configuration['columns'] = 10;

    // @todo Document why this is necessary.
    $configuration['lines'] = 2;
    $test_cases['long_column'] = [
      $configuration,
      <<<EOF
This is a file that contains the version after
the maximum number of columns to test has been surpassed. @version 1.2.3
EOF
,
    ];
    return $test_cases;
  }

  /**
   * Tests that version detection succeeds with a version in the file.
   *
   * @dataProvider providerTestDetectVersion
   *
   * @covers ::detectVersion
   */
  public function testDetectVersion($configuration, $file_contents, $version) {
    $library = $this
      ->setupLibrary();
    $detector = $this
      ->setupDetector($configuration);
    $this
      ->setupFile($configuration['file'], $file_contents);
    $library
      ->setVersion($version)
      ->shouldBeCalled();
    $detector
      ->detectVersion($library
      ->reveal());
  }

  /**
   * @return array
   */
  public function providerTestDetectVersion() {
    $test_cases = [];
    $configuration = [
      'file' => 'CHANGELOG.txt',
      'pattern' => '/@version (\\d+\\.\\d+\\.\\d+)/',
    ];
    $version = '1.2.3';
    $test_cases['version'] = [
      $configuration,
      <<<EOF
This a file with a version

@version {<span class="php-variable">$version</span>}
EOF
,
      $version,
    ];
    return $test_cases;
  }

  /**
   * Sets up the library prophecy and returns it.
   *
   * @return \Prophecy\Prophecy\ObjectProphecy
   */
  protected function setupLibrary() {
    $library = $this
      ->prophesize(VersionedLibraryInterface::class);
    $library
      ->willImplement(LocalLibraryInterface::class);
    $library
      ->getId()
      ->willReturn($this->libraryId);
    $library
      ->getLocalPath()
      ->willReturn('libraries/' . $this->libraryId);
    return $library;
  }

  /**
   * Sets up the version detector for testing and returns it.
   *
   * @param array $configuration
   *   The plugin configuration to set the version detector up with.
   *
   * @return \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector
   *   The line pattern version detector to test.
   */
  protected function setupDetector(array $configuration = []) {
    $app_root = 'root';
    vfsStream::setup($app_root);
    $plugin_id = 'line_pattern';
    $plugin_definition = [
      'id' => $plugin_id,
      'class' => LinePatternDetector::class,
      'provider' => 'libraries',
    ];
    return new LinePatternDetector($configuration, $plugin_id, $plugin_definition, 'vfs://' . $app_root);
  }

  /**
   * @param $file
   * @param $file_contents
   */
  protected function setupFile($file, $file_contents) {
    vfsStream::create([
      'libraries' => [
        $this->libraryId => [
          $file => $file_contents,
        ],
      ],
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinePatternDetectorTest::$libraryId protected property
LinePatternDetectorTest::providerTestDetectVersion public function
LinePatternDetectorTest::providerTestDetectVersionNoVersion public function
LinePatternDetectorTest::setupDetector protected function Sets up the version detector for testing and returns it.
LinePatternDetectorTest::setupFile protected function _contents
LinePatternDetectorTest::setupLibrary protected function Sets up the library prophecy and returns it.
LinePatternDetectorTest::testDetectVersion public function Tests that version detection succeeds with a version in the file.
LinePatternDetectorTest::testDetectVersionMissingFile public function Tests that version detection fails for a missing file.
LinePatternDetectorTest::testDetectVersionNonLocal public function Tests that version detection fails for a non-local library.
LinePatternDetectorTest::testDetectVersionNoVersion public function Tests that version detection fails without a version in the file.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed 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 340