- Return 0.0.0 if no version

- Add unit test
This commit is contained in:
Steve Boyd 2018-07-11 16:24:57 +12:00
parent 4429acb6ec
commit eebb738bd6
2 changed files with 41 additions and 7 deletions

View File

@ -71,13 +71,7 @@ class TikaServerTextExtractor extends FileTextExtractor
public function isAvailable()
{
$version = $this->getVersion();
// ensure that the version number has a major, minor and patch number
// reason being that version_compare('1.7', '1.7.0') will return -1 instead of 0
for ($i = 0; $i < 2; $i++) {
if (substr_count($version, '.') < 2) {
$version .= '.0';
}
}
$version = $this->normaliseVersion($version);
return $this->getServerEndpoint() &&
$this->getClient()->isAvailable() &&
version_compare($version, '1.7.0') >= 0;
@ -121,4 +115,24 @@ class TikaServerTextExtractor extends FileTextExtractor
{
return $this->getClient()->tika($path);
}
/**
* Ensure that the version number has a major, minor and patch number
* Reason being that version_compare('1.7', '1.7.0') will return -1 instead of 0
*
* @param $version
* @return string
*/
protected function normaliseVersion($version)
{
if (!$version) {
return '0.0.0';
}
for ($i = 0; $i < 2; $i++) {
if (substr_count($version, '.') < 2) {
$version .= '.0';
}
}
return $version;
}
}

View File

@ -40,4 +40,24 @@ class TikaTextExtractorTest extends SapphireTest
$this->assertTrue($extractor->supportsMime('text/html'));
$this->assertFalse($extractor->supportsMime('application/not-supported'));
}
public function testNormaliseVersion()
{
$extractor = new TikaServerTextExtractor();
$reflection = new ReflectionClass($extractor);
$method = $reflection->getMethod('normaliseVersion');
$method->setAccessible(true);
$arr = [
'1.7.1' => '1.7.1',
'1.7' => '1.7.0',
'1' => '1.0.0',
null => '0.0.0',
'v1.5' => 'v1.5.0',
'carrot' => 'carrot.0.0'
];
foreach ($arr as $input => $expected) {
$actual = $method->invoke($extractor, $input);
$this->assertEquals($expected, $actual);
}
}
}