3 namespace Drupal\Tests\libraries\Unit\Plugin\libraries\VersionDetector;
5 use Drupal\libraries\ExternalLibrary\Local\LocalLibraryInterface;
6 use Drupal\libraries\ExternalLibrary\Version\VersionedLibraryInterface;
7 use Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector;
8 use Drupal\Tests\UnitTestCase;
9 use org\bovigo\vfs\vfsStream;
12 * Tests the line pattern version detector.
16 * @coversDefaultClass \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector
18 class LinePatternDetectorTest extends UnitTestCase {
20 protected $libraryId = 'test_library';
23 * Tests that version detection fails for a non-local library.
25 * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
27 * @covers ::detectVersion
29 public function testDetectVersionNonLocal() {
30 $library = $this->prophesize(VersionedLibraryInterface::class);
31 $detector = $this->setupDetector();
32 $detector->detectVersion($library->reveal());
36 * Tests that version detection fails for a missing file.
38 * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
40 * @covers ::detectVersion
42 public function testDetectVersionMissingFile() {
43 $library = $this->setupLibrary();
45 $detector = $this->setupDetector(['file' => 'CHANGELOG.txt']);
46 $detector->detectVersion($library->reveal());
50 * Tests that version detection fails without a version in the file.
52 * @dataProvider providerTestDetectVersionNoVersion
54 * @covers ::detectVersion
56 public function testDetectVersionNoVersion($configuration, $file_contents) {
57 $library = $this->setupLibrary();
59 $detector = $this->setupDetector($configuration);
60 $this->setupFile($configuration['file'], $file_contents);
62 $library->setVersion()->shouldNotBeCalled();
63 $detector->detectVersion($library->reveal());
69 public function providerTestDetectVersionNoVersion() {
73 'file' => 'CHANGELOG.txt',
74 'pattern' => '/@version (\d+\.\d+\.\d+)/'
77 $test_cases['empty_file'] = [$configuration, ''];
79 $test_cases['no_version'] = [$configuration, <<<EOF
81 multiple lines that does
82 not contain a version.
86 $configuration['lines'] = 3;
87 $test_cases['long_file'] = [$configuration, <<<EOF
89 contains the version after
90 the maximum number of lines
91 to test has been surpassed.
97 $configuration['columns'] = 10;
98 // @todo Document why this is necessary.
99 $configuration['lines'] = 2;
100 $test_cases['long_column'] = [$configuration, <<<EOF
101 This is a file that contains the version after
102 the maximum number of columns to test has been surpassed. @version 1.2.3
110 * Tests that version detection succeeds with a version in the file.
112 * @dataProvider providerTestDetectVersion
114 * @covers ::detectVersion
116 public function testDetectVersion($configuration, $file_contents, $version) {
117 $library = $this->setupLibrary();
119 $detector = $this->setupDetector($configuration);
120 $this->setupFile($configuration['file'], $file_contents);
122 $library->setVersion($version)->shouldBeCalled();
123 $detector->detectVersion($library->reveal());
129 public function providerTestDetectVersion() {
133 'file' => 'CHANGELOG.txt',
134 'pattern' => '/@version (\d+\.\d+\.\d+)/'
138 $test_cases['version'] = [$configuration, <<<EOF
139 This a file with a version
149 * Sets up the library prophecy and returns it.
151 * @return \Prophecy\Prophecy\ObjectProphecy
153 protected function setupLibrary() {
154 $library = $this->prophesize(VersionedLibraryInterface::class);
155 $library->willImplement(LocalLibraryInterface::class);
156 $library->getId()->willReturn($this->libraryId);
157 $library->getLocalPath()->willReturn('libraries/' . $this->libraryId);
162 * Sets up the version detector for testing and returns it.
164 * @param array $configuration
165 * The plugin configuration to set the version detector up with.
167 * @return \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector
168 * The line pattern version detector to test.
170 protected function setupDetector(array $configuration = []) {
172 vfsStream::setup($app_root);
174 $plugin_id = 'line_pattern';
175 $plugin_definition = [
177 'class' => LinePatternDetector::class,
178 'provider' => 'libraries',
180 return new LinePatternDetector($configuration, $plugin_id, $plugin_definition, 'vfs://' . $app_root);
185 * @param $file_contents
187 protected function setupFile($file, $file_contents) {
190 $this->libraryId => [
191 $file => $file_contents,