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

Add VarDumper caster for readables #167

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"autoload": {
"psr-4": {
"Elao\\Enum\\": "src/"
}
},
"files": [
"src/Bridge/Symfony/VarDumper/Resources/register_caster.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
27 changes: 27 additions & 0 deletions src/Bridge/Symfony/VarDumper/Caster/ReadableEnumCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

namespace Elao\Enum\Bridge\Symfony\VarDumper\Caster;

use Elao\Enum\ReadableEnumInterface;
use Symfony\Component\VarDumper\Caster\Caster;

final class ReadableEnumCaster
{
public static function castEnum(ReadableEnumInterface $enum, $array)
{
return $array + [
// Append the readable value;
Caster::PREFIX_VIRTUAL . 'readable' => $enum->getReadable(),
];
}
}
19 changes: 19 additions & 0 deletions src/Bridge/Symfony/VarDumper/Resources/register_caster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

use Elao\Enum\Bridge\Symfony\VarDumper\Caster\ReadableEnumCaster;
use Elao\Enum\ReadableEnumInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;

if (class_exists(VarCloner::class) && !isset(VarCloner::$defaultCasters[ReadableEnumInterface::class])) {
VarCloner::$defaultCasters += [ReadableEnumInterface::class => [ReadableEnumCaster::class, 'castEnum']];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

namespace Elao\Enum\Tests\Unit\Bridge\Symfony\VarDumper\Caster;

use Elao\Enum\ReadableEnumInterface;
use Elao\Enum\Tests\Fixtures\Enum\Suit;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

class ReadableEnumCasterTest extends TestCase
{
use VarDumperTestTrait;

public static function setUpBeforeClass(): void
{
putenv('DUMP_LIGHT_ARRAY=1');
}

public static function tearDownAfterClass(): void
{
putenv('DUMP_LIGHT_ARRAY');
}

public function testCasterIsRegistered()
{
self::assertArrayHasKey(
ReadableEnumInterface::class,
VarCloner::$defaultCasters,
'the caster is registered through composer autoload files'
);
}

public function testCastReadable()
{
$expectedDump = <<<'EODUMP'
Elao\Enum\Tests\Fixtures\Enum\Suit {
+name: "Spades"
+value: "S"
readable: "suit.spades"
}
EODUMP;

$this->assertDumpEquals($expectedDump, Suit::Spades);
}

public function testCastAsHtml()
{
$dump = $this->dumpAsHtml(Suit::Spades);

$expectedDump = <<<'EODUMP'
"""
<header></header><boundary><span class=sf-dump-note>Elao\Enum\Tests\Fixtures\Enum\Suit</span> {<samp data-depth=1 class=sf-dump-expanded>\n
+<span class=sf-dump-public title="Public property">name</span>: "<span class=sf-dump-str title="6 characters">Spades</span>"\n
+<span class=sf-dump-public title="Public property">value</span>: "<span class=sf-dump-str>S</span>"\n
<span class=sf-dump-meta>readable</span>: "<span class=sf-dump-str title="11 characters">suit.spades</span>"\n
</samp>}\n
</boundary>\n
"""
EODUMP;

$this->assertDumpMatchesFormat($expectedDump, $dump);
}

private function dumpAsHtml($value): string
{
$cloner = new VarCloner();
$cloner->setMaxItems(-1);

$configure = static function (HtmlDumper $dumper) {
$dumper->setDumpHeader('<header></header>');
$dumper->setDumpBoundaries('<boundary>', '</boundary>');
};

$flags = AbstractDumper::DUMP_LIGHT_ARRAY;
$dumper = new HtmlDumper(null, null, $flags);
$configure($dumper);

return $dumper->dump($cloner->cloneVar($value)->withRefHandles(false), true);
}
}