Skip to content

Commit 8068b5d

Browse files
authored
Fail hard when update source is unreachable (#97)
1 parent 49d60ef commit 8068b5d

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/MapUpdater.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ public function selectBaseMap(string $mapClass): MapUpdater
6666
* @return list<string>
6767
* A list of error messages.
6868
*
69-
* @throws \RuntimeException
70-
* If it was not possible to access the file.
69+
* @throws SourceUpdateException
70+
* If it was not possible to access the source file.
7171
*/
7272
public function loadMapFromApacheFile(string $source_file): array
7373
{
7474
$errors = [];
7575

7676
$lines = @file($source_file);
7777
if ($lines == false) {
78-
$errors[] = "Failed accessing {$source_file}";
79-
return $errors;
78+
throw new SourceUpdateException("Failed accessing {$source_file}");
8079
}
8180
$i = 1;
8281
foreach ($lines as $line) {
@@ -110,15 +109,17 @@ public function loadMapFromApacheFile(string $source_file): array
110109
*
111110
* @return list<string>
112111
* A list of error messages.
112+
*
113+
* @throws SourceUpdateException
114+
* If it was not possible to access the source file.
113115
*/
114116
public function loadMapFromFreedesktopFile(string $source_file): array
115117
{
116118
$errors = [];
117119

118120
$contents = @file_get_contents($source_file);
119121
if ($contents == false) {
120-
$errors[] = 'Failed loading file ' . $source_file;
121-
return $errors;
122+
throw new SourceUpdateException('Failed loading file ' . $source_file);
122123
}
123124

124125
$xml = @simplexml_load_string($contents);

src/SourceUpdateException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace FileEye\MimeMap;
4+
5+
/**
6+
* Exception thrown when data sources for map updates are not available.
7+
*/
8+
class SourceUpdateException extends \RuntimeException
9+
{
10+
}

tests/src/MapUpdaterTest.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FileEye\MimeMap\Map\MiniMap;
88
use FileEye\MimeMap\MapHandler;
99
use FileEye\MimeMap\MapUpdater;
10+
use FileEye\MimeMap\SourceUpdateException;
1011
use PHPUnit\Framework\Attributes\BackupStaticProperties;
1112
use PHPUnit\Framework\Attributes\CoversClass;
1213

@@ -57,17 +58,14 @@ public function testLoadMapFromApacheFile(): void
5758

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

6565
public function testLoadMapFromApacheMissingFile(): void
6666
{
67-
$this->assertSame(
68-
["Failed accessing certainly_missing.xml"],
69-
$this->updater->loadMapFromApacheFile('certainly_missing.xml')
70-
);
67+
$this->expectException(SourceUpdateException::class);
68+
$this->updater->loadMapFromApacheFile('certainly_missing.txt');
7169
}
7270

7371
public function testApplyOverridesFailure(): void
@@ -129,10 +127,8 @@ public function testLoadMapFromFreedesktopFileZeroLines(): void
129127

130128
public function testLoadMapFromFreedesktopMissingFile(): void
131129
{
132-
$this->assertSame(
133-
["Failed loading file certainly_missing.xml"],
134-
$this->updater->loadMapFromFreedesktopFile('certainly_missing.xml')
135-
);
130+
$this->expectException(SourceUpdateException::class);
131+
$this->updater->loadMapFromFreedesktopFile('certainly_missing.xml');
136132
}
137133

138134
public function testLoadMapFromFreedesktopInvalidFile(): void

0 commit comments

Comments
 (0)