Skip to content

Commit c96d2c9

Browse files
committed
test(support): add unit tests for classes and make helpers
- Add tests verifying `classes` returns a collection filtered by class name prefix - Add tests ensuring `make` throws `InvalidArgumentException` on empty abstract array - Add parameterized tests for `make` to create `Music` instances from various abstract forms - Fix typo in test description from "can get default saved dir" to "can get default saved directory" - Unexclude `app/Support/helpers.php` from PHPUnit coverage to enable testing helpers functions Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent 5dba658 commit c96d2c9

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

app/Support/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static function (string $file, string $class) use ($filter) {
6565
// return [$class => (new ErrorHandler)->with(static fn () => new ReflectionClass($class))];
6666
return [$class => new \ReflectionClass($class)];
6767
} catch (\Throwable $throwable) {
68-
return [$class => $throwable];
68+
return [$class => $throwable]; // @codeCoverageIgnore
6969
}
7070
});
7171
}

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</include>
2828
<exclude>
2929
<!--<directory>app/Contracts/</directory>-->
30-
<file>app/Support/helpers.php</file>
30+
<!--<file>app/Support/helpers.php</file>-->
3131
</exclude>
3232
</source>
3333
<extensions>

tests/Unit/Support/HelpersTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,32 @@
1717
*
1818
* @see https://github.com/guanguans/music-dl
1919
*/
20+
21+
use App\Music;
22+
use function App\Support\classes;
23+
use function App\Support\make;
24+
25+
it('can get classes', function (): void {
26+
expect(
27+
classes(fn (string $class): bool => str($class)->startsWith('Rectors'))
28+
)->toBeCollection();
29+
})->group(__DIR__, __FILE__);
30+
31+
it('will throw `InvalidArgumentException` when abstract is empty array', function (): void {
32+
make([]);
33+
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class);
34+
35+
it('can make music', function (array|string $abstract): void {
36+
expect(make($abstract))->toBeInstanceOf(Music::class);
37+
})->group(__DIR__, __FILE__)->with([
38+
['abstract' => Music::class],
39+
['abstract' => ['__abstract' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
40+
['abstract' => ['__class' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
41+
['abstract' => ['__name' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
42+
['abstract' => ['_abstract' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
43+
['abstract' => ['_class' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
44+
['abstract' => ['_name' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
45+
['abstract' => ['abstract' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
46+
['abstract' => ['class' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
47+
['abstract' => ['name' => Music::class, 'driver' => null, 'minCallMicroseconds' => 1000]],
48+
]);

tests/Unit/Support/UtilsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use App\Support\Utils;
2222

23-
it('can get default saved dir', function (): void {
23+
it('can get default saved directory', function (): void {
2424
expect(Utils::defaultSavedDirectory())->toContain(\DIRECTORY_SEPARATOR);
2525
})->group(__DIR__, __FILE__);
2626

0 commit comments

Comments
 (0)