diff --git a/src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php b/src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php new file mode 100644 index 0000000000..42a915e8ed --- /dev/null +++ b/src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php @@ -0,0 +1,45 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php b/src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php new file mode 100644 index 0000000000..130447bcf6 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php @@ -0,0 +1,45 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index e788b7f201..6e70f6b01b 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -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 */ -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( <<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( << array:1 [