]> granicus.if.org Git - php/commitdiff
MFB: Tests for ReflectionMethod::getClosure() and ReflectionFunction::getClosure()
authorFelix De Vliegher <felixdv@php.net>
Fri, 8 Aug 2008 12:42:40 +0000 (12:42 +0000)
committerFelix De Vliegher <felixdv@php.net>
Fri, 8 Aug 2008 12:42:40 +0000 (12:42 +0000)
ext/reflection/tests/ReflectionFunction_getClosure_basic.phpt [new file with mode: 0644]
ext/reflection/tests/ReflectionFunction_getClosure_error.phpt [new file with mode: 0644]
ext/reflection/tests/ReflectionMethod_getClosure_basic.phpt [new file with mode: 0644]
ext/reflection/tests/ReflectionMethod_getClosure_error.phpt [new file with mode: 0644]

diff --git a/ext/reflection/tests/ReflectionFunction_getClosure_basic.phpt b/ext/reflection/tests/ReflectionFunction_getClosure_basic.phpt
new file mode 100644 (file)
index 0000000..832d31c
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+Test ReflectionFunction::getClosure() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : public mixed ReflectionFunction::getClosure()
+ * Description: Returns a dynamically created closure for the function 
+ * Source code: ext/reflection/php_reflection.c
+ * Alias to functions: 
+ */
+
+echo "*** Testing ReflectionFunction::getClosure() : basic functionality ***\n";
+
+function foo()
+{
+       var_dump( "Inside foo function" );
+}
+
+function bar( $arg )
+{
+       var_dump( "Arg is " . $arg );
+}
+
+$func = new ReflectionFunction( 'foo' );
+$closure = $func->getClosure();
+$closure();
+
+$func = new ReflectionFunction( 'bar' );
+$closure = $func->getClosure();
+$closure( 'succeeded' );
+
+?>
+===DONE===
+--EXPECTF--
+*** Testing ReflectionFunction::getClosure() : basic functionality ***
+%unicode|string%(19) "Inside foo function"
+%unicode|string%(16) "Arg is succeeded"
+===DONE===
diff --git a/ext/reflection/tests/ReflectionFunction_getClosure_error.phpt b/ext/reflection/tests/ReflectionFunction_getClosure_error.phpt
new file mode 100644 (file)
index 0000000..76851ea
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+Test ReflectionFunction::getClosure() function : error functionality
+--FILE--
+<?php
+/* Prototype  : public mixed ReflectionFunction::getClosure()
+ * Description: Returns a dynamically created closure for the function
+ * Source code: ext/reflection/php_reflection.c
+ * Alias to functions:
+ */
+
+echo "*** Testing ReflectionFunction::getClosure() : error conditions ***\n";
+
+function foo()
+{
+       var_dump( "Inside foo function" );
+}
+
+$func = new ReflectionFunction( 'foo' );
+$closure = $func->getClosure('bar');
+
+?>
+===DONE===
+--EXPECTF--
+*** Testing ReflectionFunction::getClosure() : error conditions ***
+
+Warning: Wrong parameter count for ReflectionFunction::getClosure() in %s on line %d
+===DONE===
diff --git a/ext/reflection/tests/ReflectionMethod_getClosure_basic.phpt b/ext/reflection/tests/ReflectionMethod_getClosure_basic.phpt
new file mode 100644 (file)
index 0000000..d8bdbe1
--- /dev/null
@@ -0,0 +1,55 @@
+--TEST--
+Test ReflectionMethod::getClosure() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : public mixed ReflectionFunction::getClosure()
+ * Description: Returns a dynamically created closure for the method 
+ * Source code: ext/reflection/php_reflection.c
+ * Alias to functions: 
+ */
+
+echo "*** Testing ReflectionMethod::getClosure() : basic functionality ***\n";
+
+class StaticExample
+{
+       static function foo()
+       {
+               var_dump( "Static Example class, Hello World!" );
+       }
+}
+
+class Example
+{
+       public $bar = 42;
+       public function foo()
+       {
+               var_dump( "Example class, bar: " . $this->bar );
+       }
+}
+
+// Initialize classes
+$class = new ReflectionClass( 'Example' );
+$staticclass = new ReflectionClass( 'StaticExample' );
+$object = new Example();
+$fakeobj = new StdClass();
+
+
+$method = $staticclass->getMethod( 'foo' );
+$closure = $method->getClosure();
+$closure();
+
+$method = $class->getMethod( 'foo' );
+
+$closure = $method->getClosure( $object );
+$closure();
+$object->bar = 34;
+$closure();
+
+?>
+===DONE===
+--EXPECTF--
+*** Testing ReflectionMethod::getClosure() : basic functionality ***
+%unicode|string%(34) "Static Example class, Hello World!"
+%unicode|string%(22) "Example class, bar: 42"
+%unicode|string%(22) "Example class, bar: 34"
+===DONE===
diff --git a/ext/reflection/tests/ReflectionMethod_getClosure_error.phpt b/ext/reflection/tests/ReflectionMethod_getClosure_error.phpt
new file mode 100644 (file)
index 0000000..a54b09d
--- /dev/null
@@ -0,0 +1,73 @@
+--TEST--
+Test ReflectionMethod::getClosure() function : error functionality
+--FILE--
+<?php
+/* Prototype  : public mixed ReflectionFunction::getClosure()
+ * Description: Returns a dynamically created closure for the method
+ * Source code: ext/reflection/php_reflection.c
+ * Alias to functions:
+ */
+
+echo "*** Testing ReflectionMethod::getClosure() : error conditions ***\n";
+
+class StaticExample
+{
+       static function foo()
+       {
+               var_dump( "Static Example class, Hello World!" );
+       }
+}
+
+class Example
+{
+       public $bar = 42;
+       public function foo()
+       {
+               var_dump( "Example class, bar: " . $this->bar );
+       }
+}
+
+// Initialize classes
+$class = new ReflectionClass( 'Example' );
+$staticclass = new ReflectionClass( 'StaticExample' );
+$method = $class->getMethod( 'foo' );
+$staticmethod = $staticclass->getMethod( 'foo' );
+$object = new Example();
+$fakeobj = new StdClass();
+
+echo "\n-- Testing ReflectionMethod::getClosure() function with more than expected no. of arguments --\n";
+var_dump( $staticmethod->getClosure( 'foobar' ) );
+var_dump( $staticmethod->getClosure( 'foo', 'bar' ) );
+var_dump( $method->getClosure( $object, 'foobar' ) );
+
+echo "\n-- Testing ReflectionMethod::getClosure() function with Zero arguments --\n";
+$closure = $method->getClosure();
+
+echo "\n-- Testing ReflectionMethod::getClosure() function with Zero arguments --\n";
+try {
+        var_dump( $method->getClosure( $fakeobj ) );
+} catch( Exception $e ) {
+        var_dump( $e->getMessage() );
+}
+
+?>
+===DONE===
+--EXPECTF--
+*** Testing ReflectionMethod::getClosure() : error conditions ***
+
+-- Testing ReflectionMethod::getClosure() function with more than expected no. of arguments --
+object(Closure)#%d (0) {
+}
+object(Closure)#%d (0) {
+}
+
+Warning: ReflectionMethod::getClosure() expects exactly 1 parameter, 2 given in %s on line %d
+NULL
+
+-- Testing ReflectionMethod::getClosure() function with Zero arguments --
+
+Warning: ReflectionMethod::getClosure() expects exactly 1 parameter, 0 given in %s on line %d
+
+-- Testing ReflectionMethod::getClosure() function with Zero arguments --
+%unicode|string%(72) "Given object is not an instance of the class this method was declared in"
+===DONE===