Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #282: Fix dumper #283

Closed
wants to merge 1 commit into from
Closed

Fix #282: Fix dumper #283

wants to merge 1 commit into from

Conversation

samdark
Copy link
Member

@samdark samdark commented Sep 25, 2024

Q A
Is bugfix? ✔️
New feature?
Breaks BC?

@samdark samdark requested a review from xepozz September 25, 2024 20:27
@samdark
Copy link
Member Author

samdark commented Sep 25, 2024

@olegbaturin would you please test it?

@@ -56,7 +56,7 @@ public function asJson(int $depth = 50, bool $format = false): string
public function asJsonObjectsMap(int $depth = 50, bool $prettyPrint = false): string
{
$this->buildObjectsCache($this->variable, $depth);
return $this->asJsonInternal($this->objects, $prettyPrint, $depth, 1, true);
return $this->asJsonInternal($this->objects, $prettyPrint, 2, 1, true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be that 2 is more than enough and 1 would do as well.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 would do as well

2 is min, because property of the root object, that is object, must be converted to the expected string

if ($depth <= $level || array_key_exists($variable::class, $this->excludedClasses)) {

but this works only if dumped variable object or function, if it's an array, than it must be dumped deeper (or no?).

@samdark
Copy link
Member Author

samdark commented Sep 25, 2024

@xepozz seem to fail tests.

@olegbaturin
Copy link

olegbaturin commented Sep 26, 2024

/**
 * @dataProvider loopAsJsonObjectMapDataProvider
 */
public function testLoopAsJsonObjectsMap(mixed $var, int $depth, $expectedResult): void
{
    $exportResult = Dumper::create($var)->asJsonObjectsMap($depth);
    $this->assertEquals($expectedResult, $exportResult);
}

public static function loopAsJsonObjectMapDataProvider(): iterable
{
    // parent->child->parent structure
    $o1 = new \stdClass();
    $o1->id = 'o1';
    $o2 = new \stdClass();
    $o2->id = 'o2';
    $o2->o1 = $o1;
    $o1->o2 = $o2;

    // 5 is a min level to reproduce buggy dumping of parent->child->parent structure
    $head1 = self::getNested(5, $o1);
    // can't imagine the expected result
    yield 'nested loop - object' => [
        $head1,
        5,
        <<<S
        {}
        S,
    ];

    // array loop must be 1 level deeper to parse loop objects
    $head2 = self::getNested(6, [$o1, $o2]);
    // can't imagine the expected result
    yield 'nested loop - array' => [
        $head2,
        6,
        <<<S
        {}
        S,
    ];

    $head3 = new \stdClass();
    $head3->id = '1';
    $head3->lv12 = [
        'id' => 2,
        'loop' => $o1,
    ];
    // can't imagine the expected result
    yield 'nested loop to object->array' => [
        $head3,
        3,
        <<<S
        {}
        S,
    ];
}

private static function getNested(int $depth, mixed $data): object
{
    $head = $lvl = new \stdClass();
    $lvl->id = 'lvl1';

    for ($i = 2; $i <= $depth; $i++) {
        $nested = new \stdClass();
        $nested->id = 'lvl'.$i;
        $lvl->{'lvl'.$i} = $nested;
        $lvl = $nested;
    }
    $lvl->loop = $data;

    return $head;
}

@olegbaturin
Copy link

@samdark I've added some tests for dumping of loop and one test for your solution.

If you show me expected results for those cases, I can make a fix.

@olegbaturin
Copy link

@olegbaturin would you please test it?

this fixes the issue, but changes some dumped structures,

@samdark
Copy link
Member Author

samdark commented Sep 26, 2024

@xepozz need your help with expected results.

@olegbaturin
Copy link

olegbaturin commented Sep 27, 2024

https://github.com/olegbaturin/yii3-debug/tree/282-fix-dumper

@samdark, @xepozz check the output to /runtime/debug please, look like this is a fix. Results in the test errors will become the expected results.

@samdark
Copy link
Member Author

samdark commented Sep 27, 2024

#284

@samdark samdark closed this Sep 27, 2024
@samdark
Copy link
Member Author

samdark commented Sep 27, 2024

@olegbaturin I like your fix more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants