DumperTest.php 4.34 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Tools;
4 5 6 7 8 9 10

use ArrayIterator;
use ArrayObject;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Doctrine\DBAL\Tools\Dumper;
11
use PHPUnit\Framework\TestCase;
Sergei Morozov's avatar
Sergei Morozov committed
12
use stdClass;
13 14 15 16
use function print_r;
use function strpos;
use function substr;

17
class DumperTest extends TestCase
18
{
19
    public function testExportObject() : void
20
    {
Sergei Morozov's avatar
Sergei Morozov committed
21
        $obj      = new stdClass();
22 23 24 25 26 27 28
        $obj->foo = 'bar';
        $obj->bar = 1234;

        $var = Dumper::export($obj, 2);
        self::assertEquals('stdClass', $var->__CLASS__);
    }

29
    public function testExportObjectWithReference() : void
30 31 32 33 34 35 36 37 38 39 40 41
    {
        $foo = 'bar';
        $bar = ['foo' => & $foo];
        $baz = (object) $bar;

        $var      = Dumper::export($baz, 2);
        $baz->foo = 'tab';

        self::assertEquals('bar', $var->foo);
        self::assertEquals('tab', $bar['foo']);
    }

42
    public function testExportArray() : void
43 44 45 46 47 48 49 50
    {
        $array              = ['a' => 'b', 'b' => ['c', 'd' => ['e', 'f']]];
        $var                = Dumper::export($array, 2);
        $expected           = $array;
        $expected['b']['d'] = 'Array(2)';
        self::assertEquals($expected, $var);
    }

51
    public function testExportDateTime() : void
52 53 54 55 56 57 58 59
    {
        $obj = new DateTime('2010-10-10 10:10:10', new DateTimeZone('UTC'));

        $var = Dumper::export($obj, 2);
        self::assertEquals('DateTime', $var->__CLASS__);
        self::assertEquals('2010-10-10T10:10:10+00:00', $var->date);
    }

60
    public function testExportDateTimeImmutable() : void
61 62 63 64 65 66 67 68
    {
        $obj = new DateTimeImmutable('2010-10-10 10:10:10', new DateTimeZone('UTC'));

        $var = Dumper::export($obj, 2);
        self::assertEquals('DateTimeImmutable', $var->__CLASS__);
        self::assertEquals('2010-10-10T10:10:10+00:00', $var->date);
    }

69
    public function testExportDateTimeZone() : void
70 71 72 73 74 75 76 77
    {
        $obj = new DateTimeImmutable('2010-10-10 12:34:56', new DateTimeZone('Europe/Rome'));

        $var = Dumper::export($obj, 2);
        self::assertEquals('DateTimeImmutable', $var->__CLASS__);
        self::assertEquals('2010-10-10T12:34:56+02:00', $var->date);
    }

78
    public function testExportArrayTraversable() : void
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    {
        $obj = new ArrayObject(['foobar']);

        $var = Dumper::export($obj, 2);
        self::assertContains('foobar', $var->__STORAGE__);

        $it = new ArrayIterator(['foobar']);

        $var = Dumper::export($it, 5);
        self::assertContains('foobar', $var->__STORAGE__);
    }

    /**
     * @param string[] $expected
     *
     * @dataProvider provideAttributesCases
     */
96
    public function testExportParentAttributes(TestAsset\ParentClass $class, array $expected) : void
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    {
        $print_r_class    = print_r($class, true);
        $print_r_expected = print_r($expected, true);

        $print_r_class    = substr($print_r_class, strpos($print_r_class, '('));
        $print_r_expected = substr($print_r_expected, strpos($print_r_expected, '('));

        self::assertSame($print_r_class, $print_r_expected);

        $var = Dumper::export($class, 3);
        $var = (array) $var;
        unset($var['__CLASS__']);

        self::assertSame($expected, $var);
    }

113 114 115 116
    /**
     * @return mixed[][]
     */
    public static function provideAttributesCases() : iterable
117 118 119 120 121 122 123
    {
        return [
            'different-attributes' => [
                new TestAsset\ChildClass(),
                [
                    'childPublicAttribute' => 4,
                    'childProtectedAttribute:protected' => 5,
124
                    'childPrivateAttribute:Doctrine\DBAL\Tests\Tools\TestAsset\ChildClass:private' => 6,
125 126
                    'parentPublicAttribute' => 1,
                    'parentProtectedAttribute:protected' => 2,
127
                    'parentPrivateAttribute:Doctrine\DBAL\Tests\Tools\TestAsset\ParentClass:private' => 3,
128 129 130 131 132 133 134
                ],
            ],
            'same-attributes' => [
                new TestAsset\ChildWithSameAttributesClass(),
                [
                    'parentPublicAttribute' => 4,
                    'parentProtectedAttribute:protected' => 5,
135 136
                    'parentPrivateAttribute:Doctrine\DBAL\Tests\Tools\TestAsset\ChildWithSameAttributesClass:private' => 6,
                    'parentPrivateAttribute:Doctrine\DBAL\Tests\Tools\TestAsset\ParentClass:private' => 3,
137 138 139 140 141
                ],
            ],
        ];
    }
}