Skip to content

Commit

Permalink
add filament v3 compatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Nov 7, 2023
1 parent 7d7db37 commit d429f74
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 48 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"filament/filament": "^2.17",
"filament/filament": "^3.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
Expand Down
32 changes: 14 additions & 18 deletions resources/views/components/turnstile.blade.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<x-dynamic-component
:component="$getFieldWrapperView()"
:id="$getId()"
:label="$getLabel()"
:label-sr-only="$isLabelHidden()"
:helper-text="$getHelperText()"
:hint="$getHint()"
:hint-action="$getHintAction()"
:hint-color="$getHintColor()"
:hint-icon="$getHintIcon()"
:required="$isRequired()"
:state-path="$getStatePath()"
>
<div x-data="{
state: $wire.entangle('{{ $getStatePath() }}').defer
@php
$id = $getId();
$statePath = $getStatePath();
$theme = $getTheme();
$size = $getSize();
$language = $getLanguage();
@endphp

<x-dynamic-component :component="$getFieldWrapperView()" :field="$field">
<div x-data="{
state: $wire.entangle('{{ $statePath }}').defer
}"
wire:ignore
>
<div id="turnstile-widget"
data-sitekey="{{config('turnstile.turnstile_site_key')}}"
data-theme="{{$getTheme()}}"
data-language="{{$getLanguage()}}"
data-size="{{$getSize()}}">
:data-theme="$theme"
:data-language="$language"
:data-size="$size">
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions src/FilamentTurnstileServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Coderflex\FilamentTurnstile;

use Filament\PluginServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class FilamentTurnstileServiceProvider extends PluginServiceProvider
class FilamentTurnstileServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
Expand Down
15 changes: 12 additions & 3 deletions src/Forms/Components/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@ public function language(string $language): static
return $this;
}

public function getTheme(): string
/**
* @return string
*/
public function getTheme()
{
return $this->evaluate($this->theme);
}

public function getSize(): string
/**
* @return string
*/
public function getSize()
{
return $this->evaluate($this->size);
}

public function getLanguage(): string
/**
* @return string
*/
public function getLanguage()
{
return $this->evaluate($this->language);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Database\Factories;

use Coderflex\FilamentTurnstile\Tests\Models\User;

class UserFactory extends \Orchestra\Testbench\Factories\UserFactory
{
protected $model = User::class;
}
21 changes: 21 additions & 0 deletions tests/Database/Migrations/create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
};
24 changes: 15 additions & 9 deletions tests/Fixtures/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;

class Login extends \Filament\Http\Livewire\Auth\Login
class Login extends \Filament\Pages\Auth\Login
{
protected function getFormSchema(): array
protected function getForms(): array
{
return array_merge(
parent::getFormSchema(),
[
Turnstile::make('cf-captcha')
->theme('auto'),
]
);
return [
'form' => $this->form(
$this->makeForm()
->schema([
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getRememberFormComponent(),
Turnstile::make('cf-captcha')
->theme('auto'),
])
->statePath('data'),
),
];
}
}
26 changes: 26 additions & 0 deletions tests/Fixtures/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Fixtures;

use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;

class Register extends \Filament\Pages\Auth\Register
{
protected function getForms(): array
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
$this->getNameFormComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent(),
Turnstile::make('cf-captcha')
->theme('auto'),
])
->statePath('data'),
),
];
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/TurnstilePanelProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Fixtures;

use Filament\Http\Middleware\Authenticate;
use Filament\Panel;
use Filament\PanelProvider;

class TurnstilePanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('turnstile')
->path('filament')
->authMiddleware([
Authenticate::class,
]);
}
}
13 changes: 13 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Coderflex\FilamentTurnstile\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use HasFactory;

protected $guarded = [];
}
38 changes: 24 additions & 14 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,53 @@
namespace Coderflex\FilamentTurnstile\Tests;

use Coderflex\FilamentTurnstile\FilamentTurnstileServiceProvider;
use Coderflex\FilamentTurnstile\Tests\Fixtures\Login;
use Filament\FilamentServiceProvider;
use Filament\Forms\FormsServiceProvider;
use Filament\Support\SupportServiceProvider;
use Livewire\Livewire;
use Livewire\LivewireServiceProvider;
use Coderflex\FilamentTurnstile\Tests\Fixtures\TurnstilePanelProvider;
use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
protected $enablesPackageDiscoveries = true;

protected function setUp(): void
{
parent::setUp();

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Coderflex\\FilamentTurnstile\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory'
);

config()->set('app.key', '6rE9Nz59bGRbeMATftriyQjrpF7DcOQm');

$this->registerLivewireComponents();
$this->setCurrentFilamentPanel();
}

protected function getPackageProviders($app)
{
return [
FilamentServiceProvider::class,
FormsServiceProvider::class,
LivewireServiceProvider::class,
SupportServiceProvider::class,
FilamentTurnstileServiceProvider::class,
TurnstilePanelProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
//
config()->set('database.default', 'testing');

$migrations = [
include __DIR__.'/Database/Migrations/create_users_table.php',
];

collect($migrations)->each(
fn ($migration) => $migration->up()
);
}

protected function registerLivewireComponents(): void
protected function setCurrentFilamentPanel(): void
{
Livewire::component('login', Login::class);
Filament::setCurrentPanel(
Filament::getPanel('turnstile')
);
}
}
19 changes: 18 additions & 1 deletion tests/TurnstileTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
<?php

use Coderflex\FilamentTurnstile\Tests\Fixtures\Login;
use Coderflex\FilamentTurnstile\Tests\Fixtures\Register;

use function Pest\Livewire\livewire;

it('has captcha field', function () {
it('can render login page', function () {
livewire(Login::class)
->assertOk();
});

it('can render register page', function () {
livewire(Register::class)
->assertOk();
});

test('login page has captcha field', function () {
livewire(Login::class)
->assertFormFieldExists('cf-captcha');
});

test('register page has captcha field', function () {
livewire(Register::class)
->assertFormFieldExists('cf-captcha');
});

0 comments on commit d429f74

Please sign in to comment.