DataTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Cloner;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Caster\Caster;
  13. use Symfony\Component\VarDumper\Caster\ClassStub;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. class DataTest extends TestCase
  17. {
  18. public function testBasicData()
  19. {
  20. $values = array(1 => 123, 4.5, 'abc', null, false);
  21. $data = $this->cloneVar($values);
  22. $clonedValues = array();
  23. $this->assertInstanceOf(Data::class, $data);
  24. $this->assertCount(count($values), $data);
  25. $this->assertFalse(isset($data->{0}));
  26. $this->assertFalse(isset($data[0]));
  27. foreach ($data as $k => $v) {
  28. $this->assertTrue(isset($data->{$k}));
  29. $this->assertTrue(isset($data[$k]));
  30. $this->assertSame(gettype($values[$k]), $data->seek($k)->getType());
  31. $this->assertSame($values[$k], $data->seek($k)->getValue());
  32. $this->assertSame($values[$k], $data->{$k});
  33. $this->assertSame($values[$k], $data[$k]);
  34. $this->assertSame((string) $values[$k], (string) $data->seek($k));
  35. $clonedValues[$k] = $v->getValue();
  36. }
  37. $this->assertSame($values, $clonedValues);
  38. }
  39. public function testObject()
  40. {
  41. $data = $this->cloneVar(new \Exception('foo'));
  42. $this->assertSame('Exception', $data->getType());
  43. $this->assertSame('foo', $data->message);
  44. $this->assertSame('foo', $data->{Caster::PREFIX_PROTECTED.'message'});
  45. $this->assertSame('foo', $data['message']);
  46. $this->assertSame('foo', $data[Caster::PREFIX_PROTECTED.'message']);
  47. $this->assertStringMatchesFormat('Exception (count=%d)', (string) $data);
  48. }
  49. public function testArray()
  50. {
  51. $values = array(array(), array(123));
  52. $data = $this->cloneVar($values);
  53. $this->assertSame($values, $data->getValue(true));
  54. $children = $data->getValue();
  55. $this->assertInternalType('array', $children);
  56. $this->assertInstanceOf(Data::class, $children[0]);
  57. $this->assertInstanceOf(Data::class, $children[1]);
  58. $this->assertEquals($children[0], $data[0]);
  59. $this->assertEquals($children[1], $data[1]);
  60. $this->assertSame($values[0], $children[0]->getValue(true));
  61. $this->assertSame($values[1], $children[1]->getValue(true));
  62. }
  63. public function testStub()
  64. {
  65. $data = $this->cloneVar(array(new ClassStub('stdClass')));
  66. $data = $data[0];
  67. $this->assertSame('string', $data->getType());
  68. $this->assertSame('stdClass', $data->getValue());
  69. $this->assertSame('stdClass', (string) $data);
  70. }
  71. public function testHardRefs()
  72. {
  73. $values = array(array());
  74. $values[1] = &$values[0];
  75. $values[2][0] = &$values[2];
  76. $data = $this->cloneVar($values);
  77. $this->assertSame(array(), $data[0]->getValue());
  78. $this->assertSame(array(), $data[1]->getValue());
  79. $this->assertEquals(array($data[2]->getValue()), $data[2]->getValue(true));
  80. $this->assertSame('array (count=3)', (string) $data);
  81. }
  82. private function cloneVar($value)
  83. {
  84. $cloner = new VarCloner();
  85. return $cloner->cloneVar($value);
  86. }
  87. }