Permalink
Browse files

Adding tests for private and protected properties

  • Loading branch information...
Ocramius authored and lstrojny committed Nov 8, 2012
1 parent 12f451c commit 627eb9e3d99c75a4d0309c1321e91a3a7ec4571c
Showing with 154 additions and 73 deletions.
  1. +154 −0 tests/classes/unset_properties.phpt
  2. +0 −73 tests/classes/unset_public_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--
+<?php
+
+class Test
+{
+ public $publicProperty = 'publicProperty set';
+
+ protected $protectedProperty = 'protectedProperty set';
+
+ private $privateProperty = 'privateProperty set';
+
+ public function __get($name)
+ {
+ return '__get "' . $name . '"';
+ }
+
+ public function __set($name, $value)
+ {
+ $this->$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
@@ -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--
-<?php
-
-class Test
-{
- public $testProperty = 'property set';
-
- public function __get($name)
- {
- return '__get ' . $name;
- }
-
- public function __set($name, $value)
- {
- $this->$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

0 comments on commit 627eb9e

Please sign in to comment.