Skip to content

Commit

Permalink
Test collection validation #13
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Nov 4, 2019
1 parent 61fe2ed commit 11e6dca
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
23 changes: 23 additions & 0 deletions tests/Types/ParentFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Barryvdh\Form\Tests\Types;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class ParentFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'required' => true,
])
->add('children', CollectionType::class, [
'entry_type' => UserFormType::class,
'allow_add' => true,
])
;
}
}
92 changes: 85 additions & 7 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
namespace Barryvdh\Form\Tests;

use Barryvdh\Form\Facade\FormFactory;
use Barryvdh\Form\Tests\Types\ParentFormType;
use Barryvdh\Form\Tests\Types\UserFormType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;

class ValidationTest extends TestCase
{

public function testValidForm()
{
/** @var UserFormType $form */
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(UserFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save user']);

Expand All @@ -34,7 +31,7 @@ public function testValidForm()

public function testMissingNameForm()
{
/** @var UserFormType $form */
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(UserFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save user']);

Expand All @@ -49,11 +46,12 @@ public function testMissingNameForm()

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->assertEquals('The name field is required.', $form->getErrors(true)[0]->getMessage());
}

public function testInvalidEmailForm()
{
/** @var UserFormType $form */
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(UserFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save user']);

Expand All @@ -69,6 +67,86 @@ public function testInvalidEmailForm()

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->assertEquals('The email must be a valid email address.', $form->getErrors(true)[0]->getMessage());
}

public function testEmptyCollectionForm()
{
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(ParentFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save parent']);

$request = $this->createPostRequest([
'parent_form' => [
'name' => 'Barry',
'save' => true,
]
]);

$form->handleRequest($request);

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->assertEquals('The children field is required.', $form->getErrors(true)[0]->getMessage());
}

public function testInvalidCollectionForm()
{
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(ParentFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save parent']);

$request = $this->createPostRequest([
'parent_form' => [
'name' => 'Barry',
'children' => [
[
'name' => 'Foo',
'email' => '[email protected]',
],
[
'name' => 'Bar',
'email' => 'bar',
]
],
'save' => true,
]
]);

$form->handleRequest($request);

$this->assertTrue($form->isSubmitted());
$this->assertFalse($form->isValid());
$this->assertEquals('The children.1.email must be a valid email address.', $form->getErrors(true)[0]->getMessage());
}

public function testValidCollectionForm()
{
/** @var \Symfony\Component\Form\Form $form */
$form = FormFactory::create(ParentFormType::class, [])
->add('save', SubmitType::class, ['label' => 'Save parent']);

$request = $this->createPostRequest([
'parent_form' => [
'name' => 'Barry',
'children' => [
[
'name' => 'Foo',
'email' => '[email protected]',
],
[
'name' => 'Bar',
'email' => '[email protected]',
]
],
'save' => true,
]
]);

$form->handleRequest($request);

$this->assertTrue($form->isSubmitted());
$this->assertTrue($form->isValid());
}

private function createPostRequest($data)
Expand Down

0 comments on commit 11e6dca

Please sign in to comment.