This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/FormContextTest.php
Bernhard Schussek a28151a8af [Form] Removed FormFactory and improved the form instantiation process
With the form factory there was no reasonable way to implement instantiation of custom form classes. So the implementation was changed to let the classes instantiate themselves. A FormContext instance with default settings has to be passed to the creation method. This context is by default configured in the DI container.

	$context = $this->get('form.context');
	// or
	$context = FormContext::buildDefault();
	$form = MyFormClass::create($context, 'author');

If you want to circumvent this process, you can also create a form manually. Remember that the services stored in the default context won't be available then unless you pass them explicitely.

	$form = new MyFormClass('author');
2011-02-01 15:27:12 +01:00

62 lines
1.6 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form;
require_once __DIR__ . '/Fixtures/Author.php';
require_once __DIR__ . '/Fixtures/TestField.php';
use Symfony\Component\Form\FormContext;
use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
class FormContextTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected function setUp()
{
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
}
public function testBuildDefaultWithCsrfProtection()
{
$context = FormContext::buildDefault($this->validator, 'secret');
$expected = array(
'validator' => $this->validator,
'csrf_provider' => new DefaultCsrfProvider('secret'),
'context' => $context,
);
$this->assertEquals($expected, $context->getOptions());
}
public function testBuildDefaultWithoutCsrfProtection()
{
$context = FormContext::buildDefault($this->validator, null, false);
$expected = array(
'validator' => $this->validator,
'context' => $context,
);
$this->assertEquals($expected, $context->getOptions());
}
/**
* @expectedException Symfony\Component\Form\Exception\FormException
*/
public function testBuildDefaultWithoutCsrfSecretThrowsException()
{
FormContext::buildDefault($this->validator, null, true);
}
}