[File] обновлён метод RemoveDir. Теперь он корректно удаляет любую директорию. Также изменился синтаксис этого метода: public static function RemoveDir (string $directory, array $errorMessages = self::REMOVE_DIRECTORY_ERROR_MESSAGES): ActionState, где $errorMessages -- массив с локализованным списком ошибок. Теперь вместо bool возвращается ActionState, куда заносятся все ошибки при удалении. [File] добавлен метод FileSize (string $filename, array $errorLocalization = self::FILE_SIZE_ERROR_MESSAGES): ActionState, который получает размер файла [File] добавлен метод FileSizeToString (int $fileSize, array $fileSizeUnits = self::FILE_SIZE_UNITS, string $decimalSeparator = ','): string, который преобразует размер файла в байтах в красивое строковое представление
94 lines
2.7 KiB
PHP
94 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace goodboyalex\php_components_pack\tests\classes;
|
|
|
|
use goodboyalex\php_components_pack\classes\File;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FileTest extends TestCase
|
|
{
|
|
public function testExtractFileName ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$this->assertEquals("test.txt", File::ExtractFileName("c:\\tmp\\test.txt"));
|
|
$this->assertEquals("test.txt", File::ExtractFileName("c:/tmp/test.txt"));
|
|
$this->assertEquals("test.txt", File::ExtractFileName("\\tmp\\test.txt"));
|
|
}
|
|
|
|
private function PrepareForTest (): void
|
|
{
|
|
require_once __DIR__ . '/../../sources/classes/File.php';
|
|
}
|
|
|
|
public function testFindFiles ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$result = File::FindFiles(__DIR__);
|
|
|
|
$this->assertIsArray($result);
|
|
$this->assertCount(8, $result);
|
|
}
|
|
|
|
public function testGetRelativePath ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$fullPath = "c:\\source\\tmp\\test.txt";
|
|
|
|
$basePath = "c:\\source\\";
|
|
|
|
$this->assertEquals("tmp\\test.txt", File::GetRelativePath($fullPath, $basePath));
|
|
}
|
|
|
|
public function testExtractFileExtension ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$this->assertEquals("txt", File::ExtractFileExtension("c:\\tmp\\test.txt"));
|
|
$this->assertEquals("txt", File::ExtractFileExtension("c:/tmp/test.txt"));
|
|
$this->assertEquals("txt", File::ExtractFileExtension("\\tmp\\test.txt"));
|
|
}
|
|
|
|
public function testExtractFileNameWithoutExtension ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("c:\\tmp\\test.txt"));
|
|
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("c:/tmp/test.txt"));
|
|
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("\\tmp\\test.txt"));
|
|
}
|
|
|
|
public function testRemoveDir ()
|
|
{
|
|
/**
|
|
* ВАЖНО! Перед запуском теста необходимо создать директорию D:\TestDelete и наполнить её ненужными файлами
|
|
*/
|
|
|
|
$this->PrepareForTest();
|
|
|
|
$result = File::RemoveDir("D:/TestDelete");
|
|
|
|
$this->assertTrue($result->Value);
|
|
$this->assertFalse(File::DirectoryExists("D:\\TestDelete"));
|
|
}
|
|
|
|
public function testFileSize ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$size = File::FileSize("C:\\Windows/explorer.exe");
|
|
|
|
$this->assertEquals(2774080, $size->Value);
|
|
}
|
|
|
|
public function testFileSizeString ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$size = File::FileSizeToString(2774080);
|
|
|
|
$this->assertEquals("2,65 МБ", $size);
|
|
}
|
|
} |