瀏覽代碼

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

Bernhard Schussek 14 年之前
父節點
當前提交
be5738564f

+ 2 - 4
src/Symfony/Component/Form/Type/AbstractType.php

@@ -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]);
     }
 }

+ 53 - 0
tests/Symfony/Tests/Component/Form/Type/AbstractTypeTest.php

@@ -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 {}