5
use PHPUnit\Framework\Attributes\DataProvider;
7
use PHPUnit\Framework\TestCase;
9
define('TMP_DIR', \dirname(__DIR__) . '/dir');
11
class DirTest extends TestCase
13
public function setUp(): void
18
// TODO: Наверно стоит очищать стандартными средствами
19
public function tearDown(): void
21
$dir = Dir::from(TMP_DIR);
25
private static function tmp_dir(string $dir = ''): string
27
return TMP_DIR . $dir;
30
private static function create_dir(string $dir): string
32
$dir = self::tmp_dir($dir);
33
if (!\mkdir(directory: $dir, recursive: true) && !\is_dir($dir)) {
34
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dir));
40
public static function paths_provider(): iterable
42
yield ['/path/to/fake', false];
43
yield [self::tmp_dir('/fake'), false];
44
yield [self::tmp_dir(), true];
47
#[DataProvider('paths_provider')]
48
public function test_exists(string $path, bool $exists): void
50
$dir = Dir::from($path);
51
$this->assertEquals($exists, $dir->exists());
54
#[DataProvider('paths_provider')]
55
public function test_not_exists(string $path, bool $exists): void
57
$dir = Dir::from($path);
58
$this->assertEquals($exists, !$dir->not_exists());
61
#[DataProvider('paths_provider')]
62
public function test__toString(string $path): void
64
$dir = Dir::from($path);
65
$this->assertEquals($path, (string)$dir);
68
public function test_rename(): void
70
$source_dir = Dir::from($source_name = self::create_dir('/to_rename'));
71
$renamed_dir = $source_dir->rename($new_name = self::tmp_dir('/renamed'));
73
$this->assertEquals($source_name, (string)$source_dir);
74
$this->assertEquals($new_name, (string)$renamed_dir);
75
$this->assertTrue($renamed_dir->exists());
76
$this->assertFalse($source_dir->exists());
79
public function test_clean(): void
84
public function test_remove(): void
89
public static function make_provider(): iterable
91
yield [self::tmp_dir('/test')];
92
yield [self::tmp_dir('/test/test')];
93
yield [self::tmp_dir('/test/test/test')];
96
#[DataProvider('make_provider')]
97
public function test_make(string $to_make_path): void
99
$to_make_dir = Dir::from($to_make_path);
101
$to_make_dir->make();
103
$this->assertDirectoryExists($to_make_path);