packages/requirements/tests/Unit/Source/LocalFileTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Source;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use Requirements\Source\LocalFile;
11use RuntimeException;
12use Tests\Fake\ProjectDirectory;
13
14#[CoversClass(LocalFile::class)]
15#[Medium]
16final class LocalFileTest extends TestCase
17{
18 public function testReadReturnsTheContents(): void
19 {
20 $project = new ProjectDirectory();
21
22 self::assertSame("Names shall start with a letter.\n", (new LocalFile())->read($project->put('notes.txt', "Names shall start with a letter.\n")));
23 }
24
25 public function testReadReturnsAnEmptyFile(): void
26 {
27 $project = new ProjectDirectory();
28
29 self::assertSame('', (new LocalFile())->read($project->put('empty.txt', '')));
30 }
31
32 public function testReadReturnsNothingForADirectory(): void
33 {
34 $project = new ProjectDirectory();
35
36 self::assertSame('', (new LocalFile())->read($project->directory));
37 }
38
39 public function testReadAcceptsTheSizeLimit(): void
40 {
41 $project = new ProjectDirectory();
42
43 self::assertSame(16777216, strlen((new LocalFile())->read($project->put('large.txt', str_repeat('x', 16777216)))));
44 }
45
46 public function testReadRejectsContentsOverTheSizeLimit(): void
47 {
48 $project = new ProjectDirectory();
49 $path = $project->put('large.txt', str_repeat('x', 16777217));
50
51 $this->expectException(RuntimeException::class);
52 $this->expectExceptionMessage('Unreadable resource or 16 MiB size limit exceeded.');
53 (new LocalFile())->read($path);
54 }
55
56 public function testReadRejectsReadableNonFiles(): void
57 {
58 $this->expectException(RuntimeException::class);
59 $this->expectExceptionMessage('Unreadable resource or 16 MiB size limit exceeded.');
60 (new LocalFile())->read('/dev/null');
61 }
62
63 public function testReadRejectsMissingFiles(): void
64 {
65 $project = new ProjectDirectory();
66
67 $this->expectException(RuntimeException::class);
68 $this->expectExceptionMessage('Unreadable resource or 16 MiB size limit exceeded.');
69 (new LocalFile())->read($project->path('missing.txt'));
70 }
71}
72