Skip to content

Commit 125c352

Browse files
committed
Merge branch 'release/v0.1.1'
2 parents 3b03356 + fd62896 commit 125c352

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

config/laravel-playwright.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,13 @@
149149
'param_separator' => ',',
150150

151151
/**
152-
* You can optionally register aliases for models or other objects, rather than having
153-
* to provide the fully namespaced class name. You can then provide the alias
154-
* when creating entities via the factory endpoint. You can also define here a function
155-
* to instruct Laravel-Playwright how to construct an object with the parameters sent
156-
* from your Playwright test suite.
152+
* You can optionally register aliases for models rather than having to provide the fully
153+
* namespaced class name. You can then provide the alias when creating entities via the
154+
* factory endpoint.
157155
*/
158156
'model_aliases' => [
159-
// 'User' => 'App\\Models\\User',
160-
// 'Post' => 'App\\Models\\Post',
161-
],
162-
163-
'param_aliases' => [
164-
// 'Carbon' => fn($date) => \Carbon\Carbon::create($date),
165-
// 'collect' => fn($items) => collect(...$items),
157+
// 'User' => '\App\Models\User::class',
158+
// 'Post' => '\App\Models\Post::class',
166159
],
167160

168161
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LaravelPlaywrightServiceProvider extends ServiceProvider
8+
{
9+
public function register()
10+
{
11+
//
12+
}
13+
14+
public function boot()
15+
{
16+
// Playwright::alias('Carbon', fn(string $date) => \Carbon\Carbon::create($date));
17+
// Playwright::alias('collect', fn($items) => collect(...$items));
18+
}
19+
}

src/Http/Controllers/LaravelPlaywrightController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Facades\Route;
1212
use Illuminate\Support\Str;
1313
use Leeovery\LaravelPlaywright\Exceptions\LaravelPlaywrightException;
14+
use Leeovery\LaravelPlaywright\Playwright;
1415
use Throwable;
1516

1617
class LaravelPlaywrightController
@@ -309,7 +310,7 @@ protected function resolveStateAttributes($state): array
309310
*/
310311
protected function resolveParamAlias(string $alias): callable
311312
{
312-
$paramAlias = data_get(config('laravel-playwright.factory.param_aliases'), $alias);
313+
$paramAlias = Playwright::getAlias($alias);
313314

314315
throw_if(is_null($paramAlias),
315316
LaravelPlaywrightException::resolvedParamAliasDoesNotExist($alias)

src/LaravelPlaywrightServiceProvider.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Leeovery\LaravelPlaywright\Commands\Database\DropDatabaseCommand;
77
use Leeovery\LaravelPlaywright\Commands\LaravelPlaywrightEnvSetup;
88
use Leeovery\LaravelPlaywright\Commands\LaravelPlaywrightEnvTeardown;
9+
use Spatie\LaravelPackageTools\Commands\InstallCommand;
910
use Spatie\LaravelPackageTools\Package;
1011
use Spatie\LaravelPackageTools\PackageServiceProvider as ServiceProvider;
1112

@@ -17,11 +18,18 @@ public function configurePackage(Package $package): void
1718
->name('laravel-playwright')
1819
->hasConfigFile('laravel-playwright')
1920
->hasRoute('playwright')
21+
->publishesServiceProvider('LaravelPlaywrightServiceProvider')
2022
->hasCommands(
2123
LaravelPlaywrightEnvSetup::class,
2224
LaravelPlaywrightEnvTeardown::class,
2325
CreateDatabaseCommand::class,
2426
DropDatabaseCommand::class,
25-
);
27+
)
28+
->hasInstallCommand(function (InstallCommand $command) {
29+
$command
30+
->publishConfigFile()
31+
->copyAndRegisterServiceProviderInApp()
32+
->askToStarRepoOnGitHub('leeovery/laravel-playwright');
33+
});
2634
}
2735
}

src/Playwright.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Leeovery\LaravelPlaywright;
4+
5+
use Closure;
6+
7+
class Playwright
8+
{
9+
public static array $aliasCallbacks = [];
10+
11+
public static function alias(string $alias, Closure $callable): static
12+
{
13+
static::$aliasCallbacks[$alias] = $callable;
14+
15+
return new static;
16+
}
17+
18+
public static function getAlias(string $alias): callable|null
19+
{
20+
return data_get(static::$aliasCallbacks, $alias);
21+
}
22+
}

0 commit comments

Comments
 (0)