]> granicus.if.org Git - php/commitdiff
tests for reflectionClass
authorZoe Slattery <zoe@php.net>
Tue, 10 Jul 2007 16:25:15 +0000 (16:25 +0000)
committerZoe Slattery <zoe@php.net>
Tue, 10 Jul 2007 16:25:15 +0000 (16:25 +0000)
ext/reflection/tests/reflectionClass_FileInfo_basic.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_FileInfo_error.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstant_basic.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstant_error.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstants_basic.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstants_error.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstructor_basic.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getConstructor_error.phpt [new file with mode: 0644]

diff --git a/ext/reflection/tests/reflectionClass_FileInfo_basic.phpt b/ext/reflection/tests/reflectionClass_FileInfo_basic.phpt
new file mode 100644 (file)
index 0000000..27b4bc2
--- /dev/null
@@ -0,0 +1,33 @@
+--TEST--
+ReflectionClass::getFileName(), ReflectionClass::getStartLine(), ReflectionClass::getEndLine()
+--FILE-- 
+<?php
+//New instance of class C - defined below
+$rc = new ReflectionClass("C");
+
+//Get the file name of the PHP script in which C is defined
+var_dump($rc->getFileName());
+
+//Get the line number at the start of the definition of class C
+var_dump($rc->getStartLine());
+
+//Get the line number at the end of the definition of class C
+var_dump($rc->getEndLine());
+
+//Same tests as above but stdclass is internal - so all results should be false. 
+$rc = new ReflectionClass("stdClass");
+var_dump($rc->getFileName());
+var_dump($rc->getStartLine());
+var_dump($rc->getEndLine());
+
+Class C {
+
+}
+?>
+--EXPECTF--
+string(%d) "%sreflectionClass_FileInfo_basic.php"
+int(20)
+int(22)
+bool(false)
+bool(false)
+bool(false)
diff --git a/ext/reflection/tests/reflectionClass_FileInfo_error.phpt b/ext/reflection/tests/reflectionClass_FileInfo_error.phpt
new file mode 100644 (file)
index 0000000..766cdf3
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+ReflectionClass::getFileName(), ReflectionClass::getStartLine(), ReflectionClass::getEndLine() - bad params
+--FILE-- 
+<?php
+Class C { }
+
+$rc = new ReflectionClass("C");
+$methods = array("getFileName", "getStartLine", "getEndLine");
+
+foreach ($methods as $method) {
+       var_dump($rc->$method());
+       var_dump($rc->$method(null));
+       var_dump($rc->$method('X', 0)); 
+}
+?>
+--EXPECTF--
+string(%d) "%s"
+
+Warning: Wrong parameter count for ReflectionClass::getFileName() in %s on line 9
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getFileName() in %s on line 10
+NULL
+int(2)
+
+Warning: Wrong parameter count for ReflectionClass::getStartLine() in %s on line 9
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getStartLine() in %s on line 10
+NULL
+int(2)
+
+Warning: Wrong parameter count for ReflectionClass::getEndLine() in %s on line 9
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getEndLine() in %s on line 10
+NULL
\ No newline at end of file
diff --git a/ext/reflection/tests/reflectionClass_getConstant_basic.phpt b/ext/reflection/tests/reflectionClass_getConstant_basic.phpt
new file mode 100644 (file)
index 0000000..6e05111
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+ReflectionClass::getConstants()
+--FILE--
+<?php
+class C {
+       const a = 'hello from C';
+}
+class D extends C {
+}
+class E extends D {
+}
+class F extends E {
+       const a = 'hello from F';
+}
+class X {
+}
+
+$classes = array("C", "D", "E", "F", "X");
+foreach($classes as $class) {
+       echo "Reflecting on class $class: \n";
+       $rc = new ReflectionClass($class);
+       var_dump($rc->getConstant('a'));
+       var_dump($rc->getConstant('doesntexist'));
+}
+?>
+--EXPECTF--
+Reflecting on class C: 
+string(12) "hello from C"
+bool(false)
+Reflecting on class D: 
+string(12) "hello from C"
+bool(false)
+Reflecting on class E: 
+string(12) "hello from C"
+bool(false)
+Reflecting on class F: 
+string(12) "hello from F"
+bool(false)
+Reflecting on class X: 
+bool(false)
+bool(false)
diff --git a/ext/reflection/tests/reflectionClass_getConstant_error.phpt b/ext/reflection/tests/reflectionClass_getConstant_error.phpt
new file mode 100644 (file)
index 0000000..907d6d8
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+ReflectionClass::getConstant() - bad params
+--FILE--
+<?php
+class C {
+       const myConst = 1;
+}
+
+$rc = new ReflectionClass("C");
+echo "Check invalid params:\n";
+var_dump($rc->getConstant());
+var_dump($rc->getConstant("myConst", "myConst"));
+var_dump($rc->getConstant(null));
+var_dump($rc->getConstant(1));
+var_dump($rc->getConstant(1.5));
+var_dump($rc->getConstant(true));
+var_dump($rc->getConstant(array(1,2,3)));
+var_dump($rc->getConstant(new C));
+?>
+--EXPECTF--
+Check invalid params:
+
+Warning: ReflectionClass::getConstant() expects exactly 1 parameter, 0 given in %s on line 8
+NULL
+
+Warning: ReflectionClass::getConstant() expects exactly 1 parameter, 2 given in %s on line 9
+NULL
+bool(false)
+bool(false)
+bool(false)
+bool(false)
+
+Warning: ReflectionClass::getConstant() expects parameter 1 to be string, array given in %s on line 14
+NULL
+
+Warning: ReflectionClass::getConstant() expects parameter 1 to be string, object given in %s on line 15
+NULL
\ No newline at end of file
diff --git a/ext/reflection/tests/reflectionClass_getConstants_basic.phpt b/ext/reflection/tests/reflectionClass_getConstants_basic.phpt
new file mode 100644 (file)
index 0000000..9abdcc8
--- /dev/null
@@ -0,0 +1,48 @@
+--TEST--
+ReflectionClass::getConstants()
+--FILE--
+<?php
+class C {
+       const a = 'hello from C';
+}
+class D extends C {
+}
+class E extends D {
+}
+class F extends E {
+       const a = 'hello from F';
+}
+class X {
+}
+
+$classes = array('C', 'D', 'E', 'F', 'X');
+foreach($classes as $class) {
+       echo "Constants from class $class: \n";
+       $rc = new ReflectionClass($class);
+       var_dump($rc->getConstants());
+}
+?>
+--EXPECTF--
+Constants from class C: 
+array(1) {
+  ["a"]=>
+  string(12) "hello from C"
+}
+Constants from class D: 
+array(1) {
+  ["a"]=>
+  string(12) "hello from C"
+}
+Constants from class E: 
+array(1) {
+  ["a"]=>
+  string(12) "hello from C"
+}
+Constants from class F: 
+array(1) {
+  ["a"]=>
+  string(12) "hello from F"
+}
+Constants from class X: 
+array(0) {
+}
diff --git a/ext/reflection/tests/reflectionClass_getConstants_error.phpt b/ext/reflection/tests/reflectionClass_getConstants_error.phpt
new file mode 100644 (file)
index 0000000..73c407d
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+ReflectionClass::getConstants()
+--FILE--
+<?php
+class X {
+}
+
+$rc = new reflectionClass('X');
+
+//Test invalid arguments
+$rc->getConstants('X');
+$rc->getConstants(true);
+$rc->getConstants(null);
+$rc->getConstants('A', 'B');
+
+?>
+--EXPECTF--
+Warning: Wrong parameter count for ReflectionClass::getConstants() in %s on line 8
+
+Warning: Wrong parameter count for ReflectionClass::getConstants() in %s on line 9
+
+Warning: Wrong parameter count for ReflectionClass::getConstants() in %s on line 10
+
+Warning: Wrong parameter count for ReflectionClass::getConstants() in %s on line 11
diff --git a/ext/reflection/tests/reflectionClass_getConstructor_basic.phpt b/ext/reflection/tests/reflectionClass_getConstructor_basic.phpt
new file mode 100644 (file)
index 0000000..f3881c5
--- /dev/null
@@ -0,0 +1,82 @@
+--TEST--
+ReflectionClass::getConstructor()
+--FILE--
+<?php
+class NewCtor {
+       function __construct() {}
+}
+
+class ExtendsNewCtor extends NewCtor {
+}
+
+class OldCtor {
+       function OldCtor() {}
+}
+
+class ExtendsOldCtor extends OldCtor {
+}
+
+
+class X {
+       function Y() {}
+}
+
+class Y extends X {
+}
+
+class OldAndNewCtor {
+       function OldAndNewCtor() {}
+       function __construct() {}
+}
+
+class NewAndOldCtor {
+       function __construct() {}
+       function NewAndOldCtor() {}
+}
+class B {
+       function B() {}
+}
+
+class C extends B {
+       function C() {}
+}
+
+class D1 extends C {
+       function __construct() {}
+}
+
+class D2 extends C {
+}
+
+$classes = array('NewCtor', 'ExtendsNewCtor', 'OldCtor', 'ExtendsOldCtor', 
+                                'OldAndNewCtor', 'NewAndOldCtor', 'B', 'C', 'D1', 'D2', 'X', 'Y');
+
+foreach ($classes as $class) {
+       $rc = new ReflectionClass($class);
+       $rm = $rc->getConstructor();
+       if ($rm != null) {
+               echo "Constructor of $class: " . $rm->getName() . "\n";
+       }  else {
+               echo "No constructor for $class\n";
+       }
+       
+}                               
+                                
+?>
+--EXPECTF--
+
+Strict Standards: Redefining already defined constructor for class OldAndNewCtor in %s on line 26
+
+Strict Standards: %s for class NewAndOldCtor in %s on line 31
+Constructor of NewCtor: __construct
+Constructor of ExtendsNewCtor: __construct
+Constructor of OldCtor: OldCtor
+Constructor of ExtendsOldCtor: OldCtor
+Constructor of OldAndNewCtor: __construct
+Constructor of NewAndOldCtor: __construct
+Constructor of B: B
+Constructor of C: C
+Constructor of D1: __construct
+Constructor of D2: C
+No constructor for X
+No constructor for Y
\ No newline at end of file
diff --git a/ext/reflection/tests/reflectionClass_getConstructor_error.phpt b/ext/reflection/tests/reflectionClass_getConstructor_error.phpt
new file mode 100644 (file)
index 0000000..8892b1d
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+ReflectionClass::getConstructor() - bad params
+--FILE--
+<?php
+class C {}
+$rc = new ReflectionClass('C');
+var_dump($rc->getConstructor(null));
+var_dump($rc->getConstructor('X'));
+var_dump($rc->getConstructor(true));
+var_dump($rc->getConstructor(array(1,2,3)));
+?>
+--EXPECTF--
+
+Warning: Wrong parameter count for ReflectionClass::getConstructor() in %s on line 4
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getConstructor() in %s on line 5
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getConstructor() in %s on line 6
+NULL
+
+Warning: Wrong parameter count for ReflectionClass::getConstructor() in %s on line 7
+NULL