VarDumperTestTraitTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Tests\Test;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Cloner\Stub;
  13. use Symfony\Component\VarDumper\Dumper\CliDumper;
  14. use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
  15. class VarDumperTestTraitTest extends TestCase
  16. {
  17. use VarDumperTestTrait;
  18. public function testItComparesLargeData()
  19. {
  20. $howMany = 700;
  21. $data = array_fill_keys(range(0, $howMany), ['a', 'b', 'c', 'd']);
  22. $expected = sprintf("array:%d [\n", $howMany + 1);
  23. for ($i = 0; $i <= $howMany; ++$i) {
  24. $expected .= <<<EODUMP
  25. $i => array:4 [
  26. 0 => "a"
  27. 1 => "b"
  28. 2 => "c"
  29. 3 => "d"
  30. ]\n
  31. EODUMP;
  32. }
  33. $expected .= "]\n";
  34. $this->assertDumpEquals($expected, $data);
  35. }
  36. public function testAllowsNonScalarExpectation()
  37. {
  38. $this->assertDumpEquals(new \ArrayObject(['bim' => 'bam']), new \ArrayObject(['bim' => 'bam']));
  39. }
  40. public function testItCanBeConfigured()
  41. {
  42. $this->setUpVarDumper($casters = [
  43. \DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
  44. $stub->class = 'DateTime';
  45. return ['date' => $date->format('d/m/Y')];
  46. },
  47. ], CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
  48. $this->assertSame(CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR, $this->varDumperConfig['flags']);
  49. $this->assertSame($casters, $this->varDumperConfig['casters']);
  50. $this->assertDumpEquals(<<<DUMP
  51. [
  52. 1,
  53. 2,
  54. DateTime {
  55. +date: "09/07/2019"
  56. }
  57. ]
  58. DUMP
  59. , [1, 2, new \DateTime('2019-07-09T0:00:00+00:00')]);
  60. $this->tearDownVarDumper();
  61. $this->assertNull($this->varDumperConfig['flags']);
  62. $this->assertSame([], $this->varDumperConfig['casters']);
  63. }
  64. }