Skip to content

Commit a162a32

Browse files
author
Andrey Helldar
committed
Added Laravel 9 support
1 parent 7c66858 commit a162a32

File tree

49 files changed

+689
-55
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+689
-55
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
"require": {
2727
"php": "^7.3 || ^8.0",
2828
"dragon-code/contracts": "^2.15",
29+
"dragon-code/laravel-support": "^3.2",
2930
"illuminate/console": "^6.0 || ^7.0 || ^8.0 || ^9.0",
3031
"illuminate/database": "^6.0 || ^7.0 || ^8.0 || ^9.0",
3132
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0",
3233
"symfony/console": "^4.3 || ^5.0 || ^6.0"
3334
},
3435
"require-dev": {
35-
"dragon-code/laravel-support": "^3.2",
3636
"mockery/mockery": "^1.3.1",
3737
"orchestra/testbench": "7.x-dev",
3838
"phpunit/phpunit": "^8.0 || ^9.0",

resources/stubs/action-9.x.stub

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use DragonCode\LaravelActions\Support\Actionable;
4+
5+
return new class extends Actionable {
6+
/**
7+
* Run the actions.
8+
*
9+
* @return void
10+
*/
11+
public function up(): void
12+
{
13+
//
14+
}
15+
16+
/**
17+
* Reverse the actions.
18+
*
19+
* @return void
20+
*/
21+
public function down(): void
22+
{
23+
//
24+
}
25+
};
File renamed without changes.

src/Concerns/Versionable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelActions\Concerns;
6+
7+
use DragonCode\LaravelSupport\Facades\AppVersion;
8+
9+
trait Versionable
10+
{
11+
protected function isLatestApp(): bool
12+
{
13+
return AppVersion::is9x();
14+
}
15+
16+
protected function isPrevApp(): bool
17+
{
18+
return ! $this->isLatestApp();
19+
}
20+
}

src/Support/MigrationCreator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DragonCode\LaravelActions\Support;
44

5+
use DragonCode\LaravelSupport\Facades\AppVersion;
56
use Illuminate\Database\Migrations\MigrationCreator as BaseMigrationCreator;
67
use Illuminate\Filesystem\Filesystem;
78

@@ -28,10 +29,12 @@ public function stubPath()
2829
return realpath($this->customStubPath);
2930
}
3031

31-
protected function getStub($table, $create)
32+
protected function getStub($table, $create): string
3233
{
33-
$stub = $this->stubPath() . '/action.stub';
34+
$stub = AppVersion::is9x() ? '/action-9.x.stub' : '/action-prev.stub';
3435

35-
return $this->files->get($stub);
36+
return $this->files->get(
37+
$this->stubPath() . $stub
38+
);
3639
}
3740
}

src/Support/Migrator.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,39 @@
44

55
use DragonCode\Contracts\LaravelActions\Actionable as ActionableContract;
66
use DragonCode\LaravelActions\Concerns\Infoable;
7+
use DragonCode\LaravelActions\Concerns\Versionable;
78
use Illuminate\Database\Migrations\Migrator as BaseMigrator;
89
use Illuminate\Support\Facades\DB;
910
use Throwable;
1011

