VarDumperTestTrait.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, $filter = 0, $message = '')
  19. {
  20. if (\is_string($filter)) {
  21. @trigger_error(sprintf('The $message argument of the "%s()" method at the 3rd position is deprecated since Symfony 3.4 and will be moved at the 4th position in 4.0.', __METHOD__), E_USER_DEPRECATED);
  22. $message = $filter;
  23. $filter = 0;
  24. }
  25. $this->assertSame(rtrim($dump), $this->getDump($data, null, $filter), $message);
  26. }
  27. public function assertDumpMatchesFormat($dump, $data, $filter = 0, $message = '')
  28. {
  29. if (\is_string($filter)) {
  30. @trigger_error(sprintf('The $message argument of the "%s()" method at the 3rd position is deprecated since Symfony 3.4 and will be moved at the 4th position in 4.0.', __METHOD__), E_USER_DEPRECATED);
  31. $message = $filter;
  32. $filter = 0;
  33. }
  34. $this->assertStringMatchesFormat(rtrim($dump), $this->getDump($data, null, $filter), $message);
  35. }
  36. protected function getDump($data, $key = null, $filter = 0)
  37. {
  38. $flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
  39. $flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
  40. $cloner = new VarCloner();
  41. $cloner->setMaxItems(-1);
  42. $dumper = new CliDumper(null, null, $flags);
  43. $dumper->setColors(false);
  44. $data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
  45. if (null !== $key && null === $data = $data->seek($key)) {
  46. return;
  47. }
  48. return rtrim($dumper->dump($data, true));
  49. }
  50. }