8000 feature #25332 [VarDumper] Allow VarDumperTestTrait expectation to be… · symfony/symfony@cd4c849 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd4c849

Browse files
committed
feature #25332 [VarDumper] Allow VarDumperTestTrait expectation to be non-scalar (romainneutron)
This PR was merged into the 4.1-dev branch. Discussion ---------- [VarDumper] Allow VarDumperTestTrait expectation to be non-scalar | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT At the moment, when using the `VarDumperTestTrait` in unit test, expecting data object is as follow: ```php class Toto { private $foo; public function __construct($foo) { $this->foo = $foo; } } class MyTest extends \PHPUnit_Framework_TestCase { use Symfony\Component\VarDumper\Test\VarDumperTestTrait; public function dummyTest() { $expected = <<<EOEXPECTED Profiler\Tests\Model\CallGraph\Toto { -foo: "baz" } EOEXPECTED; $this->assertDumpEquals($expected, new Toto('baz')); } } ``` The same test could be easily written like this with this change: ```php public function dummyTest() { $this->assertDumpEquals(new Toto('baz'), new Toto('baz')); } ``` Commits ------- 6b5ab90 [VarDumper] Allow VarDumperTestTrait expectation to be non-scalar
2 parents 8a12eb3 + 6b5ab90 commit cd4c849

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
*/
2020
trait VarDumperTestTrait
2121
{
22-
public function assertDumpEquals($dump, $data, $filter = 0, $message = '')
22+
public function assertDumpEquals($expected, $data, $filter = 0, $message = '')
2323
{
24-
$this->assertSame(rtrim($dump), $this->getDump($data, null, $filter), $message);
24+
$this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
2525
}
2626

27-
public function assertDumpMatchesFormat($dump, $data, $filter = 0, $message = '')
27+
public function assertDumpMatchesFormat($expected, $data, $filter = 0, $message = '')
2828
{
29-
$this->assertStringMatchesFormat(rtrim($dump), $this->getDump($data, null, $filter), $message);
29+
$this->assertStringMatchesFormat($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
3030
}
3131

3232
protected function getDump($data, $key = null, $filter = 0)
@@ -45,4 +45,13 @@ protected function getDump($data, $key = null, $filter = 0)
4545

4646
return rtrim($dumper->dump($data, true));
4747
}
48+
49+
private function prepareExpectation($expected, $filter)
50+
{
51+
if (!is_string($expected)) {
52+
$expected = $this->getDump($expected, null, $filter);
53+
}
54+
55+
return rtrim($expected);
56+
}
4857
}

src/Symfony/Component/VarDumper/Tests/Test/VarDumperTestTraitTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public function testItComparesLargeData()
3838

3939
$this->assertDumpEquals($expected, $data);
4040
}
41+
42+
public function testAllowsNonScalarExpectation()
43+
{
44+
$this->assertDumpEquals(new \ArrayObject(array('bim' => 'bam')), new \ArrayObject(array('bim' => 'bam')));
45+
}
4146
}

0 commit comments

Comments
 (0)
0