VarDumperTestTrait.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public function assertDumpEquals(mixed $expected, mixed $data, int $filter = 0, string $message = ''): void
  44. {
  45. $this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  46. }
  47. public function assertDumpMatchesFormat(mixed $expected, mixed $data, int $filter = 0, string $message = ''): void
  48. {
  49. $this->assertStringMatchesFormat($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
  50. }
  51. protected function getDump(mixed $data, string|int|null $key = null, int $filter = 0): ?string
  52. {
  53. if (null === $flags = $this->varDumperConfig['flags']) {
  54. $flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
  55. $flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
  56. $flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
  57. }
  58. $cloner = new VarCloner();
  59. $cloner->addCasters($this->varDumperConfig['casters']);
  60. $cloner->setMaxItems(-1);
  61. $dumper = new CliDumper(null, null, $flags);
  62. $dumper->setColors(false);
  63. $data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
  64. if (null !== $key && null === $data = $data->seek($key)) {
  65. return null;
  66. }
  67. return rtrim($dumper->dump($data, true));
  68. }
  69. private function prepareExpectation(mixed $expected, int $filter): string
  70. {
  71. if (!\is_string($expected)) {
  72. $expected = $this->getDump($expected, null, $filter);
  73. }
  74. return rtrim($expected);
  75. }
  76. }