2
0
فهرست منبع

ZF-7361 - Fixed hasMethod($methodName) and hasProperty($propertyName) on Zend_CodeGenerator_Php_Class, aswell as another bug that did not detect duplicate methods or properties correctly.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@17993 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 سال پیش
والد
کامیت
a29fb91b58
2فایلهای تغییر یافته به همراه73 افزوده شده و 20 حذف شده
  1. 2 2
      library/Zend/CodeGenerator/Php/Class.php
  2. 71 18
      tests/Zend/CodeGenerator/Php/ClassTest.php

+ 2 - 2
library/Zend/CodeGenerator/Php/Class.php

@@ -296,7 +296,7 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
             throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
         }
 
-        $this->_properties->append($property);
+        $this->_properties[$propertyName] = $property;
         return $this;
     }
 
@@ -374,7 +374,7 @@ class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
             throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
         }
 
-        $this->_methods->append($method);
+        $this->_methods[$methodName] = $method;
         return $this;
     }
 

+ 71 - 18
tests/Zend/CodeGenerator/Php/ClassTest.php

@@ -104,16 +104,24 @@ class Zend_CodeGenerator_Php_ClassTest extends PHPUnit_Framework_TestCase
         // add a new property
         $codeGenClass->setProperty(array('name' => 'prop3'));
         $this->assertEquals(count($codeGenClass->getProperties()), 3);
-        
-        try {
-            // add a property by a same name
-            $codeGenClass->setProperty(array('name' => 'prop3'));
-            $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
-        } catch (Exception $e) {
-            $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
-        }
-        
-        
+    }
+
+    public function testSetProperty_AlreadyExists_ThrowsException()
+    {
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setProperty(array('name' => 'prop3'));
+
+        $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
+
+        $codeGenClass->setProperty(array('name' => 'prop3'));
+    }
+
+    public function testSetProperty_NoArrayOrProperty_ThrowsException()
+    {
+        $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setProperty("propertyName");
     }
     
     public function testMethodAccessors()
@@ -135,14 +143,59 @@ class Zend_CodeGenerator_Php_ClassTest extends PHPUnit_Framework_TestCase
         // add a new property
         $codeGenClass->setMethod(array('name' => 'methodThree'));
         $this->assertEquals(count($codeGenClass->getMethods()), 3);
-        
-        try {
-            // add a property by a same name
-            $codeGenClass->setMethod(array('name' => 'methodThree'));
-            $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
-        } catch (Exception $e) {
-            $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
-        }
+    }
+
+    public function testSetMethod_NoMethodOrArray_ThrowsException()
+    {
+        $this->setExpectedException("Zend_CodeGenerator_Php_Exception",
+            'setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method'
+        );
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setMethod("aMethodName");
+    }
+
+    public function testSetMethod_NameAlreadyExists_ThrowsException()
+    {
+        $methodA = new Zend_CodeGenerator_Php_Method();
+        $methodA->setName("foo");
+        $methodB = new Zend_CodeGenerator_Php_Method();
+        $methodB->setName("foo");
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setMethod($methodA);
+
+        $this->setExpectedException("Zend_CodeGenerator_Php_Exception", 'A method by name foo already exists in this class.');
+
+        $codeGenClass->setMethod($methodB);
+    }
+
+    /**
+     * @group ZF-7361
+     */
+    public function testHasMethod()
+    {
+        $method = new Zend_CodeGenerator_Php_Method();
+        $method->setName('methodOne');
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setMethod($method);
+
+        $this->assertTrue($codeGenClass->hasMethod('methodOne'));
+    }
+
+    /**
+     * @group ZF-7361
+     */
+    public function testHasProperty()
+    {
+        $property = new Zend_CodeGenerator_Php_Property();
+        $property->setName('propertyOne');
+
+        $codeGenClass = new Zend_CodeGenerator_Php_Class();
+        $codeGenClass->setProperty($property);
+
+        $this->assertTrue($codeGenClass->hasProperty('propertyOne'));
     }
     
     public function testToString()