|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Pest\Browser\Exceptions\FileNotReadableException; |
| 6 | +use Pest\Browser\Exceptions\FileTooLargeException; |
| 7 | +use Pest\Browser\Support\FilePayload; |
| 8 | + |
| 9 | +/** |
| 10 | + * Create a temporary file with the given contents and extension. |
| 11 | + */ |
| 12 | +function temporaryFile(string $contents, string $extension = ''): string |
| 13 | +{ |
| 14 | + $path = tempnam(sys_get_temp_dir(), 'pest_payload'); |
| 15 | + |
| 16 | + if ($extension !== '') { |
| 17 | + unlink($path); |
| 18 | + $path .= '.'.$extension; |
| 19 | + } |
| 20 | + |
| 21 | + file_put_contents($path, $contents); |
| 22 | + |
| 23 | + return $path; |
| 24 | +} |
| 25 | + |
| 26 | +test('sends the file by value instead of by path', function (): void { |
| 27 | + $path = temporaryFile('hello world', 'txt'); |
| 28 | + |
| 29 | + $payload = FilePayload::fromPath($path)->toArray(); |
| 30 | + |
| 31 | + expect($payload)->toHaveKeys(['name', 'mimeType', 'buffer']) |
| 32 | + ->and($payload)->not->toHaveKey('localPaths') |
| 33 | + ->and($payload['buffer'])->not->toBe($path); |
| 34 | + |
| 35 | + unlink($path); |
| 36 | +}); |
| 37 | + |
| 38 | +test('encodes the buffer as base64', function (): void { |
| 39 | + $path = temporaryFile('hello world', 'txt'); |
| 40 | + |
| 41 | + $payload = FilePayload::fromPath($path)->toArray(); |
| 42 | + |
| 43 | + expect($payload['buffer'])->toBe(base64_encode('hello world')) |
| 44 | + ->and(base64_decode($payload['buffer'], true))->toBe('hello world'); |
| 45 | + |
| 46 | + unlink($path); |
| 47 | +}); |
| 48 | + |
| 49 | +test('preserves binary contents without corruption', function (): void { |
| 50 | + $contents = ''; |
| 51 | + for ($byte = 0; $byte < 256; $byte++) { |
| 52 | + $contents .= pack('C', $byte); |
| 53 | + } |
| 54 | + $contents .= "\x00\xC3\x28\xFF\xFE invalid utf-8 \r\n "; |
| 55 | + |
| 56 | + $path = temporaryFile($contents, 'bin'); |
| 57 | + |
| 58 | + $payload = FilePayload::fromPath($path)->toArray(); |
| 59 | + $decoded = (string) base64_decode($payload['buffer'], true); |
| 60 | + |
| 61 | + expect(hash('sha256', $decoded))->toBe(hash('sha256', $contents)) |
| 62 | + ->and($decoded)->toBe($contents); |
| 63 | + |
| 64 | + unlink($path); |
| 65 | +}); |
| 66 | + |
| 67 | +test('uses the file name rather than the full path', function (): void { |
| 68 | + $path = temporaryFile('contents', 'txt'); |
| 69 | + |
| 70 | + expect(FilePayload::fromPath($path)->toArray()['name'])->toBe(basename($path)); |
| 71 | + |
| 72 | + unlink($path); |
| 73 | +}); |
| 74 | + |
| 75 | +test('guesses the mime type from the extension', function (): void { |
| 76 | + $path = temporaryFile('{}', 'json'); |
| 77 | + |
| 78 | + expect(FilePayload::fromPath($path)->toArray()['mimeType'])->toBe('application/json'); |
| 79 | + |
| 80 | + unlink($path); |
| 81 | +}); |
| 82 | + |
| 83 | +test('falls back to a generic mime type for unknown extensions', function (): void { |
| 84 | + $path = temporaryFile('contents', 'unknownext'); |
| 85 | + |
| 86 | + expect(FilePayload::fromPath($path)->toArray()['mimeType'])->toBe('application/octet-stream'); |
| 87 | + |
| 88 | + unlink($path); |
| 89 | +}); |
| 90 | + |
| 91 | +test('falls back to a generic mime type when there is no extension', function (): void { |
| 92 | + $path = temporaryFile('contents'); |
| 93 | + |
| 94 | + expect(FilePayload::fromPath($path)->toArray()['mimeType'])->toBe('application/octet-stream'); |
| 95 | + |
| 96 | + unlink($path); |
| 97 | +}); |
| 98 | + |
| 99 | +test('fails when the file does not exist', function (): void { |
| 100 | + FilePayload::fromPath('/this/path/does/not/exist.txt'); |
| 101 | +})->throws(FileNotReadableException::class); |
| 102 | + |
| 103 | +test('fails when the path is a directory', function (): void { |
| 104 | + FilePayload::fromPath(sys_get_temp_dir()); |
| 105 | +})->throws(FileNotReadableException::class); |
| 106 | + |
| 107 | +test('fails when the file exceeds the playwright payload limit', function (): void { |
| 108 | + $path = temporaryFile(''); |
| 109 | + |
| 110 | + $handle = fopen($path, 'wb'); |
| 111 | + fseek($handle, 50 * 1024 * 1024); |
| 112 | + fwrite($handle, 'x'); |
| 113 | + fclose($handle); |
| 114 | + |
| 115 | + try { |
| 116 | + expect(fn (): array => FilePayload::fromPath($path)->toArray()) |
| 117 | + ->toThrow(FileTooLargeException::class); |
| 118 | + } finally { |
| 119 | + unlink($path); |
| 120 | + } |
| 121 | +}); |
0 commit comments