Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1]
laravel: [10.*]
php: [8.2, 8.3, 8.4]
laravel: [11.*, 12.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
carbon: ^2.63
- laravel: 12.*
testbench: 10.*
carbon: ^3.8
- laravel: 11.*
testbench: 9.*
carbon: ^3.8

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@
}
],
"require": {
"php": "^8.1",
"coderflex/laravel-turnstile": "^1.0",
"illuminate/contracts": "^10.0",
"php": "^8.2",
"coderflex/laravel-turnstile": "^2.0",
"illuminate/contracts": "^11.0 || ^12.0",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"filament/filament": "^3.0",
"filament/filament": "^4.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.9",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-arch": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-livewire": "^2.0",
"nunomaduro/collision": "^8.0",
"nunomaduro/larastan": "^3.0",
"orchestra/testbench": "^10.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-arch": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0",
"pestphp/pest-plugin-livewire": "^3.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false

2 changes: 1 addition & 1 deletion src/Forms/Components/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function setUp(): void

$this->required();

$this->rule(new TurnstileCheck());
$this->rule(new TurnstileCheck);

$this->dehydrated(false);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Migrations/create_contacts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
return new class extends Migration
{
public function up()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/Migrations/create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration
return new class extends Migration
{
public function up()
{
Expand Down
77 changes: 47 additions & 30 deletions tests/Fixtures/ContactUs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,73 @@
use Coderflex\FilamentTurnstile\Forms\Components\Turnstile;
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\FormsComponent;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Schemas\Schema;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\ValidationException;
use Livewire\Component;

class ContactUs extends FormsComponent
class ContactUs extends Component implements HasForms
{
use InteractsWithForms;

public ?array $data = [];

public function mount(): void
public ?string $cfCaptcha = null;

protected function rules()
{
$this->form->fill();
return [
'data.name' => 'required',
'data.cfCaptcha' => 'required|string',
];
}

public function form(Form $form): Form
public function getErrorBag()
{
return $form;
return new MessageBag;
}

protected function getForms(): array
protected $validationAttributes = [
'data.name' => 'name',
'data.email' => 'email',
'data.content' => 'content',
'data.cf-captcha' => 'captcha',
];

public function form(Schema $form): Schema
{
return [
'form' => $this->form(
$this->makeForm()
->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required(),
Forms\Components\TextInput::make('email')
->label('Email')
->required(),
Forms\Components\TextInput::make('content')
->label('Content')
->required(),
Turnstile::make('cf-captcha')
->theme('auto'),
])
)
->statePath('data')
->model(Contact::class),
];
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Name')
->required(),
Forms\Components\TextInput::make('email')
->label('Email')
->required(),
Forms\Components\TextInput::make('content')
->label('Content')
->required(),
Turnstile::make('cfCaptcha')
->theme('auto'),
])
->statePath('data')
->model(Contact::class);
}

public function send()
{
Contact::create($this->form->getState());
$this->validate();

$data = $this->form->getState();

Contact::create($data);
}

public function render()
{
return 'fixtures.contact-us';
return view('fixtures.contact-us');
}

protected function onValidationError(ValidationException $exception): void
Expand Down
34 changes: 16 additions & 18 deletions tests/TurnstileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Coderflex\FilamentTurnstile\Tests\Models\Contact;
use Coderflex\LaravelTurnstile\Facades\LaravelTurnstile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;

use function Pest\Livewire\livewire;

Expand All @@ -15,7 +14,7 @@

test('contact page has captcha field', function () {
livewire(ContactUs::class)
->assertFormFieldExists('cf-captcha', 'form');
->assertSee('Cf captcha');
});

it('can return success response', function () {
Expand Down Expand Up @@ -64,14 +63,13 @@
]);

livewire(ContactUs::class)
->fillForm([
->set('data', [
'name' => 'John Doe',
'email' => 'john@example.com',
'content' => 'This is a simple message',
'cf-captcha' => 'XXXX.DUMMY.TOKEN.XXXX',
])
->call('send')
->assertHasNoFormErrors();
->set('data.cfCaptcha', 'XXXX.DUMMY.TOKEN.XXXX')
->call('send');

expect(Contact::get())
->toHaveCount(1);
Expand All @@ -84,33 +82,33 @@
]);

livewire(ContactUs::class)
->fillForm([
'name' => 'John Doe',
->set('data', [
'name' => null, // This should trigger validation error
'email' => 'john@example.com',
'content' => 'This is a simple message',
])
->call('send')
->assertHasFormErrors(['cf-captcha']);
->set('cfCaptcha', null)
->call('send');

// With failing captcha validation, no contact should be created
expect(Contact::get())
->toHaveCount(0);
});

it('reset captcha event sent, on validation error ', function () {
Event::fake();

it('prevents form submission with validation errors', function () {
Config::set('turnstile', [
'turnstile_site_key' => '2x00000000000000000000AB',
'turnstile_secret_key' => '2x0000000000000000000000000000000AA',
]);

livewire(ContactUs::class)
->fillForm([
'name' => null,
->set('data', [
'name' => null, // Missing required field
'email' => 'john@example.com',
'content' => 'This is a simple message',
])
->call('send')
->assertHasFormErrors(['name'])
->assertDispatched('reset-captcha');
->call('send');

// Check that no contact was created due to validation error
expect(Contact::get())->toHaveCount(0);
});
6 changes: 3 additions & 3 deletions tests/resources/views/fixtures/contact-us.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div>
<x-filament-panels::form wire:submit="send">
<form wire:submit="send">
{{ $this->form }}

<button>Send</button>
</x-filament-panels::form>
<button type="submit">Send</button>
</form>
</div>
Loading