View source
<?php
class FeedsFetcherResultTest extends FeedsWebTestCase {
const FEEDS_IN_PROGRESS_DIR = 'private://feeds/in_progress';
const BOM = "";
protected $raw;
protected $fetcherResult;
public static function getInfo() {
return array(
'name' => 'FeedsFetcherResult class test',
'description' => 'Covers class FeedsFetcherResult.',
'group' => 'Feeds',
);
}
public function setUp() {
parent::setUp();
$this->raw = $this
->randomString();
$this->fetcherResult = new FeedsFetcherResult($this->raw);
}
protected function getRestoredFetcherResult(FeedsFetcherResult $result = NULL) {
if (is_null($result)) {
$result = $this->fetcherResult;
}
$serialized = serialize($this->fetcherResult);
return unserialize($serialized);
}
public function testGetRaw() {
$this
->assertEqual($this->raw, $this->fetcherResult
->getRaw());
$restored_fetcher_result = $this
->getRestoredFetcherResult();
$this
->assertEqual($this->raw, $restored_fetcher_result
->getRaw());
}
public function testGetFilePath() {
$file_path = $this->fetcherResult
->getFilePath();
$this
->assertTrue(strpos($file_path, static::FEEDS_IN_PROGRESS_DIR) === 0, format_string('The file path starts with @path.', array(
'@path' => static::FEEDS_IN_PROGRESS_DIR,
)));
$this
->assertTrue(file_exists($file_path), format_string('The file @file exists.', array(
'@file' => $file_path,
)));
$this
->assertEqual($this->raw, file_get_contents($file_path));
}
public function testGetFeedsInProgressDir() {
$this
->assertEqual(static::FEEDS_IN_PROGRESS_DIR, $this->fetcherResult
->getFeedsInProgressDir());
}
public function testConstructFilePath() {
$file_path = $this->fetcherResult
->getFeedsInProgressDir();
$this
->assertTrue(strpos($file_path, static::FEEDS_IN_PROGRESS_DIR) === 0, format_string('The file path starts with @path.', array(
'@path' => static::FEEDS_IN_PROGRESS_DIR,
)));
}
public function testRawExists() {
$this
->assertTrue($this->fetcherResult
->rawExists());
$fetcher_empty_result = new FeedsFetcherResult(NULL);
$this
->assertFalse($fetcher_empty_result
->rawExists());
$restored_fetcher_result = $this
->getRestoredFetcherResult();
$this
->assertTrue($restored_fetcher_result
->rawExists());
}
public function testFileExists() {
$this->fetcherResult
->saveRawToFile();
$this
->assertTrue($this->fetcherResult
->fileExists());
$file_path = $this->fetcherResult
->getFilePath();
drupal_unlink($file_path);
$this
->assertFalse(file_exists($file_path), format_string('The file @file is removed.', array(
'@file' => $file_path,
)));
$this
->assertFalse($this->fetcherResult
->fileExists());
}
public function testGetFileContents() {
$this->fetcherResult
->saveRawToFile();
$new_raw = $this
->randomString();
$file_path = $this->fetcherResult
->getFilePath();
file_put_contents($file_path, $new_raw);
$this
->assertEqual($new_raw, $this->fetcherResult
->getFileContents());
}
public function testSaveRawToFile() {
$this->fetcherResult
->saveRawToFile();
$file_path = $this->fetcherResult
->getFilePath();
$this
->assertEqual($this->raw, file_get_contents($file_path));
}
public function testSanitizeRaw() {
$raw_result = $this
->randomString();
$raw = static::BOM . $raw_result;
$fetcher_result = new FeedsFetcherResult($raw);
$this
->assertEqual($raw_result, $fetcher_result
->sanitizeRaw($raw));
}
public function testSanitizeRawOptimized() {
$raw_result = $this
->randomString();
$raw = static::BOM . $raw_result;
$fetcher_result = new FeedsFetcherResult($raw);
$this
->assertEqual($raw_result, $fetcher_result
->sanitizeRawOptimized($raw));
$this
->assertEqual($raw_result, $raw);
}
public function testSanitizeFile() {
$this->fetcherResult
->saveRawToFile();
$raw_result = $this
->randomString();
$raw = static::BOM . $raw_result;
$file_path = $this->fetcherResult
->getFilePath();
file_put_contents($file_path, $raw);
$this->fetcherResult
->sanitizeFile($file_path);
$this
->assertEqual($raw_result, file_get_contents($file_path));
}
public function testSerializing() {
$serialized = serialize($this->fetcherResult);
$this
->assertFalse(strpos($serialized, $this->raw), 'The raw result does not appear in the serialized data.');
$this
->assertEqual($this->raw, $this->fetcherResult
->getRaw());
}
}