From 2727e9bb6d1b5e0c3c4331e9e999d0efdd75bae8 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Thu, 8 May 2014 12:12:43 +0100 Subject: [PATCH] Unit test and improved docs on get_public_properties --- composer.json | 3 +++ inc/GlobalFunctions.php | 4 ++++ tests/GlobalFunctionsTest.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/composer.json b/composer.json index 03fd7c8..189a28d 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,9 @@ "require": { "php": ">=5.4.0" }, + "require-dev": { + "phpunit/phpunit": "*" + }, "autoload": { "files": [ "inc/GlobalFunctions.php", diff --git a/inc/GlobalFunctions.php b/inc/GlobalFunctions.php index 9c60e16..14d8e08 100644 --- a/inc/GlobalFunctions.php +++ b/inc/GlobalFunctions.php @@ -422,6 +422,10 @@ function idp($object, $property, $default = null) /** * Return an array with only the public properties * + * If calling get_object_vars withing a class, + * will return protected and private properties, + * this function fixes this instance + * * @param object $object Source object * * @return mixed diff --git a/tests/GlobalFunctionsTest.php b/tests/GlobalFunctionsTest.php index de608ec..5084393 100644 --- a/tests/GlobalFunctionsTest.php +++ b/tests/GlobalFunctionsTest.php @@ -261,4 +261,32 @@ public function testBetween() $this->assertFalse(between(2, 1, 2, false)); } + + public function testProperties() + { + $expect = ['name' => null, 'age' => null]; + $class = new PropertyClass(); + $this->assertNotEquals($expect, $class->objectVars()); + $this->assertEquals($expect, $class->publicVars()); + $this->assertEquals($expect, get_object_vars($class)); + $this->assertEquals($expect, get_public_properties($class)); + } +} + +class PropertyClass +{ + public $name; + public $age; + protected $_gender; + private $_ryan; + + public function objectVars() + { + return get_object_vars($this); + } + + public function publicVars() + { + return get_public_properties($this); + } }