Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/MapUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,16 @@ public function selectBaseMap(string $mapClass): MapUpdater
* @return list<string>
* A list of error messages.
*
* @throws \RuntimeException
* If it was not possible to access the file.
* @throws SourceUpdateException
* If it was not possible to access the source file.
*/
public function loadMapFromApacheFile(string $source_file): array
{
$errors = [];

$lines = @file($source_file);
if ($lines == false) {
$errors[] = "Failed accessing {$source_file}";
return $errors;
throw new SourceUpdateException("Failed accessing {$source_file}");
}
$i = 1;
foreach ($lines as $line) {
Expand Down Expand Up @@ -110,15 +109,17 @@ public function loadMapFromApacheFile(string $source_file): array
*
* @return list<string>
* A list of error messages.
*
* @throws SourceUpdateException
* If it was not possible to access the source file.
*/
public function loadMapFromFreedesktopFile(string $source_file): array
{
$errors = [];

$contents = @file_get_contents($source_file);
if ($contents == false) {
$errors[] = 'Failed loading file ' . $source_file;
return $errors;
throw new SourceUpdateException('Failed loading file ' . $source_file);
}

$xml = @simplexml_load_string($contents);
Expand Down
10 changes: 10 additions & 0 deletions src/SourceUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace FileEye\MimeMap;

/**
* Exception thrown when data sources for map updates are not available.
*/
class SourceUpdateException extends \RuntimeException
{
}
16 changes: 6 additions & 10 deletions tests/src/MapUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FileEye\MimeMap\Map\MiniMap;
use FileEye\MimeMap\MapHandler;
use FileEye\MimeMap\MapUpdater;
use FileEye\MimeMap\SourceUpdateException;
use PHPUnit\Framework\Attributes\BackupStaticProperties;
use PHPUnit\Framework\Attributes\CoversClass;

Expand Down Expand Up @@ -57,17 +58,14 @@ public function testLoadMapFromApacheFile(): void

public function testLoadMapFromApacheFileZeroLines(): void
{
$this->expectException(SourceUpdateException::class);
$this->updater->loadMapFromApacheFile(dirname(__FILE__) . '/../fixtures/zero.mime-types.txt');
// @phpstan-ignore method.impossibleType
$this->assertSame([], $this->newMap->getMapArray());
}

public function testLoadMapFromApacheMissingFile(): void
{
$this->assertSame(
["Failed accessing certainly_missing.xml"],
$this->updater->loadMapFromApacheFile('certainly_missing.xml')
);
$this->expectException(SourceUpdateException::class);
$this->updater->loadMapFromApacheFile('certainly_missing.txt');
}

public function testApplyOverridesFailure(): void
Expand Down Expand Up @@ -129,10 +127,8 @@ public function testLoadMapFromFreedesktopFileZeroLines(): void

public function testLoadMapFromFreedesktopMissingFile(): void
{
$this->assertSame(
["Failed loading file certainly_missing.xml"],
$this->updater->loadMapFromFreedesktopFile('certainly_missing.xml')
);
$this->expectException(SourceUpdateException::class);
$this->updater->loadMapFromFreedesktopFile('certainly_missing.xml');
}

public function testLoadMapFromFreedesktopInvalidFile(): void
Expand Down
Loading