Skip to content

Commit 07adee0

Browse files
author
Andrey Helldar
authored
Merge pull request #13 from TheDragonCode/laravel-9
Laravel 9 support
2 parents ba0bd3d + d16fea6 commit 07adee0

File tree

50 files changed

+728
-51
lines changed

Some content is hidden

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

50 files changed

+728
-51
lines changed

.github/workflows/laravel-9.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: "Laravel 9"
2+
on: [ push ]
3+
4+
jobs:
5+
build:
6+
runs-on: ubuntu-latest
7+
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
php: [ "8.0", "8.1" ]
12+
laravel: [ "9.0" ]
13+
14+
name: Laravel ${{ matrix.laravel }}, PHP ${{ matrix.php }}
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php }}
24+
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
25+
coverage: none
26+
27+
- name: Install dependencies
28+
run: |
29+
composer require laravel/framework:^${{ matrix.laravel }}
30+
composer update --prefer-stable --prefer-dist --no-progress --no-interaction
31+
- name: Execute tests
32+
run: sudo vendor/bin/phpunit

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
},
2626
"require": {
2727
"php": "^7.3 || ^8.0",
28-
"dragon-code/contracts": "^2.9",
29-
"illuminate/console": "^6.0 || ^7.0 || ^8.0",
30-
"illuminate/database": "^6.0 || ^7.0 || ^8.0",
31-
"illuminate/support": "^6.0 || ^7.0 || ^8.0",
32-
"symfony/console": "^4.3 || ^5.0"
28+
"dragon-code/contracts": "^2.15",
29+
"dragon-code/laravel-support": "^3.2",
30+
"illuminate/console": "^6.0 || ^7.0 || ^8.0 || ^9.0",
31+
"illuminate/database": "^6.0 || ^7.0 || ^8.0 || ^9.0",
32+
"illuminate/support": "^6.0 || ^7.0 || ^8.0 || ^9.0",
33+
"symfony/console": "^4.3 || ^5.0 || ^6.0"
3334
},
3435
"require-dev": {
35-
"dragon-code/laravel-support": "^3.1",
3636
"mockery/mockery": "^1.3.1",
37-
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0",
37+
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0",
3838
"phpunit/phpunit": "^8.0 || ^9.0",
3939
"ramsey/uuid": "^3.7 || ^4.0"
4040
},

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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
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;
15+
use Versionable;
1416

1517
public function usingConnection($name, callable $callback)
1618
{
@@ -29,15 +31,23 @@ public function usingConnection($name, callable $callback)
2931
* @param string $file
3032
* @param int $batch
3133
* @param bool $pretend
34+
*
35+
* @throws \Throwable
3236
*/
3337
protected function runUp($file, $batch, $pretend)
3438
{
3539
// First we will resolve a "real" instance of the migration class from this
3640
// migration file name. Once we have the instances we can run the actual
3741
// command such as "up" or "down", or we can just simulate the action.
38-
$migration = $this->resolve(
39-
$name = $this->getMigrationName($file)
40-
);
42+
if ($this->isLatestApp()) {
43+
$migration = $this->resolvePath($file);
44+
45+
$name = $this->getMigrationName($file);
46+
} else {
47+
$migration = $this->resolve(
48+
$name = $this->getMigrationName($file)
49+
);
50+
}
4151

4252
if (! $this->allowEnvironment($migration)) {
4353
$this->note("<info>Migrate:</info> {$name} was skipped on this environment");
@@ -80,9 +90,15 @@ protected function runUp($file, $batch, $pretend)
8090
*/
8191
protected function runDown($file, $migration, $pretend)
8292
{
83-
$instance = $this->resolve(
84-
$name = $this->getMigrationName($file)
85-
);
93+
if ($this->isLatestApp()) {
94+
$instance = $this->resolvePath($file);
95+
96+
$name = $this->getMigrationName($file);
97+
} else {
98+
$instance = $this->resolve(
99+
$name = $this->getMigrationName($file)
100+
);
101+
}
86102

87103
if (! $this->allowEnvironment($instance)) {
88104
$this->note("<info>Rolling back:</info> {$name} was skipped on this environment");
@@ -119,23 +135,23 @@ protected function runMigration($migration, $method)
119135
/**
120136
* Whether it is necessary to record information about the execution in the database.
121137
*
122-
* @param object $migration
138+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
123139
*
124140
* @return bool
125141
*/
126-
protected function allowLogging($migration): bool
142+
protected function allowLogging(ActionableContract $migration): bool
127143
{
128144
return $migration->isOnce();
129145
}
130146

131147
/**
132148
* Whether the action needs to be executed in the current environment.
133149
*
134-
* @param object $migration
150+
* @param \DragonCode\Contracts\LaravelActions\Actionable|object $migration
135151
*
136152
* @return bool
137153
*/
138-
protected function allowEnvironment($migration): bool
154+
protected function allowEnvironment(ActionableContract $migration): bool
139155
{
140156
$environment = config('app.env', 'production');
141157

@@ -165,7 +181,7 @@ protected function allowEnvironment($migration): bool
165181
*
166182
* @return bool
167183
*/
168-
protected function enabledTransactions($migration): bool
184+
protected function enabledTransactions(ActionableContract $migration): bool
169185
{
170186
return $migration->enabledTransactions();
171187
}
@@ -177,7 +193,7 @@ protected function enabledTransactions($migration): bool
177193
*
178194
* @return int
179195
*/
180-
protected function transactionAttempts($migration): int
196+
protected function transactionAttempts(ActionableContract $migration): int
181197
{
182198
$value = $migration->transactionAttempts();
183199

tests/Commands/CreatorTest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function testCreateAction()
1313

1414
$filename = date('Y_m_d_His') . '_foo_example.php';
1515

16-
$path = database_path('actions/' . $filename);
16+
$path = $this->targetDirectory($filename);
1717

1818
$this->assertFileDoesNotExist($path);
1919

@@ -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
}

0 commit comments

Comments
 (0)