Skip to content

Commit

Permalink
Custom formatters refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed Jan 24, 2021
1 parent c5088d1 commit 8030081
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
17 changes: 7 additions & 10 deletions library/Mockery.php
Expand Up @@ -662,23 +662,20 @@ private static function objectToArray($object, $nesting = 3)
return array('...');
}

$defaultFormatter = function ($object, $nesting) {
return array('properties' => self::extractInstancePublicProperties($object, $nesting));
};

$class = get_class($object);
$customFormatterClass = self::getConfiguration()->findObjectFormatter($class);

$formatter = self::getConfiguration()->getObjectFormatter($class, $defaultFormatter);

$array = array(
'class' => $class,
'identity' => '#' . md5(spl_object_hash($object))
);

if ($customFormatterClass) {
$customFormatter = self::getConfiguration()->getObjectFormatter($customFormatterClass);
$array = array_merge($array, $customFormatter($object));
} else {
$array = array_merge(
$array,
array('properties' => self::extractInstancePublicProperties($object, $nesting))
);
}
$array = array_merge($array, $formatter($object, $nesting));

return $array;
}
Expand Down
11 changes: 3 additions & 8 deletions library/Mockery/Configuration.php
Expand Up @@ -224,7 +224,7 @@ public function setObjectFormatter($class, $formatterCallback)
$this->_objectFormatters[$class] = $formatterCallback;
}

public function findObjectFormatter($class)
public function getObjectFormatter($class, $defaultFormatter)
{
$parentClass = $class;
do {
Expand All @@ -234,14 +234,9 @@ public function findObjectFormatter($class)
$classesAndInterfaces = array_merge($classes, class_implements($class));
foreach ($classesAndInterfaces as $type) {
if (isset($this->_objectFormatters[$type])) {
return $type;
return $this->_objectFormatters[$type];
}
}
return null;
}

public function getObjectFormatter($class)
{
return $this->_objectFormatters[$class];
return $defaultFormatter;
}
}
21 changes: 15 additions & 6 deletions tests/Mockery/WithCustomFormatterExpectationTest.php
Expand Up @@ -27,8 +27,9 @@ public function setUp(): void
{
\Mockery::getConfiguration()->setObjectFormatter(
'ClassWithCustomFormatter',
function ($object) {
function ($object, $nesting) {
return array(
"formatter" => 'ClassWithCustomFormatter',
"properties" => array(
"stringProperty" => $object->stringProperty
),
Expand All @@ -40,8 +41,9 @@ function ($object) {
);
\Mockery::getConfiguration()->setObjectFormatter(
'InterfaceWithCustomFormatter',
function ($object) {
function ($object, $nesting) {
return array(
"formatter" => 'InterfaceWithCustomFormatter',
"properties" => array(
"stringProperty" => $object->stringProperty
),
Expand All @@ -54,17 +56,24 @@ function ($object) {
}

/**
* @dataProvider findObjectFormatterDataProvider
* @dataProvider getObjectFormatterDataProvider
*/
public function testFindObjectFormatter($object, $expected)
public function testGetObjectFormatter($object, $expected)
{
$defaultFormatter = function ($class, $nesting) {
return null;
};

$formatter = \Mockery::getConfiguration()->getObjectFormatter(get_class($object), $defaultFormatter);
$formatted = $formatter($object, 1);

$this->assertEquals(
$expected,
\Mockery::getConfiguration()->findObjectFormatter(get_class($object))
$formatted ? $formatted['formatter'] : null
);
}

public function findObjectFormatterDataProvider()
public function getObjectFormatterDataProvider()
{
return array(
array(
Expand Down

0 comments on commit 8030081

Please sign in to comment.