|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +use Contributte\Tester\Toolkit; |
| 4 | +use Contributte\Utils\Exception\Runtime\FileNotFoundException; |
| 5 | +use Contributte\Utils\File; |
| 6 | +use Contributte\Utils\Http\FileResponse; |
| 7 | +use Nette\Utils\FileSystem; |
| 8 | +use Nette\Utils\Image; |
| 9 | +use Tester\Assert; |
| 10 | + |
| 11 | +require_once __DIR__ . '/../bootstrap.php'; |
| 12 | + |
| 13 | +$tempDir = __DIR__ . '/../temp/file-test'; |
| 14 | +FileSystem::createDir($tempDir); |
| 15 | + |
| 16 | +// File::getName |
| 17 | +Toolkit::test(function (): void { |
| 18 | + $file = new File('/path/to/test.txt'); |
| 19 | + Assert::same('test.txt', $file->getName()); |
| 20 | + |
| 21 | + $file2 = new File('/another/path/document.pdf'); |
| 22 | + Assert::same('document.pdf', $file2->getName()); |
| 23 | +}); |
| 24 | + |
| 25 | +// File::getTitle |
| 26 | +Toolkit::test(function (): void { |
| 27 | + $file = new File('/path/to/test.txt', 'My Document'); |
| 28 | + Assert::same('My Document', $file->getTitle()); |
| 29 | + |
| 30 | + $fileWithoutTitle = new File('/path/to/test.txt'); |
| 31 | + Assert::null($fileWithoutTitle->getTitle()); |
| 32 | +}); |
| 33 | + |
| 34 | +// File::getPath |
| 35 | +Toolkit::test(function (): void { |
| 36 | + $path = '/path/to/test.txt'; |
| 37 | + $file = new File($path); |
| 38 | + Assert::same(str_replace('/', DIRECTORY_SEPARATOR, $path), $file->getPath()); |
| 39 | +}); |
| 40 | + |
| 41 | +// File::exists |
| 42 | +Toolkit::test(function () use ($tempDir): void { |
| 43 | + $filePath = $tempDir . '/existing.txt'; |
| 44 | + FileSystem::write($filePath, 'test content'); |
| 45 | + |
| 46 | + $file = new File($filePath); |
| 47 | + Assert::true($file->exists()); |
| 48 | + |
| 49 | + $nonExisting = new File($tempDir . '/non-existing.txt'); |
| 50 | + Assert::false($nonExisting->exists()); |
| 51 | +}); |
| 52 | + |
| 53 | +// File::getSize |
| 54 | +Toolkit::test(function () use ($tempDir): void { |
| 55 | + $content = 'Hello, World!'; |
| 56 | + $filePath = $tempDir . '/size-test.txt'; |
| 57 | + FileSystem::write($filePath, $content); |
| 58 | + |
| 59 | + $file = new File($filePath); |
| 60 | + Assert::same(strlen($content), $file->getSize()); |
| 61 | +}); |
| 62 | + |
| 63 | +// File::getSize throws on non-existing file |
| 64 | +Toolkit::test(function () use ($tempDir): void { |
| 65 | + $file = new File($tempDir . '/non-existing.txt'); |
| 66 | + |
| 67 | + Assert::exception(function () use ($file): void { |
| 68 | + $file->getSize(); |
| 69 | + }, FileNotFoundException::class); |
| 70 | +}); |
| 71 | + |
| 72 | +// File::move |
| 73 | +Toolkit::test(function () use ($tempDir): void { |
| 74 | + $originalPath = $tempDir . '/original.txt'; |
| 75 | + $newPath = $tempDir . '/moved.txt'; |
| 76 | + FileSystem::write($originalPath, 'test content'); |
| 77 | + |
| 78 | + $file = new File($originalPath); |
| 79 | + $result = $file->move($newPath); |
| 80 | + |
| 81 | + Assert::same($file, $result); |
| 82 | + Assert::same(str_replace('/', DIRECTORY_SEPARATOR, $newPath), $file->getPath()); |
| 83 | + Assert::true($file->exists()); |
| 84 | + Assert::false(file_exists($originalPath)); |
| 85 | +}); |
| 86 | + |
| 87 | +// File::move throws on non-existing file |
| 88 | +Toolkit::test(function () use ($tempDir): void { |
| 89 | + $file = new File($tempDir . '/non-existing.txt'); |
| 90 | + |
| 91 | + Assert::exception(function () use ($file, $tempDir): void { |
| 92 | + $file->move($tempDir . '/destination.txt'); |
| 93 | + }, FileNotFoundException::class); |
| 94 | +}); |
| 95 | + |
| 96 | +// File::toImage |
| 97 | +Toolkit::test(function () use ($tempDir): void { |
| 98 | + $imagePath = $tempDir . '/test.png'; |
| 99 | + $image = Image::fromBlank(100, 100, Image::rgb(255, 0, 0)); |
| 100 | + $image->save($imagePath); |
| 101 | + |
| 102 | + $file = new File($imagePath); |
| 103 | + $loadedImage = $file->toImage(); |
| 104 | + |
| 105 | + Assert::type(Image::class, $loadedImage); |
| 106 | + Assert::same(100, $loadedImage->getWidth()); |
| 107 | + Assert::same(100, $loadedImage->getHeight()); |
| 108 | +}); |
| 109 | + |
| 110 | +// File::toImage throws on non-existing file |
| 111 | +Toolkit::test(function () use ($tempDir): void { |
| 112 | + $file = new File($tempDir . '/non-existing.png'); |
| 113 | + |
| 114 | + Assert::exception(function () use ($file): void { |
| 115 | + $file->toImage(); |
| 116 | + }, FileNotFoundException::class); |
| 117 | +}); |
| 118 | + |
| 119 | +// File::toResponse |
| 120 | +Toolkit::test(function () use ($tempDir): void { |
| 121 | + $filePath = $tempDir . '/download.txt'; |
| 122 | + FileSystem::write($filePath, 'downloadable content'); |
| 123 | + |
| 124 | + $file = new File($filePath, 'My Download'); |
| 125 | + $response = $file->toResponse(); |
| 126 | + |
| 127 | + Assert::type(FileResponse::class, $response); |
| 128 | +}); |
| 129 | + |
| 130 | +// File::toResponse with custom name |
| 131 | +Toolkit::test(function () use ($tempDir): void { |
| 132 | + $filePath = $tempDir . '/download2.txt'; |
| 133 | + FileSystem::write($filePath, 'downloadable content'); |
| 134 | + |
| 135 | + $file = new File($filePath); |
| 136 | + $response = $file->toResponse('custom-name.txt'); |
| 137 | + |
| 138 | + Assert::type(FileResponse::class, $response); |
| 139 | +}); |
| 140 | + |
| 141 | +// File::toResponse throws on non-existing file |
| 142 | +Toolkit::test(function () use ($tempDir): void { |
| 143 | + $file = new File($tempDir . '/non-existing.txt'); |
| 144 | + |
| 145 | + Assert::exception(function () use ($file): void { |
| 146 | + $file->toResponse(); |
| 147 | + }, FileNotFoundException::class); |
| 148 | +}); |
| 149 | + |
| 150 | +// File path normalization |
| 151 | +Toolkit::test(function (): void { |
| 152 | + $file = new File('/path/to\\mixed/separators.txt'); |
| 153 | + $path = $file->getPath(); |
| 154 | + |
| 155 | + if (DIRECTORY_SEPARATOR === '\\') { |
| 156 | + Assert::same('\\path\\to\\mixed\\separators.txt', $path); |
| 157 | + } else { |
| 158 | + Assert::same('/path/to/mixed/separators.txt', $path); |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | +FileSystem::delete($tempDir); |
0 commit comments