Skip to content

Commit

Permalink
Merge branch 'release/v0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
leeovery committed Dec 28, 2022
2 parents 5ced4d9 + 68a88eb commit 168d3b7
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 7,821 deletions.
7,801 changes: 0 additions & 7,801 deletions composer.lock

This file was deleted.

47 changes: 47 additions & 0 deletions src/Actions/BaseEnvAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Leeovery\LaravelPlaywright\Actions;

use Dotenv\Dotenv;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

abstract class BaseEnvAction
{
protected string $backupEnvName = '.env.backup';

protected function restoreEnvironment(): void
{
copy(base_path($this->backupEnvName), base_path('.env'));
unlink(base_path($this->backupEnvName));
}

protected function swapEnvironment(): void
{
copy(base_path('.env'), base_path($this->backupEnvName));
copy(base_path($this->getPlaywrightEnvFile()), base_path('.env'));
}

protected function getPlaywrightEnvFile(): string
{
$envName = config('laravel-playwright.env.name');
$environment = app()->environment();
if (file_exists(base_path($file = "{$envName}.{$environment}"))) {
return $file;
}

if (file_exists(base_path($envName))) {
return $envName;
}

abort(404, 'Playwright env file missing');
}

protected function refreshEnvironment(): void
{
Artisan::call('optimize:clear');
Dotenv::createMutable(base_path())->load();
Artisan::call('optimize');
DB::reconnect();
}
}
19 changes: 19 additions & 0 deletions src/Actions/EnvSetupAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Leeovery\LaravelPlaywright\Actions;

class EnvSetupAction extends BaseEnvAction
{
public function __invoke()
{
if (! file_exists($envFile = base_path($this->getPlaywrightEnvFile()))) {
return;
}

if (file_get_contents(base_path('.env')) !== file_get_contents($envFile)) {
$this->swapEnvironment();
}

$this->refreshEnvironment();
}
}
14 changes: 14 additions & 0 deletions src/Actions/EnvTeardownAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Leeovery\LaravelPlaywright\Actions;

class EnvTeardownAction extends BaseEnvAction
{
public function __invoke()
{
if (file_exists(base_path($this->backupEnvName))) {
$this->restoreEnvironment();
$this->refreshEnvironment();
}
}
}
19 changes: 13 additions & 6 deletions src/Commands/LaravelPlaywrightEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@

use Dotenv\Dotenv;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

abstract class LaravelPlaywrightEnv extends Command
{
protected string $backupEnvName = '.env.backup';

abstract public function handle();

protected function restoreEnvironment(): void
{
copy(base_path('.env.backup'), base_path('.env'));
unlink(base_path('.env.backup'));
copy(base_path($this->backupEnvName), base_path('.env'));
unlink(base_path($this->backupEnvName));
}

protected function backupEnvironment(): void
protected function swapEnvironment(): void
{
copy(base_path('.env'), base_path('.env.backup'));
copy(base_path($this->appTestEnvFile()), base_path('.env'));
copy(base_path('.env'), base_path($this->backupEnvName));
copy(base_path($this->getPlaywrightEnvFile()), base_path('.env'));
}

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

protected function refreshEnvironment(): void
{
Artisan::call('optimize:clear');
Dotenv::createMutable(base_path())->load();
Artisan::call('optimize');
DB::reconnect();
}
}
9 changes: 3 additions & 6 deletions src/Commands/LaravelPlaywrightEnvSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ class LaravelPlaywrightEnvSetup extends LaravelPlaywrightEnv

public function handle()
{
if (! file_exists(base_path($this->appTestEnvFile()))) {
if (! file_exists($envFile = base_path($this->getPlaywrightEnvFile()))) {
return;
}

if (
file_get_contents(base_path('.env')) !==
file_get_contents(base_path($this->appTestEnvFile()))
) {
$this->backupEnvironment();
if (file_get_contents(base_path('.env')) !== file_get_contents($envFile)) {
$this->swapEnvironment();
}

$this->refreshEnvironment();
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/LaravelPlaywrightEnvTeardown.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LaravelPlaywrightEnvTeardown extends LaravelPlaywrightEnv

public function handle()
{
if (file_exists(base_path('.env.backup'))) {
if (file_exists(base_path($this->backupEnvName))) {
$this->restoreEnvironment();
$this->refreshEnvironment();
}
Expand Down
42 changes: 35 additions & 7 deletions src/Http/Controllers/LaravelPlaywrightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Leeovery\LaravelPlaywright\Http\Controllers;

use Exception;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\Request;
use Illuminate\Routing\Route as RoutingRoute;
Expand All @@ -14,17 +15,36 @@ class LaravelPlaywrightController
{
public function migrate(Request $request)
{
Artisan::call(command: 'migrate:fresh'.($request->boolean('seed') ? ' --seed' : ''));
try {
Artisan::call('migrate:fresh --schema-path=false'.($request->boolean('seed') ? ' --seed' : ''));
} catch (Exception $exception) {
return response()->json($exception->getMessage(), 500);
}

return response()->json(Artisan::output(), 202);
}

public function setupEnv()
{
return Artisan::call(command: 'playwright:env-setup');
try {
Artisan::call('playwright:env-setup');
sleep(2);
} catch (Exception $exception) {
return response()->json($exception->getMessage(), 500);
}

return response()->json(null, 202);
}

public function tearDownEnv()
{
return Artisan::call(command: 'playwright:env-teardown');
try {
Artisan::call('playwright:env-teardown');
} catch (Exception $exception) {
return response()->json($exception->getMessage(), 500);
}

return response()->json(null, 202);
}

public function routes(Request $request)
Expand Down Expand Up @@ -130,10 +150,18 @@ public function logout()

public function artisan(Request $request)
{
Artisan::call(
command: $request->input('command'),
parameters: $request->input('parameters', [])
);
$request->validate(['command' => 'required']);

try {
Artisan::call(
command: $request->input('command'),
parameters: $request->input('parameters', [])
);
} catch (Exception $exception) {
abort($exception->getCode(), $exception->getMessage());
}

return response()->json(Artisan::output(), 202);
}

public function csrf()
Expand Down

0 comments on commit 168d3b7

Please sign in to comment.