123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\VarDumper\Tests\Cloner;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\VarDumper\Cloner\VarCloner;
- /**
- * @author Nicolas Grekas <p@tchwork.com>
- */
- class VarClonerTest extends TestCase
- {
- public function testMaxIntBoundary()
- {
- $data = array(PHP_INT_MAX => 123);
- $cloner = new VarCloner();
- $clone = $cloner->cloneVar($data);
- $expected = <<<EOTXT
- Symfony\Component\VarDumper\Cloner\Data Object
- (
- [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
- (
- [0] => Array
- (
- [0] => Array
- (
- [1] => 1
- )
- )
- [1] => Array
- (
- [%s] => 123
- )
- )
- [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
- [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- )
- EOTXT;
- $this->assertSame(sprintf($expected, PHP_INT_MAX), print_r($clone, true));
- }
- public function testClone()
- {
- $json = json_decode('{"1":{"var":"val"},"2":{"var":"val"}}');
- $cloner = new VarCloner();
- $clone = $cloner->cloneVar($json);
- $expected = <<<EOTXT
- Symfony\Component\VarDumper\Cloner\Data Object
- (
- [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
- (
- [0] => Array
- (
- [0] => Symfony\Component\VarDumper\Cloner\Stub Object
- (
- [type] => 4
- [class] => stdClass
- [value] =>
- [cut] => 0
- [handle] => %i
- [refCount] => 0
- [position] => 1
- [attr] => Array
- (
- )
- )
- )
- [1] => Array
- (
- [\000+\0001] => Symfony\Component\VarDumper\Cloner\Stub Object
- (
- [type] => 4
- [class] => stdClass
- [value] =>
- [cut] => 0
- [handle] => %i
- [refCount] => 0
- [position] => 2
- [attr] => Array
- (
- )
- )
- [\000+\0002] => Symfony\Component\VarDumper\Cloner\Stub Object
- (
- [type] => 4
- [class] => stdClass
- [value] =>
- [cut] => 0
- [handle] => %i
- [refCount] => 0
- [position] => 3
- [attr] => Array
- (
- )
- )
- )
- [2] => Array
- (
- [\000+\000var] => val
- )
- [3] => Array
- (
- [\000+\000var] => val
- )
- )
- [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
- [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- )
- EOTXT;
- $this->assertStringMatchesFormat($expected, print_r($clone, true));
- }
- public function testLimits()
- {
- // Level 0:
- $data = array(
- // Level 1:
- array(
- // Level 2:
- array(
- // Level 3:
- 'Level 3 Item 0',
- 'Level 3 Item 1',
- 'Level 3 Item 2',
- 'Level 3 Item 3',
- ),
- array(
- 'Level 3 Item 4',
- 'Level 3 Item 5',
- 'Level 3 Item 6',
- ),
- array(
- 'Level 3 Item 7',
- ),
- ),
- array(
- array(
- 'Level 3 Item 8',
- ),
- 'Level 2 Item 0',
- ),
- array(
- 'Level 2 Item 1',
- ),
- 'Level 1 Item 0',
- array(
- // Test setMaxString:
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
- 'SHORT',
- ),
- );
- $cloner = new VarCloner();
- $cloner->setMinDepth(2);
- $cloner->setMaxItems(5);
- $cloner->setMaxString(20);
- $clone = $cloner->cloneVar($data);
- $expected = <<<EOTXT
- Symfony\Component\VarDumper\Cloner\Data Object
- (
- [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
- (
- [0] => Array
- (
- [0] => Array
- (
- [2] => 1
- )
- )
- [1] => Array
- (
- [0] => Array
- (
- [2] => 2
- )
- [1] => Array
- (
- [2] => 3
- )
- [2] => Array
- (
- [2] => 4
- )
- [3] => Level 1 Item 0
- [4] => Array
- (
- [2] => 5
- )
- )
- [2] => Array
- (
- [0] => Array
- (
- [2] => 6
- )
- [1] => Array
- (
- [0] => 2
- [2] => 7
- )
- [2] => Array
- (
- [0] => 1
- [2] => 0
- )
- )
- [3] => Array
- (
- [0] => Array
- (
- [0] => 1
- [2] => 0
- )
- [1] => Level 2 Item 0
- )
- [4] => Array
- (
- [0] => Level 2 Item 1
- )
- [5] => Array
- (
- [0] => Symfony\Component\VarDumper\Cloner\Stub Object
- (
- [type] => 2
- [class] => 2
- [value] => ABCDEFGHIJKLMNOPQRST
- [cut] => 6
- [handle] => 0
- [refCount] => 0
- [position] => 0
- [attr] => Array
- (
- )
- )
- [1] => SHORT
- )
- [6] => Array
- (
- [0] => Level 3 Item 0
- [1] => Level 3 Item 1
- [2] => Level 3 Item 2
- [3] => Level 3 Item 3
- )
- [7] => Array
- (
- [0] => Level 3 Item 4
- )
- )
- [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
- [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- )
- EOTXT;
- $this->assertStringMatchesFormat($expected, print_r($clone, true));
- }
- public function testJsonCast()
- {
- if (2 == ini_get('xdebug.overload_var_dump')) {
- $this->markTestSkipped('xdebug is active');
- }
- $data = (array) json_decode('{"1":{}}');
- $cloner = new VarCloner();
- $clone = $cloner->cloneVar($data);
- $expected = <<<'EOTXT'
- object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
- ["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- array(2) {
- [0]=>
- array(1) {
- [0]=>
- array(1) {
- [1]=>
- int(1)
- }
- }
- [1]=>
- array(1) {
- ["1"]=>
- object(Symfony\Component\VarDumper\Cloner\Stub)#%i (8) {
- ["type"]=>
- int(4)
- ["class"]=>
- string(8) "stdClass"
- ["value"]=>
- NULL
- ["cut"]=>
- int(0)
- ["handle"]=>
- int(%i)
- ["refCount"]=>
- int(0)
- ["position"]=>
- int(0)
- ["attr"]=>
- array(0) {
- }
- }
- }
- }
- ["position":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- int(0)
- ["key":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- int(0)
- ["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- int(20)
- ["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- int(-1)
- ["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
- int(-1)
- }
- EOTXT;
- ob_start();
- var_dump($clone);
- $this->assertStringMatchesFormat(\PHP_VERSION_ID >= 70200 ? str_replace('"1"', '1', $expected) : $expected, ob_get_clean());
- }
- public function testCaster()
- {
- $cloner = new VarCloner(array(
- '*' => function ($obj, $array) {
- return array('foo' => 123);
- },
- __CLASS__ => function ($obj, $array) {
- ++$array['foo'];
- return $array;
- },
- ));
- $clone = $cloner->cloneVar($this);
- $expected = <<<EOTXT
- Symfony\Component\VarDumper\Cloner\Data Object
- (
- [data:Symfony\Component\VarDumper\Cloner\Data:private] => Array
- (
- [0] => Array
- (
- [0] => Symfony\Component\VarDumper\Cloner\Stub Object
- (
- [type] => 4
- [class] => %s
- [value] =>
- [cut] => 0
- [handle] => %i
- [refCount] => 0
- [position] => 1
- [attr] => Array
- (
- )
- )
- )
- [1] => Array
- (
- [foo] => 124
- )
- )
- [position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
- [maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
- [maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- [useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
- )
- EOTXT;
- $this->assertStringMatchesFormat($expected, print_r($clone, true));
- }
- }
|