Author: smarr
Date: Sat Dec 29 18:46:17 2007
New Revision: 7041

Log:
- added missing implementations
- added test cases
- still missing implementations

Added:
    experimental/Reflection/tests/test_classes/BaseClass.php
    experimental/Reflection/tests/test_classes/SomeClass.php
Modified:
    experimental/Reflection/src/class.php
    experimental/Reflection/src/method.php
    experimental/Reflection/src/parameter.php
    experimental/Reflection/src/property.php
    experimental/Reflection/src/type_factory.php
    experimental/Reflection/tests/class_external_test.php
    experimental/Reflection/tests/class_test.php
    experimental/Reflection/tests/parameter_test.php
    experimental/Reflection/tests/property_test.php
    experimental/Reflection/tests/suite.php
    experimental/Reflection/tests/test_classes/functions.php
    experimental/Reflection/tests/type_factory_test.php

Modified: experimental/Reflection/src/class.php
==============================================================================
--- experimental/Reflection/src/class.php [iso-8859-1] (original)
+++ experimental/Reflection/src/class.php [iso-8859-1] Sat Dec 29 18:46:17 2007
@@ -190,14 +190,12 @@
             $prop = parent::getProperty($name);
         }
         
-               if (is_object($prop)) {
+               if (is_object($prop) && !($prop instanceof 
ezcReflectionProperty)) {
                        return new ezcReflectionProperty($prop, $name);
-        }
-        else {
+        } else {
                        // TODO: may be we should throw an exception here
-            return null;
-        }
-        
+            return $prop;
+        }
     }
 
     /**

Modified: experimental/Reflection/src/method.php
==============================================================================
--- experimental/Reflection/src/method.php [iso-8859-1] (original)
+++ experimental/Reflection/src/method.php [iso-8859-1] Sat Dec 29 18:46:17 2007
@@ -37,19 +37,20 @@
     * @param string $name
     */
     public function __construct($class, $name) {
-        parent::__construct($class, $name);
-        $this->docParser = ezcReflectionApi::getDocParserInstance();
-        $this->docParser->parse($this->getDocComment());
-        
-        if ($class instanceof ReflectionClass) {
+               if ($class instanceof ReflectionClass) {
+                       parent::__construct($class->getName(), $name);
             $this->curClass = $class;
         }
         elseif (is_string($class)) {
+                       parent::__construct($class, $name);
             $this->curClass = new ReflectionClass($class);
         }
         else {
             $this->curClass = null;
         }
+               
+               $this->docParser = ezcReflectionApi::getDocParserInstance();
+        $this->docParser->parse($this->getDocComment());
     }
 
     /**

Modified: experimental/Reflection/src/parameter.php
==============================================================================
--- experimental/Reflection/src/parameter.php [iso-8859-1] (original)
+++ experimental/Reflection/src/parameter.php [iso-8859-1] Sat Dec 29 18:46:17 
2007
@@ -85,6 +85,18 @@
             return parent::isPassedByReference();
         }
     }
+       
+       /**
+    * @return bool
+    */
+    public function isArray() {
+        if ($this->parameter != null) {
+            return $this->parameter->isArray();
+        }
+        else {
+            return parent::isArray();
+        }
+    }
 
     /**
     * @return bool
@@ -107,6 +119,18 @@
         }
         else {
             return parent::getName();
+        }
+    }
+       
+       /**
+    * @return int
+    */
+    public function getPosition() {
+        if ($this->parameter != null) {
+            return $this->parameter->getPosition();
+        }
+        else {
+            return parent::getPosition();
         }
     }
 

Modified: experimental/Reflection/src/property.php
==============================================================================
--- experimental/Reflection/src/property.php [iso-8859-1] (original)
+++ experimental/Reflection/src/property.php [iso-8859-1] Sat Dec 29 18:46:17 
2007
@@ -17,22 +17,29 @@
  * @author Stefan Marr <[EMAIL PROTECTED]>
  */
 class ezcReflectionProperty extends ReflectionProperty {
-    /**
+    
+       /**
     * @var ezcReflectionDocParser
     */
     protected $docParser = null;
+       
+       /**
+       * @var ReflectionProperty
+       */
+       protected $reflectionSource = null;
 
     /**
     * @param mixed $class
     * @param string $name
     */
     public function __construct($class, $name) {
-        parent::__construct($class, $name);
+               if (!$class instanceof ReflectionProperty) {
+                       parent::__construct($class, $name);
+               }
+               $this->reflectionSource = $class;
 
-        if (method_exists($this, 'getDocComment')) {
-            $this->docParser = ezcReflectionApi::getDocParserInstance();
-               $this->docParser->parse($this->getDocComment());
-        }
+        $this->docParser = ezcReflectionApi::getDocParserInstance();
+               $this->docParser->parse($this->getDocComment());
     }
 
     /**
@@ -57,8 +64,136 @@
     * @return ezcReflectionClassType
     */
     public function getDeclaringClass() {
-        $class = parent::getDeclaringClass();
-        return new ezcReflectionClassType($class->getName());
+               if ( $this->reflectionSource instanceof ReflectionProperty ) {
+                       return new 
ezcReflectionClassType($this->reflectionSource->getDeclaringClass());
+               } else {
+                       $class = parent::getDeclaringClass();
+                       return new ezcReflectionClassType($class->getName());
+               }
     }
+       
+       /**
+     * Returns the doc comment for the class.
+     *
+     * @return string doc comment
+     */
+    public function getDocComment() {
+        if ( $this->reflectionSource instanceof ReflectionProperty )
+        {
+            // query external reflection object
+            $comment = $this->reflectionSource->getDocComment();
+        } else {
+            $comment = parent::getDocComment();
+        }
+        return $comment;
+    }
+       
+       /**
+     * Returns the name of the property.
+     * @return string property name
+     */
+    public function getName() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            $name = $this->reflectionSource->getName();
+        } else {
+            $name = parent::getName();
+        }
+        return $name;
+    }
+       
+       /**
+     * Returns true if this property has public as access level.
+     * @return bool
+     */
+    public function isPublic() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->isPublic();
+        } else {
+            return parent::isPublic();
+        }
+    }
+       
+       /**
+     * Returns true if this property has protected as access level.
+     * @return bool
+     */
+    public function isProtected() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->isProtected();
+        } else {
+            return parent::isProtected();
+        }
+    }
+       
+       /**
+     * Returns true if this property has private as access level.
+     * @return bool
+     */
+    public function isPrivate() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->isPrivate();
+        } else {
+            return parent::isPrivate();
+        }
+    }
+       
+       /**
+     * Returns true if this property has is a static property.
+     * @return bool
+     */
+    public function isStatic() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->isStatic();
+        } else {
+            return parent::isStatic();
+        }
+    }
+       
+       /**
+        * A default property is defined in the class definition.
+        * A non-default property is an instance specific state.
+     * @return bool
+     */
+    public function isDefault() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->isDefault();
+        } else {
+            return parent::isDefault();
+        }
+    }
+       
+       /**
+     * @return int
+     */
+    public function getModifiers() {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->getModifiers();
+        } else {
+            return parent::getModifiers();
+        }
+    }
+       
+       /**
+     * @return mixed
+     */
+    public function getValue($object) {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            return $this->reflectionSource->getValue($object);
+        } else {
+            return parent::getValue($object);
+        }
+    }
+       
+       /**
+        * @param mixed $value
+     */
+    public function setValue($object, $value) {
+        if ( $this->reflectionSource instanceof ReflectionProperty ) {
+            $this->reflectionSource->setValue($object, $value);
+        } else {
+            parent::setValue($object, $value);
+        }
+    }
+       
 }
 ?>

