Further modules included.
[yaffs-website] / web / modules / contrib / libraries / tests / src / Unit / Plugin / libraries / VersionDetector / LinePatternDetectorTest.php
1 <?php
2
3 namespace Drupal\Tests\libraries\Unit\Plugin\libraries\VersionDetector;
4
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;
10
11 /**
12  * Tests the line pattern version detector.
13  *
14  * @group libraries
15  *
16  * @coversDefaultClass \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector
17  */
18 class LinePatternDetectorTest extends UnitTestCase {
19
20   protected $libraryId = 'test_library';
21
22   /**
23    * Tests that version detection fails for a non-local library.
24    *
25    * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
26    *
27    * @covers ::detectVersion
28    */
29   public function testDetectVersionNonLocal() {
30     $library = $this->prophesize(VersionedLibraryInterface::class);
31     $detector = $this->setupDetector();
32     $detector->detectVersion($library->reveal());
33   }
34
35   /**
36    * Tests that version detection fails for a missing file.
37    *
38    * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException
39    *
40    * @covers ::detectVersion
41    */
42   public function testDetectVersionMissingFile() {
43     $library = $this->setupLibrary();
44
45     $detector = $this->setupDetector(['file' => 'CHANGELOG.txt']);
46     $detector->detectVersion($library->reveal());
47   }
48
49   /**
50    * Tests that version detection fails without a version in the file.
51    *
52    * @dataProvider providerTestDetectVersionNoVersion
53    *
54    * @covers ::detectVersion
55    */
56   public function testDetectVersionNoVersion($configuration, $file_contents) {
57     $library = $this->setupLibrary();
58
59     $detector = $this->setupDetector($configuration);
60     $this->setupFile($configuration['file'], $file_contents);
61
62     $library->setVersion()->shouldNotBeCalled();
63     $detector->detectVersion($library->reveal());
64   }
65
66   /**
67    * @return array
68    */
69   public function providerTestDetectVersionNoVersion() {
70     $test_cases = [];
71
72     $configuration = [
73       'file' => 'CHANGELOG.txt',
74       'pattern' => '/@version (\d+\.\d+\.\d+)/'
75     ];
76
77     $test_cases['empty_file'] = [$configuration, ''];
78
79     $test_cases['no_version'] = [$configuration, <<<EOF
80 This is a file with
81 multiple lines that does
82 not contain a version.
83 EOF
84     ];
85
86     $configuration['lines'] = 3;
87     $test_cases['long_file'] = [$configuration, <<<EOF
88 This is a file that
89 contains the version after
90 the maximum number of lines
91 to test has been surpassed.
92
93 @version 1.2.3
94 EOF
95     ];
96
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
103 EOF
104     ];
105
106     return $test_cases;
107   }
108
109   /**
110    * Tests that version detection succeeds with a version in the file.
111    *
112    * @dataProvider providerTestDetectVersion
113    *
114    * @covers ::detectVersion
115    */
116   public function testDetectVersion($configuration, $file_contents, $version) {
117     $library = $this->setupLibrary();
118
119     $detector = $this->setupDetector($configuration);
120     $this->setupFile($configuration['file'], $file_contents);
121
122     $library->setVersion($version)->shouldBeCalled();
123     $detector->detectVersion($library->reveal());
124   }
125
126   /**
127    * @return array
128    */
129   public function providerTestDetectVersion() {
130     $test_cases = [];
131
132     $configuration = [
133       'file' => 'CHANGELOG.txt',
134       'pattern' => '/@version (\d+\.\d+\.\d+)/'
135     ];
136     $version = '1.2.3';
137
138     $test_cases['version'] = [$configuration, <<<EOF
139 This a file with a version
140
141 @version $version
142 EOF
143     , $version];
144
145     return $test_cases;
146   }
147
148   /**
149    * Sets up the library prophecy and returns it.
150    *
151    * @return \Prophecy\Prophecy\ObjectProphecy
152    */
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);
158     return $library;
159   }
160
161   /**
162    * Sets up the version detector for testing and returns it.
163    *
164    * @param array $configuration
165    *   The plugin configuration to set the version detector up with.
166    *
167    * @return \Drupal\libraries\Plugin\libraries\VersionDetector\LinePatternDetector
168    *   The line pattern version detector to test.
169    */
170   protected function setupDetector(array $configuration = []) {
171     $app_root = 'root';
172     vfsStream::setup($app_root);
173
174     $plugin_id = 'line_pattern';
175     $plugin_definition = [
176       'id' => $plugin_id,
177       'class' => LinePatternDetector::class,
178       'provider' => 'libraries',
179     ];
180     return new LinePatternDetector($configuration, $plugin_id, $plugin_definition, 'vfs://' . $app_root);
181   }
182
183   /**
184    * @param $file
185    * @param $file_contents
186    */
187   protected function setupFile($file, $file_contents) {
188     vfsStream::create([
189       'libraries' => [
190         $this->libraryId => [
191           $file => $file_contents,
192         ],
193       ],
194     ]);
195   }
196
197 }