X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Flibraries%2Ftests%2Fsrc%2FUnit%2FPlugin%2Flibraries%2FVersionDetector%2FLinePatternDetectorTest.php;fp=web%2Fmodules%2Fcontrib%2Flibraries%2Ftests%2Fsrc%2FUnit%2FPlugin%2Flibraries%2FVersionDetector%2FLinePatternDetectorTest.php;h=5e4e34b93148f7b195e3bae014ac18db940870c5;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php b/web/modules/contrib/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php new file mode 100644 index 000000000..5e4e34b93 --- /dev/null +++ b/web/modules/contrib/libraries/tests/src/Unit/Plugin/libraries/VersionDetector/LinePatternDetectorTest.php @@ -0,0 +1,197 @@ +prophesize(VersionedLibraryInterface::class); + $detector = $this->setupDetector(); + $detector->detectVersion($library->reveal()); + } + + /** + * Tests that version detection fails for a missing file. + * + * @expectedException \Drupal\libraries\ExternalLibrary\Exception\UnknownLibraryVersionException + * + * @covers ::detectVersion + */ + public function testDetectVersionMissingFile() { + $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, <<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, <<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, + ], + ], + ]); + } + +}