Seperate Tika tests, group them for phpunit, further reduce log level, make Extractors injectable

This commit is contained in:
Robbie Averill 2018-07-03 17:15:16 +12:00
parent 397e7a5d40
commit 9e8ed243d0
4 changed files with 43 additions and 25 deletions

View File

@ -6,6 +6,7 @@ use SilverStripe\Assets\File;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\TextExtraction\Extractor\FileTextExtractor\Exception; use SilverStripe\TextExtraction\Extractor\FileTextExtractor\Exception;
@ -16,6 +17,7 @@ use SilverStripe\TextExtraction\Extractor\FileTextExtractor\Exception;
abstract class FileTextExtractor abstract class FileTextExtractor
{ {
use Configurable; use Configurable;
use Injectable;
/** /**
* Set priority from 0-100. * Set priority from 0-100.

View File

@ -58,7 +58,7 @@ class TikaRestClient extends Client
} }
} catch (RequestException $ex) { } catch (RequestException $ex) {
$msg = sprintf("Tika unavailable - %s", $ex->getMessage()); $msg = sprintf("Tika unavailable - %s", $ex->getMessage());
Injector::inst()->get(LoggerInterface::class)->notice($msg); Injector::inst()->get(LoggerInterface::class)->info($msg);
return false; return false;
} }
@ -139,7 +139,7 @@ class TikaRestClient extends Client
$msg .= ' Body: ' . $body; $msg .= ' Body: ' . $body;
} }
Injector::inst()->get(LoggerInterface::class)->notice($msg); Injector::inst()->get(LoggerInterface::class)->info($msg);
} }
return $text; return $text;

View File

@ -0,0 +1,36 @@
<?php
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extractor\TikaServerTextExtractor;
/**
* @group tika-tests
*/
class TikaServerTextExtractorTest extends SapphireTest
{
protected $usesDatabase = true;
public function testServerExtraction()
{
$extractor = TikaServerTextExtractor::create();
if (!$extractor->isAvailable()) {
$this->markTestSkipped('tika server not available');
}
// Check file
$file = new File();
$file->setFromLocalFile(dirname(__FILE__) . '/fixtures/test1.pdf');
$file->write();
$content = $extractor->getContent($file);
$this->assertContains('This is a test file with a link', $content);
// Check mime validation
$this->assertTrue($extractor->supportsMime('application/pdf'));
$this->assertTrue($extractor->supportsMime('text/html'));
$this->assertFalse($extractor->supportsMime('application/not-supported'));
}
}

View File

@ -4,11 +4,12 @@ namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extractor\TikaServerTextExtractor;
use SilverStripe\TextExtraction\Extractor\TikaTextExtractor; use SilverStripe\TextExtraction\Extractor\TikaTextExtractor;
/** /**
* Tests the {@see TikaTextExtractor} class * Tests the {@see TikaTextExtractor} class
*
* @group tika-tests
*/ */
class TikaTextExtractorTest extends SapphireTest class TikaTextExtractorTest extends SapphireTest
{ {
@ -16,7 +17,7 @@ class TikaTextExtractorTest extends SapphireTest
public function testExtraction() public function testExtraction()
{ {
$extractor = new TikaTextExtractor(); $extractor = TikaTextExtractor::create();
if (!$extractor->isAvailable()) { if (!$extractor->isAvailable()) {
$this->markTestSkipped('tika cli not available'); $this->markTestSkipped('tika cli not available');
} }
@ -34,25 +35,4 @@ class TikaTextExtractorTest extends SapphireTest
$this->assertTrue($extractor->supportsMime('text/html')); $this->assertTrue($extractor->supportsMime('text/html'));
$this->assertFalse($extractor->supportsMime('application/not-supported')); $this->assertFalse($extractor->supportsMime('application/not-supported'));
} }
public function testServerExtraction()
{
$extractor = new TikaServerTextExtractor();
if (!$extractor->isAvailable()) {
$this->markTestSkipped('tika server not available');
}
// Check file
$file = new File();
$file->setFromLocalFile(dirname(__FILE__) . '/fixtures/test1.pdf');
$file->write();
$content = $extractor->getContent($file);
$this->assertContains('This is a test file with a link', $content);
// Check mime validation
$this->assertTrue($extractor->supportsMime('application/pdf'));
$this->assertTrue($extractor->supportsMime('text/html'));
$this->assertFalse($extractor->supportsMime('application/not-supported'));
}
} }