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:
Fabien Potencier 2015-03-30 17:41:11 +02:00
commit ac5c5a5fd2
3 changed files with 97 additions and 27 deletions

View 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;
}
}

View 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;
}
}

View File

@ -13,11 +13,12 @@ namespace Symfony\Component\VarDumper\Tests;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class CliDumperTest extends \PHPUnit_Framework_TestCase
class CliDumperTest extends VarDumperTestCase
{
public function testGet()
{
@ -115,21 +116,10 @@ EOTXT
}
$var = xml_parser_create();
$ref = (int) $var;
$dumper = new CliDumper();
$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(
$this->assertDumpEquals(
<<<EOTXT
:xml {@{$ref}
:xml {
current_byte_index: 0
current_column_number: 1
current_line_number: 1
@ -138,7 +128,7 @@ EOTXT
EOTXT
,
$out
$var
);
}
@ -190,7 +180,7 @@ EOTXT
EOTXT
,
$out
$var
);
}
@ -206,17 +196,7 @@ EOTXT
$var = $this->getSpecialVars();
$dumper = new CliDumper();
$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(
$this->assertDumpEquals(
<<<EOTXT
array:3 [
0 => array:1 [