Modified: experimental/Reflection/src/type_factory.php
==============================================================================
--- experimental/Reflection/src/type_factory.php [iso-8859-1] (original)
+++ experimental/Reflection/src/type_factory.php [iso-8859-1] Sat Dec 29 
18:46:17 2007
@@ -44,7 +44,7 @@
             return new ezcReflectionArrayType($typeName);
         }
         //else it has to be a user class
-        else {
+               else {
             return new ezcReflectionClassType($typeName);
         }
     }

Modified: experimental/Reflection/tests/class_external_test.php
==============================================================================
--- experimental/Reflection/tests/class_external_test.php [iso-8859-1] 
(original)
+++ experimental/Reflection/tests/class_external_test.php [iso-8859-1] Sat Dec 
29 18:46:17 2007
@@ -17,7 +17,7 @@
 
     public function setUp()
     {
-        $this->class = new ezcReflectionClass( new ReflectionClass( 
'ezcReflectionClass' ) );
+        $this->class = new ezcReflectionClass( new MyReflectionClass( 
'SomeClass' ) );
     }
 
     public function tearDown()

Modified: experimental/Reflection/tests/class_test.php
==============================================================================
--- experimental/Reflection/tests/class_test.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/class_test.php [iso-8859-1] Sat Dec 29 
18:46:17 2007
@@ -16,7 +16,7 @@
     protected $class;
 
     public function setUp() {
-        $this->class = new ezcReflectionClass('ezcReflectionClass');
+        $this->class = new ezcReflectionClass('SomeClass');
     }
 
     public function tearDown() {
@@ -24,7 +24,7 @@
     }
 
     public function testGetName() {
-        self::assertEquals($this->class->getName(), 'ezcReflectionClass');
+        self::assertEquals('SomeClass', $this->class->getName());
     }
        
        public function testCall() {
@@ -33,11 +33,10 @@
        }
 
     public function testGetMethod() {
-        $method = $this->class->getMethod('getMethod');
+        $method = $this->class->getMethod('helloWorld');
         self::assertType('ezcReflectionMethod', $method);
-        self::assertEquals($method->getName(), 'getMethod');
+        self::assertEquals($method->getName(), 'helloWorld');
     }
