Skip to content

Commit

Permalink
Added tests for ExceptionDumper
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrukowski committed Nov 16, 2019
1 parent 5baed53 commit 8ee71ed
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/Subdumpers/ExceptionDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ public function dump($throwable)
$body->addIndent($this->container->getConfig()->getIndent());
$result->appendPart($body);

if (\count($array) < $this->container->getConfig()->getMaxChildren()) {
$childDiff = $this->container->getConfig()->getMaxChildren() - \count($array);

if ($childDiff > 0) {
$trace = $this->prepareTrace($throwable->getTrace());
$trace->addIndent($this->container->getConfig()->getIndent());
$result->appendPart($trace);
} elseif (0 === $childDiff) {
$dotsPart = new LinePart('(...)');
$dotsPart->addIndent($this->container->getConfig()->getIndent());
$result->appendPart($dotsPart);
}

$result->appendPart(new LinePart(']}'));
Expand All @@ -72,9 +78,11 @@ public function dump($throwable)
*/
private function prepareTrace(array $trace)
{
// @codeCoverageIgnoreStart
if (!$trace) {
return new LinePart('[trace] => []');
}
// @codeCoverageIgnoreEnd

if ($this->container->getDepth()->getValue() === $this->container->getConfig()->getMaxDepth()) {
return new LinePart('[trace] => [...]');
Expand Down
165 changes: 165 additions & 0 deletions tests/LightVarDumperProviders/ProviderExceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\LightVarDumperProviders;

use Awesomite\VarDumper\LightVarDumper;
use Awesomite\VarDumper\Objects\HasherFactory;

/**
* @internal
*/
final class ProviderExceptions implements \IteratorAggregate
{
public function getIterator()
{
return new \ArrayIterator(\array_merge(
$this->getExceptionWithHiddenStackTrace(),
$this->getExceptionWith1Child(),
$this->getExceptionWith2Children(),
$this->getExceptionWith3Children(),
$this->getExceptionWith4Children()
));
}

private function getExceptionWithHiddenStackTrace()
{
$dumper = new LightVarDumper();
$dumper->setMaxDepth(1);

$exception = new \RuntimeException();

$objectId = HasherFactory::create()->getHashId($exception);

$expected = <<<EXPECTED
object(RuntimeException) #{$objectId} {[
[message] => “”
[code] => 0
[file] => “(...)/tests/LightVarDumperProviders/ProviderExceptions.php:38”
[previous] => NULL
[trace] => [...]
]}
EXPECTED;

return array(
'hidden stack trace' => array(
$dumper, $exception, $expected,
),
);
}

private function getExceptionWith1Child()
{
$dumper = new LightVarDumper();
$dumper->setMaxDepth(1);
$dumper->setMaxChildren(1);

$exception = new \LogicException('My message');

$objectId = HasherFactory::create()->getHashId($exception);

$expected = <<<EXPECTED
object(LogicException) #{$objectId} {[
[message] => “My message”
(...)
]}
EXPECTED;

return array(
'1 child' => array(
$dumper, $exception, $expected,
),
);
}

private function getExceptionWith2Children()
{
$dumper = new LightVarDumper();
$dumper->setMaxDepth(1);
$dumper->setMaxChildren(2);

$exception = new \LogicException('My message');

$objectId = HasherFactory::create()->getHashId($exception);

$expected = <<<EXPECTED
object(LogicException) #{$objectId} {[
[message] => “My message”
[code] => 0
(...)
]}
EXPECTED;

return array(
'2 children' => array(
$dumper, $exception, $expected,
),
);
}

private function getExceptionWith3Children()
{
$dumper = new LightVarDumper();
$dumper->setMaxDepth(1);
$dumper->setMaxChildren(3);

$exception = new \LogicException('My message');

$objectId = HasherFactory::create()->getHashId($exception);

$expected = <<<EXPECTED
object(LogicException) #{$objectId} {[
[message] => “My message”
[code] => 0
[file] => “(...)/tests/LightVarDumperProviders/ProviderExceptions.php:117”
(...)
]}
EXPECTED;

return array(
'3 children' => array(
$dumper, $exception, $expected,
),
);
}

