API Add namespaces to tests and update SapphireTest implementation

This commit is contained in:
Robbie Averill 2018-07-03 11:35:24 +12:00
parent 66c9db8c0d
commit fe5148e678
5 changed files with 64 additions and 39 deletions

View File

@ -1,23 +1,23 @@
<?php
use SilverStripe\TextExtraction\Extension\FileTextCache,
SilverStripe\TextExtraction\Extension\FileTextCache_Database,
SilverStripe\Dev\SapphireTest,
SilverStripe\Core\Config\Config;
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Assets\File;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Cache\FileTextCache\Database;
class FileTextCacheDatabaseTest extends SapphireTest
{
public function testTruncatesByMaxLength()
{
Config::nest();
Config::modify()->set(Database::class, 'max_content_length', 5);
Config::inst()->update('FileTextCache_Database', 'max_content_length', 5);
$cache = new FileTextCache_Database();
$file = $this->getMock('File', array('write'));
$cache = new Database();
$file = $this->getMockBuilder(File::class)->setMethods(['write'])->getMock();
$content = '0123456789';
$cache->save($file, $content);
$this->assertEquals($cache->load($file), '01234');
Config::unnest();
$this->assertEquals($cache->load($file), '01234');
}
}

View File

@ -1,46 +1,53 @@
<?php
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Assets\File;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extension\FileTextExtractable;
class FileTextExtractableTest extends SapphireTest
{
protected $requiredExtensions = array(
'File' => array('FileTextExtractable')
);
protected $usesDatabase = true;
public function setUp()
protected static $required_extensions = [
File::class => [
FileTextExtractable::class,
],
];
protected function setUp()
{
parent::setUp();
// Ensure that html is a valid extension
Config::inst()
->nest()
->update('File', 'allowed_extensions', array('html'));
}
public function tearDown()
{
Config::unnest();
parent::tearDown();
Config::modify()->merge(File::class, 'allowed_extensions', ['html']);
}
public function testExtractFileAsText()
{
// Create a copy of the file, as it may be clobbered by the test
// ($file->extractFileAsText() calls $file->write)
copy(BASE_PATH.'/textextraction/tests/fixtures/test1.html', BASE_PATH.'/textextraction/tests/fixtures/test1-copy.html');
copy(
dirname(__FILE__) . '/fixtures/test1.html',
dirname(__FILE__) . '/fixtures/test1-copy.html'
);
// Use HTML, since the extractor is always available
$file = new File(array(
$file = new File([
'Name' => 'test1-copy.html',
'Filename' => 'textextraction/tests/fixtures/test1-copy.html'
));
'Filename' => dirname(__FILE__) . '/fixtures/test1-copy.html'
]);
$file->write();
$content = $file->extractFileAsText();
$this->assertContains('Test Headline', $content);
$this->assertContains('Test Text', $content);
$this->assertEquals($content, $file->FileContentCache);
if (file_exists(BASE_PATH.'/textextraction/tests/fixtures/test1-copy.html')) {
unlink(BASE_PATH.'/textextraction/tests/fixtures/test1-copy.html');
if (file_exists(dirname(__FILE__) . '/fixtures/test1-copy.html')) {
unlink(dirname(__FILE__) . '/fixtures/test1-copy.html');
}
}
}

View File

@ -1,11 +1,18 @@
<?php
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extractor\HTMLTextExtractor;
class HTMLTextExtractorTest extends SapphireTest
{
public function testExtraction()
{
$extractor = new HTMLTextExtractor();
$content = $extractor->getContent(Director::baseFolder() . '/textextraction/tests/fixtures/test1.html');
$content = $extractor->getContent(dirname(__FILE__) . '/fixtures/test1.html');
$this->assertContains('Test Headline', $content);
$this->assertNotContains('Test Comment', $content, 'Strips HTML comments');
$this->assertNotContains('Test Style', $content, 'Strips non-content style tags');

View File

@ -1,17 +1,22 @@
<?php
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extractor\FileTextExtractor\Exception;
use SilverStripe\TextExtraction\Extractor\PDFTextExtractor;
class PDFTextExtractorTest extends SapphireTest
{
public function testExtraction()
{
$extractor = new PDFTextExtractor();
if (!$extractor->isAvailable()) {
$this->setExpectedException(
'FileTextExtractor_Exception',
'getRawOutput called on unavailable extractor'
);
$this->expectException(Exception::class);
$this->expectExceptionMessage('getRawOutput called on unavailable extractor');
}
$content = $extractor->getContent(Director::baseFolder() . '/textextraction/tests/fixtures/test1.pdf');
$content = $extractor->getContent(dirname(__FILE__) . '/fixtures/test1.pdf');
$this->assertContains('This is a test file with a link', $content);
}
}

View File

@ -1,5 +1,11 @@
<?php
namespace SilverStripe\TextExtraction\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\TextExtraction\Extractor\TikaServerTextExtractor;
use SilverStripe\TextExtraction\Extractor\TikaTextExtractor;
/**
* Tests the {@see TikaTextExtractor} class
*/
@ -13,7 +19,7 @@ class TikaTextExtractorTest extends SapphireTest
}
// Check file
$file = Director::baseFolder() . '/textextraction/tests/fixtures/test1.pdf';
$file = dirname(__FILE__) . '/fixtures/test1.pdf';
$content = $extractor->getContent($file);
$this->assertContains('This is a test file with a link', $content);
@ -31,7 +37,7 @@ class TikaTextExtractorTest extends SapphireTest
}
// Check file
$file = Director::baseFolder() . '/textextraction/tests/fixtures/test1.pdf';
$file = dirname(__FILE__) . '/fixtures/test1.pdf';
$content = $extractor->getContent($file);
$this->assertContains('This is a test file with a link', $content);