Skip to content

Commit 3169ba0

Browse files
committed
add \Astrotomic\PhpunitAssertions\PathAssertions
1 parent a8257ea commit 3169ba0

File tree

5 files changed

+113
-9
lines changed

5 files changed

+113
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ This will prevent any method name conflicts with core, your custom or other trai
137137
\Astrotomic\PhpunitAssertions\UrlAssertions::assertComponent('gummibeer', 'https://gummibeer@astrotomic.info', PHP_URL_USER);
138138
```
139139

140+
### Path
141+
142+
```php
143+
\Astrotomic\PhpunitAssertions\PathAssertions::assertDirname('/foo/bar', '/foo/bar/image.jpg');
144+
\Astrotomic\PhpunitAssertions\PathAssertions::assertBasename('image.jpg', '/foo/bar/image.jpg');
145+
\Astrotomic\PhpunitAssertions\PathAssertions::assertFilename('image', '/foo/bar/image.jpg');
146+
\Astrotomic\PhpunitAssertions\PathAssertions::assertExtension('jpg', '/foo/bar/image.jpg');
147+
```
148+
140149
### UUID
141150

142151
`composer require --dev ramsey/uuid:^4.0`

phpunit.xml.dist

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
35
backupGlobals="false"
46
backupStaticAttributes="false"
57
colors="true"
@@ -8,16 +10,15 @@
810
convertNoticesToExceptions="true"
911
convertWarningsToExceptions="true"
1012
processIsolation="false"
11-
stopOnFailure="false"
12-
>
13+
stopOnFailure="false">
1314
<testsuites>
1415
<testsuite name="Test Suite">
1516
<directory>tests</directory>
1617
</testsuite>
1718
</testsuites>
18-
<filter>
19-
<whitelist>
19+
<coverage>
20+
<include>
2021
<directory suffix=".php">src/</directory>
21-
</whitelist>
22-
</filter>
22+
</include>
23+
</coverage>
2324
</phpunit>

src/PathAssertions.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions;
4+
5+
use PHPUnit\Framework\Assert as PHPUnit;
6+
7+
final class PathAssertions
8+
{
9+
public static function assertDirname(string $expected, $actual): void
10+
{
11+
self::assertComponent($expected, $actual, PATHINFO_DIRNAME);
12+
}
13+
14+
public static function assertBasename(string $expected, $actual): void
15+
{
16+
self::assertComponent($expected, $actual, PATHINFO_BASENAME);
17+
}
18+
19+
public static function assertFilename(string $expected, $actual): void
20+
{
21+
self::assertComponent($expected, $actual, PATHINFO_FILENAME);
22+
}
23+
24+
public static function assertExtension(string $expected, $actual): void
25+
{
26+
self::assertComponent($expected, $actual, PATHINFO_EXTENSION);
27+
}
28+
29+
public static function assertComponent($expected, $actual, int $component): void
30+
{
31+
PHPUnit::assertIsString($actual);
32+
PHPUnit::assertSame($expected, pathinfo($actual, $component));
33+
}
34+
}

tests/PathAssertionsTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests;
4+
5+
use Astrotomic\PhpunitAssertions\PathAssertions;
6+
7+
final class PathAssertionsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @dataProvider hundredTimes
12+
*/
13+
public static function it_can_validate_dirname(): void
14+
{
15+
$directory = '/'.self::randomString().'/'.self::randomString();
16+
$filename = self::randomString();
17+
$extension = self::randomString(3);
18+
19+
PathAssertions::assertDirname($directory, $directory.'/'.$filename.'.'.$extension);
20+
}
21+
22+
/**
23+
* @test
24+
* @dataProvider hundredTimes
25+
*/
26+
public static function it_can_validate_basename(): void
27+
{
28+
$directory = '/'.self::randomString().'/'.self::randomString();
29+
$filename = self::randomString();
30+
$extension = self::randomString(3);
31+
32+
PathAssertions::assertBasename($filename.'.'.$extension, $directory.'/'.$filename.'.'.$extension);
33+
}
34+
35+
/**
36+
* @test
37+
* @dataProvider hundredTimes
38+
*/
39+
public static function it_can_validate_filename(): void
40+
{
41+
$directory = '/'.self::randomString().'/'.self::randomString();
42+
$filename = self::randomString();
43+
$extension = self::randomString(3);
44+
45+
PathAssertions::assertFilename($filename, $directory.'/'.$filename.'.'.$extension);
46+
}
47+
48+
/**
49+
* @test
50+
* @dataProvider hundredTimes
51+
*/
52+
public static function it_can_validate_extension(): void
53+
{
54+
$directory = '/'.self::randomString().'/'.self::randomString();
55+
$filename = self::randomString();
56+
$extension = self::randomString(3);
57+
58+
PathAssertions::assertExtension($extension, $directory.'/'.$filename.'.'.$extension);
59+
}
60+
}

tests/Utils/Randomize.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ protected static function randomArray(int $count, bool $assoc = false): array
3939
$array = array_fill(0, $count, null);
4040

4141
$array = array_map(static function (): string {
42-
return self::randomString(self::randomInt(4, 64));
42+
return self::randomString(self::randomInt(8, 64));
4343
}, $array);
4444

4545
if ($assoc) {
4646
$values = $array;
4747
$array = [];
4848

4949
foreach ($values as $value) {
50-
$array[self::randomString(self::randomInt(4, 64))] = $value;
50+
$array[self::randomString(self::randomInt(8, 64))] = $value;
5151
}
5252
}
5353

0 commit comments

Comments
 (0)