-
 
     public function testGetConstructor() {
         $method = $this->class->getConstructor();
@@ -67,18 +66,18 @@
     public function testGetParentClass() {
         $parent = $this->class->getParentClass();
 
-        self::assertType('ReflectionClass', $parent);
-        self::assertEquals($parent->getName(), 'ReflectionClass');
+        self::assertType('ezcReflectionClass', $parent);
+        self::assertEquals('BaseClass', $parent->getName());
 
         $parentParent = $parent->getParentClass();
         self::assertNull($parentParent);
     }
 
     public function testGetProperty() {
-        $prop = $this->class->getProperty('docParser');
+        $prop = $this->class->getProperty('fields');
 
         self::assertType('ezcReflectionProperty', $prop);
-        self::assertEquals('docParser', $prop->getName());
+        self::assertEquals('fields', $prop->getName());
 
         try {
             $prop = $this->class->getProperty('none-existing-property');
@@ -138,7 +137,7 @@
     public function testGetTags() {
         $tags = $this->class->getTags();
 
-        $expectedTags = array('package', 'version', 'author', 'author');
+        $expectedTags = array('licence', 'donotdocument', 'testclass', 
'ignore');
         ReflectionTestHelper::expectedTags($expectedTags, $tags, $this);
 
         $expectedTags = array('webservice', 'foobar');

Modified: experimental/Reflection/tests/parameter_test.php
==============================================================================
--- experimental/Reflection/tests/parameter_test.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/parameter_test.php [iso-8859-1] Sat Dec 29 
18:46:17 2007
@@ -51,21 +51,95 @@
     public function testGetDeclaringFunction() {
         $func = new ezcReflectionFunction('m1');
         $params = $func->getParameters();
-        //$decFunc = $params[0]->getDeclaringFunction();
-
-        //TODO: implement, why is this function is missing on win32 5.1.5??
-        self::markTestSkipped();
+        
+               $decFunc = $params[0]->getDeclaringFunction();
+               self::assertTrue($decFunc instanceof ezcReflectionFunction);
+        self::assertEquals('m1', $decFunc->getName());
     }
 
     public function testGetDeclaringClass() {
         $method = new ezcReflectionMethod('TestMethods', 'm3');
         $params = $method->getParameters();
 
-        //$params[0]->getDeclaringClass();
-        //TODO: implement, why is this function is missing on win32 5.1.5??
-        self::markTestSkipped();
+        $class = $params[0]->getDeclaringClass();
+               self::assertTrue($class instanceof ezcReflectionClass);
+        self::assertEquals('TestMethods', $class->getName());
     }
+    
+    public function testGetName() {
+               $func = new ezcReflectionFunction('m1');
+        $params = $func->getParameters();
+               self::assertEquals('test', $params[0]->getName());
+       }
 
+    public function testIsPassedByReference() {
+               $func = new ezcReflectionFunction('m1');
+        $params = $func->getParameters();
+               self::assertFalse($params[0]->isPassedByReference());
+               self::assertTrue($params[2]->isPassedByReference());
+       }
+       
+    public function testIsArray() {
+               $func = new ezcReflectionFunction('m1');
+        $params = $func->getParameters();
+               self::assertFalse($params[0]->isArray());
+       }
+       
+    public function testAllowsNull() {
+               $func = new ezcReflectionFunction('m1');
+        $params = $func->getParameters();
+               self::assertTrue($params[0]->allowsNull());
+       }
+       
+    public function isOptional() {
+               $func = new ezcReflectionFunction('mmm');
+               $param = $func->getParameter('t');
+               self::assertTrue($param->isOptional());
+               
+               $func = new ezcReflectionFunction('m1');
+               $param = $func->getParameter('test');
+               self::assertFalse($param->isOptional());
+       }
+       
+       public function testIsDefaultValueAvailable() {
+               $func = new ezcReflectionFunction('mmm');
+               $param = $func->getParameters();
+               $param = $param[0];
+               self::assertTrue($param->isDefaultValueAvailable());
+               
+               $func = new ezcReflectionFunction('m1');
+               $param = $func->getParameters();
+               $param = $param[0];
+               self::assertFalse($param->isDefaultValueAvailable());
+       }
+       
+       /**
+       * @expectedException ReflectionException
+       */
+       public function testGetDefaultValue() {
+               $func = new ezcReflectionFunction('mmm');
+               $param = $func->getParameters();
+               $param = $param[0];
+               self::assertEquals('foo', $param->getDefaultValue());
+               
+               $func = new ezcReflectionFunction('m1');
+               $param = $func->getParameters();
+               $param = $param[0];
+               self::assertEquals(null, $param->getDefaultValue()); //should 
throw exception
+       }
+       
+       public function testGetPosition() {
+               $func = new ezcReflectionFunction('mmm');
+               $param = $func->getParameters();
+               $param = $param[0];
+               self::assertEquals(0, $param->getPosition());
+               
+               $func = new ezcReflectionFunction('m1');
+               $param = $func->getParameters();
+               $param = $param[1];
+               self::assertEquals(1, $param->getPosition());
+       }
+       
     public static function suite()
     {
          return new PHPUnit_Framework_TestSuite( "ezcReflectionParameterTest" 
);

Modified: experimental/Reflection/tests/property_test.php
==============================================================================
--- experimental/Reflection/tests/property_test.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/property_test.php [iso-8859-1] Sat Dec 29 
18:46:17 2007
@@ -10,22 +10,85 @@
 
 class ezcReflectionPropertyTest extends ezcTestCase
 {
+    /**
+     * @var ezcReflectionProperty
+     */
+    protected $refProp;
+
+    public function setUp() {
+        $class = new ezcReflectionClass('SomeClass');
+               $this->refProp = $class->getProperty('fields');
+    }
+
+    public function tearDown() {
+        unset($this->refProp);
+    }
+
     public function testGetType() {
-        $method = new ezcReflectionMethod('ezcReflectionClass', 'isTagged');
-        $params = $method->getParameters();
-        $type = $params[0]->getType();
-        self::assertType('ezcReflectionPrimitiveType', $type);
-        self::assertEquals('string', $type->toString());
+        $type = $this->refProp->getType();
+        self::assertType('ezcReflectionArrayType', $type);
+        self::assertEquals('int[]', $type->toString());
     }
 
     public function testGetDeclaringClass() {
-        $method = new ezcReflectionMethod('ezcReflectionClass', 'isTagged');
-        $params = $method->getParameters();
-        $class = $params[0]->getDeclaringClass();
+        $class = $this->refProp->getDeclaringClass();
         self::assertType('ezcReflectionClassType', $class);
-        self::assertEquals('ezcReflectionClass', $class->toString());
+        self::assertEquals('SomeClass', $class->toString());
     }
-
+       
+       public function testGetName() {
+               self::assertEquals('fields', $this->refProp->getName());
+       }
+       
+    public function testIsPublic() {
+               self::assertFalse($this->refProp->isPublic());
+       }
+       
+       public function testIsPrivate() {
+               self::assertTrue($this->refProp->isPrivate());
+       }
+    
+       public function testIsProtected() {
+               self::assertFalse($this->refProp->isProtected());
+       }
+       
+       public function testIsStatic() {
+               self::assertFalse($this->refProp->isStatic());
+       }
+    
+       public function testIsDefault() {
+               self::assertTrue($this->refProp->isDefault());
+       }
+    
+       public function testGetModifiers() {
+               self::assertEquals(1024, $this->refProp->getModifiers());
+       }
+       
+       /**
+       * @expectedException ReflectionException
+       */
+       public function testGetValue() {
+               $o = new SomeClass();
+               self::assertEquals(null, $this->refProp->getValue($o));
+       }
+       
+       /**
+       * @expectedException ReflectionException
+       */
+       public function testSetValue() {
+               $o = new SomeClass();
+               //self::assertEquals(null, $this->refProp->getValue($o));
+               $this->refProp->setValue($o, 3);
+               //self::assertEquals(3, $this->refProp->getValue($o));
+       }
+       
+       public function testGetDocComment() {
+               $o = new SomeClass();
+               self::assertEquals("/**
+       * @var int[]
+       */", $this->refProp->getDocComment($o));
+       }
+       
 
     public static function suite()
     {

Modified: experimental/Reflection/tests/suite.php
==============================================================================
--- experimental/Reflection/tests/suite.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/suite.php [iso-8859-1] Sat Dec 29 18:46:17 
2007
@@ -25,6 +25,8 @@
 require_once 'test_classes/methods2.php';
 require_once 'test_classes/MyReflectionClass.php';
 require_once 'test_classes/webservice.php';
+require_once 'test_classes/BaseClass.php';
+require_once 'test_classes/SomeClass.php';
 
 require_once 'test_helper.php';
 

Added: experimental/Reflection/tests/test_classes/BaseClass.php
==============================================================================
--- experimental/Reflection/tests/test_classes/BaseClass.php (added)
+++ experimental/Reflection/tests/test_classes/BaseClass.php [iso-8859-1] Sat 
Dec 29 18:46:17 2007
@@ -1,0 +1,11 @@
+<?php
+
+class BaseClass {
+
+    public function doSomeMetaProgramming() 
+       {
+               return true;
+       }
+}
+
+?>

Added: experimental/Reflection/tests/test_classes/SomeClass.php
==============================================================================
--- experimental/Reflection/tests/test_classes/SomeClass.php (added)
+++ experimental/Reflection/tests/test_classes/SomeClass.php [iso-8859-1] Sat 
Dec 29 18:46:17 2007
@@ -1,0 +1,25 @@
+<?php
+/**
+* @licence New BSD like
+* @donotdocument
+* @testclass
+* @ignore
+*/
+class SomeClass extends BaseClass {
+
+       /**
+       * @var int[]
+       */
+       private $fields;
+
+       public function __construct() {
+               // echo "New SomeClass instance created.\n";
+       }
+       
+    public function helloWorld() 
+       {
+               return true;
+       }
+}
+
+?>

Modified: experimental/Reflection/tests/test_classes/functions.php
==============================================================================
--- experimental/Reflection/tests/test_classes/functions.php [iso-8859-1] 
(original)
+++ experimental/Reflection/tests/test_classes/functions.php [iso-8859-1] Sat 
Dec 29 18:46:17 2007
@@ -6,12 +6,14 @@
  * @author
  * @param string $test
  * @param ezcReflectionApi $test2
- * @param NoneExistingType $test3
+ * @param ReflectionClass $test3
  * @return string Hello World
  */
-function m1($test, $test2, $test3) {
+function m1($test, $test2, &$test3) {
     return 'Hello World';
 }
+
+function mmm($t = 'foo') {}
 
 /**
  * @param void $DocuFlaw

Modified: experimental/Reflection/tests/type_factory_test.php
==============================================================================
--- experimental/Reflection/tests/type_factory_test.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/type_factory_test.php [iso-8859-1] Sat Dec 29 
18:46:17 2007
@@ -40,15 +40,18 @@
 
     /**
      * Test with class types
+        * @expectedException ReflectionException
      */
     public function testGetTypeClass() {
-        $classes = array('ReflectionClass', 'NoneExistingClassFooBarr', 
'ezcTestClass');
+        $classes = array('ReflectionClass', 'ezcTestClass');
         $factory = new ezcReflectionTypeFactoryImpl();
         foreach ($classes as $class) {
                $type = $factory->getType($class);
                self::assertType('ezcReflectionType', $type);
             self::assertType('ezcReflectionClassType', $type);
         }
+               
+               $type = $factory->getType('NoneExistingClass');
     }
 
 


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to