Skip to content

Commit

Permalink
Merge branch 'release/v0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
leeovery committed Jan 27, 2023
2 parents 3b03356 + fd62896 commit 125c352
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
17 changes: 5 additions & 12 deletions config/laravel-playwright.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,13 @@
'param_separator' => ',',

/**
* You can optionally register aliases for models or other objects, rather than having
* to provide the fully namespaced class name. You can then provide the alias
* when creating entities via the factory endpoint. You can also define here a function
* to instruct Laravel-Playwright how to construct an object with the parameters sent
* from your Playwright test suite.
* You can optionally register aliases for models rather than having to provide the fully
* namespaced class name. You can then provide the alias when creating entities via the
* factory endpoint.
*/
'model_aliases' => [
// 'User' => 'App\\Models\\User',
// 'Post' => 'App\\Models\\Post',
],

'param_aliases' => [
// 'Carbon' => fn($date) => \Carbon\Carbon::create($date),
// 'collect' => fn($items) => collect(...$items),
// 'User' => '\App\Models\User::class',
// 'Post' => '\App\Models\Post::class',
],

],
Expand Down
19 changes: 19 additions & 0 deletions resources/stubs/LaravelPlaywrightServiceProvider.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class LaravelPlaywrightServiceProvider extends ServiceProvider
{
public function register()
{
//
}

public function boot()
{
// Playwright::alias('Carbon', fn(string $date) => \Carbon\Carbon::create($date));
// Playwright::alias('collect', fn($items) => collect(...$items));
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/LaravelPlaywrightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use Leeovery\LaravelPlaywright\Exceptions\LaravelPlaywrightException;
use Leeovery\LaravelPlaywright\Playwright;
use Throwable;

class LaravelPlaywrightController
Expand Down Expand Up @@ -309,7 +310,7 @@ protected function resolveStateAttributes($state): array
*/
protected function resolveParamAlias(string $alias): callable
{
$paramAlias = data_get(config('laravel-playwright.factory.param_aliases'), $alias);
$paramAlias = Playwright::getAlias($alias);

throw_if(is_null($paramAlias),
LaravelPlaywrightException::resolvedParamAliasDoesNotExist($alias)
Expand Down
10 changes: 9 additions & 1 deletion src/LaravelPlaywrightServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Leeovery\LaravelPlaywright\Commands\Database\DropDatabaseCommand;
use Leeovery\LaravelPlaywright\Commands\LaravelPlaywrightEnvSetup;
use Leeovery\LaravelPlaywright\Commands\LaravelPlaywrightEnvTeardown;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider as ServiceProvider;

Expand All @@ -17,11 +18,18 @@ public function configurePackage(Package $package): void
->name('laravel-playwright')
->hasConfigFile('laravel-playwright')
->hasRoute('playwright')
->publishesServiceProvider('LaravelPlaywrightServiceProvider')
->hasCommands(
LaravelPlaywrightEnvSetup::class,
LaravelPlaywrightEnvTeardown::class,
CreateDatabaseCommand::class,
DropDatabaseCommand::class,
);
)
->hasInstallCommand(function (InstallCommand $command) {
$command
->publishConfigFile()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub('leeovery/laravel-playwright');
});
}
}
22 changes: 22 additions & 0 deletions src/Playwright.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Leeovery\LaravelPlaywright;

use Closure;

class Playwright
{
public static array $aliasCallbacks = [];

public static function alias(string $alias, Closure $callable): static
{
static::$aliasCallbacks[$alias] = $callable;

return new static;
}

public static function getAlias(string $alias): callable|null
{
return data_get(static::$aliasCallbacks, $alias);
}
}

0 comments on commit 125c352

Please sign in to comment.