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;
|
|
|
|
|
2012-12-14 18:27:28 +00:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
2012-12-30 15:38:36 +00:00
|
|
|
use Symfony\Component\Form\FormConfigBuilder;
|
2013-04-15 21:11:02 +01:00
|
|
|
use Symfony\Component\Form\Exception\InvalidArgumentException;
|
2012-12-14 18:27:28 +00:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
class FormConfigTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function getHtml4Ids()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('z0', true),
|
|
|
|
array('A0', true),
|
|
|
|
array('A9', true),
|
|
|
|
array('Z0', true),
|
|
|
|
array('#', false),
|
|
|
|
array('a#', false),
|
|
|
|
array('a$', false),
|
|
|
|
array('a%', false),
|
|
|
|
array('a ', false),
|
|
|
|
array("a\t", false),
|
|
|
|
array("a\n", false),
|
|
|
|
array('a-', true),
|
|
|
|
array('a_', true),
|
|
|
|
array('a:', true),
|
|
|
|
// Periods are allowed by the HTML4 spec, but disallowed by us
|
|
|
|
// because they break the generated property paths
|
|
|
|
array('a.', false),
|
|
|
|
// 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.
|
|
|
|
array('0', true),
|
|
|
|
array('9', true),
|
|
|
|
// Contrary to the HTML4 spec, we allow names starting with an
|
|
|
|
// underscore, since this is already a widely used practice in
|
|
|
|
// Symfony2.
|
|
|
|
// For root forms, leading underscores will be stripped from the
|
|
|
|
// "id" attribute to produce valid HTML4.
|
|
|
|
array('_', true),
|
2012-12-14 18:27:28 +00:00
|
|
|
// Integers are allowed
|
|
|
|
array(0, true),
|
|
|
|
array(123, true),
|
|
|
|
// NULL is allowed
|
|
|
|
array(null, true),
|
|
|
|
// Other types are not
|
|
|
|
array(1.23, false),
|
|
|
|
array(5., false),
|
|
|
|
array(true, false),
|
|
|
|
array(new \stdClass(), false),
|
2012-05-16 18:25:27 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getHtml4Ids
|
|
|
|
*/
|
2012-05-20 10:18:31 +01:00
|
|
|
public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
|
2012-05-16 18:25:27 +01:00
|
|
|
{
|
|
|
|
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
|
|
|
|
|
|
|
try {
|
2012-07-28 09:59:23 +01:00
|
|
|
new FormConfigBuilder($name, null, $dispatcher);
|
2012-05-16 18:25:27 +01:00
|
|
|
if (!$accepted) {
|
|
|
|
$this->fail(sprintf('The value "%s" should not be accepted', $name));
|
|
|
|
}
|
2012-12-14 18:27:28 +00:00
|
|
|
} catch (UnexpectedTypeException $e) {
|
|
|
|
// if the value was not accepted, but should be, rethrow exception
|
|
|
|
if ($accepted) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-04-15 21:11:02 +01:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2012-05-16 18:25:27 +01:00
|
|
|
// if the value was not accepted, but should be, rethrow exception
|
|
|
|
if ($accepted) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('GET');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPost()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('POST');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPut()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('PUT');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsDelete()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('DELETE');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetMethodAllowsPatch()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('PATCH');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-15 21:11:02 +01:00
|
|
|
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
|
2012-12-30 15:38:36 +00:00
|
|
|
*/
|
|
|
|
public function testSetMethodDoesNotAllowOtherValues()
|
|
|
|
{
|
|
|
|
$this->getConfigBuilder()->setMethod('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getConfigBuilder($name = 'name')
|
|
|
|
{
|
|
|
|
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
|
|
|
|
|
|
|
return new FormConfigBuilder($name, null, $dispatcher);
|
|
|
|
}
|
2012-05-16 18:25:27 +01:00
|
|
|
}
|