Skip to content

Commit

Permalink
Unit test and improved docs on get_public_properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed May 8, 2014
1 parent 392ad76 commit 2727e9b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"require": {
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "*"
},
"autoload": {
"files": [
"inc/GlobalFunctions.php",
Expand Down
4 changes: 4 additions & 0 deletions inc/GlobalFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/GlobalFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 2727e9b

Please sign in to comment.