Skip to content

Commit

Permalink
Don't extract static properties, as they won't get serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
EdeMeijer committed Sep 7, 2014
1 parent ecf2301 commit 081f515
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 35 deletions.
23 changes: 17 additions & 6 deletions src/EdeMeijer/SerializeDebugger/ObjectPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ private function initializeParentPrivateProperties(ReflectionClass $reflector)
*/
private function getAccessibleReflectionProperties(ReflectionClass $reflector)
{
return $reflector->getProperties(
ReflectionProperty::IS_PRIVATE |
ReflectionProperty::IS_PROTECTED |
ReflectionProperty::IS_PUBLIC
);
return $this->getWithoutStatic($reflector->getProperties());
}

/**
Expand All @@ -125,6 +121,21 @@ private function getAccessibleReflectionProperties(ReflectionClass $reflector)
*/
private function getPrivateReflectionProperties(ReflectionClass $reflector)
{
return $reflector->getProperties(ReflectionProperty::IS_PRIVATE);
return $this->getWithoutStatic($reflector->getProperties(ReflectionProperty::IS_PRIVATE));
}

/**
* @param ReflectionProperty[] $properties
* @return ReflectionProperty[]
*/
private function getWithoutStatic(array $properties)
{
$filtered = [];
foreach ($properties as $property) {
if (!$property->isStatic()) {
$filtered[] = $property;
}
}
return $filtered;
}
}
7 changes: 7 additions & 0 deletions test/EdeMeijer/SerializeDebugger/Test/Mocks/TestSubClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ class TestSubClass extends TestBaseClass
public $publicSubVar = 789;
/** @var string */
public $overriddenBaseVar = 'def';

/** @var string */
public static $publicStaticSubVar = 'ghi';
/** @var string */
protected static $protectedStaticSubVar = 'jkl';
/** @var string */
private static $privateStaticSubVar = 'jkl';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace EdeMeijer\SerializeDebugger\Test;


use EdeMeijer\SerializeDebugger\ObjectPropertyExtractor;
use EdeMeijer\SerializeDebugger\Test\Mocks\TestSubClass;

class ObjectPropertyExtractorTest extends \PHPUnit_Framework_TestCase
{
public function testExtraction()
{
require_once __DIR__ . '/Mocks/TestBaseClass.php';
require_once __DIR__ . '/Mocks/TestSubClass.php';

$objWithInheritance = new TestSubClass();

$SUT = new ObjectPropertyExtractor($objWithInheritance);

$extractedProperties = $SUT->getProperties();

// We expect 7 properties, no static properties
$this->assertCount(7, $extractedProperties);
// Check if all expected properties are indexed
$this->assertContains('privateSubVar', $extractedProperties);
$this->assertContains('protectedSubVar', $extractedProperties);
$this->assertContains('publicSubVar', $extractedProperties);
$this->assertContains('overriddenBaseVar', $extractedProperties);
$this->assertContains('protectedBaseVar', $extractedProperties);
$this->assertContains('publicBaseVar', $extractedProperties);
$this->assertContains('privateBaseVar', $extractedProperties);
// Check if value of overridden property is the one of the sub class
$overriddenBaseVar = $SUT->getValue('overriddenBaseVar');
$this->assertEquals('def', $overriddenBaseVar);
}
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace EdeMeijer\SerializeDebugger\Test;
namespace EdeMeijer\SerializeDebugger\Test\Unit;

use EdeMeijer\SerializeDebugger\Debugger;
use EdeMeijer\SerializeDebugger\Test\Mocks\TestSubClass;
use stdClass;

class DebuggerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -63,29 +62,6 @@ public function testItHandlesCircularReferencesCorrectly()
$this->assertSame($nodes[0], $nodes[1]->getChildNodes()['aRef']);
}

public function testItReadsAllKindsOfObjectPropertiesCorrectly()
{
require_once __DIR__ . '/Mocks/TestBaseClass.php';
require_once __DIR__ . '/Mocks/TestSubClass.php';

$objWithInheritance = new TestSubClass();
$nodes = $this->SUT->debug($objWithInheritance)->getRawNodes();
$childNodes = $nodes[0]->getChildNodes();

// We expect 8 nodes; the base object and 7 properties
$this->assertCount(8, $nodes);
// Check if all expected properties are indexed
$this->assertArrayHasKey('privateSubVar', $childNodes);
$this->assertArrayHasKey('protectedSubVar', $childNodes);
$this->assertArrayHasKey('publicSubVar', $childNodes);
$this->assertArrayHasKey('overriddenBaseVar', $childNodes);
$this->assertArrayHasKey('protectedBaseVar', $childNodes);
$this->assertArrayHasKey('publicBaseVar', $childNodes);
$this->assertArrayHasKey('privateBaseVar', $childNodes);
// Check if value of overridden property is the one of the sub class
$this->assertEquals('def', $childNodes['overriddenBaseVar']->getData());
}

/**
* @return array
*/
Expand All @@ -98,7 +74,11 @@ public function basicClassificationProvider()
[null, $base . 'PrimitiveType'],
[true, $base . 'PrimitiveType'],
[[], $base . 'ArrayType'],
[function () {}, $base . 'ClosureType'],
[
function () {
},
$base . 'ClosureType'
],
[new stdClass(), $base . 'ObjectType'],
[new Debugger(), $base . 'ObjectType'],
[fopen(__FILE__, 'r'), $base . 'ResourceType'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace EdeMeijer\SerializeDebugger\Test;
namespace EdeMeijer\SerializeDebugger\Test\Unit;

use EdeMeijer\SerializeDebugger\Result\PlainTextFormatter;
use EdeMeijer\SerializeDebugger\Result\ResultItem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace EdeMeijer\SerializeDebugger\Test;
namespace EdeMeijer\SerializeDebugger\Test\Unit;

use EdeMeijer\SerializeDebugger\Debugger;
use EdeMeijer\SerializeDebugger\Result\ResultItem;
Expand All @@ -10,7 +10,7 @@
use EdeMeijer\SerializeDebugger\Type\PrimitiveType;
use EdeMeijer\SerializeDebugger\Type\ResourceType;

class DebugResultTest extends \PHPUnit_Framework_TestCase
class ResultTest extends \PHPUnit_Framework_TestCase
{
/** @var Debugger */
private $debugger;
Expand Down

0 comments on commit 081f515

Please sign in to comment.