From bbb308ad915127ee7d907d38af6f6375740caf45 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Tue, 11 Feb 2020 05:47:47 +0100 Subject: [PATCH] Added __debugInfo with human readable properties to EnumSet & EnumMap --- src/EnumMap.php | 16 ++++++++++++ src/EnumSet.php | 15 +++++++++++ tests/MabeEnumTest/EnumMapTest.php | 29 +++++++++++++++++++++ tests/MabeEnumTest/EnumSetTest.php | 23 ++++++++++++++++ tests/MabeEnumTest/TestAsset/EnumMapExt.php | 17 ++++++++++++ tests/MabeEnumTest/TestAsset/EnumSetExt.php | 17 ++++++++++++ 6 files changed, 117 insertions(+) create mode 100644 tests/MabeEnumTest/TestAsset/EnumMapExt.php create mode 100644 tests/MabeEnumTest/TestAsset/EnumSetExt.php diff --git a/src/EnumMap.php b/src/EnumMap.php index 73796fc0..b229544f 100644 --- a/src/EnumMap.php +++ b/src/EnumMap.php @@ -54,6 +54,22 @@ public function __construct(string $enumeration, iterable $map = null) } } + /** + * Add virtual private property "__pairs" with a list of key-value-pairs + * to the result of var_dump. + * + * This helps debugging as internally the map is using the ordinal number. + * + * @return array + */ + public function __debugInfo(): array { + $dbg = (array)$this; + $dbg["\0" . self::class . "\0__pairs"] = array_map(function ($k, $v) { + return [$k, $v]; + }, $this->getKeys(), $this->getValues()); + return $dbg; + } + /* write access (mutable) */ /** diff --git a/src/EnumSet.php b/src/EnumSet.php index df393366..cc47f3ae 100644 --- a/src/EnumSet.php +++ b/src/EnumSet.php @@ -106,6 +106,21 @@ public function __construct(string $enumeration, iterable $enumerators = null) } } + /** + * Add virtual private property "__enumerators" with a list of enumerator values set + * to the result of var_dump. + * + * This helps debugging as internally the enumerators of this EnumSet gets stored + * as either integer or binary bit-array. + * + * @return array + */ + public function __debugInfo() { + $dbg = (array)$this; + $dbg["\0" . self::class . "\0__enumerators"] = $this->getValues(); + return $dbg; + } + /** * Get the classname of the enumeration * @return string diff --git a/tests/MabeEnumTest/EnumMapTest.php b/tests/MabeEnumTest/EnumMapTest.php index dd602d58..0973f46f 100644 --- a/tests/MabeEnumTest/EnumMapTest.php +++ b/tests/MabeEnumTest/EnumMapTest.php @@ -7,6 +7,7 @@ use MabeEnumTest\TestAsset\Enum32; use MabeEnumTest\TestAsset\EnumBasic; use MabeEnumTest\TestAsset\EnumInheritance; +use MabeEnumTest\TestAsset\EnumMapExt; use PHPUnit\Framework\TestCase; use UnexpectedValueException; @@ -488,6 +489,34 @@ public function testIsEmpty() $this->assertTrue($map2->isEmpty()); } + public function testDebugInfo() + { + $map = new EnumMapExt(EnumBasic::class); + foreach (EnumBasic::getEnumerators() as $i => $enumerator) { + $map->add($enumerator, $i); + } + + $dbg = $map->__debugInfo(); + + $privateEnumMapPrefix = "\0" . EnumMap::class . "\0"; + $privateEnumMapExtPrefix = "\0" . EnumMapExt::class . "\0"; + $protectedEnumMapExtPrefix = "\0*\0"; + $publicEnumMapExtPrefix = ''; + + // assert real properties still exists + $this->assertArrayHasKey("{$privateEnumMapPrefix}enumeration", $dbg); + $this->assertArrayHasKey("{$privateEnumMapPrefix}map", $dbg); + $this->assertArrayHasKey("{$privateEnumMapExtPrefix}priv", $dbg); + $this->assertArrayHasKey("{$protectedEnumMapExtPrefix}prot", $dbg); + $this->assertArrayHasKey("{$publicEnumMapExtPrefix}pub", $dbg); + + // assert virtual private property __pairs + $this->assertArrayHasKey("{$privateEnumMapPrefix}__pairs", $dbg); + $this->assertSame(array_map(function ($k, $v) { + return [$k, $v]; + }, $map->getKeys(), $map->getValues()), $dbg["{$privateEnumMapPrefix}__pairs"]); + } + /* deprecated */ public function testContains() diff --git a/tests/MabeEnumTest/EnumSetTest.php b/tests/MabeEnumTest/EnumSetTest.php index c1a714f8..6e8724c8 100644 --- a/tests/MabeEnumTest/EnumSetTest.php +++ b/tests/MabeEnumTest/EnumSetTest.php @@ -12,6 +12,7 @@ use MabeEnumTest\TestAsset\Enum64; use MabeEnumTest\TestAsset\Enum65; use MabeEnumTest\TestAsset\Enum66; +use MabeEnumTest\TestAsset\EnumSetExt; use PHPUnit\Framework\TestCase; /** @@ -956,6 +957,28 @@ public function testSetSymDiffThrowsInvalidArgumentException() $set1->setSymDiff($set2); } + public function testDebugInfo() + { + $set = new EnumSetExt(EnumBasic::class, EnumBasic::getEnumerators()); + $dbg = $set->__debugInfo(); + + $privateEnumSetPrefix = "\0" . EnumSet::class . "\0"; + $privateEnumSetExtPrefix = "\0" . EnumSetExt::class . "\0"; + $protectedEnumSetExtPrefix = "\0*\0"; + $publicEnumSetExtPrefix = ''; + + // assert real properties still exists + $this->assertArrayHasKey("{$privateEnumSetPrefix}enumeration", $dbg); + $this->assertArrayHasKey("{$privateEnumSetPrefix}bitset", $dbg); + $this->assertArrayHasKey("{$privateEnumSetExtPrefix}priv", $dbg); + $this->assertArrayHasKey("{$protectedEnumSetExtPrefix}prot", $dbg); + $this->assertArrayHasKey("{$publicEnumSetExtPrefix}pub", $dbg); + + // assert virtual private property __enumerators + $this->assertArrayHasKey("{$privateEnumSetPrefix}__enumerators", $dbg); + $this->assertSame(EnumBasic::getValues(), $dbg["{$privateEnumSetPrefix}__enumerators"]); + } + /* deprecated */ /** @deprecated */ diff --git a/tests/MabeEnumTest/TestAsset/EnumMapExt.php b/tests/MabeEnumTest/TestAsset/EnumMapExt.php new file mode 100644 index 00000000..ba372568 --- /dev/null +++ b/tests/MabeEnumTest/TestAsset/EnumMapExt.php @@ -0,0 +1,17 @@ +