4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Csrf\EventListener
;
14 use Symfony\Component\Form\FormBuilder
;
15 use Symfony\Component\Form\FormEvent
;
16 use Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener
;
18 class CsrfValidationListenerTest
extends \PHPUnit_Framework_TestCase
20 protected $dispatcher;
22 protected $csrfProvider;
24 protected function setUp()
26 if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
27 $this->markTestSkipped('The "EventDispatcher" component is not available');
30 $this->dispatcher
= $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
31 $this->factory
= $this->getMock('Symfony\Component\Form\FormFactoryInterface');
32 $this->csrfProvider
= $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
33 $this->form
= $this->getBuilder('post')
34 ->setDataMapper($this->getDataMapper())
38 protected function tearDown()
40 $this->dispatcher
= null;
41 $this->factory
= null;
42 $this->csrfProvider
= null;
46 protected function getBuilder($name = 'name')
48 return new FormBuilder($name, null, $this->dispatcher
, $this->factory
, array('compound' => true));
51 protected function getForm($name = 'name')
53 return $this->getBuilder($name)->getForm();
56 protected function getDataMapper()
58 return $this->getMock('Symfony\Component\Form\DataMapperInterface');
61 protected function getMockForm()
63 return $this->getMock('Symfony\Component\Form\Test\FormInterface');
66 // https://github.com/symfony/symfony/pull/5838
67 public function testStringFormData()
70 $event = new FormEvent($this->form
, $data);
72 $validation = new CsrfValidationListener('csrf', $this->csrfProvider
, 'unknown', 'Invalid.');
73 $validation->preSubmit($event);
75 // Validate accordingly
76 $this->assertSame($data, $event->getData());