Skip to content

Commit fea899e

Browse files
Fixed a call of operations
1 parent b11ae23 commit fea899e

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

src/Data/Casts/PathCast.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,30 @@
55
namespace DragonCode\LaravelDeployOperations\Data\Casts;
66

77
use DragonCode\LaravelDeployOperations\Helpers\ConfigHelper;
8+
use Illuminate\Support\Str;
89
use Spatie\LaravelData\Casts\Cast;
910
use Spatie\LaravelData\Support\Creation\CreationContext;
1011
use Spatie\LaravelData\Support\DataProperty;
1112

1213
use function app;
14+
use function realpath;
1315

1416
class PathCast implements Cast
1517
{
1618
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): string
1719
{
20+
$path = $this->config()->basePath((string) $value);
21+
1822
if ($properties['realpath'] ?? false) {
19-
return $value ?: $this->config()->basePath();
23+
return $value ?: $path;
2024
}
2125

22-
return $this->config()->basePath($value);
26+
return $this->filename($path) ?: $path;
27+
}
28+
29+
protected function filename(string $path): false|string
30+
{
31+
return realpath(Str::finish($path, '.php'));
2332
}
2433

2534
protected function config(): ConfigHelper

src/Services/MigratorService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Illuminate\Support\Facades\DB;
2020
use Throwable;
2121

22+
use function file_exists;
23+
use function is_file;
2224
use function method_exists;
2325
use function realpath;
2426

@@ -148,6 +150,10 @@ protected function disallowBefore(Operation $operation, OptionsData $options): b
148150

149151
protected function resolvePath(string $filename, string $path): string
150152
{
153+
if (file_exists($path) && is_file($path)) {
154+
return $path;
155+
}
156+
151157
$withExtension = Str::finish($filename, '.php');
152158

153159
if ($this->file->exists($withExtension) && $this->file->isFile($withExtension)) {

tests/Commands/OperationsTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DragonCode\LaravelDeployOperations\Constants\Names;
88
use DragonCode\LaravelDeployOperations\Jobs\OperationJob;
99
use Exception;
10-
use Illuminate\Database\Console\Migrations\MigrateCommand;
10+
use Illuminate\Database\Console\Migrations\RollbackCommand;
1111
use Illuminate\Support\Facades\DB;
1212
use Illuminate\Support\Facades\Queue;
1313
use Illuminate\Support\Str;
@@ -750,8 +750,6 @@ public function testSync()
750750

751751
public function testViaMigrationMethod()
752752
{
753-
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');
754-
755753
$this->copyViaMigrations();
756754

757755
$table = 'test';
@@ -767,7 +765,7 @@ public function testViaMigrationMethod()
767765
$this->assertDatabaseOperationDoesntLike($table, 'invoke', column: 'value');
768766
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
769767

770-
$this->artisan(MigrateCommand::class)->assertExitCode(0);
768+
$this->loadMigrationsFrom(__DIR__ . '/../fixtures/migrations_with_operations');
771769

772770
$this->assertDatabaseCount($table, 2);
773771
$this->assertDatabaseCount($this->table, 2);
@@ -777,5 +775,19 @@ public function testViaMigrationMethod()
777775
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
778776
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
779777
$this->assertDatabaseOperationHas($table, 'up_down', column: 'value');
778+
779+
$this->artisan(RollbackCommand::class, [
780+
'--path' => __DIR__ . '/../fixtures/migrations_with_operations',
781+
'--realpath' => true,
782+
])->assertSuccessful();
783+
784+
$this->assertDatabaseCount($table, 1);
785+
$this->assertDatabaseCount($this->table, 0);
786+
$this->assertDatabaseOperationDoesntLike($this->table, 'custom');
787+
$this->assertDatabaseOperationDoesntLike($this->table, 'invoke');
788+
$this->assertDatabaseOperationDoesntLike($this->table, 'up_down');
789+
$this->assertDatabaseOperationDoesntLike($table, 'custom', column: 'value');
790+
$this->assertDatabaseOperationHas($table, 'invoke', column: 'value');
791+
$this->assertDatabaseOperationDoesntLike($table, 'up_down', column: 'value');
780792
}
781793
}

tests/fixtures/migrations_with_operations/2025_03_31_213847_call_invokable.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use Illuminate\Database\Migrations\Migration;
66

77
return new class extends Migration {
8-
public function up(): void
9-
{
10-
//
11-
}
8+
public function up(): void {}
129

1310
public function down(): void {}
1411

tests/fixtures/migrations_with_operations/2025_03_31_213921_call_up_down.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
use Illuminate\Database\Migrations\Migration;
66

77
return new class extends Migration {
8-
public function up(): void
9-
{
10-
//
11-
}
8+
public function up(): void {}
129

1310
public function down(): void {}
1411

0 commit comments

Comments
 (0)