2012-05-16 18:25:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Tests;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2012-12-30 15:38:36 +00:00
|
|
|
use Symfony\Component\Form\FormConfigBuilder;
|
2012-12-14 18:27:28 +00:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2017-02-08 07:24:27 +00:00
|
|
|
class FormConfigTest extends TestCase
|
2012-05-16 18:25:27 +01:00
|
|
|
{
|
|
|
|
public function getHtml4Ids()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
|
|
|
['z0'],
|
|
|
|
['A0'],
|
|
|
|
['A9'],
|
|
|
|
['Z0'],
|
|
|
|
['#', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
['a#', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
['a$', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
['a%', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
['a ', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
["a\t", 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
["a\n", 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
|
|
|
['a-'],
|
|
|
|
['a_'],
|
|
|
|
['a:'],
|
2012-05-16 18:25:27 +01:00
|
|
|
// Periods are allowed by the HTML4 spec, but disallowed by us
|
|
|
|
// because they break the generated property paths
|
2019-01-16 09:39:14 +00:00
|
|
|
['a.', 'Symfony\Component\Form\Exception\InvalidArgumentException'],
|
2012-05-16 18:25:27 +01:00
|
|
|
// Contrary to the HTML4 spec, we allow names starting with a
|
|
|
|
// number, otherwise naming fields by collection indices is not
|
|
|
|
// possible.
|
|
|
|
// For root forms, leading digits will be stripped from the
|
|
|
|
// "id" attribute to produce valid HTML4.
|
2019-01-16 09:39:14 +00:00
|
|
|
['0'],
|
|
|
|
['9'],
|
2012-05-16 18:25:27 +01:00
|
|
|
// Contrary to the HTML4 spec, we allow names starting with an
|
|
|
|
// underscore, since this is already a widely used practice in
|
2014-11-24 12:25:40 +00:00
|
|
|
// Symfony.
|
2012-05-16 18:25:27 +01:00
|
|
|
// For root forms, leading underscores will be stripped from the
|
|
|
|
// "id" attribute to produce valid HTML4.
|
2019-01-16 09:39:14 +00:00
|
|
|
['_'],
|
2012-12-14 18:27:28 +00:00
|
|
|
// Integers are allowed
|
2019-01-16 09:39:14 +00:00
|
|
|
[0],
|
|
|
|
[123],
|
2012-12-14 18:27:28 +00:00
|
|
|
// NULL is allowed
|
2019-01-16 09:39:14 +00:00
|
|
|
[null],
|
|
|
|
];
|
2012-05-16 18:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getHtml4Ids
|
|
|
|
*/
|
2017-03-18 09:10:35 +00:00
|
|
|
public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedException = null)
|
2012-05-16 18:25:27 +01:00
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
2012-05-16 18:25:27 +01:00
|
|
|
|
2019-08-01 15:27:42 +01:00
|
|
|
if (null !== $expectedException) {
|
2017-03-18 09:10:35 +00:00
|
|
|
$this->expectException($expectedException);
|
2012-05-16 18:25:27 +01:00
|
|
|
}
|
2017-03-18 09:10:35 +00:00
|
|
|
|
|
|
|
$formConfigBuilder = new FormConfigBuilder($name, null, $dispatcher);
|
|
|
|
|
|
|
|
$this->assertSame((string) $name, $formConfigBuilder->getName());
|
2012-05-16 18:25:27 +01:00
|
|
|
}
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$config = $this->getConfigBuilder()->getFormConfig();
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->assertInstanceOf('Symfony\Component\Form\NativeRequestHandler', $config->getRequestHandler());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
public function testGetRequestHandlerReusesNativeRequestHandlerInstance()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$config1 = $this->getConfigBuilder()->getFormConfig();
|
|
|
|
$config2 = $this->getConfigBuilder()->getFormConfig();
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->assertSame($config1->getRequestHandler(), $config2->getRequestHandler());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsGet()
|
|
|
|
{
|
2017-03-18 09:10:35 +00:00
|
|
|
$formConfigBuilder = $this->getConfigBuilder();
|
|
|
|
$formConfigBuilder->setMethod('GET');
|
|
|
|
|
|
|
|
self::assertSame('GET', $formConfigBuilder->getMethod());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPost()
|
|
|
|
{
|
2017-03-18 09:10:35 +00:00
|
|
|
$formConfigBuilder = $this->getConfigBuilder();
|
|
|
|
$formConfigBuilder->setMethod('POST');
|
|
|
|
|
|
|
|
self::assertSame('POST', $formConfigBuilder->getMethod());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPut()
|
|
|
|
{
|
2017-03-18 09:10:35 +00:00
|
|
|
$formConfigBuilder = $this->getConfigBuilder();
|
|
|
|
$formConfigBuilder->setMethod('PUT');
|
|
|
|
|
|
|
|
self::assertSame('PUT', $formConfigBuilder->getMethod());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsDelete()
|
|
|
|
{
|
2017-03-18 09:10:35 +00:00
|
|
|
$formConfigBuilder = $this->getConfigBuilder();
|
|
|
|
$formConfigBuilder->setMethod('DELETE');
|
|
|
|
|
|
|
|
self::assertSame('DELETE', $formConfigBuilder->getMethod());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPatch()
|
|
|
|
{
|
2017-03-18 09:10:35 +00:00
|
|
|
$formConfigBuilder = $this->getConfigBuilder();
|
|
|
|
$formConfigBuilder->setMethod('PATCH');
|
|
|
|
|
|
|
|
self::assertSame('PATCH', $formConfigBuilder->getMethod());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getConfigBuilder($name = 'name')
|
|
|
|
{
|
2016-12-19 09:02:29 +00:00
|
|
|
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
return new FormConfigBuilder($name, null, $dispatcher);
|
|
|
|
}
|
2012-05-16 18:25:27 +01:00
|
|
|
}
|