50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Support\CheckedFile;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use RuntimeException;
|
|
|
|
class CheckedFileTest extends TestCase
|
|
{
|
|
public function test_empty_file_is_valid_content_with_a_real_hash_and_zero_size(): void
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'investmoz-file-');
|
|
try {
|
|
self::assertSame('', CheckedFile::read($path));
|
|
self::assertSame(hash('sha256', ''), CheckedFile::sha256($path));
|
|
self::assertSame(0, CheckedFile::size($path));
|
|
} finally {
|
|
unlink($path);
|
|
}
|
|
}
|
|
|
|
public static function operations(): array
|
|
{
|
|
return [['read'], ['sha256'], ['size']];
|
|
}
|
|
|
|
public function test_integrity_check_reads_again_after_content_changes(): void
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'investmoz-integrity-');
|
|
try {
|
|
file_put_contents($path, 'original');
|
|
$expected = CheckedFile::sha256($path);
|
|
self::assertTrue(CheckedFile::matches($path, $expected));
|
|
file_put_contents($path, 'changed');
|
|
self::assertFalse(CheckedFile::matches($path, $expected));
|
|
} finally {
|
|
unlink($path);
|
|
}
|
|
}
|
|
|
|
#[DataProvider('operations')]
|
|
public function test_missing_file_cannot_be_converted_to_empty_content_or_a_false_hash(string $operation): void
|
|
{
|
|
$this->expectException(RuntimeException::class);
|
|
CheckedFile::$operation(sys_get_temp_dir().'/investmoz-absent-'.bin2hex(random_bytes(12)));
|
|
}
|
|
}
|