|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the aether/aether. |
| 5 | + * |
| 6 | + * Copyright (C) 2024 Dominik Szamburski |
| 7 | + * |
| 8 | + * This software may be modified and distributed under the terms |
| 9 | + * of the MIT license. See the LICENSE file for details. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Aether\Tests\Filesystem; |
| 13 | + |
| 14 | +use Aether\Filesystem\Filesystem; |
| 15 | +use Aether\Filesystem\FilesystemInterface; |
| 16 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +#[CoversClass(Filesystem::class)] |
| 20 | +class FilesystemTest extends TestCase { |
| 21 | + private ?string $tempDir; |
| 22 | + private FilesystemInterface $filesystem; |
| 23 | + |
| 24 | + protected function setUp(): void { |
| 25 | + $this->filesystem = new Filesystem(); |
| 26 | + $this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tmp'; |
| 27 | + $this->filesystem->mkdir($this->tempDir); |
| 28 | + } |
| 29 | + |
| 30 | + protected function tearDown(): void { |
| 31 | + if ($this->filesystem->exists($this->tempDir)) { |
| 32 | + $this->filesystem->rmdir($this->tempDir); |
| 33 | + } |
| 34 | + |
| 35 | + $this->tempDir = null; |
| 36 | + } |
| 37 | + |
| 38 | + public function testFileExists(): void { |
| 39 | + $basePath = $this->tempDir . DIRECTORY_SEPARATOR; |
| 40 | + $this->filesystem->touch($basePath . 'file.txt'); |
| 41 | + |
| 42 | + self::assertFalse($this->filesystem->exists($basePath . 'file122.txt')); |
| 43 | + self::assertTrue($this->filesystem->exists($basePath . 'file.txt')); |
| 44 | + } |
| 45 | + |
| 46 | + public function testReadFileContent(): void { |
| 47 | + $basePath = $this->tempDir . DIRECTORY_SEPARATOR . 'file1.txt'; |
| 48 | + \file_put_contents($basePath, 'test content'); |
| 49 | + self::assertSame('test content', $this->filesystem->read($basePath)); |
| 50 | + } |
| 51 | + |
| 52 | + public function testWriteFile(): void { |
| 53 | + $basePath = $this->tempDir . DIRECTORY_SEPARATOR . 'file.txt'; |
| 54 | + |
| 55 | + $this->filesystem->write($basePath, 'test content'); |
| 56 | + self::assertSame('test content', $this->filesystem->read($basePath)); |
| 57 | + } |
| 58 | + |
| 59 | + public function testAppendContentToFile(): void { |
| 60 | + $basePath = $this->tempDir . DIRECTORY_SEPARATOR . 'file.txt'; |
| 61 | + $this->filesystem->write($basePath, 'test content'); |
| 62 | + $this->filesystem->append($basePath, 'test content'); |
| 63 | + |
| 64 | + self::assertSame('test contenttest content', $this->filesystem->read($basePath)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testRemoveFiles(): void { |
| 68 | + $basePath = $this->tempDir . DIRECTORY_SEPARATOR . 'file1.txt'; |
| 69 | + $this->filesystem->touch($basePath); |
| 70 | + $this->filesystem->unlink($basePath); |
| 71 | + |
| 72 | + self::assertFalse($this->filesystem->exists($basePath)); |
| 73 | + } |
| 74 | + |
| 75 | + public function testRemoveDirectory(): void { |
| 76 | + $this->filesystem->mkdir($this->tempDir . DIRECTORY_SEPARATOR . 'dir'); |
| 77 | + $this->filesystem->rmdir($this->tempDir); |
| 78 | + |
| 79 | + self::assertFalse($this->filesystem->exists($this->tempDir)); |
| 80 | + } |
| 81 | + |
| 82 | + public function testRemoveDirectoryRecursive(): void { |
| 83 | + $this->filesystem->mkdir($this->tempDir . DIRECTORY_SEPARATOR . 'dir' . DIRECTORY_SEPARATOR . 'dir1', 0777, true); |
| 84 | + $this->filesystem->rmdir($this->tempDir); |
| 85 | + |
| 86 | + self::assertFalse($this->filesystem->exists($this->tempDir)); |
| 87 | + } |
| 88 | + public function testCopyFile() |
| 89 | + { |
| 90 | + $source = $this->tempDir . '/file1.txt'; |
| 91 | + $destination = $this->tempDir . '/file2.txt'; |
| 92 | + |
| 93 | + $this->filesystem->write($source, 'test content'); |
| 94 | + $this->filesystem->copy($source, $destination); |
| 95 | + |
| 96 | + self::assertTrue($this->filesystem->exists($destination)); |
| 97 | + self::assertEquals('test content', $this->filesystem->read($destination)); |
| 98 | + } |
| 99 | + |
| 100 | + public function testMoveFile() |
| 101 | + { |
| 102 | + $source = $this->tempDir . '/file1.txt'; |
| 103 | + $destination = $this->tempDir . '/file2.txt'; |
| 104 | + |
| 105 | + $this->filesystem->write($source, 'test content'); |
| 106 | + $this->filesystem->move($source, $destination); |
| 107 | + |
| 108 | + self::assertFalse($this->filesystem->exists($source)); |
| 109 | + self::assertTrue($this->filesystem->exists($destination)); |
| 110 | + self::assertEquals('test content', $this->filesystem->read($destination)); |
| 111 | + } |
| 112 | + |
| 113 | + public function testName(): void { |
| 114 | + $file = $this->tempDir . DIRECTORY_SEPARATOR .'file1.txt'; |
| 115 | + $this->filesystem->touch($file); |
| 116 | + |
| 117 | + self::assertSame('file1', $this->filesystem->name($file)); |
| 118 | + } |
| 119 | + |
| 120 | + public function testDirname(): void { |
| 121 | + $file = $this->tempDir . DIRECTORY_SEPARATOR .'file1.txt'; |
| 122 | + $this->filesystem->touch($file); |
| 123 | + |
| 124 | + self::assertSame('/tmp/tmp', $this->filesystem->dirname($file)); |
| 125 | + } |
| 126 | + |
| 127 | + public function testBasename(): void { |
| 128 | + $file = $this->tempDir . DIRECTORY_SEPARATOR .'file1.txt'; |
| 129 | + $this->filesystem->touch($file); |
| 130 | + |
| 131 | + self::assertSame('file1.txt', $this->filesystem->basename($file)); |
| 132 | + } |
| 133 | + |
| 134 | + public function testExtension(): void { |
| 135 | + $file = $this->tempDir . DIRECTORY_SEPARATOR .'file1.txt'; |
| 136 | + $this->filesystem->touch($file); |
| 137 | + |
| 138 | + self::assertSame('txt', $this->filesystem->extension($file)); |
| 139 | + } |
| 140 | + |
| 141 | + public function testFileSize(): void { |
| 142 | + $file = $this->tempDir . DIRECTORY_SEPARATOR .'file1.txt'; |
| 143 | + $this->filesystem->touch($file); |
| 144 | + |
| 145 | + self::assertSame(0, $this->filesystem->filesize($file)); |
| 146 | + } |
| 147 | + |
| 148 | + public function testMimeType() |
| 149 | + { |
| 150 | + $path = $this->tempDir . '/file.txt'; |
| 151 | + $this->filesystem->write($path, 'test'); |
| 152 | + |
| 153 | + self::assertEquals('text/plain', $this->filesystem->mimeType($path)); |
| 154 | + } |
| 155 | +} |
0 commit comments