- membuat config baru untuk secret key - implementasi test - menyesuaikan validasi - kasih return type
202 lines
5.6 KiB
PHP
202 lines
5.6 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Forms\Studio\Setting\ContactForm;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Livewire\Livewire;
|
|
|
|
function createContactForm()
|
|
{
|
|
// Create a minimal Livewire component for testing
|
|
$component = new class extends \Livewire\Component
|
|
{
|
|
public ContactForm $form;
|
|
|
|
public function mount()
|
|
{
|
|
$this->form = new ContactForm($this, 'form');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return '<div></div>';
|
|
}
|
|
};
|
|
|
|
$component->mount();
|
|
|
|
return $component->form;
|
|
}
|
|
|
|
it('has correct validation rules', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
expect($rules)->toHaveKey('phone_number');
|
|
expect($rules)->toHaveKey('email');
|
|
expect($rules)->toHaveKey('address');
|
|
expect($rules)->toHaveKey('map_embed');
|
|
|
|
expect($rules['phone_number'])->toContain('required');
|
|
expect($rules['phone_number'])->toContain('string');
|
|
expect($rules['phone_number'])->toContain('min:10');
|
|
expect($rules['phone_number'])->toContain('max:20');
|
|
|
|
expect($rules['email'])->toContain('nullable');
|
|
expect($rules['email'])->toContain('email');
|
|
expect($rules['email'])->toContain('max:100');
|
|
|
|
expect($rules['address'])->toContain('required');
|
|
expect($rules['address'])->toContain('string');
|
|
expect($rules['address'])->toContain('min:10');
|
|
expect($rules['address'])->toContain('max:500');
|
|
|
|
expect($rules['map_embed'])->toContain('nullable');
|
|
expect($rules['map_embed'])->toContain('string');
|
|
expect($rules['map_embed'])->toContain('max:2000');
|
|
});
|
|
|
|
it('validates required fields', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '',
|
|
'address' => '',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('phone_number'))->toBeTrue();
|
|
expect($validator->errors()->has('address'))->toBeTrue();
|
|
});
|
|
|
|
it('validates phone number format', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => 'invalid',
|
|
'address' => 'Valid Address',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('phone_number'))->toBeTrue();
|
|
});
|
|
|
|
it('validates email format', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '0812 3456 789',
|
|
'email' => 'invalid-email',
|
|
'address' => 'Valid Address',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('email'))->toBeTrue();
|
|
});
|
|
|
|
it('validates phone number length', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '123',
|
|
'address' => 'Valid Address',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('phone_number'))->toBeTrue();
|
|
});
|
|
|
|
it('validates address length', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '+628123456789',
|
|
'address' => 'Short',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('address'))->toBeTrue();
|
|
});
|
|
|
|
it('validates email max length', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '+628123456789',
|
|
'email' => str_repeat('a', 101).'@example.com',
|
|
'address' => 'Valid Address',
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('email'))->toBeTrue();
|
|
});
|
|
|
|
it('validates map embed max length', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '+628123456789',
|
|
'address' => 'Valid Address',
|
|
'map_embed' => str_repeat('a', 2001),
|
|
], $rules);
|
|
|
|
expect($validator->fails())->toBeTrue();
|
|
expect($validator->errors()->has('map_embed'))->toBeTrue();
|
|
});
|
|
|
|
it('accepts nullable email', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '0812 3456 789',
|
|
'email' => null,
|
|
'address' => 'Valid Address',
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('accepts nullable map embed', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '0812 3456 789',
|
|
'address' => 'Valid Address',
|
|
'map_embed' => null,
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('passes validation with valid data', function () {
|
|
$form = createContactForm();
|
|
$rules = $form->rules();
|
|
|
|
$validator = Validator::make([
|
|
'phone_number' => '0812 3456 789',
|
|
'email' => 'test@example.com',
|
|
'address' => 'Valid Address with proper length',
|
|
'map_embed' => '<iframe>valid embed</iframe>',
|
|
], $rules);
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('has correct validation attributes', function () {
|
|
$form = createContactForm();
|
|
$attributes = $form->validationAttributes();
|
|
|
|
expect($attributes)->toHaveKey('phone_number', 'nomor telepon');
|
|
expect($attributes)->toHaveKey('email', 'email');
|
|
expect($attributes)->toHaveKey('address', 'alamat');
|
|
expect($attributes)->toHaveKey('map_embed', 'embed peta');
|
|
});
|