Skip to content

Commit b7deb92

Browse files
committed
refactor(Sanitizer): Update mapWithKeys to map
- Update mapWithKeys to map in the sanitizes method of Sanitizer trait - Remove array_unshift call in the sanitizes method
1 parent 87833ef commit b7deb92

File tree

6 files changed

+70
-63
lines changed

6 files changed

+70
-63
lines changed

app/Concerns/Sanitizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ trait Sanitizer
1919
{
2020
public function sanitizes(Collection $songs, string $keyword): Collection
2121
{
22-
return $songs->mapWithKeys(function (array $song, int $index) use ($keyword): array {
22+
return $songs->map(function (array $song, int $index) use ($keyword): array {
2323
$song = $this->sanitize($song, $keyword);
2424
array_unshift($song, "<fg=cyan>$index</>");
2525

26-
return [$index => $song];
26+
return $song;
2727
});
2828
}
2929

app/Support/Utils.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ final class Utils
2323
public static function getDefaultSaveDir(): string
2424
{
2525
$saveDir = windows_os()
26-
? sprintf('%s\\Downloads\\MusicDL\\', exec('echo %USERPROFILE%') ?: sprintf('C:\\Users\\%s', get_current_user()))
26+
? sprintf('%s\\Downloads\\MusicDL\\', exec('echo %USERPROFILE%') ?: sprintf('C:\\Users\\%s', get_current_user())) // @codeCoverageIgnore
2727
: sprintf('%s/Downloads/MusicDL/', exec('echo $HOME') ?: exec('cd ~; pwd'));
2828

2929
if (! is_dir($saveDir) && ! mkdir($saveDir, 0755, true) && ! is_dir($saveDir)) {
30-
throw new RuntimeException(sprintf('The directory "%s" was not created.', $saveDir));
30+
throw new RuntimeException(sprintf('The directory "%s" was not created.', $saveDir)); // @codeCoverageIgnore
3131
}
3232

3333
return $saveDir;
@@ -41,7 +41,7 @@ public static function getSavePath(array $song, ?string $saveDir = null, string
4141
$saveDir = Str::finish($saveDir ?? self::getDefaultSaveDir(), \DIRECTORY_SEPARATOR);
4242

4343
if (! is_dir($saveDir) && ! mkdir($saveDir, 0755, true) && ! is_dir($saveDir)) {
44-
throw new RuntimeException(sprintf('The directory "%s" was not created.', $saveDir));
44+
throw new RuntimeException(sprintf('The directory "%s" was not created.', $saveDir)); // @codeCoverageIgnore
4545
}
4646

4747
return sprintf(

tests/Datasets/Songs.php

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* This source file is subject to the MIT license that is bundled.
1111
*/
1212

13-
dataset('originalSongs', [
13+
dataset('songs', [
1414
[
1515
[
1616
1 => [
@@ -72,47 +72,3 @@
7272
],
7373
],
7474
]);
75-
76-
dataset('originalSong', [
77-
[
78-
[
79-
'id' => 1386737246,
80-
'name' => '公路之光',
81-
'artist' => ['腰乐队'],
82-
'album' => '他们说忘了摇滚有问题',
83-
'pic_id' => '109951164323917099',
84-
'url_id' => 1386737246,
85-
'lyric_id' => 1386737246,
86-
'source' => 'netease',
87-
'url' => 'http://m8.music.126.net/20231125211927/8af8b7988921529700b6c770fd2022f4/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/3307382749/f61f/636f/6518/ee00afde1ad90a3153401cae2d813de0.mp3',
88-
'size' => 12142803,
89-
'br' => 320,
90-
],
91-
[
92-
'id' => 390593,
93-
'name' => '你饿吗',
94-
'artist' => ['腰乐队'],
95-
'album' => '明日小城',
96-
'pic_id' => '76965813960184',
97-
'url_id' => 390593,
98-
'lyric_id' => 390593,
99-
'source' => 'netease',
100-
'url' => 'http://m7.music.126.net/20231125211927/d638dcb80d53828a275456f7a7355884/ymusic/9f50/1194/614e/227ed156b06a6b4fa9aac9b9351669f1.mp3',
101-
'size' => 8712028,
102-
'br' => 192,
103-
],
104-
[
105-
'id' => 1386735587,
106-
'name' => '在这宁静的水坑路',
107-
'artist' => ['腰乐队'],
108-
'album' => '他们说忘了摇滚有问题',
109-
'pic_id' => '109951164323917099',
110-
'url_id' => 1386735587,
111-
'lyric_id' => 1386735587,
112-
'source' => 'netease',
113-
'url' => 'http://m8.music.126.net/20231125210148/a57d961c47436fe024009b73b67183cf/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/3307387265/081d/9dc7/8d6e/5e354180f0d2925a257c5e2b55037828.mp3',
114-
'size' => 16259701,
115-
'br' => 320,
116-
],
117-
],
118-
]);

tests/Feature/MusicCommandTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,33 @@
1414
use App\Concerns\Sanitizer;
1515
use App\Music\SequenceMusic;
1616
use Illuminate\Support\Arr;
17-
use Illuminate\Support\Facades\File;
1817
use Laravel\Prompts\Prompt;
1918

2019
uses(Sanitizer::class);
2120

2221
beforeEach(function (): void {
23-
Prompt::fallbackWhen(true);
22+
// Prompt::fallbackWhen(true);
2423
});
2524

26-
it('can search and download music', function ($originalSongs): void {
25+
it('can search and download music', function ($songs): void {
2726
$mockSequenceMusic = Mockery::mock(SequenceMusic::class);
28-
$mockSequenceMusic->allows('search')->andReturn(collect($originalSongs));
27+
$mockSequenceMusic->allows('search')->andReturn(collect($songs));
2928
$mockSequenceMusic->allows('download')->andReturnUndefined();
3029
App\Facades\Music::shouldReceive('driver')->andReturn($mockSequenceMusic->makePartial());
3130

32-
$options = $this->sanitizes(collect($originalSongs), '不只是南方')
31+
$options = $this->sanitizes(collect($songs), '不只是南方')
3332
->transform(static fn (array $song): string => implode(' ', Arr::except($song, [0])))
3433
->prepend(config('music-dl.all_songs'));
3534
$this
3635
->artisan(MusicCommand::class, [
3736
'keyword' => '不只是南方',
3837
'--dir' => downloads_path(),
39-
// '--driver' => 'fork',
38+
'--driver' => 'sequence',
4039
'--no-continue' => true,
4140
'--sources' => 'netease',
4241
])
4342
->expectsConfirmation(config('music-dl.confirm_label'), 'yes')
4443
// ->expectsQuestion(config('music-dl.select_label'), [config('music-dl.all_songs')])
4544
->expectsChoice(config('music-dl.select_label'), [config('music-dl.all_songs')], $options->all())
4645
->assertSuccessful();
47-
})->group(__DIR__, __FILE__)->with('originalSongs');
46+
})->group(__DIR__, __FILE__)->with('songs');

tests/Unit/Concerns/SanitizerTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,39 @@
1515
use App\Concerns\Sanitizer;
1616
use Illuminate\Support\Collection;
1717

18-
it('can batch sanitize songs', function (array $originalSongs): void {
18+
it('can batch sanitize songs', function (array $songs): void {
1919
expect(new class() {
2020
use Sanitizer;
21-
})->sanitizes(collect($originalSongs), '腰乐队')->toBeInstanceOf(Collection::class);
22-
})->group(__DIR__, __FILE__)->with('originalSongs');
21+
})->sanitizes(collect($songs), '腰乐队')->toBeInstanceOf(Collection::class);
22+
})->group(__DIR__, __FILE__)->with([
23+
[
24+
[
25+
1 => [
26+
'id' => 1386737246,
27+
'name' => '公路之光',
28+
'artist' => ['腰乐队'],
29+
'album' => '他们说忘了摇滚有问题',
30+
'pic_id' => '109951164323917099',
31+
'url_id' => 1386737246,
32+
'lyric_id' => 1386737246,
33+
'source' => 'netease',
34+
'url' => 'http://m8.music.126.net/20231125211927/8af8b7988921529700b6c770fd2022f4/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/3307382749/f61f/636f/6518/ee00afde1ad90a3153401cae2d813de0.mp3',
35+
'size' => 12142803,
36+
'br' => 320,
37+
],
38+
2 => [
39+
'id' => 390593,
40+
'name' => '你饿吗',
41+
'artist' => ['腰乐队'],
42+
'album' => '明日小城',
43+
'pic_id' => '76965813960184',
44+
'url_id' => 390593,
45+
'lyric_id' => 390593,
46+
'source' => 'netease',
47+
'url' => 'http://m7.music.126.net/20231125211927/d638dcb80d53828a275456f7a7355884/ymusic/9f50/1194/614e/227ed156b06a6b4fa9aac9b9351669f1.mp3',
48+
'size' => 8712028,
49+
'br' => 192,
50+
],
51+
],
52+
],
53+
]);

tests/Unit/Support/UtilsTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,29 @@
1616

1717
it('can get default save dir', function (): void {
1818
expect(Utils::getDefaultSaveDir())->toBeDirectory();
19+
20+
// $this->getFunctionMock(class_namespace(Utils::class), 'windows_os')
21+
// ->expects($this->once())
22+
// ->willReturn(true);
23+
// expect(Utils::getDefaultSaveDir())->toBeString();
1924
})->group(__DIR__, __FILE__);
2025

21-
it('can get save path', function ($originalSong): void {
22-
expect(Utils::getSavePath($originalSong))->toBeString();
23-
})->group(__DIR__, __FILE__)->with('originalSong');
26+
it('can get save path', function ($song): void {
27+
expect(Utils::getSavePath($song))->toBeString();
28+
})->group(__DIR__, __FILE__)->with([
29+
[
30+
[
31+
'id' => 1386737246,
32+
'name' => '公路之光',
33+
'artist' => ['腰乐队'],
34+
'album' => '他们说忘了摇滚有问题',
35+
'pic_id' => '109951164323917099',
36+
'url_id' => 1386737246,
37+
'lyric_id' => 1386737246,
38+
'source' => 'netease',
39+
'url' => 'http://m8.music.126.net/20231125211927/8af8b7988921529700b6c770fd2022f4/ymusic/obj/w5zDlMODwrDDiGjCn8Ky/3307382749/f61f/636f/6518/ee00afde1ad90a3153401cae2d813de0.mp3',
40+
'size' => 12142803,
41+
'br' => 320,
42+
],
43+
],
44+
]);

0 commit comments

Comments
 (0)