rob-revisited/ide/tests/DebuggerPropertyTest.php

55 lines
2.5 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Debug\Debugger;
use JumPHP\Ide\Debug\XmlNode;
use PHPUnit\Framework\TestCase;
/** What the Variables tree and the hover inspector get for a variable (no Xdebug needed: DBGp's XML as Xdebug sends it). */
final class DebuggerPropertyTest extends TestCase
{
/** @return array<string, mixed> */
private function property(string $xml, bool $withChildren = true): array
{
$node = XmlNode::parse('<response xmlns="urn:debugger_protocol_v1">' . $xml . '</response>');
self::assertNotNull($node);
$debugger = new Debugger('/tmp', static function (): void {
});
$method = new \ReflectionMethod($debugger, 'property');
return $method->invoke($debugger, $node->child('property'), $withChildren);
}
public function testABigArrayTellsItsSizeAndItsPaging(): void
{
$p = $this->property('<property name="$list" fullname="$list" type="array" children="1" numchildren="100" page="1" pagesize="32">'
. '<property name="32" fullname="$list[32]" type="int"><![CDATA[7]]></property></property>');
self::assertSame('array(100)', $p['value']);
self::assertSame(100, $p['size']);
self::assertSame(1, $p['page']);
self::assertSame(32, $p['pagesize']);
self::assertCount(1, $p['items']);
self::assertSame('$list[32]', $p['items'][0]['fullname']);
self::assertArrayNotHasKey('size', $p['items'][0], 'a scalar has no children to page');
}
public function testAPropertyHasItsVisibility(): void
{
$p = $this->property('<property name="hp" fullname="$hero->hp" facet="private static" type="int"><![CDATA[3]]></property>');
self::assertSame('private static', $p['facet']);
$q = $this->property('<property name="$x" fullname="$x" type="int"><![CDATA[3]]></property>');
self::assertArrayNotHasKey('facet', $q);
}
public function testALongStringKeepsItsWholeTextAndLength(): void
{
$text = str_repeat('ab', 300);
$p = $this->property('<property name="$s" fullname="$s" type="string" size="600" encoding="base64"><![CDATA[' . base64_encode($text) . ']]></property>');
self::assertSame(600, $p['length']);
self::assertSame($text, $p['full']);
self::assertLessThan(600, mb_strlen($p['value']));
$short = $this->property('<property name="$s" fullname="$s" type="string" size="2"><![CDATA[hi]]></property>');
self::assertSame('hi', $short['value']);
self::assertArrayNotHasKey('full', $short);
}
}