From af96e5326fe1fefb712ff39240d54d44fcea883b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 26 Oct 2012 03:54:05 +0200 Subject: [PATCH 1/3] Adding regression test for behavior of magic methods with unset public properties Verifies that after having unset a public property, any access to it, be it read or write, causes calls to public magic methods Signed-off-by: Marco Pivetta --- tests/classes/unset_public_properties.phpt | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 tests/classes/unset_public_properties.phpt diff --git a/tests/classes/unset_public_properties.phpt b/tests/classes/unset_public_properties.phpt new file mode 100644 index 0000000000000..8c0096ee41352 --- /dev/null +++ b/tests/classes/unset_public_properties.phpt @@ -0,0 +1,74 @@ +--TEST-- +Un-setting public instance properties causes magic methods to be called when trying to access them from outside class scope +--FILE-- +$name = $value; + echo '__set ' . $name . ' to ' . $value; + } + + public function __isset($name) + { + echo '__isset ' . $name; + return isset($this->$name); + } + + public function getTestProperty() + { + return $this->testProperty; + } + + public function setTestProperty($testProperty) + { + $this->testProperty = $testProperty; + } +} + +$o = new Test; + +echo $o->testProperty; +echo "\n"; +isset($o->testProperty); +echo "\n"; +unset($o->testProperty); +isset($o->testProperty); +echo "\n"; +echo $o->testProperty; +echo "\n"; +echo $o->getTestProperty(); +echo "\n"; +echo $o->setTestProperty('new value via setter'); +echo "\n"; +echo $o->testProperty; +echo "\n"; +unset($o->testProperty); +$o->testProperty = 'new value via public access'; +echo "\n"; +isset($o->testProperty); +echo "\n"; +echo $o->testProperty; + +?> +====DONE==== +--EXPECTF-- +property set + +__isset testProperty +__get testProperty +__get testProperty +__set testProperty to new value via setter +new value via setter +__set testProperty to new value via public access + +new value via public access From a594587b1194a338e6d5accacdcba1808f2ef56b Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Wed, 7 Nov 2012 18:56:32 +0100 Subject: [PATCH 2/3] Fixing test according to @jpauli's suggestions --- tests/classes/unset_public_properties.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/classes/unset_public_properties.phpt b/tests/classes/unset_public_properties.phpt index 8c0096ee41352..ead933646a2e0 100644 --- a/tests/classes/unset_public_properties.phpt +++ b/tests/classes/unset_public_properties.phpt @@ -60,7 +60,6 @@ echo "\n"; echo $o->testProperty; ?> -====DONE==== --EXPECTF-- property set From e75764507516d00a83ba6df9e25f454efcf2a2c2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 8 Nov 2012 05:51:01 +0100 Subject: [PATCH 3/3] Adding tests for private and protected properties --- tests/classes/unset_properties.phpt | 154 +++++++++++++++++++++ tests/classes/unset_public_properties.phpt | 73 ---------- 2 files changed, 154 insertions(+), 73 deletions(-) create mode 100644 tests/classes/unset_properties.phpt delete mode 100644 tests/classes/unset_public_properties.phpt diff --git a/tests/classes/unset_properties.phpt b/tests/classes/unset_properties.phpt new file mode 100644 index 0000000000000..7f9b569887dcd --- /dev/null +++ b/tests/classes/unset_properties.phpt @@ -0,0 +1,154 @@ +--TEST-- +Un-setting instance properties causes magic methods to be called when trying to access them from outside the magic +methods themselves. +--FILE-- +$name = $value; + echo '__set "' . $name . '" to "' . $value . '"'; + } + + public function __isset($name) + { + echo '__isset "' . $name . '"'; + return isset($this->$name); + } + + public function getPublicProperty() + { + return $this->publicProperty; + } + + public function setPublicProperty($publicProperty) + { + $this->publicProperty = $publicProperty; + } + + public function unsetProtectedProperty() + { + unset($this->protectedProperty); + } + + public function getProtectedProperty() + { + return $this->protectedProperty; + } + + public function setProtectedProperty($protectedProperty) + { + $this->protectedProperty = $protectedProperty; + } + + public function unsetPrivateProperty() + { + unset($this->privateProperty); + } + + public function getPrivateProperty() + { + return $this->privateProperty; + } + + public function setPrivateProperty($privateProperty) + { + $this->privateProperty = $privateProperty; + } +} + +// verifying public property +$o = new Test; +echo $o->publicProperty; +echo "\n"; +var_export(isset($o->publicProperty)); +echo "\n"; +unset($o->publicProperty); +isset($o->publicProperty); +echo "\n"; +echo $o->publicProperty; +echo "\n"; +echo $o->getPublicProperty(); +echo "\n"; +echo $o->setPublicProperty('new publicProperty value via setter'); +echo "\n"; +echo $o->publicProperty; +echo "\n"; +unset($o->publicProperty); +$o->publicProperty = 'new publicProperty value via public access'; +echo "\n"; +var_export(isset($o->publicProperty)); +echo "\n"; +echo $o->publicProperty; +echo "\n\n"; + +// verifying protected property +echo $o->getProtectedProperty(); +echo "\n"; +$o->unsetProtectedProperty(); +var_export(isset($o->protectedProperty)); +echo "\n"; +echo $o->getProtectedProperty(); +echo "\n"; +echo $o->setProtectedProperty('new protectedProperty value via setter'); +echo "\n"; +var_export(isset($o->protectedProperty)); +echo "\n"; +echo $o->getProtectedProperty(); +echo "\n\n"; + +// verifying private property +echo $o->getPrivateProperty(); +echo "\n"; +$o->unsetPrivateProperty(); +var_export(isset($o->privateProperty)); +echo "\n"; +echo $o->getPrivateProperty(); +echo "\n"; +echo $o->setPrivateProperty('new privateProperty value via setter'); +echo "\n"; +var_export(isset($o->privateProperty)); +echo "\n"; +echo $o->getPrivateProperty(); +echo "\n\n"; + +?> + +--EXPECTF-- +publicProperty set +true +__isset "publicProperty" +__get "publicProperty" +__get "publicProperty" +__set "publicProperty" to "new publicProperty value via setter" +new publicProperty value via setter +__set "publicProperty" to "new publicProperty value via public access" +true +new publicProperty value via public access + +protectedProperty set +__isset "protectedProperty"__isset "protectedProperty"false +__get "protectedProperty" +__set "protectedProperty" to "new protectedProperty value via setter" +__isset "protectedProperty"true +new protectedProperty value via setter + +privateProperty set +__isset "privateProperty"__isset "privateProperty"false +__get "privateProperty" +__set "privateProperty" to "new privateProperty value via setter" +__isset "privateProperty"true +new privateProperty value via setter \ No newline at end of file diff --git a/tests/classes/unset_public_properties.phpt b/tests/classes/unset_public_properties.phpt deleted file mode 100644 index ead933646a2e0..0000000000000 --- a/tests/classes/unset_public_properties.phpt +++ /dev/null @@ -1,73 +0,0 @@ ---TEST-- -Un-setting public instance properties causes magic methods to be called when trying to access them from outside class scope ---FILE-- -$name = $value; - echo '__set ' . $name . ' to ' . $value; - } - - public function __isset($name) - { - echo '__isset ' . $name; - return isset($this->$name); - } - - public function getTestProperty() - { - return $this->testProperty; - } - - public function setTestProperty($testProperty) - { - $this->testProperty = $testProperty; - } -} - -$o = new Test; - -echo $o->testProperty; -echo "\n"; -isset($o->testProperty); -echo "\n"; -unset($o->testProperty); -isset($o->testProperty); -echo "\n"; -echo $o->testProperty; -echo "\n"; -echo $o->getTestProperty(); -echo "\n"; -echo $o->setTestProperty('new value via setter'); -echo "\n"; -echo $o->testProperty; -echo "\n"; -unset($o->testProperty); -$o->testProperty = 'new value via public access'; -echo "\n"; -isset($o->testProperty); -echo "\n"; -echo $o->testProperty; - -?> ---EXPECTF-- -property set - -__isset testProperty -__get testProperty -__get testProperty -__set testProperty to new value via setter -new value via setter -__set testProperty to new value via public access - -new value via public access