From 1cdb655b115748975b802bc57973d080a40a67b4 Mon Sep 17 00:00:00 2001 From: Saro0h Date: Fri, 26 Dec 2014 01:42:48 +0100 Subject: [PATCH] [Form] Removed depracted events PRE_BIND, BIND and POST_BIND --- .../Component/Form/Deprecated/FormEvents.php | 28 -- .../EventListener/BindRequestListener.php | 84 ------ .../Type/FormTypeHttpFoundationExtension.php | 8 - src/Symfony/Component/Form/FormEvents.php | 26 -- .../LegacyBindRequestListenerTest.php | 255 ------------------ .../Component/Form/Tests/SimpleFormTest.php | 8 +- 6 files changed, 4 insertions(+), 405 deletions(-) delete mode 100644 src/Symfony/Component/Form/Deprecated/FormEvents.php delete mode 100644 src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php delete mode 100644 src/Symfony/Component/Form/Tests/Extension/HttpFoundation/EventListener/LegacyBindRequestListenerTest.php diff --git a/src/Symfony/Component/Form/Deprecated/FormEvents.php b/src/Symfony/Component/Form/Deprecated/FormEvents.php deleted file mode 100644 index 862879e75a610..0000000000000 --- a/src/Symfony/Component/Form/Deprecated/FormEvents.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Deprecated; - -trigger_error('Constants PRE_BIND, BIND and POST_BIND on class Symfony\Component\Form\FormEvents were deprecated in Symfony 2.3 and will be removed in 3.0. Use PRE_SUBMIT, SUBMIT and POST_SUBMIT instead.', E_USER_DEPRECATED); - -/** - * @deprecated since 2.7, to be removed in 3.0. - * @internal - */ -final class FormEvents -{ - const PRE_BIND = 'form.pre_bind'; - const BIND = 'form.bind'; - const POST_BIND = 'form.post_bind'; - - private function __construct() - { - } -} diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php b/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php deleted file mode 100644 index bb144ed65ad6e..0000000000000 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Extension\HttpFoundation\EventListener; - -use Symfony\Component\Form\FormEvents; -use Symfony\Component\Form\FormEvent; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Symfony\Component\HttpFoundation\Request; - -/** - * @author Bernhard Schussek - * - * @deprecated Deprecated since version 2.3, to be removed in 3.0. Pass the - * Request instance to {@link Form::handleRequest()} instead. - */ -class BindRequestListener implements EventSubscriberInterface -{ - public static function getSubscribedEvents() - { - // High priority in order to supersede other listeners - return array(FormEvents::PRE_SUBMIT => array('preBind', 128)); - } - - public function preBind(FormEvent $event) - { - $form = $event->getForm(); - - /* @var Request $request */ - $request = $event->getData(); - - // Only proceed if we actually deal with a Request - if (!$request instanceof Request) { - return; - } - - // Uncomment this as soon as the deprecation note should be shown - // trigger_error('Passing a Request instance to Form::submit() is deprecated since version 2.3 and will be disabled in 3.0. Call Form::process($request) instead.', E_USER_DEPRECATED); - - $name = $form->getConfig()->getName(); - $default = $form->getConfig()->getCompound() ? array() : null; - - // For request methods that must not have a request body we fetch data - // from the query string. Otherwise we look for data in the request body. - switch ($request->getMethod()) { - case 'GET': - case 'HEAD': - case 'TRACE': - $data = '' === $name - ? $request->query->all() - : $request->query->get($name, $default); - - break; - - default: - if ('' === $name) { - // Form bound without name - $params = $request->request->all(); - $files = $request->files->all(); - } else { - $params = $request->request->get($name, $default); - $files = $request->files->get($name, $default); - } - - if (is_array($params) && is_array($files)) { - $data = array_replace_recursive($params, $files); - } else { - $data = $params ?: $files; - } - - break; - } - - $event->setData($data); - } -} diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php index 9cb0dc4476c59..a3bd84372880b 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/Type/FormTypeHttpFoundationExtension.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Form\Extension\HttpFoundation\Type; use Symfony\Component\Form\AbstractTypeExtension; -use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener; use Symfony\Component\Form\RequestHandlerInterface; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler; @@ -22,11 +21,6 @@ */ class FormTypeHttpFoundationExtension extends AbstractTypeExtension { - /** - * @var BindRequestListener - */ - private $listener; - /** * @var RequestHandlerInterface */ @@ -37,7 +31,6 @@ class FormTypeHttpFoundationExtension extends AbstractTypeExtension */ public function __construct(RequestHandlerInterface $requestHandler = null) { - $this->listener = new BindRequestListener(); $this->requestHandler = $requestHandler ?: new HttpFoundationRequestHandler(); } @@ -46,7 +39,6 @@ public function __construct(RequestHandlerInterface $requestHandler = null) */ public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->addEventSubscriber($this->listener); $builder->setRequestHandler($this->requestHandler); } diff --git a/src/Symfony/Component/Form/FormEvents.php b/src/Symfony/Component/Form/FormEvents.php index 317472c8a00a4..33597525f7b8a 100644 --- a/src/Symfony/Component/Form/FormEvents.php +++ b/src/Symfony/Component/Form/FormEvents.php @@ -10,8 +10,6 @@ namespace Symfony\Component\Form; -use Symfony\Component\Form\Deprecated\FormEvents as Deprecated; - /** * @author Bernhard Schussek */ @@ -73,30 +71,6 @@ final class FormEvents */ const POST_SET_DATA = 'form.post_set_data'; - /** - * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use - * {@link PRE_SUBMIT} instead. - * - * @Event - */ - const PRE_BIND = Deprecated::PRE_BIND; - - /** - * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use - * {@link SUBMIT} instead. - * - * @Event - */ - const BIND = Deprecated::BIND; - - /** - * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use - * {@link POST_SUBMIT} instead. - * - * @Event - */ - const POST_BIND = Deprecated::POST_BIND; - private function __construct() { } diff --git a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/EventListener/LegacyBindRequestListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/EventListener/LegacyBindRequestListenerTest.php deleted file mode 100644 index 183d3a7a3a975..0000000000000 --- a/src/Symfony/Component/Form/Tests/Extension/HttpFoundation/EventListener/LegacyBindRequestListenerTest.php +++ /dev/null @@ -1,255 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Form\Tests\Extension\HttpFoundation\EventListener; - -use Symfony\Component\Form\Extension\HttpFoundation\EventListener\BindRequestListener; -use Symfony\Component\Form\Form; -use Symfony\Component\Form\FormConfigBuilder; -use Symfony\Component\Form\FormEvent; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\File\UploadedFile; - -/** - * @author Bernhard Schussek - */ -class LegacyBindRequestListenerTest extends \PHPUnit_Framework_TestCase -{ - private $values; - - private $filesPlain; - - private $filesNested; - - /** - * @var UploadedFile - */ - private $uploadedFile; - - protected function setUp() - { - $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); - - $path = tempnam(sys_get_temp_dir(), 'sf2'); - touch($path); - - $this->values = array( - 'name' => 'Bernhard', - 'image' => array('filename' => 'foobar.png'), - ); - - $this->filesPlain = array( - 'image' => array( - 'error' => UPLOAD_ERR_OK, - 'name' => 'upload.png', - 'size' => 123, - 'tmp_name' => $path, - 'type' => 'image/png', - ), - ); - - $this->filesNested = array( - 'error' => array('image' => UPLOAD_ERR_OK), - 'name' => array('image' => 'upload.png'), - 'size' => array('image' => 123), - 'tmp_name' => array('image' => $path), - 'type' => array('image' => 'image/png'), - ); - - $this->uploadedFile = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK); - } - - protected function tearDown() - { - unlink($this->uploadedFile->getRealPath()); - } - - public function requestMethodProvider() - { - return array( - array('POST'), - array('PUT'), - array('DELETE'), - array('PATCH'), - ); - } - - /** - * @dataProvider requestMethodProvider - */ - public function testSubmitRequest($method) - { - $values = array('author' => $this->values); - $files = array('author' => $this->filesNested); - $request = new Request(array(), $values, array(), array(), $files, array( - 'REQUEST_METHOD' => $method, - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertEquals(array( - 'name' => 'Bernhard', - 'image' => $this->uploadedFile, - ), $event->getData()); - } - - /** - * @dataProvider requestMethodProvider - */ - public function testSubmitRequestWithEmptyName($method) - { - $request = new Request(array(), $this->values, array(), array(), $this->filesPlain, array( - 'REQUEST_METHOD' => $method, - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('', null, $dispatcher); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertEquals(array( - 'name' => 'Bernhard', - 'image' => $this->uploadedFile, - ), $event->getData()); - } - - /** - * @dataProvider requestMethodProvider - */ - public function testSubmitEmptyRequestToCompoundForm($method) - { - $request = new Request(array(), array(), array(), array(), array(), array( - 'REQUEST_METHOD' => $method, - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $config->setCompound(true); - $config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface')); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - // Default to empty array - $this->assertEquals(array(), $event->getData()); - } - - /** - * @dataProvider requestMethodProvider - */ - public function testSubmitEmptyRequestToSimpleForm($method) - { - $request = new Request(array(), array(), array(), array(), array(), array( - 'REQUEST_METHOD' => $method, - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $config->setCompound(false); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - // Default to null - $this->assertNull($event->getData()); - } - - public function testSubmitGetRequest() - { - $values = array('author' => $this->values); - $request = new Request($values, array(), array(), array(), array(), array( - 'REQUEST_METHOD' => 'GET', - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertEquals(array( - 'name' => 'Bernhard', - 'image' => array('filename' => 'foobar.png'), - ), $event->getData()); - } - - public function testSubmitGetRequestWithEmptyName() - { - $request = new Request($this->values, array(), array(), array(), array(), array( - 'REQUEST_METHOD' => 'GET', - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('', null, $dispatcher); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertEquals(array( - 'name' => 'Bernhard', - 'image' => array('filename' => 'foobar.png'), - ), $event->getData()); - } - - public function testSubmitEmptyGetRequestToCompoundForm() - { - $request = new Request(array(), array(), array(), array(), array(), array( - 'REQUEST_METHOD' => 'GET', - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $config->setCompound(true); - $config->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface')); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertEquals(array(), $event->getData()); - } - - public function testSubmitEmptyGetRequestToSimpleForm() - { - $request = new Request(array(), array(), array(), array(), array(), array( - 'REQUEST_METHOD' => 'GET', - )); - - $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $config = new FormConfigBuilder('author', null, $dispatcher); - $config->setCompound(false); - $form = new Form($config); - $event = new FormEvent($form, $request); - - $listener = new BindRequestListener(); - $listener->preBind($event); - - $this->assertNull($event->getData()); - } -} diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index 69e75c94ca263..367f115304e62 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -122,19 +122,19 @@ public function testDataIsInitializedFromSubmit() public function testFalseIsConvertedToNull() { $mock = $this->getMockBuilder('\stdClass') - ->setMethods(array('preBind')) + ->setMethods(array('preSubmit')) ->getMock(); $mock->expects($this->once()) - ->method('preBind') + ->method('preSubmit') ->with($this->callback(function ($event) { return null === $event->getData(); })); $config = new FormConfigBuilder('name', null, $this->dispatcher); - $config->addEventListener(FormEvents::PRE_SUBMIT, array($mock, 'preBind')); + $config->addEventListener(FormEvents::PRE_SUBMIT, array($mock, 'preSubmit')); $form = new Form($config); - $form->submit(false); + $form->bind(false); $this->assertTrue($form->isValid()); $this->assertNull($form->getData());