Skip to content

Commit 168d3b7

Browse files
committed
Merge branch 'release/v0.0.1'
2 parents 5ced4d9 + 68a88eb commit 168d3b7

8 files changed

+132
-7821
lines changed

composer.lock

-7,801
This file was deleted.

src/Actions/BaseEnvAction.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Leeovery\LaravelPlaywright\Actions;
4+
5+
use Dotenv\Dotenv;
6+
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\DB;
8+
9+
abstract class BaseEnvAction
10+
{
11+
protected string $backupEnvName = '.env.backup';
12+
13+
protected function restoreEnvironment(): void
14+
{
15+
copy(base_path($this->backupEnvName), base_path('.env'));
16+
unlink(base_path($this->backupEnvName));
17+
}
18+
19+
protected function swapEnvironment(): void
20+
{
21+
copy(base_path('.env'), base_path($this->backupEnvName));
22+
copy(base_path($this->getPlaywrightEnvFile()), base_path('.env'));
23+
}
24+
25+
protected function getPlaywrightEnvFile(): string
26+
{
27+
$envName = config('laravel-playwright.env.name');
28+
$environment = app()->environment();
29+
if (file_exists(base_path($file = "{$envName}.{$environment}"))) {
30+
return $file;
31+
}
32+
33+
if (file_exists(base_path($envName))) {
34+
return $envName;
35+
}
36+
37+
abort(404, 'Playwright env file missing');
38+
}
39+
40+
protected function refreshEnvironment(): void
41+
{
42+
Artisan::call('optimize:clear');
43+
Dotenv::createMutable(base_path())->load();
44+
Artisan::call('optimize');
45+
DB::reconnect();
46+
}
47+
}

src/Actions/EnvSetupAction.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Leeovery\LaravelPlaywright\Actions;
4+
5+
class EnvSetupAction extends BaseEnvAction
6+
{
7+
public function __invoke()
8+
{
9+
if (! file_exists($envFile = base_path($this->getPlaywrightEnvFile()))) {
10+
return;
11+
}
12+
13+
if (file_get_contents(base_path('.env')) !== file_get_contents($envFile)) {
14+
$this->swapEnvironment();
15+
}
16+
17+
$this->refreshEnvironment();
18+
}
19+
}

src/Actions/EnvTeardownAction.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Leeovery\LaravelPlaywright\Actions;
4+
5+
class EnvTeardownAction extends BaseEnvAction
6+
{
7+
public function __invoke()
8+
{
9+
if (file_exists(base_path($this->backupEnvName))) {
10+
$this->restoreEnvironment();
11+
$this->refreshEnvironment();
12+
}
13+
}
14+
}

src/Commands/LaravelPlaywrightEnv.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@
44

55
use Dotenv\Dotenv;
66
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Artisan;
8+
use Illuminate\Support\Facades\DB;
79

