FormFactoryTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. use Symfony\Component\Form\FormFactory;
  14. use Symfony\Component\Form\FormContext;
  15. use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
  16. class FormFactoryTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $validator;
  19. protected function setUp()
  20. {
  21. $this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  22. }
  23. public function testBuildDefaultWithCsrfProtection()
  24. {
  25. $factory = FormFactory::buildDefault($this->validator, 'secret');
  26. $context = new FormContext();
  27. $context->validator($this->validator);
  28. $context->csrfProtection(true);
  29. $context->csrfProvider(new DefaultCsrfProvider('secret'));
  30. $this->assertEquals(new FormFactory($context), $factory);
  31. }
  32. public function testBuildDefaultWithoutCsrfProtection()
  33. {
  34. $factory = FormFactory::buildDefault($this->validator, null, false);
  35. $context = new FormContext();
  36. $context->validator($this->validator);
  37. $context->csrfProtection(false);
  38. $this->assertEquals(new FormFactory($context), $factory);
  39. }
  40. /**
  41. * @expectedException Symfony\Component\Form\Exception\FormException
  42. */
  43. public function testBuildDefaultWithoutCsrfSecretThrowsException()
  44. {
  45. FormFactory::buildDefault($this->validator, null, true);
  46. }
  47. }