New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom object formatters #766
Merged
davedevelopment
merged 2 commits into
mockery:master
from
MartinMystikJonas:custom-object-formatters
Feb 24, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
<?php | ||
/** | ||
* Mockery | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://github.com/padraic/mockery/master/LICENSE | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to padraic@php.net so we can send you a copy immediately. | ||
* | ||
* @category Mockery | ||
* @package Mockery | ||
* @subpackage UnitTests | ||
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com) | ||
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License | ||
*/ | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class WithCustomFormatterExpectationTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
\Mockery::getConfiguration()->setObjectFormatter( | ||
'ClassWithCustomFormatter', | ||
function ($object, $nesting) { | ||
return array( | ||
"formatter" => 'ClassWithCustomFormatter', | ||
"properties" => array( | ||
"stringProperty" => $object->stringProperty | ||
), | ||
"getters" => array( | ||
"gettedProperty" => $object->getArrayProperty() | ||
) | ||
); | ||
} | ||
); | ||
\Mockery::getConfiguration()->setObjectFormatter( | ||
'InterfaceWithCustomFormatter', | ||
function ($object, $nesting) { | ||
return array( | ||
"formatter" => 'InterfaceWithCustomFormatter', | ||
"properties" => array( | ||
"stringProperty" => $object->stringProperty | ||
), | ||
"getters" => array( | ||
"gettedProperty" => $object->getArrayProperty() | ||
) | ||
); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getObjectFormatterDataProvider | ||
*/ | ||
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, | ||
$formatted ? $formatted['formatter'] : null | ||
); | ||
} | ||
|
||
public function getObjectFormatterDataProvider() | ||
{ | ||
return array( | ||
array( | ||
new \StdClass(), | ||
null | ||
), | ||
array( | ||
new ClassWithoutCustomFormatter(), | ||
null | ||
), | ||
array( | ||
new ClassWithCustomFormatter(), | ||
'ClassWithCustomFormatter' | ||
), | ||
array( | ||
new ClasschildOfWithCustomFormatter(), | ||
'ClassWithCustomFormatter' | ||
), | ||
array( | ||
new ClassImplementsWithCustomFormatter(), | ||
'InterfaceWithCustomFormatter' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider formatObjectsDataProvider | ||
*/ | ||
public function testFormatObjects($obj, $shouldContains, $shouldNotContains) | ||
{ | ||
$string = Mockery::formatObjects(array($obj)); | ||
foreach ($shouldContains as $containString) { | ||
$this->assertStringContainsString($containString, $string); | ||
} | ||
foreach ($shouldNotContains as $containString) { | ||
$this->assertStringNotContainsString($containString, $string); | ||
} | ||
} | ||
|
||
public function formatObjectsDataProvider() | ||
{ | ||
return array( | ||
array( | ||
new ClassWithoutCustomFormatter(), | ||
array( | ||
'stringProperty', | ||
'numberProperty', | ||
'arrayProperty' | ||
), | ||
array( | ||
'privateProperty' | ||
) | ||
), | ||
array( | ||
new ClassWithCustomFormatter(), | ||
array( | ||
'stringProperty', | ||
'gettedProperty' | ||
), | ||
array( | ||
'numberProperty', | ||
'privateProperty' | ||
) | ||
), | ||
array( | ||
new ClassImplementsWithCustomFormatter(), | ||
array( | ||
'stringProperty', | ||
'gettedProperty' | ||
), | ||
array( | ||
'numberProperty', | ||
'privateProperty' | ||
) | ||
), | ||
); | ||
} | ||
} | ||
|
||
class ClassWithoutCustomFormatter | ||
{ | ||
public $stringProperty = "a string"; | ||
public $numberProperty = 123; | ||
public $arrayProperty = array('a', 'nother', 'array'); | ||
private $privateProperty = "private"; | ||
} | ||
|
||
class ClassWithCustomFormatter | ||
{ | ||
public $stringProperty = "a string"; | ||
public $numberProperty = 123; | ||
private $arrayProperty = array('a', 'nother', 'array'); | ||
private $privateProperty = "private"; | ||
|
||
public function getArrayProperty() | ||
{ | ||
return $this->arrayProperty; | ||
} | ||
} | ||
|
||
class ClassChildOfWithCustomFormatter extends ClassWithCustomFormatter | ||
{ | ||
} | ||
|
||
interface InterfaceWithCustomFormatter | ||
{ | ||
} | ||
|
||
class ClassImplementsWithCustomFormatter implements InterfaceWithCustomFormatter | ||
{ | ||
public $stringProperty = "a string"; | ||
public $numberProperty = 123; | ||
private $privateProperty = "private"; | ||
private $arrayProperty = array('a', 'nother', 'array'); | ||
|
||
public function getArrayProperty() | ||
{ | ||
return $this->arrayProperty; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is often useful to be able to compare object identities not only their class + some optional custom info from formatter. Should I remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean why the md5. Just use
spl_object_id
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besause spl_object_id is hard to compare visually becaue usually two instances have most characters of spl_object_hash identical. Using of md5 of spl_object_hash is quite common practice i saw in many libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example I just used it on two objects and I got:
000000002f73941b000000004f808351 and 000000002ffa4e380000000026e9a821
If you use md5 it becomes different at the first sight:
69850868ab17d5cd4d7effccd55ba63c and 43147e68957af1fcbd045979c3c3fda3