private function getExceptionWith4Children()
{
$dumper = new LightVarDumper();
$dumper->setMaxDepth(1);
$dumper->setMaxChildren(4);

$exception = new \LogicException('My message');

$objectId = HasherFactory::create()->getHashId($exception);

$expected = <<<EXPECTED
object(LogicException) #{$objectId} {[
[message] => “My message”
[code] => 0
[file] => “(...)/tests/LightVarDumperProviders/ProviderExceptions.php:144”
[previous] => NULL
(...)
]}
EXPECTED;

return array(
'4 children' => array(
$dumper, $exception, $expected,
),
);
}
}
38 changes: 28 additions & 10 deletions tests/LightVarDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Awesomite\VarDumper\LightVarDumperProviders\ProviderDump;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderDumpConstants;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderDynamicDump;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderExceptions;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderIndent;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderMaxChildren;
use Awesomite\VarDumper\LightVarDumperProviders\ProviderMaxDepth;
Expand Down Expand Up @@ -65,6 +66,23 @@ public function providerDump()
);
}

/**
* @dataProvider providerThrowable
*
* @param VarDumperInterface $dumper
* @param \Exception|\Throwable $data
* @param string $expectedDump
*/
public function testThrowable(VarDumperInterface $dumper, $data, $expectedDump)
{
$this->assertSame($expectedDump, $dumper->dumpAsString($data));
}

public function providerThrowable()
{
return \iterator_to_array(new ProviderExceptions());
}

/**
* HHVM changes order of properties.
*
Expand Down Expand Up @@ -102,7 +120,7 @@ public function testNativeDumper()
{
$dumper = new LightVarDumper();

$subdumper = $this->readPrivateProperty($dumper, 'subdumper');
$subdumper = $this->getPrivateProperty($dumper, 'subdumper');
$refSubDumper = new \ReflectionProperty($subdumper, 'subdumpers');
$refSubDumper->setAccessible(true);
$refSubDumper->setValue($subdumper, array(new NativeDumper()));
Expand All @@ -116,11 +134,11 @@ public function testNativeDumper()
* @expectedException \RuntimeException
* @expectedExceptionMessage None of the subdumpers supports this variable [NULL]
*/
public function testNoSupportedContainer()
public function testNoSupportedSubdumper()
{
$dumper = new LightVarDumper();

$subdumper = $this->readPrivateProperty($dumper, 'subdumper');
$subdumper = $this->getPrivateProperty($dumper, 'subdumper');
$refSubDumper = new \ReflectionProperty($subdumper, 'subdumpers');
$refSubDumper->setAccessible(true);
$refSubDumper->setValue($subdumper, array());
Expand Down Expand Up @@ -423,15 +441,15 @@ private function reinitAllDumpers()
private function assertZeroDepth(LightVarDumper $dumper)
{
/** @var IntValue $depth */
$depth = $this->readPrivateProperty($this->readContainer($dumper), 'depth');
$depth = $this->getPrivateProperty($this->getContainer($dumper), 'depth');

$this->assertSame(0, $depth->getValue());
}

private function assertEmptyReferences(LightVarDumper $dumper)
{
$references = $this->readContainer($dumper)->getReferences();
$array = $this->readPrivateProperty($references, 'array');
$references = $this->getContainer($dumper)->getReferences();
$array = $this->getPrivateProperty($references, 'array');
$this->assertSame(0, \count($array));
}

Expand All @@ -440,15 +458,15 @@ private function assertEmptyReferences(LightVarDumper $dumper)
*
* @return Container
*/
private function readContainer(LightVarDumper $dumper)
private function getContainer(LightVarDumper $dumper)
{
/** @var SubdumpersCollection $subdumper */
$subdumper = $this->readPrivateProperty($dumper, 'subdumper');
$subdumper = $this->getPrivateProperty($dumper, 'subdumper');

return $this->readPrivateProperty($subdumper, 'container');
return $this->getPrivateProperty($subdumper, 'container');
}

private function readPrivateProperty($object, $name)
private function getPrivateProperty($object, $name)
{
$property = new \ReflectionProperty(\get_class($object), $name);
$property->setAccessible(true);
Expand Down

0 comments on commit 8ee71ed

Please sign in to comment.