Created
February 29, 2012 09:10
-
-
Save thekid/1939343 to your computer and use it in GitHub Desktop.
Recipe: Refactor public to private
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| uses('unittest.TestCase'); | |
| abstract class FixtureTest extends TestCase { | |
| protected $fixture= NULL; | |
| protected abstract function newFixtureInstance(); | |
| public function setUp() { | |
| $this->fixture= $this->newFixtureInstance(); | |
| } | |
| #[@test] | |
| public function accessParams() { | |
| $this->assertEquals(array(), $this->fixture->params); | |
| } | |
| #[@test] | |
| public function modifyParams() { | |
| $value= array('a' => 'b'); | |
| $this->fixture->params= $value; | |
| $this->assertEquals($value, $this->fixture->params); | |
| } | |
| #[@test] | |
| public function writeParam() { | |
| $this->fixture->params['a']= 'b'; | |
| $this->assertEquals(array('a' => 'b'), $this->fixture->params); | |
| } | |
| #[@test] | |
| public function readParam() { | |
| $this->fixture->params= array('a' => 'b'); | |
| $this->assertEquals('b', $this->fixture->params['a']); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class FixtureV1 extends Object { | |
| public $params= array(); | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| uses('FixtureTest', 'FixtureV1', 'FixtureV2'); | |
| class FixtureV1Test extends FixtureTest { | |
| protected function newFixtureInstance() { | |
| return new FixtureV1(); | |
| } | |
| #[@test] | |
| public function serializationRoundtrip() { | |
| $this->fixture->params= array('a' => 'b'); | |
| $serialized= serialize($this->fixture); | |
| // Simulate system boundary | |
| $serialized= str_replace('"FixtureV1"', '"FixtureV2"', $serialized); | |
| var_dump($serialized); | |
| $v2= unserialize($serialized); | |
| $this->assertEquals($this->fixture->params, $v2->params); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class FixtureV2 extends Object { | |
| protected $_params= array(); | |
| protected $__serialization= FALSE; | |
| public function __set($name, $value) { | |
| if ('params' === $name) { | |
| if ($this->__serialization) { | |
| $this->{'params'}= $value; // "Invent" property | |
| } else { | |
| $this->_params= $value; | |
| } | |
| return; | |
| } | |
| parent::__set($name, $value); | |
| } | |
| public function &__get($name) { | |
| if ('params' === $name) { | |
| return $this->_params; | |
| } | |
| return parent::__get($name); | |
| } | |
| public function __sleep() { | |
| $this->__serialization= TRUE; | |
| $this->params= $this->_params; | |
| $this->__serialization= FALSE; | |
| // The filter is to prevent public properties with leading "\0" | |
| // being "invented". For performance, this could be omitted. | |
| return array_filter(array_keys((array)$this), function($key) { | |
| return "\0" !== $key{0}; | |
| }); | |
| } | |
| public function __wakeup() { | |
| $this->_params= $this->params; | |
| unset($this->params); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| uses('FixtureTest', 'FixtureV2'); | |
| class FixtureV2Test extends FixtureTest { | |
| protected function newFixtureInstance() { | |
| return new FixtureV2(); | |
| } | |
| #[@test] | |
| public function serializationRoundtrip() { | |
| $this->fixture->params= array('a' => 'b'); | |
| $serialized= serialize($this->fixture); | |
| // Simulate system boundary | |
| $serialized= str_replace('"FixtureV2"', '"FixtureV1"', $serialized); | |
| var_dump($serialized); | |
| $v1= unserialize($serialized); | |
| $this->assertEquals($this->fixture->params, $v1->params); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment