[Form] Improved regular expression in AbstractType::getName() and added unit tests

This commit is contained in:
Bernhard Schussek 2011-04-17 17:58:37 +02:00
parent 4905bfd285
commit be5738564f
2 changed files with 55 additions and 4 deletions

View File

@ -47,10 +47,8 @@ abstract class AbstractType implements FormTypeInterface
public function getName()
{
if (preg_match('/\\\\([a-z]+)(?:Form|Type)$/im', get_class($this), $matches)) {
$name = strtolower($matches[1]);
}
preg_match('/\\\\(\w+?)(Form)?(Type)?$/i', get_class($this), $matches);
return $name;
return strtolower($matches[1]);
}
}

View File

@ -0,0 +1,53 @@
<?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\Tests\Component\Form\Type;
use Symfony\Component\Form\Type\AbstractType;
class AbstractTypeTest extends TestCase
{
public function testGetNameWithNoSuffix()
{
$type = new MyTest();
$this->assertEquals('mytest', $type->getName());
}
public function testGetNameWithTypeSuffix()
{
$type = new MyTestType();
$this->assertEquals('mytest', $type->getName());
}
public function testGetNameWithFormSuffix()
{
$type = new MyTestForm();
$this->assertEquals('mytest', $type->getName());
}
public function testGetNameWithFormTypeSuffix()
{
$type = new MyTestFormType();
$this->assertEquals('mytest', $type->getName());
}
}
class MyTest extends AbstractType {}
class MyTestType extends AbstractType {}
class MyTestForm extends AbstractType {}
class MyTestFormType extends AbstractType {}