VarDumperTestTrait.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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($expected, $data, $filter = 0, $message = '')
  19. {
  20. $this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  21. }
  22. public function assertDumpMatchesFormat($expected, $data, $filter = 0, $message = '')
  23. {
  24. $this->assertStringMatchesFormat($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  25. }
  26. protected function getDump($data, $key = null, $filter = 0)
  27. {
  28. $flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
  29. $flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
  30. $flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
  31. $cloner = new VarCloner();
  32. $cloner->setMaxItems(-1);
  33. $dumper = new CliDumper(null, null, $flags);
  34. $dumper->setColors(false);
  35. $data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
  36. if (null !== $key && null === $data = $data->seek($key)) {
  37. return;
  38. }
  39. return rtrim($dumper->dump($data, true));
  40. }
  41. private function prepareExpectation($expected, $filter)
  42. {
  43. if (!\is_string($expected)) {
  44. $expected = $this->getDump($expected, null, $filter);
  45. }
  46. return rtrim($expected);
  47. }
  48. }