| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\VarDumper\Test;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
|
|
15 |
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
19 |
*/
|
|
|
20 |
trait VarDumperTestTrait
|
|
|
21 |
{
|
|
|
22 |
/**
|
|
|
23 |
* @internal
|
|
|
24 |
*/
|
|
|
25 |
private array $varDumperConfig = [
|
|
|
26 |
'casters' => [],
|
|
|
27 |
'flags' => null,
|
|
|
28 |
];
|
|
|
29 |
|
|
|
30 |
protected function setUpVarDumper(array $casters, int $flags = null): void
|
|
|
31 |
{
|
|
|
32 |
$this->varDumperConfig['casters'] = $casters;
|
|
|
33 |
$this->varDumperConfig['flags'] = $flags;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @after
|
|
|
38 |
*/
|
|
|
39 |
protected function tearDownVarDumper(): void
|
|
|
40 |
{
|
|
|
41 |
$this->varDumperConfig['casters'] = [];
|
|
|
42 |
$this->varDumperConfig['flags'] = null;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function assertDumpEquals(mixed $expected, mixed $data, int $filter = 0, string $message = '')
|
|
|
46 |
{
|
|
|
47 |
$this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function assertDumpMatchesFormat(mixed $expected, mixed $data, int $filter = 0, string $message = '')
|
|
|
51 |
{
|
|
|
52 |
$this->assertStringMatchesFormat($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
protected function getDump(mixed $data, string|int $key = null, int $filter = 0): ?string
|
|
|
56 |
{
|
|
|
57 |
if (null === $flags = $this->varDumperConfig['flags']) {
|
|
|
58 |
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
|
|
|
59 |
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
|
|
|
60 |
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$cloner = new VarCloner();
|
|
|
64 |
$cloner->addCasters($this->varDumperConfig['casters']);
|
|
|
65 |
$cloner->setMaxItems(-1);
|
|
|
66 |
$dumper = new CliDumper(null, null, $flags);
|
|
|
67 |
$dumper->setColors(false);
|
|
|
68 |
$data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
|
|
|
69 |
if (null !== $key && null === $data = $data->seek($key)) {
|
|
|
70 |
return null;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
return rtrim($dumper->dump($data, true));
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
private function prepareExpectation(mixed $expected, int $filter): string
|
|
|
77 |
{
|
|
|
78 |
if (!\is_string($expected)) {
|
|
|
79 |
$expected = $this->getDump($expected, null, $filter);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
return rtrim($expected);
|
|
|
83 |
}
|
|
|
84 |
}
|