-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemImageSourceReaderTest.php
More file actions
86 lines (67 loc) · 2.69 KB
/
FilesystemImageSourceReaderTest.php
File metadata and controls
86 lines (67 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Tests\Unit\Pdf;
use LibreSign\XObjectTemplate\Pdf\FilesystemImageSourceReader;
use LibreSign\XObjectTemplate\Pdf\WarningToExceptionConverterInterface;
use LibreSign\XObjectTemplate\Tests\Support\UsesTemporaryFiles;
use PHPUnit\Framework\TestCase;
final class FilesystemImageSourceReaderTest extends TestCase
{
use UsesTemporaryFiles;
protected function tearDown(): void
{
$this->tearDownTemporaryFiles();
}
public function testReadReturnsContentsForReadableFiles(): void
{
$reader = new FilesystemImageSourceReader();
$path = $this->createTemporaryFile('png', 'contents');
self::assertSame('contents', $reader->read($path));
}
public function testReadUsesInjectedWarningConverter(): void
{
$reader = new FilesystemImageSourceReader(new class implements WarningToExceptionConverterInterface {
public function run(callable $operation, string $message): mixed
{
return 'converted-contents';
}
});
$path = $this->createTemporaryFile('png', 'disk-contents');
self::assertSame('converted-contents', $reader->read($path));
}
public function testReadRejectsNonStringWarningConverterResult(): void
{
$reader = new FilesystemImageSourceReader(new class implements WarningToExceptionConverterInterface {
public function run(callable $operation, string $message): mixed
{
return false;
}
});
$path = $this->createTemporaryFile('png', 'disk-contents');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Failed to read image source "%s".', $path));
$reader->read($path);
}
public function testReadRejectsMissingSources(): void
{
$reader = new FilesystemImageSourceReader();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('must be an existing file');
$reader->read('/tmp/does-not-exist-preview.png');
}
public function testReadRejectsUnreadableSources(): void
{
$reader = new FilesystemImageSourceReader();
$path = $this->createTemporaryFile('png', 'contents');
chmod($path, 0);
try {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Image source "%s" must be readable.', $path));
$reader->read($path);
} finally {
chmod($path, 0644);
}
}
}