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, ], ], ]); } }