Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrukowski committed Apr 21, 2018
1 parent 7c12072 commit 22c0def
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 0.11.2 (2018-04-21)

* [Fixes from 1.0.1](https://github.com/awesomite/var-dumper/blob/v1.0.1/CHANGELOG.md#101---2018-04-21)

## 0.11.1 (2018-01-10)

* Fixed bug - `Awesomite\VarDumper\InternalVarDumper::dumpAsString`
Expand Down
29 changes: 28 additions & 1 deletion src/Properties/AbstractProperties.php
Expand Up @@ -21,7 +21,34 @@ abstract class AbstractProperties implements PropertiesInterface
protected function getDeclaredProperties()
{
$reflection = new \ReflectionObject($this->object);
$result = array();

return $reflection->getProperties();
if ($reflection->hasMethod('__get')) {
foreach (\array_keys(\get_object_vars($this->object)) as $name) {
$result[] = $reflection->getProperty($name);
}

return $result;
}

foreach ($reflection->getProperties() as $property) {
$continue = false;
\set_error_handler(
function () use (&$continue) {
$continue = true;
},
E_NOTICE
);
$property->setAccessible(true);
$property->getValue($this->object);
\restore_error_handler();
if ($continue) {
continue;
}

$result[] = $property;
}

return $result;
}
}
25 changes: 25 additions & 0 deletions tests/MagicMethods/GetterObject.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\MagicMethods;

/**
* @internal
*/
class GetterObject extends RemovedProperty
{
private $c = 'c';

public function __get($name)
{
return $this->$name;
}
}
111 changes: 111 additions & 0 deletions tests/MagicMethods/MagicMethodsTest.php
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\MagicMethods;

use Awesomite\VarDumper\BaseTestCase;
use Awesomite\VarDumper\LightVarDumper;
use Awesomite\VarDumper\Properties\Properties;
use Awesomite\VarDumper\Properties\PropertyInterface;

/**
* @internal
*/
class MagicMethodsTest extends BaseTestCase
{
public function testDump()
{
$dumper = new LightVarDumper();
$object = new RemovedProperty();

$expected
= <<<'EXPECTED'
(2) {
private $a => “a”
private $b => “b”
}
EXPECTED;
$this->assertContains($expected, $dumper->dumpAsString($object));

$parts = array(
'private $b =>',
);
if (!\defined('HHVM_VERSION') && \version_compare(PHP_VERSION, '7.0') >= 0) {
$parts[] = '(1) {';
} else {
$parts[] = '(2) {';
$parts[] = 'private $a => NULL';
}
$object->without('a');
foreach ($parts as $part) {
$this->assertContains($part, $dumper->dumpAsString($object));
}

$object->c = 'c';
$parts = array(
'private $b =>',
'$c =>',
);
if (!\defined('HHVM_VERSION') && \version_compare(PHP_VERSION, '7.0') >= 0) {
$parts[] = '(2) {';
} else {
$parts[] = '(3) {';
$parts[] = 'private $a => NULL';
}
foreach ($parts as $part) {
$this->assertContains($part, $dumper->dumpAsString($object));
}

$getter = new GetterObject();
$this->assertContains('(0) {}', $dumper->dumpAsString($getter));
}

/**
* @dataProvider providerAbstractProperties
*
* @param $object
* @param array $expectedProperties
* @param array $diff
*/
public function testAbstractProperties($object, array $expectedProperties, $diff)
{
$reader = new Properties($object);

if (!\defined('HHVM_VERSION') && \version_compare(PHP_VERSION, '7.0') >= 0) {
$this->assertSame(\count($expectedProperties), \count($reader->getProperties()));
} else {
$this->assertSame(\count($expectedProperties) + \count($diff), \count($reader->getProperties()));
}

foreach ($expectedProperties as $expectedProperty) {
$contains = false;
foreach ($reader->getProperties() as $property) {
/** @var PropertyInterface $property */
if ($contains = $property->getName() === $expectedProperty) {
$contains = true;
break;
}
}
$this->assertTrue($contains);
}
}

public function providerAbstractProperties()
{
return array(
array(new RemovedProperty(), array('a', 'b'), array()),
array(new GetterObject(), array(), array()),
array(RemovedProperty::createWithout(array('a')), array('b'), array('a')),
array(RemovedProperty::createWithout(array('a'))->with('c'), array('b', 'c'), array('a')),
array(RemovedProperty::createWithout(array('a', 'b')), array(), array('a', 'b')),
);
}
}
51 changes: 51 additions & 0 deletions tests/MagicMethods/RemovedProperty.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\MagicMethods;

/**
* @internal
*/
class RemovedProperty
{
private $a = 'a';

private $b = 'b';

public function without($name)
{
unset($this->$name);

return $this;
}

public function with($name, $value = null)
{
$this->$name = $value;

return $this;
}

/**
* @param string[] $props
*
* @return static
*/
public static function createWithout(array $props)
{
$result = new static();
foreach ($props as $prop) {
$result->without($prop);
}

return $result;
}
}

0 comments on commit 22c0def

Please sign in to comment.