VarDumperTestTrait.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 PHPUnit\Framework\Attributes\After;
  12. use Symfony\Component\VarDumper\Cloner\VarCloner;
  13. use Symfony\Component\VarDumper\Dumper\CliDumper;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. trait VarDumperTestTrait
  18. {
  19. /**
  20. * @internal
  21. */
  22. private array $varDumperConfig = [
  23. 'casters' => [],
  24. 'flags' => null,
  25. ];
  26. /**
  27. * @param array<string, callable> $casters
  28. */
  29. protected function setUpVarDumper(array $casters, ?int $flags = null): void
  30. {
  31. $this->varDumperConfig['casters'] = $casters;
  32. $this->varDumperConfig['flags'] = $flags;
  33. }
  34. /**
  35. * @after
  36. */
  37. #[After]
  38. protected function tearDownVarDumper(): void
  39. {
  40. $this->varDumperConfig['casters'] = [];
  41. $this->varDumperConfig['flags'] = null;
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function assertDumpEquals(mixed $expected, mixed $data, int $filter = 0, string $message = '')
  47. {
  48. $this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function assertDumpMatchesFormat(mixed $expected, mixed $data, int $filter = 0, string $message = '')
  54. {
  55. $this->assertStringMatchesFormat($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  56. }
  57. protected function getDump(mixed $data, string|int|null $key = null, int $filter = 0): ?string
  58. {
  59. if (null === $flags = $this->varDumperConfig['flags']) {
  60. $flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
  61. $flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
  62. $flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
  63. }
  64. $cloner = new VarCloner();
  65. $cloner->addCasters($this->varDumperConfig['casters']);
  66. $cloner->setMaxItems(-1);
  67. $dumper = new CliDumper(null, null, $flags);
  68. $dumper->setColors(false);
  69. $data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
  70. if (null !== $key && null === $data = $data->seek($key)) {
  71. return null;
  72. }
  73. return rtrim($dumper->dump($data, true));
  74. }
  75. private function prepareExpectation(mixed $expected, int $filter): string
  76. {
  77. if (!\is_string($expected)) {
  78. $expected = $this->getDump($expected, null, $filter);
  79. }
  80. return rtrim($expected);
  81. }
  82. }