VarDumperTestTrait.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Test;
  11. use Symfony\Component\VarDumper\Cloner\VarCloner;
  12. use Symfony\Component\VarDumper\Dumper\CliDumper;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. trait VarDumperTestTrait
  17. {
  18. public function assertDumpEquals($dump, $data, $message = '')
  19. {
  20. $this->assertSame(rtrim($dump), $this->getVarDumperDump($data), $message);
  21. }
  22. public function assertDumpMatchesFormat($dump, $data, $message = '')
  23. {
  24. $this->assertStringMatchesFormat(rtrim($dump), $this->getVarDumperDump($data), $message);
  25. }
  26. private function getVarDumperDump($data)
  27. {
  28. $h = fopen('php://memory', 'r+b');
  29. $cloner = new VarCloner();
  30. $cloner->setMaxItems(-1);
  31. $dumper = new CliDumper($h);
  32. $dumper->setColors(false);
  33. $dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
  34. $data = stream_get_contents($h, -1, 0);
  35. fclose($h);
  36. return rtrim($data);
  37. }
  38. }