This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/Tests/FormConfigTest.php

156 lines
5.5 KiB
PHP
Raw Normal View History

<?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;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
2017-02-08 07:24:27 +00:00
class FormConfigTest extends TestCase
{
public function getHtml4Ids()
{
return array(
2017-03-18 09:10:35 +00:00
array('z0'),
array('A0'),
array('A9'),
array('Z0'),
array('#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array('a#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array('a$', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array('a%', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array('a ', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array("a\t", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array("a\n", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
array('a-'),
array('a_'),
array('a:'),
// Periods are allowed by the HTML4 spec, but disallowed by us
// because they break the generated property paths
2017-03-18 09:10:35 +00:00
array('a.', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
// 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.
2017-03-18 09:10:35 +00:00
array('0'),
array('9'),
// 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.
// For root forms, leading underscores will be stripped from the
// "id" attribute to produce valid HTML4.
2017-03-18 09:10:35 +00:00
array('_'),
// Integers are allowed
2017-03-18 09:10:35 +00:00
array(0),
array(123),
// NULL is allowed
2017-03-18 09:10:35 +00:00
array(null),
// Other types are not
2017-03-18 09:10:35 +00:00
array(1.23, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
array(5., 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
array(true, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
array(new \stdClass(), 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
);
}
/**
* @dataProvider getHtml4Ids
*/
2017-03-18 09:10:35 +00:00
public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedException = null)
{
2016-12-19 09:02:29 +00:00
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
2017-03-18 09:10:35 +00:00
if (null !== $expectedException && method_exists($this, 'expectException')) {
$this->expectException($expectedException);
} elseif (null !== $expectedException) {
$this->setExpectedException($expectedException);
}
2017-03-18 09:10:35 +00:00
$formConfigBuilder = new FormConfigBuilder($name, null, $dispatcher);
$this->assertSame((string) $name, $formConfigBuilder->getName());
}
2012-12-30 15:38:36 +00:00
public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
2012-12-30 15:38:36 +00:00
{
$config = $this->getConfigBuilder()->getFormConfig();
$this->assertInstanceOf('Symfony\Component\Form\NativeRequestHandler', $config->getRequestHandler());
2012-12-30 15:38:36 +00:00
}
public function testGetRequestHandlerReusesNativeRequestHandlerInstance()
2012-12-30 15:38:36 +00:00
{
$config1 = $this->getConfigBuilder()->getFormConfig();
$config2 = $this->getConfigBuilder()->getFormConfig();
$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
}
/**
* @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')
{
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);
}
}