Skip to content

Commit cb0405c

Browse files
Fixed sorting files
1 parent e795621 commit cb0405c

File tree

5 files changed

+147
-11
lines changed

5 files changed

+147
-11
lines changed

src/Helpers/Sorter.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Helpers;
6+
7+
use Closure;
8+
use DragonCode\Support\Facades\Filesystem\Path;
9+
use DragonCode\Support\Facades\Helpers\Arr;
10+
11+
class Sorter
12+
{
13+
public function byValues(array $items): array
14+
{
15+
return Arr::sort($items, $this->callback());
16+
}
17+
18+
public function byKeys(array $items): array
19+
{
20+
return Arr::ksort($items, $this->callback());
21+
}
22+
23+
protected function callback(): Closure
24+
{
25+
return function (string $a, string $b): int {
26+
$current = Path::filename($a);
27+
$next = Path::filename($b);
28+
29+
if ($current === $next) {
30+
return 0;
31+
}
32+
33+
return $current < $next ? -1 : 1;
34+
};
35+
}
36+
}

src/Processors/Processor.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use DragonCode\LaravelActions\Contracts\Notification;
1010
use DragonCode\LaravelActions\Helpers\Config;
1111
use DragonCode\LaravelActions\Helpers\Git;
12+
use DragonCode\LaravelActions\Helpers\Sorter;
1213
use DragonCode\LaravelActions\Repositories\ActionRepository;
1314
use DragonCode\LaravelActions\Services\Migrator;
1415
use DragonCode\LaravelActions\Values\Options;
@@ -25,16 +26,17 @@ abstract class Processor
2526
abstract public function handle(): void;
2627

2728
public function __construct(
28-
protected Options $options,
29-
protected InputInterface $input,
30-
protected OutputStyle $output,
31-
protected Config $config,
29+
protected Options $options,
30+
protected InputInterface $input,
31+
protected OutputStyle $output,
32+
protected Config $config,
3233
protected ActionRepository $repository,
33-
protected Git $git,
34-
protected File $file,
35-
protected Migrator $migrator,
36-
protected Notification $notification,
37-
protected Dispatcher $events
34+
protected Git $git,
35+
protected File $file,
36+
protected Migrator $migrator,
37+
protected Notification $notification,
38+
protected Dispatcher $events,
39+
protected Sorter $sorter
3840
) {
3941
$this->notification->setOutput($this->output);
4042
$this->repository->setConnection($this->options->connection);
@@ -49,7 +51,9 @@ protected function getFiles(string $path, ?Closure $filter = null): array
4951
return [$file];
5052
}
5153

52-
return $this->file->names($path, $filter, true);
54+
return $this->sorter->byValues(
55+
$this->file->names($path, $filter, true)
56+
);
5357
}
5458

5559
protected function runCommand(string $command, array $options = []): void

tests/Commands/MigrateTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DragonCode\LaravelActions\Constants\Names;
66
use Exception;
7+
use Illuminate\Support\Facades\DB;
78
use Illuminate\Support\Str;
89
use Tests\TestCase;
910
use Throwable;
@@ -521,4 +522,37 @@ public function testDI(): void
521522
$this->assertDatabaseMigrationHas($table, 'invoke_down', column: 'value');
522523
$this->assertDatabaseMigrationHas($table, 'invoke', column: 'value');
523524
}
525+
526+
public function testSorting(): void
527+
{
528+
$files = [];
529+
530+
$this->artisan(Names::INSTALL)->assertExitCode(0);
531+
532+
$files[] = date('Y_m_d_His_') . 'test1';
533+
$this->artisan(Names::MAKE, ['name' => 'test1'])->assertExitCode(0);
534+
sleep(2);
535+
$files[] = 'foo/' . date('Y_m_d_His_') . 'test2';
536+
$this->artisan(Names::MAKE, ['name' => 'foo/test2'])->assertExitCode(0);
537+
sleep(2);
538+
$files[] = 'bar/' . date('Y_m_d_His_') . 'test3';
539+
$this->artisan(Names::MAKE, ['name' => 'bar/test3'])->assertExitCode(0);
540+
sleep(2);
541+
$files[] = 'foo/' . date('Y_m_d_His_') . 'test4';
542+
$this->artisan(Names::MAKE, ['name' => 'foo/test4'])->assertExitCode(0);
543+
sleep(2);
544+
$files[] = 'bar/' . date('Y_m_d_His_') . 'test5';
545+
$this->artisan(Names::MAKE, ['name' => 'bar/test5'])->assertExitCode(0);
546+
sleep(2);
547+
$files[] = date('Y_m_d_His_') . 'test6';
548+
$this->artisan(Names::MAKE, ['name' => 'test6'])->assertExitCode(0);
549+
550+
$this->assertDatabaseCount($this->table, 0);
551+
$this->artisan(Names::MIGRATE)->assertExitCode(0);
552+
$this->assertDatabaseCount($this->table, 6);
553+
554+
$records = DB::table($this->table)->orderBy('id')->pluck('action')->toArray();
555+
556+
$this->assertSame($files, $records);
557+
}
524558
}

tests/Services/GitTest.php renamed to tests/Helpers/GitTest.php

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

33
declare(strict_types=1);
44

5-
namespace Tests\Services;
5+
namespace Tests\Helpers;
66

77
use DragonCode\LaravelActions\Helpers\Git;
88
use Tests\TestCase;

tests/Helpers/SorterTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Helpers;
6+
7+
use DragonCode\LaravelActions\Helpers\Sorter;
8+
use Tests\TestCase;
9+
10+
class SorterTest extends TestCase
11+
{
12+
public function testByValues(): void
13+
{
14+
$expected = [
15+
'2022_10_13_013321_test1',
16+
'foo/2022_10_13_013321_test2',
17+
'bar/2022_10_13_013321_test3',
18+
'foo/2022_10_13_013321_test4',
19+
'bar/2022_10_13_013321_test5',
20+
'2022_10_13_013321_test6',
21+
];
22+
23+
$values = [
24+
'2022_10_13_013321_test1',
25+
'2022_10_13_013321_test6',
26+
'bar/2022_10_13_013321_test3',
27+
'bar/2022_10_13_013321_test5',
28+
'foo/2022_10_13_013321_test2',
29+
'foo/2022_10_13_013321_test4',
30+
];
31+
32+
$this->assertSame($expected, $this->sorter()->byValues($values));
33+
}
34+
35+
public function testByKeys(): void
36+
{
37+
$expected = [
38+
'2022_10_13_013321_test1' => 1,
39+
'foo/2022_10_13_013321_test2' => 2,
40+
'bar/2022_10_13_013321_test3' => 3,
41+
'foo/2022_10_13_013321_test4' => 4,
42+
'bar/2022_10_13_013321_test5' => 5,
43+
'2022_10_13_013321_test6' => 6,
44+
];
45+
46+
$values = [
47+
'2022_10_13_013321_test1' => 1,
48+
'2022_10_13_013321_test6' => 6,
49+
'bar/2022_10_13_013321_test3' => 3,
50+
'bar/2022_10_13_013321_test5' => 5,
51+
'foo/2022_10_13_013321_test2' => 2,
52+
'foo/2022_10_13_013321_test4' => 4,
53+
];
54+
55+
$this->assertSame($expected, $this->sorter()->byKeys($values));
56+
}
57+
58+
protected function sorter(): Sorter
59+
{
60+
return new Sorter();
61+
}
62+
}

0 commit comments

Comments
 (0)