]> granicus.if.org Git - php/commitdiff
Adding tests for private and protected properties
authorMarco Pivetta <ocramius@gmail.com>
Thu, 8 Nov 2012 04:51:01 +0000 (05:51 +0100)
committerLars Strojny <lstrojny@php.net>
Sun, 2 Dec 2012 18:47:18 +0000 (19:47 +0100)
tests/classes/unset_properties.phpt [new file with mode: 0644]
tests/classes/unset_public_properties.phpt [deleted file]

diff --git a/tests/classes/unset_properties.phpt b/tests/classes/unset_properties.phpt
new file mode 100644 (file)
index 0000000..7f9b569
--- /dev/null
@@ -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
\ 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 (file)
index ead9336..0000000
+++ /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--
-<?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