Skip to content

Commit

Permalink
Various missing tests, coverage stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Nov 16, 2024
1 parent 14b7dcc commit 371e67a
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 17 deletions.
Binary file modified build/kint.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Parser/ClassHooksPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getTypes(): array
public function getTriggers(): int
{
if (!KINT_PHP84) {
return Parser::TRIGGER_NONE;
return Parser::TRIGGER_NONE; // @codeCoverageIgnore
}

return Parser::TRIGGER_SUCCESS;
Expand Down
14 changes: 5 additions & 9 deletions src/Parser/HtmlPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Kint\Value\AbstractValue;
use Kint\Value\Context\BaseContext;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;

class HtmlPlugin extends AbstractPlugin implements PluginCompleteInterface
{
Expand All @@ -57,8 +58,8 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa

try {
$html = HTMLDocument::createFromString($var, LIBXML_NOERROR);
} catch (DOMException $e) {
return $v;
} catch (DOMException $e) { // @codeCoverageIgnore
return $v; // @codeCoverageIgnore
}

$c = $v->getContext();
Expand All @@ -72,17 +73,12 @@ public function parseComplete(&$var, AbstractValue $v, int $trigger): AbstractVa

$out = $this->getParser()->parse($html->childNodes, $base);
$iter = $out->getRepresentation('iterator');
$contents = [];

if ($out->flags & AbstractValue::FLAG_DEPTH_LIMIT) {
$out->flags |= AbstractValue::FLAG_GENERATED;
$contents = [$out];
$v->addRepresentation(new ValueRepresentation('HTML', $out), 0);
} elseif ($iter instanceof ContainerRepresentation) {
$contents = $iter->getContents();
}

if ($contents) {
$v->addRepresentation(new ContainerRepresentation('HTML', $contents), 0);
$v->addRepresentation(new ContainerRepresentation('HTML', $iter->getContents()), 0);
}

return $v;
Expand Down
6 changes: 2 additions & 4 deletions src/Value/Context/MethodContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ class MethodContext extends ClassDeclaredContext
/**
* Whether the method was inherited from a parent class or interface.
*
* It's important to note that while private parent methods are considered
* "inherited" they are shown to the user as "Declared" in the
* CallableDefinitionRepresentation since they can't be overridden, while
* the class name is prepended for clarity.
* It's important to note that we never show static methods as
* "inherited" except when abstract via an interface.
*/
public bool $inherited = false;

Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/TestAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Kint\Test\Fixtures;

abstract class TestAbstract implements TestInterface
{
}
10 changes: 7 additions & 3 deletions tests/Fixtures/TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Kint\Test\Fixtures;

class TestClass
class TestClass extends TestAbstract
{
const DUMP_FILE = __FILE__;
const DUMP_LINE = 60;
const DUMP_LINE = 64;

public $pub;
protected $pro;
Expand All @@ -28,7 +28,11 @@ public function __construct()
$this->pri = array('pri');
}

private static function staticMethod()
public function normalMethod()
{
}

public static function staticMethod()
{
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/TestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Kint\Test\Fixtures;

interface TestInterface
{
public function normalMethod();
public static function staticMethod();
}
29 changes: 29 additions & 0 deletions tests/Parser/ClassHooksPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use Kint\Test\Fixtures\Php84TestClass;
use Kint\Test\KintTestCase;
use Kint\Value\Context\BaseContext;
use Kint\Value\FixedWidthValue;
use Kint\Value\InstanceValue;

/**
* @coversNothing
Expand Down Expand Up @@ -153,4 +155,31 @@ public function testParse()
$this->assertFalse($props['f'][0]->getCallableBag()->return_reference);
$this->assertSame('$value', $props['f'][0]->getCallableBag()->getParams());
}

/**
* @covers \Kint\Parser\ClassHooksPlugin::parseComplete
*/
public function testBadParse()
{
if (!KINT_PHP84) {
$this->markTestSkipped('Not testing ClassHooksPlugin below PHP 8.4');
}

$p = new Parser(5);
$chp = new ClassHooksPlugin($p);
$p->addPlugin($chp);

$b = new BaseContext('$v');
$b->access_path = '$v';
$v = new Php84ChildTestClass();
$o = new InstanceValue($b, \get_class($v), \spl_object_hash($v), \spl_object_id($v));

$out = $chp->parseComplete($v, $o, Parser::TRIGGER_SUCCESS);
$this->assertSame($o, $out);

$o = new FixedWidthValue($b, 123);

$out = $chp->parseComplete($v, $o, Parser::TRIGGER_SUCCESS);
$this->assertSame($o, $out);
}
}
29 changes: 29 additions & 0 deletions tests/Parser/HtmlPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Kint\Value\Context\BaseContext;
use Kint\Value\DomNodeValue;
use Kint\Value\Representation\ContainerRepresentation;
use Kint\Value\Representation\ValueRepresentation;

/**
* @coversNothing
Expand Down Expand Up @@ -124,6 +125,34 @@ public function testParse()
$this->assertCount(2, $r->getContents());
}

/**
* @covers \Kint\Parser\HtmlPlugin::parseComplete
*/
public function testParseDepthLimit()
{
if (!KINT_PHP84) {
$this->markTestSkipped('Not testing HtmlPlugin below PHP 8.4');
}

$p = new Parser(1);
$p->addPlugin(new HtmlPlugin($p));
$p->addPlugin(new DomPlugin($p));

$v = [self::TEST_HTML];
$b = new BaseContext('$v');
$b->access_path = '$v';

$o = $p->parse($v, clone $b);
$o = $o->getContents()[0];

$this->assertEquals(false, $o->flags & AbstractValue::FLAG_GENERATED);
$r = $o->getRepresentation('html');
$this->assertInstanceOf(ValueRepresentation::class, $r);

$this->assertEquals(true, $r->getValue()->flags & AbstractValue::FLAG_GENERATED);
$this->assertEquals(true, $r->getValue()->flags & AbstractValue::FLAG_DEPTH_LIMIT);
}

/**
* @covers \Kint\Parser\HtmlPlugin::getTriggers
* @covers \Kint\Parser\HtmlPlugin::getTypes
Expand Down
65 changes: 65 additions & 0 deletions tests/Value/Context/MethodContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace Kint\Test\Value\Context;

use Kint\Parser\AbstractPlugin;
use Kint\Parser\PluginInterface;
use Kint\Test\Fixtures\TestClass;
use Kint\Test\KintTestCase;
use Kint\Value\Context\BaseContext;
Expand All @@ -40,6 +42,63 @@
*/
class MethodContextTest extends KintTestCase
{
/**
* @covers \Kint\Value\Context\MethodContext::__construct
*/
public function testConstruct()
{
$r = new ReflectionMethod(AbstractPlugin::class, 'getTypes');
$m = new MethodContext($r);
$this->assertSame('getTypes', $m->name);
$this->assertSame(PluginInterface::class, $m->owner_class);
$this->assertSame(ClassDeclaredContext::ACCESS_PUBLIC, $m->access);
$this->assertSame(1, $m->depth);
$this->assertFalse($m->static);
$this->assertTrue($m->abstract);
$this->assertFalse($m->final);
$this->assertFalse($m->inherited);

$r = new ReflectionMethod(TestClass::class, 'staticMethod');
$m = new MethodContext($r);
$this->assertSame('staticMethod', $m->name);
$this->assertSame(TestClass::class, $m->owner_class);
$this->assertSame(ClassDeclaredContext::ACCESS_PUBLIC, $m->access);
$this->assertTrue($m->static);
$this->assertFalse($m->abstract);
$this->assertFalse($m->final);
$this->assertFalse($m->inherited);

$r = new ReflectionMethod(TestClass::class, 'arrayHint');
$m = new MethodContext($r);
$this->assertSame('arrayHint', $m->name);
$this->assertSame(TestClass::class, $m->owner_class);
$this->assertSame(ClassDeclaredContext::ACCESS_PRIVATE, $m->access);
$this->assertFalse($m->static);
$this->assertFalse($m->abstract);
$this->assertFalse($m->final);
$this->assertFalse($m->inherited);

$r = new ReflectionMethod(TestClass::class, 'finalMethod');
$m = new MethodContext($r);
$this->assertSame('finalMethod', $m->name);
$this->assertSame(TestClass::class, $m->owner_class);
$this->assertSame(ClassDeclaredContext::ACCESS_PUBLIC, $m->access);
$this->assertFalse($m->static);
$this->assertFalse($m->abstract);
$this->assertTrue($m->final);
$this->assertFalse($m->inherited);

$r = new ReflectionMethod(TestClass::class, 'mix');
$m = new MethodContext($r);
$this->assertSame('mix', $m->name);
$this->assertSame(TestClass::class, $m->owner_class);
$this->assertSame(ClassDeclaredContext::ACCESS_PROTECTED, $m->access);
$this->assertTrue($m->static);
$this->assertFalse($m->abstract);
$this->assertTrue($m->final);
$this->assertFalse($m->inherited);
}

/**
* @covers \Kint\Value\Context\MethodContext::setAccessPathFromParent
*/
Expand All @@ -55,12 +114,18 @@ public function testSetAccessPathFromParent()
$m->setAccessPathFromParent($o);
$this->assertSame('new \\Kint\\Test\\Fixtures\\TestClass()', $m->getAccessPath());

$m->setAccessPathFromParent(null);
$this->assertNull($m->getAccessPath());

$reflection = new ReflectionMethod(TestClass::class, 'staticMethod');
$m = new MethodContext($reflection);
$this->assertNull($m->getAccessPath());
$m->setAccessPathFromParent($o);
$this->assertSame('\\Kint\\Test\\Fixtures\\TestClass::staticMethod()', $m->getAccessPath());

$m->setAccessPathFromParent(null);
$this->assertSame('\\Kint\\Test\\Fixtures\\TestClass::staticMethod()', $m->getAccessPath());

$reflection = new ReflectionMethod(TestClass::class, 'finalMethod');
$m = new MethodContext($reflection);
$this->assertNull($m->getAccessPath());
Expand Down

0 comments on commit 371e67a

Please sign in to comment.