810
abstract class LaravelPlaywrightEnv extends Command
911
{
12+
protected string $backupEnvName = '.env.backup';
13+
1014
abstract public function handle();
1115

1216
protected function restoreEnvironment(): void
1317
{
14-
copy(base_path('.env.backup'), base_path('.env'));
15-
unlink(base_path('.env.backup'));
18+
copy(base_path($this->backupEnvName), base_path('.env'));
19+
unlink(base_path($this->backupEnvName));
1620
}
1721

18-
protected function backupEnvironment(): void
22+
protected function swapEnvironment(): void
1923
{
20-
copy(base_path('.env'), base_path('.env.backup'));
21-
copy(base_path($this->appTestEnvFile()), base_path('.env'));
24+
copy(base_path('.env'), base_path($this->backupEnvName));
25+
copy(base_path($this->getPlaywrightEnvFile()), base_path('.env'));
2226
}
2327

24-
protected function appTestEnvFile(): string
28+
protected function getPlaywrightEnvFile(): string
2529
{
2630
$envName = config('laravel-playwright.env.name');
2731
if (file_exists(base_path($file = "{$envName}.{$this->laravel->environment()}"))) {
@@ -37,6 +41,9 @@ protected function appTestEnvFile(): string
3741

3842
protected function refreshEnvironment(): void
3943
{
44+
Artisan::call('optimize:clear');
4045
Dotenv::createMutable(base_path())->load();
46+
Artisan::call('optimize');
47+
DB::reconnect();
4148
}
4249
}

src/Commands/LaravelPlaywrightEnvSetup.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ class LaravelPlaywrightEnvSetup extends LaravelPlaywrightEnv
1010

1111
public function handle()
1212
{
13-
if (! file_exists(base_path($this->appTestEnvFile()))) {
13+
if (! file_exists($envFile = base_path($this->getPlaywrightEnvFile()))) {
1414
return;
1515
}
1616

17-
if (
18-
file_get_contents(base_path('.env')) !==
19-
file_get_contents(base_path($this->appTestEnvFile()))
20-
) {
21-
$this->backupEnvironment();
17+
if (file_get_contents(base_path('.env')) !== file_get_contents($envFile)) {
18+
$this->swapEnvironment();
2219
}
2320

2421
$this->refreshEnvironment();

src/Commands/LaravelPlaywrightEnvTeardown.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LaravelPlaywrightEnvTeardown extends LaravelPlaywrightEnv
1010

1111
public function handle()
1212
{
13-
if (file_exists(base_path('.env.backup'))) {
13+
if (file_exists(base_path($this->backupEnvName))) {
1414
$this->restoreEnvironment();
1515
$this->refreshEnvironment();
1616
}

src/Http/Controllers/LaravelPlaywrightController.php

+35-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Leeovery\LaravelPlaywright\Http\Controllers;
44

5+
use Exception;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67
use Illuminate\Http\Request;
78
use Illuminate\Routing\Route as RoutingRoute;
@@ -14,17 +15,36 @@ class LaravelPlaywrightController
1415
{
1516
public function migrate(Request $request)
1617
{
17-
Artisan::call(command: 'migrate:fresh'.($request->boolean('seed') ? ' --seed' : ''));
18+
try {
19+
Artisan::call('migrate:fresh --schema-path=false'.($request->boolean('seed') ? ' --seed' : ''));
20+
} catch (Exception $exception) {
21+
return response()->json($exception->getMessage(), 500);
22+
}
23+
24+
return response()->json(Artisan::output(), 202);
1825
}
1926

2027
public function setupEnv()
2128
{
22-
return Artisan::call(command: 'playwright:env-setup');
29+
try {
30+
Artisan::call('playwright:env-setup');
31+
sleep(2);
32+
} catch (Exception $exception) {
33+
return response()->json($exception->getMessage(), 500);
34+
}
35+
36+
return response()->json(null, 202);
2337
}
2438

2539
public function tearDownEnv()
2640
{
27-
return Artisan::call(command: 'playwright:env-teardown');
41+
try {
42+
Artisan::call('playwright:env-teardown');
43+
} catch (Exception $exception) {
44+
return response()->json($exception->getMessage(), 500);
45+
}
46+
47+
return response()->json(null, 202);
2848
}
2949

3050
public function routes(Request $request)
@@ -130,10 +150,18 @@ public function logout()
130150

131151
public function artisan(Request $request)
132152
{
133-
Artisan::call(
134-
command: $request->input('command'),
135-
parameters: $request->input('parameters', [])
136-
);
153+
$request->validate(['command' => 'required']);
154+
155+
try {
156+
Artisan::call(
157+
command: $request->input('command'),
158+
parameters: $request->input('parameters', [])
159+
);
160+
} catch (Exception $exception) {
161+
abort($exception->getCode(), $exception->getMessage());
162+
}
163+
164+
return response()->json(Artisan::output(), 202);
137165
}
138166

139167
public function csrf()

0 commit comments

Comments
 (0)