Переглянути джерело

[ZF-10158] Zend_Controller_Action_Helper_Abstract#getName does not work with namespaces since get_class returns the fully qualified class name

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23366 44c647ce-9c0f-0410-b52a-842ac1e357ba
jan 15 роки тому
батько
коміт
d10d1ea85f

+ 8 - 6
library/Zend/Controller/Action/Helper/Abstract.php

@@ -142,13 +142,15 @@ abstract class Zend_Controller_Action_Helper_Abstract
      */
     public function getName()
     {
-        $full_class_name = get_class($this);
-
-        if (strpos($full_class_name, '_') !== false) {
-            $helper_name = strrchr($full_class_name, '_');
-            return ltrim($helper_name, '_');
+        $fullClassName = get_class($this);
+        if (strpos($fullClassName, '_') !== false) {
+            $helperName = strrchr($fullClassName, '_');
+            return ltrim($helperName, '_');
+        } elseif (strpos($fullClassName, '\\') !== false) {
+            $helperName = strrchr($fullClassName, '\\');
+            return ltrim($helperName, '\\');
         } else {
-            return $full_class_name;
+            return $fullClassName;
         }
     }
 }

+ 5 - 0
tests/Zend/Controller/Action/Helper/AllTests.php

@@ -69,6 +69,11 @@ class Zend_Controller_Action_Helper_AllTests
         $suite->addTestSuite('Zend_Controller_Action_Helper_UrlTest');
         $suite->addTestSuite('Zend_Controller_Action_Helper_ViewRendererTest');
 
+        if (version_compare(PHP_VERSION, '5.3', '>=')) {
+            require_once 'Zend/Controller/Action/Helper/NamespaceTest.php';
+            $suite->addTestSuite('Zend_Controller_Action_Helper_NamespaceTest');
+        }
+
         return $suite;
     }
 }

+ 72 - 0
tests/Zend/Controller/Action/Helper/NamespaceTest.php

@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Controller
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id:$
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    require_once dirname(__FILE__) . '/../../../../TestHelper.php';
+    define('PHPUnit_MAIN_METHOD', 'Zend_Controller_Action_Helper_NamespaceTest::main');
+}
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'PHPUnit/Framework/TestSuite.php';
+
+/**
+ * Test class for Zend_Controller_Action_Helper_Abstract.
+ *
+ * @category   Zend
+ * @package    Zend_Controller
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Controller
+ * @group      Zend_Controller_Action
+ * @group      Zend_Controller_Action_Helper
+ */
+class Zend_Controller_Action_Helper_NamespaceTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Runs the test methods of this class.
+     *
+     * @return void
+     */
+    public static function main()
+    {
+        require_once 'PHPUnit/TextUI/TestRunner.php';
+        $suite  = new PHPUnit_Framework_TestSuite('Zend_Controller_Action_Helper_NamespaceTest');
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+    
+    /**
+     * @group ZF-10158
+     */
+    public function testGetNameWithNamespace()
+    {
+        require_once dirname(__FILE__) . '/../../_files/Helpers/NamespacedHelper.php';
+        $helper = new MyApp\Controller\Action\Helper\NamespacedHelper;
+        $this->assertEquals('NamespacedHelper', $helper->getName());
+    }
+}
+
+// Call Zend_Controller_Action_Helper_NamespaceTest::main() if this source file is executed directly.
+if (PHPUnit_MAIN_METHOD == 'Zend_Controller_Action_Helper_NamespaceTest::main') {
+    Zend_Controller_Action_Helper_NamespaceTest::main();
+}

+ 5 - 0
tests/Zend/Controller/_files/Helpers/NamespacedHelper.php

@@ -0,0 +1,5 @@
+<?php
+
+namespace MyApp\Controller\Action\Helper;
+require_once 'Zend/Controller/Action/Helper/Abstract.php';
+class NamespacedHelper extends \Zend_Controller_Action_Helper_Abstract {}