Skip to content

Commit 507a71f

Browse files
committed
Merge pull request #1827 from Soullivaneuh/deprecated-form-options
Add tests for form types
2 parents 1a65ac8 + 8e79558 commit 507a71f

File tree

8 files changed

+209
-0
lines changed

8 files changed

+209
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use FOS\UserBundle\Form\Type\ChangePasswordFormType;
6+
use FOS\UserBundle\Tests\TestUser;
7+
8+
class ChangePasswordFormTypeTest extends ValidatorExtensionTypeTestCase
9+
{
10+
public function testSubmit()
11+
{
12+
$user = new TestUser();
13+
$user->setPassword('foo');
14+
15+
$form = $this->factory->create(new ChangePasswordFormType('FOS\UserBundle\Tests\TestUser'), $user);
16+
$formData = array(
17+
'current_password' => 'foo',
18+
'plainPassword' => array(
19+
'first' => 'bar',
20+
'second' => 'bar',
21+
),
22+
);
23+
$form->submit($formData);
24+
25+
$this->assertTrue($form->isSynchronized());
26+
$this->assertEquals($user, $form->getData());
27+
$this->assertEquals('bar', $user->getPlainPassword());
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use FOS\UserBundle\Form\Type\GroupFormType;
6+
use FOS\UserBundle\Tests\TestGroup;
7+
8+
class GroupFormTypeTest extends TypeTestCase
9+
{
10+
public function testSubmit()
11+
{
12+
$group = new TestGroup('foo');
13+
14+
$form = $this->factory->create(new GroupFormType('FOS\UserBundle\Tests\TestGroup'), $group);
15+
$formData = array(
16+
'name' => 'bar',
17+
);
18+
$form->submit($formData);
19+
20+
$this->assertTrue($form->isSynchronized());
21+
$this->assertEquals($group, $form->getData());
22+
$this->assertEquals('bar', $group->getName());
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use FOS\UserBundle\Form\Type\ProfileFormType;
6+
use FOS\UserBundle\Tests\TestUser;
7+
8+
class ProfileFormTypeTest extends ValidatorExtensionTypeTestCase
9+
{
10+
public function testSubmit()
11+
{
12+
$user = new TestUser();
13+
14+
$form = $this->factory->create(new ProfileFormType('FOS\UserBundle\Tests\TestUser'), $user);
15+
$formData = array(
16+
'username' => 'bar',
17+
'email' => 'john@doe.com',
18+
);
19+
$form->submit($formData);
20+
21+
$this->assertTrue($form->isSynchronized());
22+
$this->assertEquals($user, $form->getData());
23+
$this->assertEquals('bar', $user->getUsername());
24+
$this->assertEquals('john@doe.com', $user->getEmail());
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use FOS\UserBundle\Form\Type\RegistrationFormType;
6+
use FOS\UserBundle\Tests\TestUser;
7+
8+
class RegistrationFormTypeTest extends ValidatorExtensionTypeTestCase
9+
{
10+
public function testSubmit()
11+
{
12+
$user = new TestUser();
13+
14+
$form = $this->factory->create(new RegistrationFormType('FOS\UserBundle\Tests\TestUser'), $user);
15+
$formData = array(
16+
'username' => 'bar',
17+
'email' => 'john@doe.com',
18+
'plainPassword' => array(
19+
'first' => 'test',
20+
'second' => 'test',
21+
)
22+
);
23+
$form->submit($formData);
24+
25+
$this->assertTrue($form->isSynchronized());
26+
$this->assertEquals($user, $form->getData());
27+
$this->assertEquals('bar', $user->getUsername());
28+
$this->assertEquals('john@doe.com', $user->getEmail());
29+
$this->assertEquals('test', $user->getPlainPassword());
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use FOS\UserBundle\Form\Type\ResettingFormType;
6+
use FOS\UserBundle\Tests\TestUser;
7+
8+
class ResettingFormTypeTest extends ValidatorExtensionTypeTestCase
9+
{
10+
public function testSubmit()
11+
{
12+
$user = new TestUser();
13+
14+
$form = $this->factory->create(new ResettingFormType('FOS\UserBundle\Tests\TestUser'), $user);
15+
$formData = array(
16+
'plainPassword' => array(
17+
'first' => 'test',
18+
'second' => 'test',
19+
)
20+
);
21+
$form->submit($formData);
22+
23+
$this->assertTrue($form->isSynchronized());
24+
$this->assertEquals($user, $form->getData());
25+
$this->assertEquals('test', $user->getPlainPassword());
26+
}
27+
}

Tests/Form/Type/TypeTestCase.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use Symfony\Component\Form\FormBuilder;
6+
use Symfony\Component\Form\Forms;
7+
use Symfony\Component\Form\Test\TypeTestCase as BaseTypeTestCase;
8+
9+
/**
10+
* Class TypeTestCase
11+
*
12+
* @author Sullivan Senechal <soullivaneuh@gmail.com>
13+
*
14+
* Could be removed for using directly base class since PR: https://github.com/symfony/symfony/pull/14506
15+
*/
16+
abstract class TypeTestCase extends BaseTypeTestCase
17+
{
18+
protected function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->factory = Forms::createFormFactoryBuilder()
23+
->addExtensions($this->getExtensions())
24+
->addTypeExtensions($this->getTypeExtensions())
25+
->getFormFactory();
26+
27+
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
28+
}
29+
30+
protected function getTypeExtensions()
31+
{
32+
return array();
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests\Form\Type;
4+
5+
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
6+
use Symfony\Component\Validator\ConstraintViolationList;
7+
8+
/**
9+
* Class ValidatorExtensionTypeTestCase
10+
* FormTypeValidatorExtension added as default. Useful for form types with `constraints` option
11+
*
12+
* @author Sullivan Senechal <soullivaneuh@gmail.com>
13+
*/
14+
class ValidatorExtensionTypeTestCase extends TypeTestCase
15+
{
16+
protected function getTypeExtensions()
17+
{
18+
$validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
19+
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
20+
21+
return array(
22+
new FormTypeValidatorExtension($validator),
23+
);
24+
}
25+
}

Tests/TestGroup.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace FOS\UserBundle\Tests;
4+
5+
use FOS\UserBundle\Model\Group;
6+
7+
class TestGroup extends Group
8+
{
9+
public function setId($id)
10+
{
11+
$this->id = $id;
12+
}
13+
}

0 commit comments

Comments
 (0)