From fd81f61e0d7456aa298fd53effa17235b5923530 Mon Sep 17 00:00:00 2001 From: Steve Seear Date: Tue, 18 Dec 2007 17:20:05 +0000 Subject: [PATCH] Adding PHPT tests for the ReflectionProperty class. --- .../tests/reflectionProperty_basic1.phpt | 162 ++++++++++++++++++ .../tests/reflectionProperty_basic2.phpt | 105 ++++++++++++ .../reflectionProperty_constructor_error.phpt | 46 +++++ .../tests/reflectionProperty_error.phpt | 70 ++++++++ .../reflectionProperty_export_basic.phpt | 18 ++ .../reflectionProperty_export_error.phpt | 56 ++++++ ...Property_getDeclaringClass_variation1.phpt | 29 ++++ ...eflectionProperty_getDocComment_basic.phpt | 102 +++++++++++ ...eflectionProperty_getDocComment_error.phpt | 31 ++++ ...reflectionProperty_getModifiers_basic.phpt | 48 ++++++ .../reflectionProperty_getValue_error.phpt | 83 +++++++++ .../reflectionProperty_isDefault_basic.phpt | 65 +++++++ .../reflectionProperty_setValue_error.phpt | 102 +++++++++++ 13 files changed, 917 insertions(+) create mode 100644 ext/reflection/tests/reflectionProperty_basic1.phpt create mode 100644 ext/reflection/tests/reflectionProperty_basic2.phpt create mode 100644 ext/reflection/tests/reflectionProperty_constructor_error.phpt create mode 100644 ext/reflection/tests/reflectionProperty_error.phpt create mode 100644 ext/reflection/tests/reflectionProperty_export_basic.phpt create mode 100644 ext/reflection/tests/reflectionProperty_export_error.phpt create mode 100644 ext/reflection/tests/reflectionProperty_getDeclaringClass_variation1.phpt create mode 100644 ext/reflection/tests/reflectionProperty_getDocComment_basic.phpt create mode 100644 ext/reflection/tests/reflectionProperty_getDocComment_error.phpt create mode 100644 ext/reflection/tests/reflectionProperty_getModifiers_basic.phpt create mode 100644 ext/reflection/tests/reflectionProperty_getValue_error.phpt create mode 100644 ext/reflection/tests/reflectionProperty_isDefault_basic.phpt create mode 100644 ext/reflection/tests/reflectionProperty_setValue_error.phpt diff --git a/ext/reflection/tests/reflectionProperty_basic1.phpt b/ext/reflection/tests/reflectionProperty_basic1.phpt new file mode 100644 index 0000000000..f7c1c8f10d --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_basic1.phpt @@ -0,0 +1,162 @@ +--TEST-- +Test usage of ReflectionProperty methods __toString(), export(), getName(), isPublic(), isPrivate(), isProtected(), isStatic(), getValue() and setValue(). +--SKIPIF-- + +--FILE-- +__toString()); + echo "export():\n"; + var_dump(ReflectionProperty::export($class, $property, true)); + echo "export():\n"; + var_dump(ReflectionProperty::export($class, $property, false)); + echo "getName():\n"; + var_dump($propInfo->getName()); + echo "isPublic():\n"; + var_dump($propInfo->isPublic()); + echo "isPrivate():\n"; + var_dump($propInfo->isPrivate()); + echo "isProtected():\n"; + var_dump($propInfo->isProtected()); + echo "isStatic():\n"; + var_dump($propInfo->isStatic()); + $instance = new $class(); + if ($propInfo->isPublic()) { + echo "getValue():\n"; + var_dump($propInfo->getValue($instance)); + $propInfo->setValue($instance, "NewValue"); + echo "getValue() after a setValue():\n"; + var_dump($propInfo->getValue($instance)); + } + echo "\n**********************************\n"; +} + +class TestClass { + public $pub; + static public $stat = "static property"; + protected $prot = 4; + private $priv = "keepOut"; +} + +reflectProperty("TestClass", "pub"); +reflectProperty("TestClass", "stat"); +reflectProperty("TestClass", "prot"); +reflectProperty("TestClass", "priv"); + +?> +--EXPECT-- +********************************** +Reflecting on property TestClass::pub + +__toString(): +string(35) "Property [ public $pub ] +" +export(): +string(35) "Property [ public $pub ] +" +export(): +Property [ public $pub ] + +NULL +getName(): +string(3) "pub" +isPublic(): +bool(true) +isPrivate(): +bool(false) +isProtected(): +bool(false) +isStatic(): +bool(false) +getValue(): +NULL +getValue() after a setValue(): +string(8) "NewValue" + +********************************** +********************************** +Reflecting on property TestClass::stat + +__toString(): +string(33) "Property [ public static $stat ] +" +export(): +string(33) "Property [ public static $stat ] +" +export(): +Property [ public static $stat ] + +NULL +getName(): +string(4) "stat" +isPublic(): +bool(true) +isPrivate(): +bool(false) +isProtected(): +bool(false) +isStatic(): +bool(true) +getValue(): +string(15) "static property" +getValue() after a setValue(): +string(8) "NewValue" + +********************************** +********************************** +Reflecting on property TestClass::prot + +__toString(): +string(39) "Property [ protected $prot ] +" +export(): +string(39) "Property [ protected $prot ] +" +export(): +Property [ protected $prot ] + +NULL +getName(): +string(4) "prot" +isPublic(): +bool(false) +isPrivate(): +bool(false) +isProtected(): +bool(true) +isStatic(): +bool(false) + +********************************** +********************************** +Reflecting on property TestClass::priv + +__toString(): +string(37) "Property [ private $priv ] +" +export(): +string(37) "Property [ private $priv ] +" +export(): +Property [ private $priv ] + +NULL +getName(): +string(4) "priv" +isPublic(): +bool(false) +isPrivate(): +bool(true) +isProtected(): +bool(false) +isStatic(): +bool(false) + +********************************** + + diff --git a/ext/reflection/tests/reflectionProperty_basic2.phpt b/ext/reflection/tests/reflectionProperty_basic2.phpt new file mode 100644 index 0000000000..f2b5ff4fb9 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_basic2.phpt @@ -0,0 +1,105 @@ +--TEST-- +Test usage of ReflectionProperty methods isDefault(), getModifiers(), getDeclaringClass() and getDocComment(). +--SKIPIF-- + +--FILE-- +isDefault()); + echo "getModifiers():\n"; + var_dump($propInfo->getModifiers()); + echo "getDeclaringClass():\n"; + var_dump($propInfo->getDeclaringClass()); + echo "getDocComment():\n"; + var_dump($propInfo->getDocComment()); + echo "\n**********************************\n"; +} + +class TestClass { + public $pub; + static public $stat = "static property"; + /** + * This property has a comment. + */ + protected $prot = 4; + private $priv = "keepOut"; +} + +reflectProperty("TestClass", "pub"); +reflectProperty("TestClass", "stat"); +reflectProperty("TestClass", "prot"); +reflectProperty("TestClass", "priv"); + +?> +--EXPECTF-- +********************************** +Reflecting on property TestClass::pub + +isDefault(): +bool(true) +getModifiers(): +int(256) +getDeclaringClass(): +object(ReflectionClass)#%d (1) { + ["name"]=> + string(9) "TestClass" +} +getDocComment(): +bool(false) + +********************************** +********************************** +Reflecting on property TestClass::stat + +isDefault(): +bool(true) +getModifiers(): +int(257) +getDeclaringClass(): +object(ReflectionClass)#%d (1) { + ["name"]=> + string(9) "TestClass" +} +getDocComment(): +bool(false) + +********************************** +********************************** +Reflecting on property TestClass::prot + +isDefault(): +bool(true) +getModifiers(): +int(512) +getDeclaringClass(): +object(ReflectionClass)#%d (1) { + ["name"]=> + string(9) "TestClass" +} +getDocComment(): +string(%d) "/** + * This property has a comment. + */" + +********************************** +********************************** +Reflecting on property TestClass::priv + +isDefault(): +bool(true) +getModifiers(): +int(1024) +getDeclaringClass(): +object(ReflectionClass)#%d (1) { + ["name"]=> + string(9) "TestClass" +} +getDocComment(): +bool(false) + +********************************** diff --git a/ext/reflection/tests/reflectionProperty_constructor_error.phpt b/ext/reflection/tests/reflectionProperty_constructor_error.phpt new file mode 100644 index 0000000000..46cdc87ea9 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_constructor_error.phpt @@ -0,0 +1,46 @@ +--TEST-- +Test ReflectionProperty class constructor errors. +--SKIPIF-- + +--FILE-- +getMessage(); +} + +echo "\n\nWrong property parameter type:\n"; +try { + $propInfo = new ReflectionProperty($a, 'TestClass'); +} +catch(ReflectionException $e) { + echo $e->getMessage(); +} + +echo "\n\nNon-existent property:\n"; +try { + $propInfo = new ReflectionProperty('TestClass', "nonExistentProperty"); +} +catch(Exception $e) { + echo $e->getMessage(); +} + +?> +--EXPECT-- +Non-existent class: +Class NonExistentClass does not exist + +Wrong property parameter type: +The parameter class is expected to be either a string or an object + +Non-existent property: +Property TestClass::$nonExistentProperty does not exist diff --git a/ext/reflection/tests/reflectionProperty_error.phpt b/ext/reflection/tests/reflectionProperty_error.phpt new file mode 100644 index 0000000000..bae255c232 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_error.phpt @@ -0,0 +1,70 @@ +--TEST-- +Test ReflectionProperty class errors. +--SKIPIF-- + +--FILE-- +getName(1)); +var_dump($rp->isPrivate(1)); +var_dump($rp->isProtected(1)); +var_dump($rp->isPublic(1)); +var_dump($rp->isStatic(1)); +var_dump($rp->getModifiers(1)); +var_dump($rp->isDefault(1)); + +?> +--EXPECTF-- + +Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 0 given in %s on line %d +object(ReflectionProperty)#%d (2) { + ["name"]=> + string(0) "" + ["class"]=> + string(0) "" +} + +Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 1 given in %s on line %d +object(ReflectionProperty)#%d (2) { + ["name"]=> + string(0) "" + ["class"]=> + string(0) "" +} + +Warning: ReflectionProperty::__construct() expects exactly 2 parameters, 3 given in %s on line %d +object(ReflectionProperty)#%d (2) { + ["name"]=> + string(0) "" + ["class"]=> + string(0) "" +} + +Warning: Wrong parameter count for ReflectionProperty::getName() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::isPrivate() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::isProtected() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::isPublic() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::isStatic() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::getModifiers() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::isDefault() in %s on line %d +NULL diff --git a/ext/reflection/tests/reflectionProperty_export_basic.phpt b/ext/reflection/tests/reflectionProperty_export_basic.phpt new file mode 100644 index 0000000000..331fdb6a28 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_export_basic.phpt @@ -0,0 +1,18 @@ +--TEST-- +Test ReflectionProperty::export() usage. +--SKIPIF-- + +--FILE-- + +--EXPECT-- +Property [ public $proper ] + +NULL diff --git a/ext/reflection/tests/reflectionProperty_export_error.phpt b/ext/reflection/tests/reflectionProperty_export_error.phpt new file mode 100644 index 0000000000..9351846f6b --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_export_error.phpt @@ -0,0 +1,56 @@ +--TEST-- +Test ReflectionProperty::export() errors. +--SKIPIF-- + +--FILE-- +getMessage(); +} + +echo "\n\nWrong property parameter type:\n"; +try { + ReflectionProperty::export($a, 'TestClass', false); +} +catch(ReflectionException $e) { + echo $e->getMessage(); +} + +echo "\n\nNon-existent property:\n"; +try { + ReflectionProperty::export('TestClass', "nonExistentProperty", true); +} +catch(Exception $e) { + echo $e->getMessage(); +} + +echo "\n\nIncorrect number of args:\n"; +ReflectionProperty::export(); +ReflectionProperty::export('TestClass', "nonExistentProperty", true, false); + +?> +--EXPECTF-- +Non-existent class: +Class NonExistentClass does not exist + +Wrong property parameter type: +The parameter class is expected to be either a string or an object + +Non-existent property: +Property TestClass::$nonExistentProperty does not exist + +Incorrect number of args: + +Warning: ReflectionProperty::export() expects at least 2 parameters, 0 given in %s on line %d + +Warning: ReflectionProperty::export() expects at most 3 parameters, 4 given in %s on line %d diff --git a/ext/reflection/tests/reflectionProperty_getDeclaringClass_variation1.phpt b/ext/reflection/tests/reflectionProperty_getDeclaringClass_variation1.phpt new file mode 100644 index 0000000000..3df3b26055 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_getDeclaringClass_variation1.phpt @@ -0,0 +1,29 @@ +--TEST-- +Test ReflectionProperty::getDeclaringClass() with inherited properties. +--SKIPIF-- + +--FILE-- +getDeclaringClass()); + +echo "Wrong number of params:\n"; +$propInfo->getDeclaringClass(1); + +?> +--EXPECTF-- +object(ReflectionClass)#%d (1) { + ["name"]=> + string(1) "A" +} +Wrong number of params: + +Warning: Wrong parameter count for ReflectionProperty::getDeclaringClass() in %s on line %d diff --git a/ext/reflection/tests/reflectionProperty_getDocComment_basic.phpt b/ext/reflection/tests/reflectionProperty_getDocComment_basic.phpt new file mode 100644 index 0000000000..44416b7fcb --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_getDocComment_basic.phpt @@ -0,0 +1,102 @@ +--TEST-- +Test ReflectionProperty::getDocComment() usage. +--SKIPIF-- + +--FILE-- +getProperties(); + foreach($rps as $rp) { + echo "\n\n---> Doc comment for $class::$" . $rp->getName() . ":\n"; + var_dump($rp->getDocComment()); + } +} + +?> +--EXPECTF-- + +---> Doc comment for A::$a: +string(%d) "/** + * My Doc Comment for $a + * + */" + + +---> Doc comment for A::$b: +bool(false) + + +---> Doc comment for A::$c: +bool(false) + + +---> Doc comment for A::$d: +string(%d) "/** + * My Doc Comment for $d + */" + + +---> Doc comment for A::$e: +bool(false) + + +---> Doc comment for A::$f: +string(%d) "/** + * Doc comment for $f + */" + + +---> Doc comment for B::$a: +bool(false) + + +---> Doc comment for B::$b: +string(%d) "/** A doc comment for $b */" + + +---> Doc comment for B::$c: +bool(false) + + +---> Doc comment for B::$e: +string(%d) "/** A doc comment for $e */" + + +---> Doc comment for B::$d: +string(%d) "/** + * My Doc Comment for $d + */" + + +---> Doc comment for B::$f: +string(%d) "/** + * Doc comment for $f + */" diff --git a/ext/reflection/tests/reflectionProperty_getDocComment_error.phpt b/ext/reflection/tests/reflectionProperty_getDocComment_error.phpt new file mode 100644 index 0000000000..8c1b68e81f --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_getDocComment_error.phpt @@ -0,0 +1,31 @@ +--TEST-- +Test ReflectionProperty::getDocComment() errors. +--SKIPIF-- + +--FILE-- +getDocComment(null)); +var_dump($rc->getDocComment('X')); +var_dump($rc->getDocComment(true)); +var_dump($rc->getDocComment(array(1, 2, 3))); + +?> +--EXPECTF-- + +Warning: Wrong parameter count for ReflectionProperty::getDocComment() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::getDocComment() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::getDocComment() in %s on line %d +NULL + +Warning: Wrong parameter count for ReflectionProperty::getDocComment() in %s on line %d +NULL diff --git a/ext/reflection/tests/reflectionProperty_getModifiers_basic.phpt b/ext/reflection/tests/reflectionProperty_getModifiers_basic.phpt new file mode 100644 index 0000000000..907a7e7be2 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_getModifiers_basic.phpt @@ -0,0 +1,48 @@ +--TEST-- +Test ReflectionProperty::getModifiers() usage. +--SKIPIF-- + +--FILE-- +getModifiers()); + $rp = new ReflectionProperty("D", "a$i"); + echo "D::a$i: "; + var_dump($rp->getModifiers()); +} + +?> +--EXPECTF-- +C::a1: int(256) +D::a1: int(256) +C::a2: int(512) +D::a2: int(512) +C::a3: int(1024) +D::a3: int(3072) +C::a4: int(257) +D::a4: int(257) +C::a5: int(513) +D::a5: int(513) +C::a6: int(1025) +D::a6: int(3073) \ No newline at end of file diff --git a/ext/reflection/tests/reflectionProperty_getValue_error.phpt b/ext/reflection/tests/reflectionProperty_getValue_error.phpt new file mode 100644 index 0000000000..acfc3b0a09 --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_getValue_error.phpt @@ -0,0 +1,83 @@ +--TEST-- +Test ReflectionProperty::getValue() errors. +--SKIPIF-- + +--FILE-- +getValue()); + +echo "\nToo many args:\n"; +var_dump($propInfo->getValue($instance, true)); + +echo "\nWrong type of arg:\n"; +var_dump($propInfo->getValue(true)); + +echo "\nInstance without property:\n"; +$propInfo = new ReflectionProperty('TestClass', 'stat'); + +echo "\nStatic property / too many args:\n"; +var_dump($propInfo->getValue($instance, true)); + +echo "\nStatic property / wrong type of arg:\n"; +var_dump($propInfo->getValue(true)); + +echo "\nProtected property:\n"; +try { + $propInfo = new ReflectionProperty('TestClass', 'prot'); + var_dump($propInfo->getValue($instance)); +} +catch(Exception $exc) { + echo $exc->getMessage(); +} + +echo "\n\nInstance without property:\n"; +$propInfo = new ReflectionProperty('TestClass', 'pub2'); +var_dump($propInfo->getValue($instanceWithNoProperties)); + +?> +--EXPECTF-- +Too few args: + +Warning: ReflectionProperty::getValue() expects exactly 1 parameter, 0 given in %s on line %d +NULL + +Too many args: + +Warning: ReflectionProperty::getValue() expects exactly 1 parameter, 2 given in %s on line %d +NULL + +Wrong type of arg: + +Warning: ReflectionProperty::getValue() expects parameter 1 to be object, boolean given in %s on line %d +NULL + +Instance without property: + +Static property / too many args: +string(15) "static property" + +Static property / wrong type of arg: +string(15) "static property" + +Protected property: +Cannot access non-public member TestClass::prot + +Instance without property: +NULL diff --git a/ext/reflection/tests/reflectionProperty_isDefault_basic.phpt b/ext/reflection/tests/reflectionProperty_isDefault_basic.phpt new file mode 100644 index 0000000000..57c3d0f3cc --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_isDefault_basic.phpt @@ -0,0 +1,65 @@ +--TEST-- +Test ReflectionProperty::isDefault() usage. +--SKIPIF-- + +--FILE-- +isDefault()); + echo "\n**********************************\n"; +} + +class TestClass { + public $pub; + static public $stat = "static property"; + protected $prot = 4; + private $priv = "keepOut"; +} + +reflectProperty("TestClass", "pub"); +reflectProperty("TestClass", "stat"); +reflectProperty("TestClass", "prot"); +reflectProperty("TestClass", "priv"); + +echo "Wrong number of params:\n"; +$propInfo = new ReflectionProperty('TestClass', 'pub'); +$propInfo->isDefault(1); + +?> +--EXPECTF-- +********************************** +Reflecting on property TestClass::pub + +isDefault(): +bool(true) + +********************************** +********************************** +Reflecting on property TestClass::stat + +isDefault(): +bool(true) + +********************************** +********************************** +Reflecting on property TestClass::prot + +isDefault(): +bool(true) + +********************************** +********************************** +Reflecting on property TestClass::priv + +isDefault(): +bool(true) + +********************************** +Wrong number of params: + +Warning: Wrong parameter count for ReflectionProperty::isDefault() in %s on line %d diff --git a/ext/reflection/tests/reflectionProperty_setValue_error.phpt b/ext/reflection/tests/reflectionProperty_setValue_error.phpt new file mode 100644 index 0000000000..960778235c --- /dev/null +++ b/ext/reflection/tests/reflectionProperty_setValue_error.phpt @@ -0,0 +1,102 @@ +--TEST-- +Test ReflectionProperty::setValue() error cases. +--SKIPIF-- + +--FILE-- +setValue()); +var_dump($propInfo->setValue($instance)); + +echo "\nToo many args:\n"; +var_dump($propInfo->setValue($instance, "NewValue", true)); + +echo "\nWrong type of arg:\n"; +var_dump($propInfo->setValue(true, "NewValue")); +$propInfo = new ReflectionProperty('TestClass', 'stat'); + +echo "\nStatic property / too many args:\n"; +var_dump($propInfo->setValue($instance, "NewValue", true)); + +echo "\nStatic property / too few args:\n"; +var_dump($propInfo->setValue("A new value")); +var_dump(TestClass::$stat); +var_dump($propInfo->setValue()); +var_dump(TestClass::$stat); + +echo "\nStatic property / wrong type of arg:\n"; +var_dump($propInfo->setValue(true, "Another new value")); +var_dump(TestClass::$stat); + +echo "\nProtected property:\n"; +try { + $propInfo = new ReflectionProperty('TestClass', 'prot'); + var_dump($propInfo->setValue($instance, "NewValue")); +} +catch(Exception $exc) { + echo $exc->getMessage(); +} + +echo "\n\nInstance without property:\n"; +$propInfo = new ReflectionProperty('TestClass', 'pub2'); +var_dump($propInfo->setValue($instanceWithNoProperties, "NewValue")); +var_dump($instanceWithNoProperties->pub2); +?> +--EXPECTF-- +Too few args: + +Warning: ReflectionProperty::setValue() expects exactly 2 parameters, 0 given in %s on line %d +NULL + +Warning: ReflectionProperty::setValue() expects exactly 2 parameters, 1 given in %s on line %d +NULL + +Too many args: + +Warning: ReflectionProperty::setValue() expects exactly 2 parameters, 3 given in %s on line %d +NULL + +Wrong type of arg: + +Warning: ReflectionProperty::setValue() expects parameter 1 to be object, boolean given in %s on line %d +NULL + +Static property / too many args: + +Warning: ReflectionProperty::setValue() expects exactly 2 parameters, 3 given in %s on line %d +NULL + +Static property / too few args: +NULL +string(11) "A new value" + +Warning: ReflectionProperty::setValue() expects exactly 2 parameters, 0 given in %s on line %d +NULL +string(11) "A new value" + +Static property / wrong type of arg: +NULL +string(17) "Another new value" + +Protected property: +Cannot access non-public member TestClass::prot + +Instance without property: +NULL +string(8) "NewValue" -- 2.40.0