-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPathAssertions.php
53 lines (43 loc) · 1.54 KB
/
PathAssertions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Astrotomic\PhpunitAssertions;
use PHPUnit\Framework\Assert as PHPUnit;
final class PathAssertions
{
public static function assertDirname(string $expected, $actual): void
{
self::assertComponent($expected, $actual, PATHINFO_DIRNAME);
}
public static function assertBasename(string $expected, $actual): void
{
self::assertComponent($expected, $actual, PATHINFO_BASENAME);
}
public static function assertFilename(string $expected, $actual): void
{
self::assertComponent($expected, $actual, PATHINFO_FILENAME);
}
public static function assertExtension(string $expected, $actual): void
{
self::assertComponent($expected, $actual, PATHINFO_EXTENSION);
}
public static function assertComponent($expected, $actual, int $component): void
{
PHPUnit::assertIsString($actual);
PHPUnit::assertSame($expected, pathinfo($actual, $component));
}
private static function agnosticPath(string $path): string
{
if (DIRECTORY_SEPARATOR === '/') {
if (str_contains($path, '\\')) {
return str_replace('\\', DIRECTORY_SEPARATOR, $path);
}
return $path;
}
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
public static function assertOsAgnosticPath(string $expected, $actual): void
{
$osNormalizedExpected = PathAssertions::agnosticPath($expected);
PHPUnit::assertIsString($actual);
PHPUnit::assertSame($osNormalizedExpected, $actual);
}
}