1112
class Migrator extends BaseMigrator
1213
{
1314
use Infoable;
14-
15-
public function usingConnection($name, callable $callback)
16-
{
17-
$prev = $this->resolver->getDefaultConnection();
18-
19-
$this->setConnection($name);
20-
21-
return tap($callback(), function () use ($prev) {
22-
$this->setConnection($prev);
23-
});
24-
}
15+
use Versionable;
2516

2617
/**
2718
* Run "up" a migration instance.
2819
*
2920
* @param string $file
3021
* @param int $batch
3122
* @param bool $pretend
23+
*
24+
* @throws \Throwable
3225
*/
3326
protected function runUp($file, $batch, $pretend)
3427
{
3528
// First we will resolve a "real" instance of the migration class from this
3629
// migration file name. Once we have the instances we can run the actual
3730
// command such as "up" or "down", or we can just simulate the action.
38-
$migration = $this->resolve(
39-
$name = $this->getMigrationName($file)
40-
);
31+
if ($this->isLatestApp()) {
32+
$migration = $this->resolvePath($file);
33+
34+
$name = $this->getMigrationName($file);
35+
} else {
36+
$migration = $this->resolve(
37+
$name = $this->getMigrationName($file)
38+
);
39+
}
4140

4241
if (! $this->allowEnvironment($migration)) {
4342
$this->note("<info>Migrate:</info> {$name} was skipped on this environment");
@@ -80,9 +79,15 @@ protected function runUp($file, $batch, $pretend)
8079
*/
8180
protected function runDown($file, $migration, $pretend)
8281
{
83-
$instance = $this->resolve(
84-
$name = $this->getMigrationName($file)
85-
);
82+
if ($this->isLatestApp()) {
83+
$instance = $this->resolvePath($file);
84+
85+
$name = $this->getMigrationName($file);
86+
} else {
87+
$instance = $this->resolve(
88+
$name = $this->getMigrationName($file)
89+
);
90+
}
8691

8792
if (! $this->allowEnvironment($instance)) {
8893
$this->note("<info>Rolling back:</info> {$name} was skipped on this environment");
@@ -119,23 +124,23 @@ protected function runMigration($migration, $method)
119124
/**
120125
* Whether it is necessary to record information about the execution in the database.
121126
*
122-
* @param object $migration
127+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
123128
*
124129
* @return bool
125130
*/
126-
protected function allowLogging($migration): bool
131+
protected function allowLogging(ActionableContract $migration): bool
127132
{
128133
return $migration->isOnce();
129134
}
130135

131136
/**
132137
* Whether the action needs to be executed in the current environment.
133138
*
134-
* @param object $migration
139+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
135140
*
136141
* @return bool
137142
*/
138-
protected function allowEnvironment($migration): bool
143+
protected function allowEnvironment(ActionableContract $migration): bool
139144
{
140145
$environment = config('app.env', 'production');
141146

@@ -165,7 +170,7 @@ protected function allowEnvironment($migration): bool
165170
*
166171
* @return bool
167172
*/
168-
protected function enabledTransactions($migration): bool
173+
protected function enabledTransactions(ActionableContract $migration): bool
169174
{
170175
return $migration->enabledTransactions();
171176
}
@@ -177,7 +182,7 @@ protected function enabledTransactions($migration): bool
177182
*
178183
* @return int
179184
*/
180-
protected function transactionAttempts($migration): int
185+
protected function transactionAttempts(ActionableContract $migration): int
181186
{
182187
$value = $migration->transactionAttempts();
183188

tests/Commands/CreatorTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ public function testCreateAction()
2222
$this->assertFileExists($path);
2323
}
2424

25-
public function testAlreadyExists()
25+
public function testDuplicateOnPrev()
2626
{
27+
if ($this->isLatestApp()) {
28+
$this->assertTrue(true);
29+
30+
return;
31+
}
32+
2733
$this->expectException(InvalidArgumentException::class);
2834
$this->expectExceptionMessage('A BarExample class already exists.');
2935

@@ -32,4 +38,24 @@ public function testAlreadyExists()
3238
$this->artisan('make:migration:action', compact('name'))->run();
3339
$this->artisan('make:migration:action', compact('name'))->run();
3440
}
41+
42+
public function testDuplicateOnLatest()
43+
{
44+
if ($this->isPrevApp()) {
45+
$this->assertTrue(true);
46+
47+
return;
48+
}
49+
50+
$name = 'BarExample';
51+
52+
$time1 = date('Y_m_d_His');
53+
$this->artisan('make:migration:action', compact('name'))->run();
54+
55+
$time2 = date('Y_m_d_His');
56+
$this->artisan('make:migration:action', compact('name'))->run();
57+
58+
$this->assertFileExists($this->targetDirectory($time1 . '_bar_example.php'));
59+
$this->assertFileExists($this->targetDirectory($time2 . '_bar_example.php'));
60+
}
3561
}

tests/Commands/MakeTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Commands;
44

5+
use DragonCode\LaravelSupport\Facades\AppVersion;
56
use Tests\TestCase;
67

78
class MakeTest extends TestCase
@@ -20,9 +21,10 @@ public function testMakingFiles()
2021

2122
$this->assertFileExists($path);
2223

23-
$this->assertEquals(
24-
file_get_contents(__DIR__ . '/../fixtures/stubs/make_example.stub'),
25-
file_get_contents($path)
26-
);
24+
$expected = AppVersion::is9x()
25+
? __DIR__ . '/../fixtures/app/9.x/stubs/make_example.stub'
26+
: __DIR__ . '/../fixtures/app/prev/stubs/make_example.stub';
27+
28+
$this->assertEquals(file_get_contents($expected), file_get_contents($path));
2729
}
2830
}

tests/Concerns/Actionable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Tests\Concerns;
44

5+
use DragonCode\LaravelSupport\Facades\AppVersion;
6+
57
trait Actionable
68
{
79
protected function getMigrationPath(): string
810
{
9-
return __DIR__ . '/../fixtures/actions';
11+
return AppVersion::is9x()
12+
? __DIR__ . '/../fixtures/app/9.x/actions'
13+
: __DIR__ . '/../fixtures/app/prev/actions';
1014
}
1115
}

tests/Concerns/Files.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,47 @@ protected function freshFiles(): void
1515

1616
protected function copyFiles(): void
1717
{
18-
File::copyDirectory(__DIR__ . '/../fixtures/actions', $this->targetDirectory());
18+
$source = $this->isLatestApp()
19+
? __DIR__ . '/../fixtures/app/9.x/actions'
20+
: __DIR__ . '/../fixtures/app/prev/actions';
21+
22+
File::copyDirectory($source, $this->targetDirectory());
1923
}
2024

2125
protected function copySuccessFailureMethod()
2226
{
23-
File::copy(
24-
__DIR__ . '/../fixtures/actions_failed/2021_12_23_165048_run_success_on_failed.php',
25-
$this->targetDirectory('2021_12_23_165048_run_success_on_failed.php')
26-
);
27+
$source = $this->isLatestApp()
28+
? __DIR__ . '/../fixtures/app/9.x/actions_failed/2021_12_23_165048_run_success_on_failed.php'
29+
: __DIR__ . '/../fixtures/app/prev/actions_failed/2021_12_23_165048_run_success_on_failed.php';
30+
31+
File::copy($source, $this->targetDirectory('2021_12_23_165048_run_success_on_failed.php'));
2732
}
2833

2934
protected function copyFailedMethod()
3035
{
31-
File::copy(
32-
__DIR__ . '/../fixtures/actions_failed/2021_12_23_184029_run_failed_failure.php',
33-
$this->targetDirectory('2021_12_23_184029_run_failed_failure.php')
34-
);
36+
$source = $this->isLatestApp()
37+
? __DIR__ . '/../fixtures/app/9.x/actions_failed/2021_12_23_184029_run_failed_failure.php'
38+
: __DIR__ . '/../fixtures/app/prev/actions_failed/2021_12_23_184029_run_failed_failure.php';
39+
40+
File::copy($source, $this->targetDirectory('2021_12_23_184029_run_failed_failure.php'));
3541
}
3642

3743
protected function copySuccessTransaction(): void
3844
{
39-
File::copy(
40-
__DIR__ . '/../fixtures/stubs/2021_02_15_124237_test_success_transactions.stub',
41-
$this->targetDirectory('2021_02_15_124237_test_success_transactions.php')
42-
);
45+
$source = $this->isLatestApp()
46+
? __DIR__ . '/../fixtures/app/9.x/stubs/2021_02_15_124237_test_success_transactions.stub'
47+
: __DIR__ . '/../fixtures/app/prev/stubs/2021_02_15_124237_test_success_transactions.stub';
48+
49+
File::copy($source, $this->targetDirectory('2021_02_15_124237_test_success_transactions.php'));
4350
}
4451

4552
protected function copyFailedTransaction(): void
4653
{
47-
File::copy(
48-
__DIR__ . '/../fixtures/stubs/2021_02_15_124852_test_failed_transactions.stub',
49-
$this->targetDirectory('2021_02_15_124852_test_failed_transactions.php')
50-
);
54+
$source = $this->isLatestApp()
55+
? __DIR__ . '/../fixtures/app/9.x/stubs/2021_02_15_124852_test_failed_transactions.stub'
56+
: __DIR__ . '/../fixtures/app/prev/stubs/2021_02_15_124852_test_failed_transactions.stub';
57+
58+
File::copy($source, $this->targetDirectory('2021_02_15_124852_test_failed_transactions.php'));
5159
}
5260

5361
protected function targetDirectory(string $path = null): string

0 commit comments

Comments
 (0)