feature #14077 [VarDumper] Add VarDumperTestCase and related trait (nicolas-grekas)
This PR was merged into the 2.7 branch.
Discussion
----------
[VarDumper] Add VarDumperTestCase and related trait
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
This allows writing assertions that use the dump of a variable for comparison.
Commits
-------
57da9ae
[VarDumper] Add VarDumperTestCase and related trait
This commit is contained in:
commit
ac5c5a5fd2
45
src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php
Normal file
45
src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\VarDumper\Test;
|
||||||
|
|
||||||
|
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||||
|
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
abstract class VarDumperTestCase extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
public function assertDumpEquals($dump, $data, $message = '')
|
||||||
|
{
|
||||||
|
$this->assertSame($dump, $this->getVarDumperDump($data), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertDumpMatchesFormat($dump, $data, $message = '')
|
||||||
|
{
|
||||||
|
$this->assertStringMatchesFormat($dump, $this->getVarDumperDump($data), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getVarDumperDump($data)
|
||||||
|
{
|
||||||
|
$h = fopen('php://memory', 'r+b');
|
||||||
|
$cloner = new VarCloner();
|
||||||
|
$dumper = new CliDumper($h);
|
||||||
|
$dumper->setColors(false);
|
||||||
|
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
|
||||||
|
fseek($h, 0);
|
||||||
|
$data = stream_get_contents($h);
|
||||||
|
fclose($h);
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
45
src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Normal file
45
src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\VarDumper\Test;
|
||||||
|
|
||||||
|
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||||
|
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
trait VarDumperTestTrait
|
||||||
|
{
|
||||||
|
public function assertDumpEquals($dump, $data, $message = '')
|
||||||
|
{
|
||||||
|
$this->assertSame($dump, $this->getVarDumperDump($data), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertDumpMatchesFormat($dump, $data, $message = '')
|
||||||
|
{
|
||||||
|
$this->assertStringMatchesFormat($dump, $this->getVarDumperDump($data), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getVarDumperDump($data)
|
||||||
|
{
|
||||||
|
$h = fopen('php://memory', 'r+b');
|
||||||
|
$cloner = new VarCloner();
|
||||||
|
$dumper = new CliDumper($h);
|
||||||
|
$dumper->setColors(false);
|
||||||
|
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
|
||||||
|
fseek($h, 0);
|
||||||
|
$data = stream_get_contents($h);
|
||||||
|
fclose($h);
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
}
|
@ -13,11 +13,12 @@ namespace Symfony\Component\VarDumper\Tests;
|
|||||||
|
|
||||||
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||||
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||||
|
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Nicolas Grekas <p@tchwork.com>
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
*/
|
*/
|
||||||
class CliDumperTest extends \PHPUnit_Framework_TestCase
|
class CliDumperTest extends VarDumperTestCase
|
||||||
{
|
{
|
||||||
public function testGet()
|
public function testGet()
|
||||||
{
|
{
|
||||||
@ -115,21 +116,10 @@ EOTXT
|
|||||||
}
|
}
|
||||||
|
|
||||||
$var = xml_parser_create();
|
$var = xml_parser_create();
|
||||||
$ref = (int) $var;
|
|
||||||
|
|
||||||
$dumper = new CliDumper();
|
$this->assertDumpEquals(
|
||||||
$dumper->setColors(false);
|
|
||||||
$cloner = new VarCloner();
|
|
||||||
|
|
||||||
$data = $cloner->cloneVar($var);
|
|
||||||
$out = fopen('php://memory', 'r+b');
|
|
||||||
$dumper->dump($data, $out);
|
|
||||||
rewind($out);
|
|
||||||
$out = stream_get_contents($out);
|
|
||||||
|
|
||||||
$this->assertSame(
|
|
||||||
<<<EOTXT
|
<<<EOTXT
|
||||||
:xml {@{$ref}
|
:xml {
|
||||||
current_byte_index: 0
|
current_byte_index: 0
|
||||||
current_column_number: 1
|
current_column_number: 1
|
||||||
current_line_number: 1
|
current_line_number: 1
|
||||||
@ -138,7 +128,7 @@ EOTXT
|
|||||||
|
|
||||||
EOTXT
|
EOTXT
|
||||||
,
|
,
|
||||||
$out
|
$var
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +180,7 @@ EOTXT
|
|||||||
|
|
||||||
EOTXT
|
EOTXT
|
||||||
,
|
,
|
||||||
$out
|
$var
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,17 +196,7 @@ EOTXT
|
|||||||
|
|
||||||
$var = $this->getSpecialVars();
|
$var = $this->getSpecialVars();
|
||||||
|
|
||||||
$dumper = new CliDumper();
|
$this->assertDumpEquals(
|
||||||
$dumper->setColors(false);
|
|
||||||
$cloner = new VarCloner();
|
|
||||||
|
|
||||||
$data = $cloner->cloneVar($var);
|
|
||||||
$out = fopen('php://memory', 'r+b');
|
|
||||||
$dumper->dump($data, $out);
|
|
||||||
rewind($out);
|
|
||||||
$out = stream_get_contents($out);
|
|
||||||
|
|
||||||
$this->assertSame(
|
|
||||||
<<<EOTXT
|
<<<EOTXT
|
||||||
array:3 [
|
array:3 [
|
||||||
0 => array:1 [
|
0 => array:1 [
|
||||||
|
Reference in New Issue
Block a user